[systemd-devel] [PATCH] tests: add tests to test-hashmap

Ronny Chevalier chevalier.ronny at gmail.com
Sun Jun 15 13:46:05 PDT 2014


add tests for:
  - hashmap_remove_and_put
  - hashmap_first_key
  - hashmap_last
  - hashmap_steal_first_key
  - hashmap_clear_free_free
---
 src/test/test-hashmap.c | 88 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 88 insertions(+)

diff --git a/src/test/test-hashmap.c b/src/test/test-hashmap.c
index 56a9b58..ccef61f 100644
--- a/src/test/test-hashmap.c
+++ b/src/test/test-hashmap.c
@@ -231,6 +231,32 @@ 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_func, string_compare_func);
+        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;
@@ -491,6 +517,63 @@ static void test_hashmap_many(void) {
         hashmap_free(h);
 }
 
+static void test_hashmap_first_key(void) {
+        _cleanup_hashmap_free_ Hashmap *m = NULL;
+
+        m = hashmap_new(string_hash_func, string_compare_func);
+        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_last(void) {
+        _cleanup_hashmap_free_ Hashmap *m = NULL;
+
+        m = hashmap_new(string_hash_func, string_compare_func);
+        assert_se(m);
+
+        assert_se(!hashmap_last(m));
+        assert_se(hashmap_put(m, "key 1", (void *) (const char *) "val 1") == 1);
+        assert_se(streq(hashmap_last(m), "val 1"));
+        assert_se(hashmap_put(m, "key 2", (void *) (const char *) "bar") == 1);
+        assert_se(streq(hashmap_last(m), "bar"));
+        assert_se(hashmap_remove(m, "key 2"));
+        assert_se(streq(hashmap_last(m), "val 1"));
+}
+
+static void test_hashmap_steal_first_key(void) {
+        _cleanup_hashmap_free_ Hashmap *m = NULL;
+
+        m = hashmap_new(string_hash_func, string_compare_func);
+        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_func, string_compare_func);
+        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) {
         const uint64_t a = 0x100, b = 0x101;
 
@@ -518,6 +601,7 @@ 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();
@@ -528,6 +612,10 @@ int main(int argc, const char *argv[]) {
         test_hashmap_get();
         test_hashmap_size();
         test_hashmap_many();
+        test_hashmap_first_key();
+        test_hashmap_last();
+        test_hashmap_steal_first_key();
+        test_hashmap_clear_free_free();
         test_uint64_compare_func();
         test_trivial_compare_func();
         test_string_compare_func();
-- 
2.0.0



More information about the systemd-devel mailing list