da2bf9
From dcb475e97a48cddacab3ab5178fb351c702cdfb8 Mon Sep 17 00:00:00 2001
da2bf9
From: Yu Watanabe <watanabe.yu+github@gmail.com>
da2bf9
Date: Sun, 25 Nov 2018 21:54:44 +0900
da2bf9
Subject: [PATCH] test: add tests for destructors of hashmap or set
da2bf9
da2bf9
(cherry picked from commit 98233ee5e031cf39f5be73651a1f05c52927116b)
da2bf9
da2bf9
Resolves: #2037807
da2bf9
---
da2bf9
 src/test/test-hashmap-plain.c | 38 +++++++++++++++++++++++++++++++++++
da2bf9
 src/test/test-set.c           | 19 ++++++++++++++++++
da2bf9
 2 files changed, 57 insertions(+)
da2bf9
da2bf9
diff --git a/src/test/test-hashmap-plain.c b/src/test/test-hashmap-plain.c
da2bf9
index b695d4ee35..a34de067fc 100644
da2bf9
--- a/src/test/test-hashmap-plain.c
da2bf9
+++ b/src/test/test-hashmap-plain.c
da2bf9
@@ -867,6 +867,43 @@ static void test_hashmap_clear_free_free(void) {
da2bf9
 
da2bf9
         hashmap_clear_free_free(m);
da2bf9
         assert_se(hashmap_isempty(m));
da2bf9
+
da2bf9
+        assert_se(hashmap_put(m, strdup("key 1"), strdup("value 1")) == 1);
da2bf9
+        assert_se(hashmap_put(m, strdup("key 2"), strdup("value 2")) == 1);
da2bf9
+        assert_se(hashmap_put(m, strdup("key 3"), strdup("value 3")) == 1);
da2bf9
+
da2bf9
+        hashmap_clear_free_free(m);
da2bf9
+        assert_se(hashmap_isempty(m));
da2bf9
+}
da2bf9
+
da2bf9
+DEFINE_PRIVATE_HASH_OPS_WITH_KEY_DESTRUCTOR(test_hash_ops_key, char, string_hash_func, string_compare_func, free);
da2bf9
+DEFINE_PRIVATE_HASH_OPS_FULL(test_hash_ops_full, char, string_hash_func, string_compare_func, free, char, free);
da2bf9
+
da2bf9
+static void test_hashmap_clear_free_with_destructor(void) {
da2bf9
+        _cleanup_hashmap_free_ Hashmap *m = NULL;
da2bf9
+
da2bf9
+        log_info("%s", __func__);
da2bf9
+
da2bf9
+        m = hashmap_new(&test_hash_ops_key);
da2bf9
+        assert_se(m);
da2bf9
+
da2bf9
+        assert_se(hashmap_put(m, strdup("key 1"), NULL) == 1);
da2bf9
+        assert_se(hashmap_put(m, strdup("key 2"), NULL) == 1);
da2bf9
+        assert_se(hashmap_put(m, strdup("key 3"), NULL) == 1);
da2bf9
+
da2bf9
+        hashmap_clear_free(m);
da2bf9
+        assert_se(hashmap_isempty(m));
da2bf9
+        m = hashmap_free(m);
da2bf9
+
da2bf9
+        m = hashmap_new(&test_hash_ops_full);
da2bf9
+        assert_se(m);
da2bf9
+
da2bf9
+        assert_se(hashmap_put(m, strdup("key 1"), strdup("value 1")) == 1);
da2bf9
+        assert_se(hashmap_put(m, strdup("key 2"), strdup("value 2")) == 1);
da2bf9
+        assert_se(hashmap_put(m, strdup("key 3"), strdup("value 3")) == 1);
da2bf9
+
da2bf9
+        hashmap_clear_free(m);
da2bf9
+        assert_se(hashmap_isempty(m));
da2bf9
 }
da2bf9
 
da2bf9
 static void test_hashmap_reserve(void) {
da2bf9
@@ -924,5 +961,6 @@ void test_hashmap_funcs(void) {
da2bf9
         test_hashmap_steal_first_key();
da2bf9
         test_hashmap_steal_first();
da2bf9
         test_hashmap_clear_free_free();
da2bf9
+        test_hashmap_clear_free_with_destructor();
da2bf9
         test_hashmap_reserve();
da2bf9
 }
da2bf9
diff --git a/src/test/test-set.c b/src/test/test-set.c
da2bf9
index 6307403e4c..340edeb65f 100644
da2bf9
--- a/src/test/test-set.c
da2bf9
+++ b/src/test/test-set.c
da2bf9
@@ -45,6 +45,24 @@ static void test_set_free_with_destructor(void) {
da2bf9
         assert_se(items[3].seen == 0);
da2bf9
 }
da2bf9
 
da2bf9
+DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(item_hash_ops, void, trivial_hash_func, trivial_compare_func, Item, item_seen);
da2bf9
+
da2bf9
+static void test_set_free_with_hash_ops(void) {
da2bf9
+        Set *m;
da2bf9
+        struct Item items[4] = {};
da2bf9
+        unsigned i;
da2bf9
+
da2bf9
+        assert_se(m = set_new(&item_hash_ops));
da2bf9
+        for (i = 0; i < ELEMENTSOF(items) - 1; i++)
da2bf9
+                assert_se(set_put(m, items + i) == 1);
da2bf9
+
da2bf9
+        m = set_free(m);
da2bf9
+        assert_se(items[0].seen == 1);
da2bf9
+        assert_se(items[1].seen == 1);
da2bf9
+        assert_se(items[2].seen == 1);
da2bf9
+        assert_se(items[3].seen == 0);
da2bf9
+}
da2bf9
+
da2bf9
 static void test_set_put(void) {
da2bf9
         _cleanup_set_free_ Set *m = NULL;
da2bf9
 
da2bf9
@@ -64,6 +82,7 @@ static void test_set_put(void) {
da2bf9
 int main(int argc, const char *argv[]) {
da2bf9
         test_set_steal_first();
da2bf9
         test_set_free_with_destructor();
da2bf9
+        test_set_free_with_hash_ops();
da2bf9
         test_set_put();
da2bf9
 
da2bf9
         return 0;