From b0172a365e63c6afec8e96f478fae4a001c3e3a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nils=20O=2E=20Sel=C3=A5sdal?= Date: Thu, 17 Oct 2013 22:28:32 +0200 Subject: [PATCH] test uc_htable_clear. Add comments, fix uc_htable_next --- src/htable.c | 12 ++++++++++-- test/test_htable.c | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/src/htable.c b/src/htable.c index 3fab8e6..02fc876 100644 --- a/src/htable.c +++ b/src/htable.c @@ -39,6 +39,7 @@ struct UHNode *uc_htable_first_hash(const struct UHTable *table, size_t hash) return it; } +//given a node, find the next one in the chain with the given hash struct UHNode *uc_htable_next_hash_hlp(struct UHNode *next, size_t hash) { while (next && next->hash != hash) { @@ -48,11 +49,13 @@ struct UHNode *uc_htable_next_hash_hlp(struct UHNode *next, size_t hash) return next; } -struct UHNode *uc_htable_next_hlp(struct UHTable *table, size_t bucket) +//find a node, starting at bucket +struct UHNode *uc_htable_iter_hlp(struct UHTable *table, size_t bucket) { size_t i; struct UHNode *found = NULL; + //search through buckets until a node is found for (i = bucket; i < table->cnt_buckets; i++) { found = uc_htable_first_bucket(table, i); if (found != NULL) { @@ -68,7 +71,8 @@ struct UHNode *uc_htable_next(struct UHTable *table, struct UHNode *node) found = uc_htable_next_bucket(node); if (found == NULL) { - found = uc_htable_next_hlp(table, node->hash + 1); + size_t current_bucket = uc_htable_bucket(table, node->hash); + found = uc_htable_iter_hlp(table, current_bucket + 1); } return found; @@ -139,11 +143,14 @@ int uc_htable_resize(struct UHTable *table, size_t buckets) return EINVAL; } + //create new temp table rc = uc_htable_init(&tmp_table, buckets); if (rc != 0) { return rc; } + + //move all the nodes to the new temp table for (h = 0; h < table->cnt_buckets; h++) { struct UHNode *next; struct UHNode *node; @@ -156,6 +163,7 @@ int uc_htable_resize(struct UHTable *table, size_t buckets) } } + //replace the existing table with the resized temp table uc_htable_swap(&tmp_table, table); uc_htable_destroy(&tmp_table); diff --git a/test/test_htable.c b/test/test_htable.c index 523a34e..32e363b 100644 --- a/test/test_htable.c +++ b/test/test_htable.c @@ -64,6 +64,8 @@ START_TEST (test_htable1) fail_if(my_find(&table, "Four", hash_str("Four")) != &str[3]); fail_if(my_find(&table, "Five", hash_str("Five")) != &str[4]); + uc_htable_destroy(&table); + END_TEST START_TEST (test_htable_hash_collision) @@ -92,6 +94,8 @@ START_TEST (test_htable_hash_collision) fail_if(my_find(&table, "..", bad_hash("..")) != &str[3]); fail_if(my_find(&table, "...", bad_hash("...")) != &str[0]); + uc_htable_destroy(&table); + END_TEST START_TEST (test_htable_resize) @@ -125,6 +129,7 @@ START_TEST (test_htable_resize) fail_if(my_find(&table, "Four", hash_str("Four")) != &str[3]); fail_if(my_find(&table, "Five", hash_str("Five")) != &str[4]); + uc_htable_destroy(&table); END_TEST @@ -140,6 +145,8 @@ START_TEST (test_htable_fail_init_resize) rc = uc_htable_resize(&table, 0); ck_assert_int_ne(rc, 0); + uc_htable_destroy(&table); + END_TEST START_TEST (test_htable_has_node) @@ -161,6 +168,8 @@ START_TEST (test_htable_has_node) rc = uc_htable_has_node(&table, &str2.node); ck_assert_int_eq(rc, 0); + uc_htable_destroy(&table); + END_TEST START_TEST (test_htable_remove) @@ -204,6 +213,30 @@ START_TEST (test_htable_remove) //this one should still be here fail_if(my_find(&table, "Other", hash_str("Other")) != &str3); + uc_htable_destroy(&table); + +END_TEST + +START_TEST (test_htable_clear) + struct MyString str1 = {"One", {0,NULL}}; + + struct UHTable table; + int rc; + + rc = uc_htable_init(&table, 100); + ck_assert_int_eq(rc, 0); + + uc_htable_insert(&table, &str1.node, hash_str(str1.str)); + ck_assert_int_eq(uc_htable_count(&table), 1); + + fail_if(my_find(&table, "One", hash_str("One")) != &str1); + + uc_htable_clear(&table); + ck_assert_int_eq(uc_htable_count(&table), 0); + fail_if(my_find(&table, "One", hash_str("One")) != NULL); + + uc_htable_destroy(&table); + END_TEST Suite *htable_suite(void) @@ -217,6 +250,7 @@ Suite *htable_suite(void) tcase_add_test(tc, test_htable_fail_init_resize); tcase_add_test(tc, test_htable_has_node); tcase_add_test(tc, test_htable_remove); + tcase_add_test(tc, test_htable_clear); suite_add_tcase(s, tc);