]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
basic/hashmap: make _ensure_allocated return 1 on actual allocations
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 27 May 2020 12:17:43 +0000 (14:17 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 27 May 2020 14:48:04 +0000 (16:48 +0200)
Also, make test_hashmap_ensure_allocated() actually test
hashmap_ensure_allocated().

src/basic/hashmap.c
src/test/test-hashmap-plain.c

index efbe95bb9e33f90e087fdad9e45b95da5e87a315..b45938cd3bc37b1485288800466b1c0ebcd75327 100644 (file)
@@ -835,7 +835,7 @@ static int hashmap_base_ensure_allocated(HashmapBase **h, const struct hash_ops
                 return -ENOMEM;
 
         *h = q;
-        return 0;
+        return 1;
 }
 
 int internal_hashmap_ensure_allocated(Hashmap **h, const struct hash_ops *hash_ops  HASHMAP_DEBUG_PARAMS) {
index 57cf89ff53427b5845e77075dcb8f1e3c6d34b9a..09fe71f20556fe1713bec175e35376eef5b6ded7 100644 (file)
@@ -251,7 +251,7 @@ static void test_hashmap_put(void) {
 
         log_info("/* %s */", __func__);
 
-        assert_se(hashmap_ensure_allocated(&m, &string_hash_ops) >= 0);
+        assert_se(hashmap_ensure_allocated(&m, &string_hash_ops) == 1);
         assert_se(m);
 
         valid_hashmap_put = hashmap_put(m, "key 1", val1);
@@ -451,18 +451,20 @@ static void test_hashmap_remove_and_replace(void) {
 }
 
 static void test_hashmap_ensure_allocated(void) {
-        Hashmap *m;
-        int valid_hashmap;
+        _cleanup_hashmap_free_ Hashmap *m = NULL;
+        int r;
 
         log_info("/* %s */", __func__);
 
-        m = hashmap_new(&string_hash_ops);
+        r = hashmap_ensure_allocated(&m, &string_hash_ops);
+        assert_se(r == 1);
 
-        valid_hashmap = hashmap_ensure_allocated(&m, &string_hash_ops);
-        assert_se(valid_hashmap == 0);
+        r = hashmap_ensure_allocated(&m, &string_hash_ops);
+        assert_se(r == 0);
 
-        assert_se(m);
-        hashmap_free(m);
+        /* different hash ops shouldn't matter at this point */
+        r = hashmap_ensure_allocated(&m, &trivial_hash_ops);
+        assert_se(r == 0);
 }
 
 static void test_hashmap_foreach_key(void) {
@@ -557,8 +559,7 @@ static void test_hashmap_foreach(void) {
 }
 
 static void test_hashmap_merge(void) {
-        Hashmap *m;
-        Hashmap *n;
+        Hashmap *m, *n;
         char *val1, *val2, *val3, *val4, *r;
 
         log_info("/* %s */", __func__);
@@ -572,8 +573,8 @@ static void test_hashmap_merge(void) {
         val4 = strdup("my val4");
         assert_se(val4);
 
-        n = hashmap_new(&string_hash_ops);
         m = hashmap_new(&string_hash_ops);
+        n = hashmap_new(&string_hash_ops);
 
         hashmap_put(m, "Key 1", val1);
         hashmap_put(m, "Key 2", val2);
@@ -586,8 +587,8 @@ static void test_hashmap_merge(void) {
         r = hashmap_get(m, "Key 4");
         assert_se(r && streq(r, "my val4"));
 
-        assert_se(n);
         assert_se(m);
+        assert_se(n);
         hashmap_free(n);
         hashmap_free_free(m);
 }