From 82beca0432545857578537a206e0a6ccca7de252 Mon Sep 17 00:00:00 2001 From: Vincent Sanders Date: Sun, 12 Jul 2015 17:28:03 +0100 Subject: Complete hash table tests and clean up ineterface. --- test/hashtable.c | 173 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 173 insertions(+) (limited to 'test/hashtable.c') diff --git a/test/hashtable.c b/test/hashtable.c index 2939f4f85..d69b10870 100644 --- a/test/hashtable.c +++ b/test/hashtable.c @@ -32,6 +32,96 @@ #include "utils/hashtable.h" +#define NELEMS(x) (sizeof(x) / sizeof((x)[0])) + +struct test_pairs { + const char* test; + const char* res; +}; + +static struct hash_table *match_hash_a; +static struct hash_table *match_hash_b; + +static struct hash_table *dict_hash; + +static const struct test_pairs match_tests[] = { + { "cow", "moo" }, + { "pig", "oink" }, + { "chicken", "cluck" }, + { "dog", "woof" }, + { "sheep", "baaa" }, +}; + +/* Fixtures */ + +static void match_hashtable_create(void) +{ + int idx; + + match_hash_a = hash_create(79); + ck_assert(match_hash_a != NULL); + + match_hash_b = hash_create(103); + ck_assert(match_hash_b != NULL); + + for (idx = 0; idx < NELEMS(match_tests); idx++) { + hash_add(match_hash_a, + match_tests[idx].test, + match_tests[idx].res); + hash_add(match_hash_b, + match_tests[idx].res, + match_tests[idx].test); + } +} + +static void match_hashtable_teardown(void) +{ + hash_destroy(match_hash_a); + hash_destroy(match_hash_b); +} + + +/** + * create dictionary hashtable + * + * hashtable constructed from the odd/even rows of the + * dictionary + */ +static void dict_hashtable_create(int dict_hash_size) +{ + FILE *dictf; + char keybuf[BUFSIZ], valbuf[BUFSIZ]; + + dictf = fopen("/usr/share/dict/words", "r"); + ck_assert(dictf != NULL); + + dict_hash = hash_create(dict_hash_size); + ck_assert(dict_hash != NULL); + + while (!feof(dictf)) { + fscanf(dictf, "%s", keybuf); + fscanf(dictf, "%s", valbuf); + hash_add(dict_hash, keybuf, valbuf); + } + + fclose(dictf); +} + +static void dicts_hashtable_create(void) +{ + dict_hashtable_create(1031); +} + +static void dictl_hashtable_create(void) +{ + dict_hashtable_create(7919); +} + +static void dict_hashtable_teardown(void) +{ + hash_destroy(dict_hash); +} + /* Tests */ /** @@ -109,12 +199,60 @@ START_TEST(hashtable_positive_test) } END_TEST + +START_TEST(hashtable_matcha_test) +{ + const char *res; + + res = hash_get(match_hash_a, match_tests[_i].test); + ck_assert(res != NULL); + ck_assert_str_eq(res, match_tests[_i].res); +} +END_TEST + + +START_TEST(hashtable_matchb_test) +{ + const char *res; + + res = hash_get(match_hash_b, match_tests[_i].res); + ck_assert(res != NULL); + ck_assert_str_eq(res, match_tests[_i].test); +} +END_TEST + + +START_TEST(hashtable_dict_test) +{ + FILE *dictf; + char keybuf[BUFSIZ], valbuf[BUFSIZ]; + const char *res; + + dictf = fopen("/usr/share/dict/words", "r"); + ck_assert(dictf != NULL); + + while (!feof(dictf)) { + fscanf(dictf, "%s", keybuf); + fscanf(dictf, "%s", valbuf); + + res = hash_get(dict_hash, keybuf); + ck_assert(res != NULL); + ck_assert_str_eq(res, valbuf); + } + + fclose(dictf); +} +END_TEST + /* Suite */ Suite *hashtable_suite(void) { Suite *s; TCase *tc_create; + TCase *tc_match; + TCase *tc_dict_s; + TCase *tc_dict_l; s = suite_create("hash table filter"); @@ -127,7 +265,42 @@ Suite *hashtable_suite(void) suite_add_tcase(s, tc_create); + /* Matching entry tests */ + tc_match = tcase_create("Match"); + + tcase_add_checked_fixture(tc_match, + match_hashtable_create, + match_hashtable_teardown); + + tcase_add_loop_test(tc_match, + hashtable_matcha_test, + 0, NELEMS(match_tests)); + tcase_add_loop_test(tc_match, + hashtable_matchb_test, + 0, NELEMS(match_tests)); + + suite_add_tcase(s, tc_match); + + /* small table dictionary test */ + tc_dict_s = tcase_create("small table dictionary"); + tcase_add_checked_fixture(tc_dict_s, + dicts_hashtable_create, + dict_hashtable_teardown); + + tcase_add_test(tc_dict_s, hashtable_dict_test); + + suite_add_tcase(s, tc_dict_s); + + + /* large table dictionary test */ + tc_dict_l = tcase_create("large table dictionary"); + tcase_add_checked_fixture(tc_dict_l, + dictl_hashtable_create, + dict_hashtable_teardown); + + tcase_add_test(tc_dict_l, hashtable_dict_test); + suite_add_tcase(s, tc_dict_l); return s; } -- cgit v1.2.3