test uc_htable_clear. Add comments, fix uc_htable_next
This commit is contained in:
+10
-2
@@ -39,6 +39,7 @@ struct UHNode *uc_htable_first_hash(const struct UHTable *table, size_t hash)
|
|||||||
return it;
|
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)
|
struct UHNode *uc_htable_next_hash_hlp(struct UHNode *next, size_t hash)
|
||||||
{
|
{
|
||||||
while (next && next->hash != 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;
|
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;
|
size_t i;
|
||||||
struct UHNode *found = NULL;
|
struct UHNode *found = NULL;
|
||||||
|
|
||||||
|
//search through buckets until a node is found
|
||||||
for (i = bucket; i < table->cnt_buckets; i++) {
|
for (i = bucket; i < table->cnt_buckets; i++) {
|
||||||
found = uc_htable_first_bucket(table, i);
|
found = uc_htable_first_bucket(table, i);
|
||||||
if (found != NULL) {
|
if (found != NULL) {
|
||||||
@@ -68,7 +71,8 @@ struct UHNode *uc_htable_next(struct UHTable *table, struct UHNode *node)
|
|||||||
|
|
||||||
found = uc_htable_next_bucket(node);
|
found = uc_htable_next_bucket(node);
|
||||||
if (found == NULL) {
|
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;
|
return found;
|
||||||
@@ -139,11 +143,14 @@ int uc_htable_resize(struct UHTable *table, size_t buckets)
|
|||||||
return EINVAL;
|
return EINVAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//create new temp table
|
||||||
rc = uc_htable_init(&tmp_table, buckets);
|
rc = uc_htable_init(&tmp_table, buckets);
|
||||||
if (rc != 0) {
|
if (rc != 0) {
|
||||||
return rc;
|
return rc;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//move all the nodes to the new temp table
|
||||||
for (h = 0; h < table->cnt_buckets; h++) {
|
for (h = 0; h < table->cnt_buckets; h++) {
|
||||||
struct UHNode *next;
|
struct UHNode *next;
|
||||||
struct UHNode *node;
|
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_swap(&tmp_table, table);
|
||||||
uc_htable_destroy(&tmp_table);
|
uc_htable_destroy(&tmp_table);
|
||||||
|
|
||||||
|
|||||||
@@ -64,6 +64,8 @@ START_TEST (test_htable1)
|
|||||||
fail_if(my_find(&table, "Four", hash_str("Four")) != &str[3]);
|
fail_if(my_find(&table, "Four", hash_str("Four")) != &str[3]);
|
||||||
fail_if(my_find(&table, "Five", hash_str("Five")) != &str[4]);
|
fail_if(my_find(&table, "Five", hash_str("Five")) != &str[4]);
|
||||||
|
|
||||||
|
uc_htable_destroy(&table);
|
||||||
|
|
||||||
END_TEST
|
END_TEST
|
||||||
|
|
||||||
START_TEST (test_htable_hash_collision)
|
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[3]);
|
||||||
fail_if(my_find(&table, "...", bad_hash("...")) != &str[0]);
|
fail_if(my_find(&table, "...", bad_hash("...")) != &str[0]);
|
||||||
|
|
||||||
|
uc_htable_destroy(&table);
|
||||||
|
|
||||||
END_TEST
|
END_TEST
|
||||||
|
|
||||||
START_TEST (test_htable_resize)
|
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, "Four", hash_str("Four")) != &str[3]);
|
||||||
fail_if(my_find(&table, "Five", hash_str("Five")) != &str[4]);
|
fail_if(my_find(&table, "Five", hash_str("Five")) != &str[4]);
|
||||||
|
|
||||||
|
uc_htable_destroy(&table);
|
||||||
|
|
||||||
END_TEST
|
END_TEST
|
||||||
|
|
||||||
@@ -140,6 +145,8 @@ START_TEST (test_htable_fail_init_resize)
|
|||||||
rc = uc_htable_resize(&table, 0);
|
rc = uc_htable_resize(&table, 0);
|
||||||
ck_assert_int_ne(rc, 0);
|
ck_assert_int_ne(rc, 0);
|
||||||
|
|
||||||
|
uc_htable_destroy(&table);
|
||||||
|
|
||||||
END_TEST
|
END_TEST
|
||||||
|
|
||||||
START_TEST (test_htable_has_node)
|
START_TEST (test_htable_has_node)
|
||||||
@@ -161,6 +168,8 @@ START_TEST (test_htable_has_node)
|
|||||||
rc = uc_htable_has_node(&table, &str2.node);
|
rc = uc_htable_has_node(&table, &str2.node);
|
||||||
ck_assert_int_eq(rc, 0);
|
ck_assert_int_eq(rc, 0);
|
||||||
|
|
||||||
|
uc_htable_destroy(&table);
|
||||||
|
|
||||||
END_TEST
|
END_TEST
|
||||||
|
|
||||||
START_TEST (test_htable_remove)
|
START_TEST (test_htable_remove)
|
||||||
@@ -204,6 +213,30 @@ START_TEST (test_htable_remove)
|
|||||||
//this one should still be here
|
//this one should still be here
|
||||||
fail_if(my_find(&table, "Other", hash_str("Other")) != &str3);
|
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
|
END_TEST
|
||||||
|
|
||||||
Suite *htable_suite(void)
|
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_fail_init_resize);
|
||||||
tcase_add_test(tc, test_htable_has_node);
|
tcase_add_test(tc, test_htable_has_node);
|
||||||
tcase_add_test(tc, test_htable_remove);
|
tcase_add_test(tc, test_htable_remove);
|
||||||
|
tcase_add_test(tc, test_htable_clear);
|
||||||
|
|
||||||
|
|
||||||
suite_add_tcase(s, tc);
|
suite_add_tcase(s, tc);
|
||||||
|
|||||||
Reference in New Issue
Block a user