summaryrefslogtreecommitdiff
path: root/src/test
diff options
context:
space:
mode:
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2017-11-28 12:35:49 +0100
committerSven Eden <yamakuzure@gmx.net>2017-11-28 12:35:49 +0100
commit6c66e1744f4b338d6b2f003920356d63bc6259f3 (patch)
treef29ad59ac6bf74b0a3c6fe3675e3525d7ee557af /src/test
parent84dc26bac24fa3a916147d375d28f8bc41e7b3b5 (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')
-rw-r--r--src/test/test-hashmap.c24
-rw-r--r--src/test/test-set.c24
2 files changed, 48 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();
diff --git a/src/test/test-set.c b/src/test/test-set.c
index 3fab350cf..21540fa1d 100644
--- a/src/test/test-set.c
+++ b/src/test/test-set.c
@@ -39,6 +39,29 @@ static void test_set_steal_first(void) {
assert_se(set_isempty(m));
}
+typedef struct Item {
+ int seen;
+} Item;
+static void item_seen(Item *item) {
+ item->seen++;
+}
+
+static void test_set_free_with_destructor(void) {
+ Set *m;
+ struct Item items[4] = {};
+ unsigned i;
+
+ assert_se(m = set_new(NULL));
+ for (i = 0; i < ELEMENTSOF(items) - 1; i++)
+ assert_se(set_put(m, items + i) == 1);
+
+ m = set_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_set_put(void) {
_cleanup_set_free_ Set *m = NULL;
@@ -101,6 +124,7 @@ static void test_set_make(void) {
int main(int argc, const char *argv[]) {
test_set_steal_first();
+ test_set_free_with_destructor();
test_set_put();
test_set_make();