/** * utest.c * Fun with unit testing! */ // +---------+-------------------------------------------------------- // | Headers | // +---------+ #include #include #include "utest.h" // +---------+-------------------------------------------------------- // | Globals | // +---------+ int tests = 0; int errors = 0; // +-----------------+------------------------------------------------ // | Local Functions | // +-----------------+ static int string_equal (char *str1, char *str2) { if (str1 == str2) return 1; else if ((str1 == NULL) || (str2 == NULL)) return 0; else return (strcmp (str1, str2) == 0); } // string_equal // +-----------+------------------------------------------------------ // | Functions | // +-----------+ void check_int (char *command, int result, int expected) { ++tests; if (result != expected) { ++errors; fprintf (stderr, "ERROR\n"); fprintf (stderr, " Command: %s\n", command); fprintf (stderr, " Result: %d\n", result); fprintf (stderr, " Expected: %d\n", expected); } } // check_int void check_ptr (char *command, void *result, void *expected) { ++tests; if (result != expected) { ++errors; fprintf (stderr, "ERROR\n"); fprintf (stderr," Command: %s\n", command); } } // check_ptr void check_str (char *command, char *result, char *expected) { ++tests; if (! string_equal (result, expected)) { ++errors; fprintf (stderr, "ERROR\n"); fprintf (stderr, " Command: %s\n", command); if (result != NULL) fprintf (stderr, " Result: \"%s\"\n", result); else fprintf (stderr, " Result: NULL\n"); if (expected != NULL) fprintf (stderr, " Expected: \"%s\"\n", expected); else fprintf (stderr, " Expected: NULL\n"); } } // check_str int utest_tests (void) { return tests; } // utest_tests int utest_errors (void) { return errors; } // utest_errors