Test UC_HTABLE_FOR_EACH

This commit is contained in:
Nils O. Selåsdal
2013-10-17 22:38:00 +02:00
parent b0172a365e
commit 7ff33c113b
2 changed files with 47 additions and 3 deletions
+4 -3
View File
@@ -99,6 +99,7 @@ struct UHNode *uc_htable_first_hash(const struct UHTable *table, size_t hash);
//internal helper //internal helper
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);
/** /**
* Get the next node with the same hash * Get the next node with the same hash
* @param node previous node * @param node previous node
@@ -111,7 +112,7 @@ static inline struct UHNode *uc_htable_next_hash(const struct UHNode *node)
//internal helper //internal helper
struct UHNode *uc_htable_next_hlp(struct UHTable *table, size_t bucket); struct UHNode *uc_htable_iter_hlp(struct UHTable *table, size_t bucket);
/** Get the first node in the hash table /** Get the first node in the hash table
* *
@@ -120,7 +121,7 @@ struct UHNode *uc_htable_next_hlp(struct UHTable *table, size_t bucket);
*/ */
static inline struct UHNode *uc_htable_first(struct UHTable *table) static inline struct UHNode *uc_htable_first(struct UHTable *table)
{ {
return uc_htable_next_hlp(table, 0); return uc_htable_iter_hlp(table, 0);
} }
/** Get the next node in the hash table /** Get the next node in the hash table
@@ -149,7 +150,7 @@ void uc_htable_swap(struct UHTable *a, struct UHTable *b);
(node_iter) != NULL;\ (node_iter) != NULL;\
(node_iter) = uc_htable_next_hash((node_iter))) (node_iter) = uc_htable_next_hash((node_iter)))
#define UC_HTABLE_FOREACH(table, node_iter, hash)\ #define UC_HTABLE_FOREACH(table, node_iter)\
for ((node_iter) = uc_htable_first((table));\ for ((node_iter) = uc_htable_first((table));\
(node_iter) != NULL;\ (node_iter) != NULL;\
(node_iter) = uc_htable_next((table), (node_iter))) (node_iter) = uc_htable_next((table), (node_iter)))
+43
View File
@@ -239,6 +239,48 @@ START_TEST (test_htable_clear)
END_TEST END_TEST
START_TEST (test_htable_foreach)
struct MyString str1 = {"One", {0,NULL}};
struct MyString str2 = {"One", {0,NULL}};
struct MyString str3 = {"Other",{0,NULL}};
struct UHTable table;
struct UHNode *it;
int rc;
int found1 = 0;
int found2 = 0;
int found3 = 0;
rc = uc_htable_init(&table, 1000);
ck_assert_int_eq(rc, 0);
uc_htable_insert(&table, &str1.node, hash_str(str1.str));
uc_htable_insert(&table, &str2.node, hash_str(str2.str));
uc_htable_insert(&table, &str3.node, hash_str(str3.str));
ck_assert_int_eq(uc_htable_count(&table), 3);
UC_HTABLE_FOREACH(&table, it) {
struct MyString *my_str = UC_CONTAINER_OF(it, struct MyString, node);
if (my_str == &str1) {
found1++;
}
if (my_str == &str2) {
found2++;
}
if (my_str == &str3) {
found3++;
}
}
ck_assert_int_eq(found1, 1);
ck_assert_int_eq(found2, 1);
ck_assert_int_eq(found3, 1);
uc_htable_destroy(&table);
END_TEST
Suite *htable_suite(void) Suite *htable_suite(void)
{ {
Suite *s = suite_create("htable"); Suite *s = suite_create("htable");
@@ -251,6 +293,7 @@ Suite *htable_suite(void)
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); tcase_add_test(tc, test_htable_clear);
tcase_add_test(tc, test_htable_foreach);
suite_add_tcase(s, tc); suite_add_tcase(s, tc);