From 9f41543fb1a6e958b519154f47bac4dc823d4a64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nils=20O=2E=20Sel=C3=A5sdal?= Date: Thu, 17 Oct 2013 21:00:57 +0200 Subject: [PATCH] hash table test --- test/test_htable.c | 125 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 test/test_htable.c diff --git a/test/test_htable.c b/test/test_htable.c new file mode 100644 index 0000000..46c1436 --- /dev/null +++ b/test/test_htable.c @@ -0,0 +1,125 @@ +#include +#include +#include +#include + +struct MyString { + char str[32]; + struct UHNode node; +}; + +size_t hash_str(const char *str) +{ + size_t hash = 31; + while (*str) { + hash *= *str++; + } + + return hash; +} + +struct MyString *my_find(struct UHTable *table, const char *str) +{ + struct UHNode *it; + size_t hash = hash_str(str); + + UC_HTABLE_FOREACH_HASH(table, it, hash) { + struct MyString *my_str = UC_CONTAINER_OF(it, struct MyString, node); + if (strcmp(my_str->str, str) == 0) { + return my_str; + } + } + + return NULL; +} + + + +START_TEST (test_htable1) + struct MyString str[5] = { + {"One", {0,NULL}}, + {"Two", {0,NULL}}, + {"Three",{0,NULL}}, + {"Four", {0,NULL}}, + {"Five", {0,NULL}} + }; + struct UHTable table; + int i; + + uc_htable_init(&table, 3); + + for (i = 0; i < 5; i++) { + uc_htable_insert(&table, &str[i].node, hash_str(str[i].str)); + } + + ck_assert_int_eq(uc_htable_count(&table), 5); + + fail_if(my_find(&table, "One") != &str[0]); + fail_if(my_find(&table, "Two") != &str[1]); + fail_if(my_find(&table, "Three") != &str[2]); + fail_if(my_find(&table, "Four") != &str[3]); + fail_if(my_find(&table, "Five") != &str[4]); + +END_TEST + +START_TEST (test_htable_resize) + struct MyString str[5] = { + {"One", {0,NULL}}, + {"Two", {0,NULL}}, + {"Three",{0,NULL}}, + {"Four", {0,NULL}}, + {"Five", {0,NULL}} + }; + struct UHTable table; + int i; + int rc; + + rc = uc_htable_init(&table, 3); + ck_assert_int_eq(rc, 0); + + for (i = 0; i < 5; i++) { + uc_htable_insert(&table, &str[i].node, hash_str(str[i].str)); + } + ck_assert_int_eq(uc_htable_count(&table), 5); + + rc = uc_htable_resize(&table, 1024); + ck_assert_int_eq(rc, 0); + + ck_assert_int_eq(uc_htable_count(&table), 5); + + fail_if(my_find(&table, "One") != &str[0]); + fail_if(my_find(&table, "Two") != &str[1]); + fail_if(my_find(&table, "Three") != &str[2]); + fail_if(my_find(&table, "Four") != &str[3]); + fail_if(my_find(&table, "Five") != &str[4]); + +END_TEST + +START_TEST (test_htable_fail_init_resize) + int rc; + struct UHTable table; + rc = uc_htable_init(&table, 0); + ck_assert_int_ne(rc, 0); + + rc = uc_htable_init(&table, 1); + ck_assert_int_eq(rc, 0); + + rc = uc_htable_resize(&table, 0); + ck_assert_int_ne(rc, 0); + +END_TEST +Suite *htable_suite(void) +{ + Suite *s = suite_create("htable"); + TCase *tc = tcase_create("htable tests"); + + tcase_add_test(tc, test_htable1); + tcase_add_test(tc, test_htable_resize); + tcase_add_test(tc, test_htable_fail_init_resize); + + + suite_add_tcase(s, tc); + + return s; +} +