]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
set: modify the previously incorrect definition of set_copy and add test for it
authorMaanya Goenka <t-magoenka@microsoft.com>
Mon, 16 Aug 2021 22:55:51 +0000 (15:55 -0700)
committerMaanya Goenka <t-magoenka@microsoft.com>
Fri, 20 Aug 2021 17:02:49 +0000 (10:02 -0700)
src/basic/set.h
src/test/test-set.c

index 0f8673934f118d72a45de0c028be154be42a48f0..5cae13160bc84999adb00326956d982e7c77cb26 100644 (file)
@@ -26,7 +26,7 @@ static inline Set* set_free_free(Set *s) {
 
 /* no set_free_free_free */
 
-#define set_copy(s) ((Set*) _hashmap_copy(HASHMAP_BASE(h)  HASHMAP_DEBUG_SRC_ARGS))
+#define set_copy(s) ((Set*) _hashmap_copy(HASHMAP_BASE(s)  HASHMAP_DEBUG_SRC_ARGS))
 
 int _set_ensure_allocated(Set **s, const struct hash_ops *hash_ops HASHMAP_DEBUG_PARAMS);
 #define set_ensure_allocated(h, ops) _set_ensure_allocated(h, ops HASHMAP_DEBUG_SRC_ARGS)
index f89c968f21a461761d5e7756a80e1a11c8510c48..fd12021cfa7e4fa8aa17eabe58bb08009c269ab5 100644 (file)
@@ -117,6 +117,38 @@ static void test_set_ensure_allocated(void) {
         assert_se(set_size(m) == 0);
 }
 
+static void test_set_copy(void) {
+        Set *s, *copy;
+        char *key1, *key2, *key3, *key4;
+
+        log_info("/* %s */", __func__);
+
+        key1 = strdup("key1");
+        assert_se(key1);
+        key2 = strdup("key2");
+        assert_se(key2);
+        key3 = strdup("key3");
+        assert_se(key3);
+        key4 = strdup("key4");
+        assert_se(key4);
+
+        s = set_new(&string_hash_ops);
+        assert_se(s);
+
+        assert_se(set_put(s, key1) >= 0);
+        assert_se(set_put(s, key2) >= 0);
+        assert_se(set_put(s, key3) >= 0);
+        assert_se(set_put(s, key4) >= 0);
+
+        copy = set_copy(s);
+        assert_se(copy);
+
+        assert(set_equal(s, copy));
+
+        set_free(s);
+        set_free_free(copy);
+}
+
 static void test_set_ensure_put(void) {
         _cleanup_set_free_ Set *m = NULL;
 
@@ -311,6 +343,7 @@ int main(int argc, const char *argv[]) {
         test_set_ensure_consume();
         test_set_strjoin();
         test_set_equal();
+        test_set_copy();
 
         return 0;
 }