]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/test/test-hashmap-plain.c
Add SPDX license identifiers to source files under the LGPL
[thirdparty/systemd.git] / src / test / test-hashmap-plain.c
index f7917521c572e8dd61fc47cbe7fd1a1840dd3f71..1725766e5a72cef384648dd02bf6c601145d01e5 100644 (file)
@@ -1,3 +1,4 @@
+/* SPDX-License-Identifier: LGPL-2.1+ */
 /***
   This file is part of systemd
 
   along with systemd; If not, see <http://www.gnu.org/licenses/>.
 ***/
 
+#include "alloc-util.h"
+#include "env-util.h"
+#include "hashmap.h"
+#include "log.h"
+#include "string-util.h"
 #include "strv.h"
 #include "util.h"
-#include "hashmap.h"
+
+static bool arg_slow = false;
 
 void test_hashmap_funcs(void);
 
@@ -27,6 +34,8 @@ static void test_hashmap_replace(void) {
         Hashmap *m;
         char *val1, *val2, *val3, *val4, *val5, *r;
 
+        log_info("%s", __func__);
+
         m = hashmap_new(&string_hash_ops);
 
         val1 = strdup("val1");
@@ -65,6 +74,8 @@ static void test_hashmap_copy(void) {
         Hashmap *m, *copy;
         char *val1, *val2, *val3, *val4, *r;
 
+        log_info("%s", __func__);
+
         val1 = strdup("val1");
         assert_se(val1);
         val2 = strdup("val2");
@@ -101,6 +112,8 @@ static void test_hashmap_get_strv(void) {
         char **strv;
         char *val1, *val2, *val3, *val4;
 
+        log_info("%s", __func__);
+
         val1 = strdup("val1");
         assert_se(val1);
         val2 = strdup("val2");
@@ -137,6 +150,8 @@ static void test_hashmap_move_one(void) {
         Hashmap *m, *n;
         char *val1, *val2, *val3, *val4, *r;
 
+        log_info("%s", __func__);
+
         val1 = strdup("val1");
         assert_se(val1);
         val2 = strdup("val2");
@@ -176,6 +191,8 @@ static void test_hashmap_move(void) {
         Hashmap *m, *n;
         char *val1, *val2, *val3, *val4, *r;
 
+        log_info("%s", __func__);
+
         val1 = strdup("val1");
         assert_se(val1);
         val2 = strdup("val2");
@@ -194,8 +211,8 @@ static void test_hashmap_move(void) {
         hashmap_put(m, "key 3", val3);
         hashmap_put(m, "key 4", val4);
 
-        assert(hashmap_move(n, NULL) == 0);
-        assert(hashmap_move(n, m) == 0);
+        assert_se(hashmap_move(n, NULL) == 0);
+        assert_se(hashmap_move(n, m) == 0);
 
         assert_se(hashmap_size(m) == 1);
         r = hashmap_get(m, "key 1");
@@ -218,6 +235,8 @@ static void test_hashmap_update(void) {
         Hashmap *m;
         char *val1, *val2, *r;
 
+        log_info("%s", __func__);
+
         m = hashmap_new(&string_hash_ops);
         val1 = strdup("old_value");
         assert_se(val1);
@@ -245,14 +264,21 @@ static void test_hashmap_put(void) {
         Hashmap *m = NULL;
         int valid_hashmap_put;
         void *val1 = (void*) "val 1";
+        void *val2 = (void*) "val 2";
+        _cleanup_free_ char* key1 = NULL;
 
-        hashmap_ensure_allocated(&m, &string_hash_ops);
+        log_info("%s", __func__);
+
+        assert_se(hashmap_ensure_allocated(&m, &string_hash_ops) >= 0);
         assert_se(m);
 
         valid_hashmap_put = hashmap_put(m, "key 1", val1);
         assert_se(valid_hashmap_put == 1);
         assert_se(hashmap_put(m, "key 1", val1) == 0);
-        assert_se(hashmap_put(m, "key 1", (void *)"val 2") == -EEXIST);
+        assert_se(hashmap_put(m, "key 1", val2) == -EEXIST);
+        key1 = strdup("key 1");
+        assert_se(hashmap_put(m, key1, val1) == 0);
+        assert_se(hashmap_put(m, key1, val2) == -EEXIST);
 
         hashmap_free(m);
 }
@@ -261,6 +287,8 @@ static void test_hashmap_remove(void) {
         _cleanup_hashmap_free_ Hashmap *m = NULL;
         char *r;
 
+        log_info("%s", __func__);
+
         r = hashmap_remove(NULL, "key 1");
         assert_se(r == NULL);
 
@@ -289,6 +317,8 @@ static void test_hashmap_remove2(void) {
         char val2[] = "val 2";
         void *r, *r2;
 
+        log_info("%s", __func__);
+
         r = hashmap_remove2(NULL, "key 1", &r2);
         assert_se(r == NULL);
 
@@ -316,26 +346,31 @@ static void test_hashmap_remove_value(void) {
         _cleanup_hashmap_free_ Hashmap *m = NULL;
         char *r;
 
-        r = hashmap_remove_value(NULL, "key 1", (void*) "val 1");
+        char val1[] = "val 1";
+        char val2[] = "val 2";
+
+        log_info("%s", __func__);
+
+        r = hashmap_remove_value(NULL, "key 1", val1);
         assert_se(r == NULL);
 
         m = hashmap_new(&string_hash_ops);
         assert_se(m);
 
-        r = hashmap_remove_value(m, "key 1", (void*) "val 1");
+        r = hashmap_remove_value(m, "key 1", val1);
         assert_se(r == NULL);
 
-        hashmap_put(m, "key 1", (void*) "val 1");
-        hashmap_put(m, "key 2", (void*) "val 2");
+        hashmap_put(m, "key 1", val1);
+        hashmap_put(m, "key 2", val2);
 
-        r = hashmap_remove_value(m, "key 1", (void*) "val 1");
+        r = hashmap_remove_value(m, "key 1", val1);
         assert_se(streq(r, "val 1"));
 
         r = hashmap_get(m, "key 2");
         assert_se(streq(r, "val 2"));
         assert_se(!hashmap_get(m, "key 1"));
 
-        r = hashmap_remove_value(m, "key 2", (void*) "val 1");
+        r = hashmap_remove_value(m, "key 2", val1);
         assert_se(r == NULL);
 
         r = hashmap_get(m, "key 2");
@@ -348,6 +383,8 @@ static void test_hashmap_remove_and_put(void) {
         int valid;
         char *r;
 
+        log_info("%s", __func__);
+
         m = hashmap_new(&string_hash_ops);
         assert_se(m);
 
@@ -380,6 +417,9 @@ static void test_hashmap_remove_and_replace(void) {
         void *key2 = UINT_TO_PTR(2);
         void *key3 = UINT_TO_PTR(3);
         void *r;
+        int i, j;
+
+        log_info("%s", __func__);
 
         m = hashmap_new(&trivial_hash_ops);
         assert_se(m);
@@ -407,12 +447,33 @@ static void test_hashmap_remove_and_replace(void) {
         r = hashmap_get(m, key2);
         assert_se(r == key2);
         assert_se(!hashmap_get(m, key3));
+
+        /* Repeat this test several times to increase the chance of hitting
+         * the less likely case in hashmap_remove_and_replace where it
+         * compensates for the backward shift. */
+        for (i = 0; i < 20; i++) {
+                hashmap_clear(m);
+
+                for (j = 1; j < 7; j++)
+                        hashmap_put(m, UINT_TO_PTR(10*i + j), UINT_TO_PTR(10*i + j));
+                valid = hashmap_remove_and_replace(m, UINT_TO_PTR(10*i + 1),
+                                                   UINT_TO_PTR(10*i + 2),
+                                                   UINT_TO_PTR(10*i + 2));
+                assert_se(valid == 0);
+                assert_se(!hashmap_get(m, UINT_TO_PTR(10*i + 1)));
+                for (j = 2; j < 7; j++) {
+                        r = hashmap_get(m, UINT_TO_PTR(10*i + j));
+                        assert_se(r == UINT_TO_PTR(10*i + j));
+                }
+        }
 }
 
 static void test_hashmap_ensure_allocated(void) {
         Hashmap *m;
         int valid_hashmap;
 
+        log_info("%s", __func__);
+
         m = hashmap_new(&string_hash_ops);
 
         valid_hashmap = hashmap_ensure_allocated(&m, &string_hash_ops);
@@ -434,12 +495,15 @@ static void test_hashmap_foreach_key(void) {
                 "key 3\0"
                 "key 4\0";
 
+        log_info("%s", __func__);
+
         m = hashmap_new(&string_hash_ops);
 
         NULSTR_FOREACH(key, key_table)
                 hashmap_put(m, key, (void*) (const char*) "my dummy val");
 
         HASHMAP_FOREACH_KEY(s, key, m, i) {
+                assert(s);
                 if (!key_found[0] && streq(key, "key 1"))
                         key_found[0] = true;
                 else if (!key_found[1] && streq(key, "key 2"))
@@ -463,6 +527,8 @@ static void test_hashmap_foreach(void) {
         char *val1, *val2, *val3, *val4, *s;
         unsigned count;
 
+        log_info("%s", __func__);
+
         val1 = strdup("my val1");
         assert_se(val1);
         val2 = strdup("my val2");
@@ -513,6 +579,8 @@ static void test_hashmap_merge(void) {
         Hashmap *n;
         char *val1, *val2, *val3, *val4, *r;
 
+        log_info("%s", __func__);
+
         val1 = strdup("my val1");
         assert_se(val1);
         val2 = strdup("my val2");
@@ -546,6 +614,8 @@ static void test_hashmap_contains(void) {
         Hashmap *m;
         char *val1;
 
+        log_info("%s", __func__);
+
         val1 = strdup("my val");
         assert_se(val1);
 
@@ -566,6 +636,8 @@ static void test_hashmap_isempty(void) {
         Hashmap *m;
         char *val1;
 
+        log_info("%s", __func__);
+
         val1 = strdup("my val");
         assert_se(val1);
 
@@ -583,6 +655,8 @@ static void test_hashmap_size(void) {
         Hashmap *m;
         char *val1, *val2, *val3, *val4;
 
+        log_info("%s", __func__);
+
         val1 = strdup("my val");
         assert_se(val1);
         val2 = strdup("my val");
@@ -613,6 +687,8 @@ static void test_hashmap_get(void) {
         char *r;
         char *val;
 
+        log_info("%s", __func__);
+
         val = strdup("my val");
         assert_se(val);
 
@@ -640,6 +716,8 @@ static void test_hashmap_get2(void) {
         char key_orig[] = "Key 1";
         void *key_copy;
 
+        log_info("%s", __func__);
+
         val = strdup("my val");
         assert_se(val);
 
@@ -657,7 +735,7 @@ static void test_hashmap_get2(void) {
         r = hashmap_get2(m, key_orig, &key_copy);
         assert_se(streq(r, val));
         assert_se(key_orig != key_copy);
-        assert_se(streq(key_orig, key_orig));
+        assert_se(streq(key_orig, key_copy));
 
         r = hashmap_get2(m, "no such key", NULL);
         assert_se(r == NULL);
@@ -666,8 +744,8 @@ static void test_hashmap_get2(void) {
         hashmap_free_free_free(m);
 }
 
-static unsigned long crippled_hashmap_func(const void *p, const uint8_t hash_key[HASH_KEY_SIZE]) {
-        return trivial_hash_func(p, hash_key) & 0xff;
+static void crippled_hashmap_func(const void *p, struct siphash *state) {
+        return trivial_hash_func(INT_TO_PTR(PTR_TO_INT(p) & 0xff), state);
 }
 
 static const struct hash_ops crippled_hashmap_ops = {
@@ -679,14 +757,15 @@ static void test_hashmap_many(void) {
         Hashmap *h;
         unsigned i, j;
         void *v, *k;
-        static const struct {
+        const struct {
                 const struct hash_ops *ops;
                 unsigned n_entries;
         } tests[] = {
-                { .ops = NULL,                  .n_entries = 1 << 20 },
-                { .ops = &crippled_hashmap_ops, .n_entries = 1 << 11 },
+                { .ops = NULL,                  .n_entries = arg_slow ? 1 << 20 : 240 },
+                { .ops = &crippled_hashmap_ops, .n_entries = arg_slow ? 1 << 14 : 140 },
         };
 
+        log_info("%s (%s)", __func__, arg_slow ? "slow" : "fast");
 
         for (j = 0; j < ELEMENTSOF(tests); j++) {
                 assert_se(h = hashmap_new(tests[j].ops));
@@ -717,6 +796,8 @@ static void test_hashmap_many(void) {
 static void test_hashmap_first(void) {
         _cleanup_hashmap_free_ Hashmap *m = NULL;
 
+        log_info("%s", __func__);
+
         m = hashmap_new(&string_hash_ops);
         assert_se(m);
 
@@ -734,6 +815,8 @@ static void test_hashmap_first(void) {
 static void test_hashmap_first_key(void) {
         _cleanup_hashmap_free_ Hashmap *m = NULL;
 
+        log_info("%s", __func__);
+
         m = hashmap_new(&string_hash_ops);
         assert_se(m);
 
@@ -751,6 +834,8 @@ static void test_hashmap_first_key(void) {
 static void test_hashmap_steal_first_key(void) {
         _cleanup_hashmap_free_ Hashmap *m = NULL;
 
+        log_info("%s", __func__);
+
         m = hashmap_new(&string_hash_ops);
         assert_se(m);
 
@@ -766,6 +851,8 @@ static void test_hashmap_steal_first(void) {
         int seen[3] = {};
         char *val;
 
+        log_info("%s", __func__);
+
         m = hashmap_new(&string_hash_ops);
         assert_se(m);
 
@@ -784,6 +871,8 @@ static void test_hashmap_steal_first(void) {
 static void test_hashmap_clear_free_free(void) {
         _cleanup_hashmap_free_ Hashmap *m = NULL;
 
+        log_info("%s", __func__);
+
         m = hashmap_new(&string_hash_ops);
         assert_se(m);
 
@@ -798,6 +887,8 @@ static void test_hashmap_clear_free_free(void) {
 static void test_hashmap_reserve(void) {
         _cleanup_hashmap_free_ Hashmap *m = NULL;
 
+        log_info("%s", __func__);
+
         m = hashmap_new(&string_hash_ops);
 
         assert_se(hashmap_reserve(m, 1) == 0);
@@ -813,6 +904,14 @@ static void test_hashmap_reserve(void) {
 }
 
 void test_hashmap_funcs(void) {
+        int r;
+
+        log_parse_environment();
+        log_open();
+
+        r = getenv_bool("SYSTEMD_SLOW_TESTS");
+        arg_slow = r >= 0 ? r : SYSTEMD_SLOW_TESTS_DEFAULT;
+
         test_hashmap_copy();
         test_hashmap_get_strv();
         test_hashmap_move_one();