diff options
author | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2017-11-28 12:35:49 +0100 |
---|---|---|
committer | Sven Eden <yamakuzure@gmx.net> | 2017-11-28 12:35:49 +0100 |
commit | 6c66e1744f4b338d6b2f003920356d63bc6259f3 (patch) | |
tree | f29ad59ac6bf74b0a3c6fe3675e3525d7ee557af /src/test/test-hashmap.c | |
parent | 84dc26bac24fa3a916147d375d28f8bc41e7b3b5 (diff) |
Add set/hashmap helpers for non-trivial freeing and use where straighforward
A macro is needed because otherwise we couldn't ensure type safety.
Some simple tests are included.
No functional change intended.
Diffstat (limited to 'src/test/test-hashmap.c')
-rw-r--r-- | src/test/test-hashmap.c | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/src/test/test-hashmap.c b/src/test/test-hashmap.c index 83cea360e..2615c98eb 100644 --- a/src/test/test-hashmap.c +++ b/src/test/test-hashmap.c @@ -37,6 +37,29 @@ static void test_ordered_hashmap_next(void) { assert_se(!ordered_hashmap_next(m, INT_TO_PTR(3))); } +typedef struct Item { + int seen; +} Item; +static void item_seen(Item *item) { + item->seen++; +} + +static void test_hashmap_free_with_destructor(void) { + Hashmap *m; + struct Item items[4] = {}; + unsigned i; + + assert_se(m = hashmap_new(NULL)); + for (i = 0; i < ELEMENTSOF(items) - 1; i++) + assert_se(hashmap_put(m, INT_TO_PTR(i), items + i) == 1); + + m = hashmap_free_with_destructor(m, item_seen); + assert_se(items[0].seen == 1); + assert_se(items[1].seen == 1); + assert_se(items[2].seen == 1); + assert_se(items[3].seen == 0); +} + static void test_uint64_compare_func(void) { const uint64_t a = 0x100, b = 0x101; @@ -61,6 +84,7 @@ int main(int argc, const char *argv[]) { test_ordered_hashmap_funcs(); test_ordered_hashmap_next(); + test_hashmap_free_with_destructor(); test_uint64_compare_func(); test_trivial_compare_func(); test_string_compare_func(); |