]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
test: add tests for destructors of hashmap or set
authorYu Watanabe <watanabe.yu+github@gmail.com>
Sun, 25 Nov 2018 12:54:44 +0000 (21:54 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Sun, 2 Dec 2018 11:05:16 +0000 (12:05 +0100)
src/test/test-hashmap-plain.c
src/test/test-set.c

index 82837da4f620d83d9b3af8ce33e371b26674054d..c04ef0b1f72f76b5a6d95ae01ae13b245df14192 100644 (file)
@@ -863,6 +863,43 @@ static void test_hashmap_clear_free_free(void) {
 
         hashmap_clear_free_free(m);
         assert_se(hashmap_isempty(m));
+
+        assert_se(hashmap_put(m, strdup("key 1"), strdup("value 1")) == 1);
+        assert_se(hashmap_put(m, strdup("key 2"), strdup("value 2")) == 1);
+        assert_se(hashmap_put(m, strdup("key 3"), strdup("value 3")) == 1);
+
+        hashmap_clear_free_free(m);
+        assert_se(hashmap_isempty(m));
+}
+
+DEFINE_PRIVATE_HASH_OPS_WITH_KEY_DESTRUCTOR(test_hash_ops_key, char, string_hash_func, string_compare_func, free);
+DEFINE_PRIVATE_HASH_OPS_FULL(test_hash_ops_full, char, string_hash_func, string_compare_func, free, char, free);
+
+static void test_hashmap_clear_free_with_destructor(void) {
+        _cleanup_hashmap_free_ Hashmap *m = NULL;
+
+        log_info("%s", __func__);
+
+        m = hashmap_new(&test_hash_ops_key);
+        assert_se(m);
+
+        assert_se(hashmap_put(m, strdup("key 1"), NULL) == 1);
+        assert_se(hashmap_put(m, strdup("key 2"), NULL) == 1);
+        assert_se(hashmap_put(m, strdup("key 3"), NULL) == 1);
+
+        hashmap_clear_free(m);
+        assert_se(hashmap_isempty(m));
+        m = hashmap_free(m);
+
+        m = hashmap_new(&test_hash_ops_full);
+        assert_se(m);
+
+        assert_se(hashmap_put(m, strdup("key 1"), strdup("value 1")) == 1);
+        assert_se(hashmap_put(m, strdup("key 2"), strdup("value 2")) == 1);
+        assert_se(hashmap_put(m, strdup("key 3"), strdup("value 3")) == 1);
+
+        hashmap_clear_free(m);
+        assert_se(hashmap_isempty(m));
 }
 
 static void test_hashmap_reserve(void) {
@@ -915,5 +952,6 @@ void test_hashmap_funcs(void) {
         test_hashmap_steal_first_key();
         test_hashmap_steal_first();
         test_hashmap_clear_free_free();
+        test_hashmap_clear_free_with_destructor();
         test_hashmap_reserve();
 }
index 6307403e4c05b74b535710b604572b5a7111876b..340edeb65f605c442793463eb2ce1d4c457f5b90 100644 (file)
@@ -45,6 +45,24 @@ static void test_set_free_with_destructor(void) {
         assert_se(items[3].seen == 0);
 }
 
+DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(item_hash_ops, void, trivial_hash_func, trivial_compare_func, Item, item_seen);
+
+static void test_set_free_with_hash_ops(void) {
+        Set *m;
+        struct Item items[4] = {};
+        unsigned i;
+
+        assert_se(m = set_new(&item_hash_ops));
+        for (i = 0; i < ELEMENTSOF(items) - 1; i++)
+                assert_se(set_put(m, items + i) == 1);
+
+        m = set_free(m);
+        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;
 
@@ -64,6 +82,7 @@ static void test_set_put(void) {
 int main(int argc, const char *argv[]) {
         test_set_steal_first();
         test_set_free_with_destructor();
+        test_set_free_with_hash_ops();
         test_set_put();
 
         return 0;