From 7ff33c113b9fdb9eebd28533d973b632cf95dacb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nils=20O=2E=20Sel=C3=A5sdal?= Date: Thu, 17 Oct 2013 22:38:00 +0200 Subject: [PATCH] Test UC_HTABLE_FOR_EACH --- include/ucore/htable.h | 7 ++++--- test/test_htable.c | 43 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/include/ucore/htable.h b/include/ucore/htable.h index 5c73563..f202662 100644 --- a/include/ucore/htable.h +++ b/include/ucore/htable.h @@ -99,6 +99,7 @@ struct UHNode *uc_htable_first_hash(const struct UHTable *table, size_t hash); //internal helper struct UHNode *uc_htable_next_hash_hlp(struct UHNode *next, size_t hash); + /** * Get the next node with the same hash * @param node previous node @@ -111,7 +112,7 @@ static inline struct UHNode *uc_htable_next_hash(const struct UHNode *node) //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 * @@ -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) { - return uc_htable_next_hlp(table, 0); + return uc_htable_iter_hlp(table, 0); } /** 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) = 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));\ (node_iter) != NULL;\ (node_iter) = uc_htable_next((table), (node_iter))) diff --git a/test/test_htable.c b/test/test_htable.c index 32e363b..925d273 100644 --- a/test/test_htable.c +++ b/test/test_htable.c @@ -239,6 +239,48 @@ START_TEST (test_htable_clear) 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 *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_remove); tcase_add_test(tc, test_htable_clear); + tcase_add_test(tc, test_htable_foreach); suite_add_tcase(s, tc);