| 1 | /********************************************************************* |
| 2 | * Filename: rot-13_test.c |
| 3 | * Author: Brad Conte (brad AT bradconte.com) |
| 4 | * Copyright: |
| 5 | * Disclaimer: This code is presented "as is" without any guarantees. |
| 6 | * Details: Performs known-answer tests on the corresponding ROT-13 |
| 7 | implementation. These tests do not encompass the full |
| 8 | range of available test vectors, however, if the tests |
| 9 | pass it is very, very likely that the code is correct |
| 10 | and was compiled properly. This code also serves as |
| 11 | example usage of the functions. |
| 12 | *********************************************************************/ |
| 13 | |
| 14 | /*************************** HEADER FILES ***************************/ |
| 15 | #include <stdio.h> |
| 16 | #include <string.h> |
| 17 | #include "rot-13.h" |
| 18 | |
| 19 | /*********************** FUNCTION DEFINITIONS ***********************/ |
| 20 | int rot13_test() |
| 21 | { |
| 22 | char text[] = {"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"}; |
| 23 | char code[] = {"NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm"}; |
| 24 | char buf[1024]; |
| 25 | int pass = 1; |
| 26 | |
| 27 | // To encode, just apply ROT-13. |
| 28 | strcpy(buf, text); |
| 29 | rot13(buf); |
| 30 | pass = pass && !strcmp(code, buf); |
| 31 | |
| 32 | // To decode, just re-apply ROT-13. |
| 33 | rot13(buf); |
| 34 | pass = pass && !strcmp(text, buf); |
| 35 | |
| 36 | return(pass); |
| 37 | } |
| 38 | |
| 39 | int main() |
| 40 | { |
| 41 | printf("ROT-13 tests: %s\n", rot13_test() ? "SUCCEEDED" : "FAILED"); |
| 42 | |
| 43 | return(0); |
| 44 | } |