/** * hash-utest * Some simple unit tests of our hash table. */ // +---------+-------------------------------------------------------- // | Headers | // +---------+ #include #include // For strcmp #include "hash.h" // What we're testing. // +--------+--------------------------------------------------------- // | Locals | // +--------+ /** * A count of tests. */ static int tests = 0; /** * A count of failed tests. */ static int errors = 0; // +---------+-------------------------------------------------------- // | Helpers | // +---------+ static void test_put (char *name, char *defn) { ++tests; if (table_put (name, defn) == 0) { ++errors; printf ("table_put(\"%s\", \"%s\") failed.\n", name, defn); } } // test_put static void test_get (char *name, char *expected) { char *defn = table_get (name); ++tests; if ((expected == NULL) && (defn != NULL)) { ++errors; printf ("For table_get (\"%s\"), expected NULL, got \"%s\"\n", name, defn); } else if ((expected != NULL) && (defn == NULL)) { ++errors; printf ("For table_get (\"%s\"), expected \"%s\", got NULL\n", name, expected); } else if ( (expected != NULL) && (defn != NULL) && (strcmp (expected, defn) != 0) ) { ++errors; printf ("For table_get (\"%s\"), expected \"%s\", got \"%s\"\n", name, expected, defn); } } // test_get // +------+----------------------------------------------------------- // | Main | // +------+ int main () { // Testing test_put ("Batman", "Robin"); test_get ("Batman", "Robin"); // Print some final information printf ("%d tests conducted.\n", tests); if (errors > 1) printf ("%d tests failed.\n", errors); else if (errors == 1) printf ("one test failed.\n"); else printf ("ALL TESTS PASSED\n"); return errors; } // main