/** * string-reverse-utests: Unit tests of string-reverse. */ // +---------+-------------------------------------------------------- // | Headers | // +---------+ #include // For printf #include // For strcpy #include "string-utils.h" // For string_reverse #include "utest.h" // For CHECK_STRING // +---------+-------------------------------------------------------- // | Helpers | // +---------+ /** * Check whether reverse works on a copy of str. Used because * string_reverse mutates the string, so we need to make sure that * we have a mutable string. Also does two tests: (1) is the value * returned the reverse; (2) did we actually reverse the underlying * string. */ void test_reverse (char *str, char *expected) { char data[1+strlen(str)]; strcpy (data, str); CHECK_STR (string_reverse (data), expected); CHECK_STR (data, expected); } // test_reverse // +------+----------------------------------------------------------- // | Main | // +------+ int main () { // A few simple strings test_reverse ("abcde", "edcba"); test_reverse ("madam im adam", "mada mi madam"); // A variety of sizes. (reverse might have problems on some sizes). // Note: These should be generated automatically, but I'm lazy. test_reverse ("", ""); test_reverse ("z", "z"); test_reverse ("yz", "zy"); test_reverse ("xyz", "zyx"); test_reverse ("wxyz", "zyxw"); test_reverse ("vwxyz", "zyxwv"); test_reverse ("uvwxyz", "zyxwvu"); test_reverse ("tuvwxyz", "zyxwvut"); test_reverse ("stuvwxyz", "zyxwvuts"); test_reverse ("rstuvwxyz", "zyxwvutsr"); if (! errors) printf ("All tests passed!\n"); else printf ("%d errors encountered\n", errors); return errors; } // main