#include #include #include "hashtable.h" int errors = 0; #define CHECK_INT(COMMAND, EXPECTED) do { \ int result = COMMAND; \ if (result != EXPECTED) \ { \ ++errors; \ fprintf (stderr, "Error in %s, expected %d, got %d.\n", #COMMAND, EXPECTED, result); \ } \ } while (0) #define CHECK_STR(COMMAND, EXPECTED) do { \ char *result = COMMAND; \ if (! string_equal (result, EXPECTED)) \ { \ ++errors; \ fprintf (stderr, "Error in [%s].\n", #COMMAND); \ } \ } while (0) 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 int main (int argc, char *argv[]) { struct hashtable *table = new_hashtable (20); // Check that assigning works CHECK_INT (assign (table, "Daryl", "Brost"), 1); CHECK_STR (get (table, "Daryl"), "Brost"); // Check that things not explicitly in the table are reported as not // there. CHECK_STR (get (table, "Sam"), (char *) NULL); // And then see that we can assign them. CHECK_INT (assign (table, "Sam", "Rebelsky"), 1); CHECK_STR (get (table, "Sam"), "Rebelsky"); // Check the result of changing an association. CHECK_INT (assign (table, "Sam", "Smith"), 1); CHECK_STR (get (table, "Sam"), "Smith"); // And we're done. fprintf (stderr, "%d errors encountered.\n", errors); return errors; } // main