}
TEST(hashmap_copy) {
- _cleanup_hashmap_free_ Hashmap *m = NULL;
- _cleanup_hashmap_free_free_ Hashmap *copy = NULL;
- char *val1, *val2, *val3, *val4, *r;
-
- val1 = strdup("val1");
- assert_se(val1);
- val2 = strdup("val2");
- assert_se(val2);
- val3 = strdup("val3");
- assert_se(val3);
- val4 = strdup("val4");
- assert_se(val4);
+ _cleanup_hashmap_free_ Hashmap *m = NULL, *copy = NULL;
- m = hashmap_new(&string_hash_ops);
+ ASSERT_NOT_NULL(m = hashmap_new(&string_hash_ops));
- hashmap_put(m, "key 1", val1);
- hashmap_put(m, "key 2", val2);
- hashmap_put(m, "key 3", val3);
- hashmap_put(m, "key 4", val4);
+ ASSERT_OK_POSITIVE(hashmap_put(m, "key 1", (void*) "val1"));
+ ASSERT_OK_POSITIVE(hashmap_put(m, "key 2", (void*) "val2"));
+ ASSERT_OK_POSITIVE(hashmap_put(m, "key 3", (void*) "val3"));
+ ASSERT_OK_POSITIVE(hashmap_put(m, "key 4", (void*) "val4"));
- copy = hashmap_copy(m);
+ ASSERT_NOT_NULL(copy = hashmap_copy(m));
- r = hashmap_get(copy, "key 1");
- ASSERT_STREQ(r, "val1");
- r = hashmap_get(copy, "key 2");
- ASSERT_STREQ(r, "val2");
- r = hashmap_get(copy, "key 3");
- ASSERT_STREQ(r, "val3");
- r = hashmap_get(copy, "key 4");
- ASSERT_STREQ(r, "val4");
+ ASSERT_STREQ(hashmap_get(copy, "key 1"), "val1");
+ ASSERT_STREQ(hashmap_get(copy, "key 2"), "val2");
+ ASSERT_STREQ(hashmap_get(copy, "key 3"), "val3");
+ ASSERT_STREQ(hashmap_get(copy, "key 4"), "val4");
}
TEST(hashmap_get_strv) {
}
TEST(hashmap_move_one) {
- _cleanup_hashmap_free_free_ Hashmap *m = NULL, *n = NULL;
+ _cleanup_hashmap_free_ Hashmap *m = NULL, *n = NULL;
char *val1, *val2, *val3, *val4, *r;
val1 = strdup("val1");
val4 = strdup("val4");
assert_se(val4);
- m = hashmap_new(&string_hash_ops);
- n = hashmap_new(&string_hash_ops);
+ m = hashmap_new(&string_hash_ops_value_free);
+ n = hashmap_new(&string_hash_ops_value_free);
hashmap_put(m, "key 1", val1);
hashmap_put(m, "key 2", val2);
}
TEST(hashmap_move) {
- _cleanup_hashmap_free_free_ Hashmap *m = NULL, *n = NULL;
+ _cleanup_hashmap_free_ Hashmap *m = NULL, *n = NULL;
char *val1, *val2, *val3, *val4, *r;
val1 = strdup("val1");
val4 = strdup("val4");
assert_se(val4);
- m = hashmap_new(&string_hash_ops);
- n = hashmap_new(&string_hash_ops);
+ m = hashmap_new(&string_hash_ops_value_free);
+ n = hashmap_new(&string_hash_ops_value_free);
hashmap_put(n, "key 1", strdup(val1));
hashmap_put(m, "key 1", val1);
}
TEST(hashmap_remove2) {
- _cleanup_hashmap_free_free_free_ Hashmap *m = NULL;
+ _cleanup_hashmap_free_ Hashmap *m = NULL;
char key1[] = "key 1";
char key2[] = "key 2";
char val1[] = "val 1";
r = hashmap_remove2(NULL, "key 1", &r2);
ASSERT_NULL(r);
- m = hashmap_new(&string_hash_ops);
+ m = hashmap_new(&string_hash_ops_free_free);
assert_se(m);
r = hashmap_remove2(m, "no such key", &r2);
}
TEST(hashmap_foreach) {
- _cleanup_hashmap_free_free_ Hashmap *m = NULL;
+ _cleanup_hashmap_free_ Hashmap *m = NULL;
bool value_found[] = { false, false, false, false };
char *val1, *val2, *val3, *val4, *s;
unsigned count;
count++;
assert_se(count == 0);
- m = hashmap_new(&string_hash_ops);
+ m = hashmap_new(&string_hash_ops_value_free);
count = 0;
HASHMAP_FOREACH(s, m)
}
TEST(hashmap_merge) {
- _cleanup_hashmap_free_free_ Hashmap *m = NULL;
- _cleanup_hashmap_free_ Hashmap *n = NULL;
+ _cleanup_hashmap_free_ Hashmap *m = NULL, *n = NULL;
char *val1, *val2, *val3, *val4, *r;
val1 = strdup("my val1");
val4 = strdup("my val4");
assert_se(val4);
- m = hashmap_new(&string_hash_ops);
+ m = hashmap_new(&string_hash_ops_value_free);
n = hashmap_new(&string_hash_ops);
hashmap_put(m, "Key 1", val1);
}
TEST(hashmap_contains) {
- _cleanup_hashmap_free_free_ Hashmap *m = NULL;
+ _cleanup_hashmap_free_ Hashmap *m = NULL;
char *val1;
val1 = strdup("my val");
assert_se(val1);
- m = hashmap_new(&string_hash_ops);
+ m = hashmap_new(&string_hash_ops_value_free);
assert_se(!hashmap_contains(m, "Key 1"));
hashmap_put(m, "Key 1", val1);
}
TEST(hashmap_isempty) {
- _cleanup_hashmap_free_free_ Hashmap *m = NULL;
+ _cleanup_hashmap_free_ Hashmap *m = NULL;
char *val1;
val1 = strdup("my val");
assert_se(val1);
- m = hashmap_new(&string_hash_ops);
+ m = hashmap_new(&string_hash_ops_value_free);
assert_se(hashmap_isempty(m));
hashmap_put(m, "Key 1", val1);
}
TEST(hashmap_size) {
- _cleanup_hashmap_free_free_ Hashmap *m = NULL;
+ _cleanup_hashmap_free_ Hashmap *m = NULL;
char *val1, *val2, *val3, *val4;
val1 = strdup("my val");
assert_se(hashmap_size(NULL) == 0);
assert_se(hashmap_buckets(NULL) == 0);
- m = hashmap_new(&string_hash_ops);
+ m = hashmap_new(&string_hash_ops_value_free);
hashmap_put(m, "Key 1", val1);
hashmap_put(m, "Key 2", val2);
}
TEST(hashmap_get) {
- _cleanup_hashmap_free_free_ Hashmap *m = NULL;
+ _cleanup_hashmap_free_ Hashmap *m = NULL;
char *r;
char *val;
r = hashmap_get(NULL, "Key 1");
ASSERT_NULL(r);
- m = hashmap_new(&string_hash_ops);
+ m = hashmap_new(&string_hash_ops_value_free);
hashmap_put(m, "Key 1", val);
}
TEST(hashmap_get2) {
- _cleanup_hashmap_free_free_free_ Hashmap *m = NULL;
+ _cleanup_hashmap_free_ Hashmap *m = NULL;
char *r;
char *val;
char key_orig[] = "Key 1";
r = hashmap_get2(NULL, key_orig, &key_copy);
ASSERT_NULL(r);
- m = hashmap_new(&string_hash_ops);
+ m = hashmap_new(&string_hash_ops_free_free);
hashmap_put(m, key_copy, val);
key_copy = NULL;
assert_se(hashmap_isempty(m));
}
-TEST(hashmap_clear_free_free) {
+DEFINE_PRIVATE_HASH_OPS_WITH_KEY_DESTRUCTOR(test_hash_ops_key, char, string_hash_func, string_compare_func, free);
+DEFINE_PRIVATE_HASH_OPS_FULL(test_hash_ops_full, char, string_hash_func, string_compare_func, free, char, free);
+
+TEST(hashmap_clear) {
_cleanup_hashmap_free_ Hashmap *m = NULL;
- m = hashmap_new(&string_hash_ops);
+ m = hashmap_new(&string_hash_ops_free_free);
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);
+ hashmap_clear(m);
assert_se(hashmap_isempty(m));
assert_se(hashmap_put(m, strdup("key 1"), strdup("value 1")) == 1);
assert_se(hashmap_put(m, strdup("key 2"), strdup("value 2")) == 1);
assert_se(hashmap_put(m, strdup("key 3"), strdup("value 3")) == 1);
- hashmap_clear_free_free(m);
+ hashmap_clear(m);
assert_se(hashmap_isempty(m));
-}
-
-DEFINE_PRIVATE_HASH_OPS_WITH_KEY_DESTRUCTOR(test_hash_ops_key, char, string_hash_func, string_compare_func, free);
-DEFINE_PRIVATE_HASH_OPS_FULL(test_hash_ops_full, char, string_hash_func, string_compare_func, free, char, free);
-
-TEST(hashmap_clear_free_with_destructor) {
- _cleanup_hashmap_free_ Hashmap *m = NULL;
+ m = hashmap_free(m);
m = hashmap_new(&test_hash_ops_key);
assert_se(m);
assert_se(hashmap_put(m, strdup("key 2"), NULL) == 1);
assert_se(hashmap_put(m, strdup("key 3"), NULL) == 1);
- hashmap_clear_free(m);
+ hashmap_clear(m);
assert_se(hashmap_isempty(m));
m = hashmap_free(m);
assert_se(hashmap_put(m, strdup("key 2"), strdup("value 2")) == 1);
assert_se(hashmap_put(m, strdup("key 3"), strdup("value 3")) == 1);
- hashmap_clear_free(m);
+ hashmap_clear(m);
assert_se(hashmap_isempty(m));
}