chiark / gitweb /
bus: fix bus_print_property() to use "int" for booleans
[elogind.git] / src / test / test-hashmap.c
index 2aead79bb1d8637b929f2749aaad6234892e72be..d9863f8dab4006eae606bbda0058da1153e9c58e 100644 (file)
@@ -26,7 +26,7 @@ static void test_hashmap_replace(void) {
         Hashmap *m;
         char *val1, *val2, *val3, *val4, *val5, *r;
 
-        m = hashmap_new(string_hash_func, string_compare_func);
+        m = hashmap_new(&string_hash_ops);
 
         val1 = strdup("val1");
         assert_se(val1);
@@ -73,7 +73,7 @@ static void test_hashmap_copy(void) {
         val4 = strdup("val4");
         assert_se(val4);
 
-        m = hashmap_new(string_hash_func, string_compare_func);
+        m = hashmap_new(&string_hash_ops);
 
         hashmap_put(m, "key 1", val1);
         hashmap_put(m, "key 2", val2);
@@ -109,7 +109,7 @@ static void test_hashmap_get_strv(void) {
         val4 = strdup("val4");
         assert_se(val4);
 
-        m = hashmap_new(string_hash_func, string_compare_func);
+        m = hashmap_new(&string_hash_ops);
 
         hashmap_put(m, "key 1", val1);
         hashmap_put(m, "key 2", val2);
@@ -141,8 +141,8 @@ static void test_hashmap_move_one(void) {
         val4 = strdup("val4");
         assert_se(val4);
 
-        m = hashmap_new(string_hash_func, string_compare_func);
-        n = hashmap_new(string_hash_func, string_compare_func);
+        m = hashmap_new(&string_hash_ops);
+        n = hashmap_new(&string_hash_ops);
 
         hashmap_put(m, "key 1", val1);
         hashmap_put(m, "key 2", val2);
@@ -168,7 +168,7 @@ static void test_hashmap_next(void) {
         Hashmap *m;
         char *val1, *val2, *val3, *val4, *r;
 
-        m = hashmap_new(string_hash_func, string_compare_func);
+        m = hashmap_new(&string_hash_ops);
         val1 = strdup("val1");
         assert_se(val1);
         val2 = strdup("val2");
@@ -199,7 +199,7 @@ static void test_hashmap_update(void) {
         Hashmap *m;
         char *val1, *val2, *r;
 
-        m = hashmap_new(string_hash_func, string_compare_func);
+        m = hashmap_new(&string_hash_ops);
         val1 = strdup("old_value");
         assert_se(val1);
         val2 = strdup("new_value");
@@ -222,7 +222,7 @@ static void test_hashmap_put(void) {
         Hashmap *m;
         int valid_hashmap_put;
 
-        m = hashmap_new(string_hash_func, string_compare_func);
+        m = hashmap_new(&string_hash_ops);
 
         valid_hashmap_put = hashmap_put(m, "key 1", (void*) (const char *) "val 1");
         assert_se(valid_hashmap_put == 1);
@@ -231,13 +231,39 @@ static void test_hashmap_put(void) {
         hashmap_free(m);
 }
 
+static void test_hashmap_remove_and_put(void) {
+        _cleanup_hashmap_free_ Hashmap *m = NULL;
+        int valid;
+        char *r;
+
+        m = hashmap_new(&string_hash_ops);
+        assert_se(m);
+
+        valid = hashmap_remove_and_put(m, "unvalid key", "new key", NULL);
+        assert_se(valid < 0);
+
+        valid = hashmap_put(m, "key 1", (void*) (const char *) "val 1");
+        assert_se(valid == 1);
+        valid = hashmap_remove_and_put(m, "key 1", "key 2", (void*) (const char *) "val 2");
+        assert_se(valid == 0);
+
+        r = hashmap_get(m, "key 2");
+        assert_se(streq(r, "val 2"));
+        assert_se(!hashmap_get(m, "key 1"));
+
+        valid = hashmap_put(m, "key 3", (void*) (const char *) "val 3");
+        assert_se(valid == 1);
+        valid = hashmap_remove_and_put(m, "key 3", "key 2", (void*) (const char *) "val 2");
+        assert_se(valid < 0);
+}
+
 static void test_hashmap_ensure_allocated(void) {
         Hashmap *m;
         int valid_hashmap;
 
-        m = hashmap_new(string_hash_func, string_compare_func);
+        m = hashmap_new(&string_hash_ops);
 
-        valid_hashmap = hashmap_ensure_allocated(&m, string_hash_func, string_compare_func);
+        valid_hashmap = hashmap_ensure_allocated(&m, &string_hash_ops);
         assert_se(valid_hashmap == 0);
 
         assert_se(m);
@@ -256,7 +282,7 @@ static void test_hashmap_foreach_key(void) {
                 "key 3\0"
                 "key 4\0";
 
-        m = hashmap_new(string_hash_func, string_compare_func);
+        m = hashmap_new(&string_hash_ops);
 
         NULSTR_FOREACH(key, key_table)
                 hashmap_put(m, key, (void*) (const char*) "my dummy val");
@@ -293,7 +319,7 @@ static void test_hashmap_foreach(void) {
         val4 = strdup("my val4");
         assert_se(val4);
 
-        m = hashmap_new(string_hash_func, string_compare_func);
+        m = hashmap_new(&string_hash_ops);
 
         hashmap_put(m, "Key 1", val1);
         hashmap_put(m, "Key 2", val2);
@@ -317,44 +343,6 @@ static void test_hashmap_foreach(void) {
         hashmap_free_free(m);
 }
 
-static void test_hashmap_foreach_backwards(void) {
-        Hashmap *m;
-        Iterator i;
-        char *val1, *val2, *val3, *val4, *s;
-        bool value_found[] = { false, false, false, false };
-
-        val1 = strdup("my val1");
-        assert_se(val1);
-        val2 = strdup("my val2");
-        assert_se(val2);
-        val3 = strdup("my val3");
-        assert_se(val3);
-        val4 = strdup("my val4");
-        assert_se(val4);
-
-        m = hashmap_new(string_hash_func, string_compare_func);
-        hashmap_put(m, "Key 1", val1);
-        hashmap_put(m, "Key 2", val2);
-        hashmap_put(m, "Key 3", val3);
-        hashmap_put(m, "Key 4", val4);
-
-        HASHMAP_FOREACH_BACKWARDS(s, m, i) {
-                if (!value_found[0] && streq(s, val1))
-                        value_found[0] = true;
-                else if (!value_found[1] && streq(s, val2))
-                        value_found[1] = true;
-                else if (!value_found[2] && streq(s, val3))
-                        value_found[2] = true;
-                else if (!value_found[3] && streq(s, val4))
-                        value_found[3] = true;
-        }
-
-        assert_se(m);
-        assert_se(value_found[0] && value_found[1] && value_found[2] && value_found[3]);
-
-        hashmap_free_free(m);
-}
-
 static void test_hashmap_merge(void) {
         Hashmap *m;
         Hashmap *n;
@@ -369,8 +357,8 @@ static void test_hashmap_merge(void) {
         val4 = strdup("my val4");
         assert_se(val4);
 
-        n = hashmap_new(string_hash_func, string_compare_func);
-        m = hashmap_new(string_hash_func, string_compare_func);
+        n = hashmap_new(&string_hash_ops);
+        m = hashmap_new(&string_hash_ops);
 
         hashmap_put(m, "Key 1", val1);
         hashmap_put(m, "Key 2", val2);
@@ -396,7 +384,7 @@ static void test_hashmap_contains(void) {
         val1 = strdup("my val");
         assert_se(val1);
 
-        m = hashmap_new(string_hash_func, string_compare_func);
+        m = hashmap_new(&string_hash_ops);
 
         assert_se(!hashmap_contains(m, "Key 1"));
         hashmap_put(m, "Key 1", val1);
@@ -413,7 +401,7 @@ static void test_hashmap_isempty(void) {
         val1 = strdup("my val");
         assert_se(val1);
 
-        m = hashmap_new(string_hash_func, string_compare_func);
+        m = hashmap_new(&string_hash_ops);
 
         assert_se(hashmap_isempty(m));
         hashmap_put(m, "Key 1", val1);
@@ -436,7 +424,7 @@ static void test_hashmap_size(void) {
         val4 = strdup("my val");
         assert_se(val4);
 
-        m = hashmap_new(string_hash_func, string_compare_func);
+        m = hashmap_new(&string_hash_ops);
 
         hashmap_put(m, "Key 1", val1);
         hashmap_put(m, "Key 2", val2);
@@ -456,7 +444,7 @@ static void test_hashmap_get(void) {
         val = strdup("my val");
         assert_se(val);
 
-        m = hashmap_new(string_hash_func, string_compare_func);
+        m = hashmap_new(&string_hash_ops);
 
         hashmap_put(m, "Key 1", val);
 
@@ -467,10 +455,78 @@ static void test_hashmap_get(void) {
         hashmap_free_free(m);
 }
 
+static void test_hashmap_many(void) {
+        Hashmap *h;
+        unsigned i;
+
+#define N_ENTRIES 100000
+
+        assert_se(h = hashmap_new(NULL));
+
+        for (i = 1; i < N_ENTRIES*3; i+=3) {
+                assert_se(hashmap_put(h, UINT_TO_PTR(i), UINT_TO_PTR(i)) >= 0);
+                assert_se(PTR_TO_UINT(hashmap_get(h, UINT_TO_PTR(i))) == i);
+        }
+
+        for (i = 1; i < 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);
+
+        assert_se(hashmap_size(h) <= hashmap_buckets(h) * 0.75);
+        assert_se(hashmap_size(h) == N_ENTRIES);
+
+        hashmap_free(h);
+}
+
+static void test_hashmap_first_key(void) {
+        _cleanup_hashmap_free_ Hashmap *m = NULL;
+
+        m = hashmap_new(&string_hash_ops);
+        assert_se(m);
+
+        assert_se(!hashmap_first_key(m));
+        assert_se(hashmap_put(m, "key 1", NULL) == 1);
+        assert_se(streq(hashmap_first_key(m), "key 1"));
+        assert_se(hashmap_put(m, "key 2", NULL) == 1);
+        assert_se(streq(hashmap_first_key(m), "key 1"));
+        assert_se(hashmap_remove(m, "key 1") == NULL);
+        assert_se(streq(hashmap_first_key(m), "key 2"));
+}
+
+static void test_hashmap_steal_first_key(void) {
+        _cleanup_hashmap_free_ Hashmap *m = NULL;
+
+        m = hashmap_new(&string_hash_ops);
+        assert_se(m);
+
+        assert_se(!hashmap_steal_first_key(m));
+        assert_se(hashmap_put(m, "key 1", NULL) == 1);
+        assert_se(streq(hashmap_steal_first_key(m), "key 1"));
+
+        assert_se(hashmap_isempty(m));
+}
+
+static void test_hashmap_clear_free_free(void) {
+        _cleanup_hashmap_free_ Hashmap *m = NULL;
+
+        m = hashmap_new(&string_hash_ops);
+        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_free(m);
+        assert_se(hashmap_isempty(m));
+}
+
 static void test_uint64_compare_func(void) {
-        assert_se(uint64_compare_func("a", "a") == 0);
-        assert_se(uint64_compare_func("a", "b") == -1);
-        assert_se(uint64_compare_func("b", "a") == 1);
+        const uint64_t a = 0x100, b = 0x101;
+
+        assert_se(uint64_compare_func(&a, &a) == 0);
+        assert_se(uint64_compare_func(&a, &b) == -1);
+        assert_se(uint64_compare_func(&b, &a) == 1);
 }
 
 static void test_trivial_compare_func(void) {
@@ -484,8 +540,7 @@ static void test_string_compare_func(void) {
         assert_se(string_compare_func("fred", "fred") == 0);
 }
 
-int main(int argc, const char *argv[])
-{
+int main(int argc, const char *argv[]) {
         test_hashmap_copy();
         test_hashmap_get_strv();
         test_hashmap_move_one();
@@ -493,15 +548,19 @@ int main(int argc, const char *argv[])
         test_hashmap_replace();
         test_hashmap_update();
         test_hashmap_put();
+        test_hashmap_remove_and_put();
         test_hashmap_ensure_allocated();
         test_hashmap_foreach();
-        test_hashmap_foreach_backwards();
         test_hashmap_foreach_key();
         test_hashmap_contains();
         test_hashmap_merge();
         test_hashmap_isempty();
         test_hashmap_get();
         test_hashmap_size();
+        test_hashmap_many();
+        test_hashmap_first_key();
+        test_hashmap_steal_first_key();
+        test_hashmap_clear_free_free();
         test_uint64_compare_func();
         test_trivial_compare_func();
         test_string_compare_func();