chiark / gitweb /
Fix service file to match installed elogind binary location
[elogind.git] / src / test / test-hashmap-plain.c
index 8f7c039e3d514a768529d5783a2cde0ee69ab0dd..1bd5c02f8789a9547d16727d7e7996728e6c7ad4 100644 (file)
   along with systemd; If not, see <http://www.gnu.org/licenses/>.
 ***/
 
+#include "alloc-util.h"
+#include "hashmap.h"
+#include "string-util.h"
 #include "strv.h"
 #include "util.h"
-#include "hashmap.h"
 
 void test_hashmap_funcs(void);
 
@@ -194,8 +196,8 @@ static void test_hashmap_move(void) {
         hashmap_put(m, "key 3", val3);
         hashmap_put(m, "key 4", val4);
 
-        hashmap_move(n, NULL);
-        hashmap_move(n, m);
+        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");
@@ -245,14 +247,19 @@ 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);
+        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);
 }
@@ -316,26 +323,29 @@ 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";
+
+        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");
@@ -380,6 +390,7 @@ 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;
 
         m = hashmap_new(&trivial_hash_ops);
         assert_se(m);
@@ -407,6 +418,25 @@ 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) {
@@ -440,6 +470,7 @@ static void test_hashmap_foreach_key(void) {
                 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"))
@@ -657,7 +688,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 +697,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 = {
@@ -684,7 +715,7 @@ static void test_hashmap_many(void) {
                 unsigned n_entries;
         } tests[] = {
                 { .ops = NULL,                  .n_entries = 1 << 20 },
-                { .ops = &crippled_hashmap_ops, .n_entries = 1 << 11 },
+                { .ops = &crippled_hashmap_ops, .n_entries = 1 << 14 },
         };
 
 
@@ -699,9 +730,9 @@ static void test_hashmap_many(void) {
                 for (i = 1; i < tests[j].n_entries*3; i++)
                         assert_se(hashmap_contains(h, UINT_TO_PTR(i)) == (i % 3 == 1));
 
-                log_info("%u <= %u * 0.75 = %g", hashmap_size(h), hashmap_buckets(h), hashmap_buckets(h) * 0.75);
+                log_info("%u <= %u * 0.8 = %g", hashmap_size(h), hashmap_buckets(h), hashmap_buckets(h) * 0.8);
 
-                assert_se(hashmap_size(h) <= hashmap_buckets(h) * 0.75);
+                assert_se(hashmap_size(h) <= hashmap_buckets(h) * 0.8);
                 assert_se(hashmap_size(h) == tests[j].n_entries);
 
                 while (!hashmap_isempty(h)) {