This is first part of hashmap test unit - last part is in development. So review is much appreciated
Thanks in advance. --- Makefile.am | 10 +++ src/test/test-hashmap.c | 203 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 213 insertions(+) create mode 100644 src/test/test-hashmap.c diff --git a/Makefile.am b/Makefile.am index d5eb7ad..a95dd6e 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1075,6 +1075,7 @@ noinst_tests += \ test-unit-name \ test-unit-file \ test-util \ + test-hashmap \ test-date \ test-sleep \ test-replace-var \ @@ -1182,6 +1183,15 @@ test_prioq_CFLAGS = \ test_prioq_LDADD = \ libsystemd-core.la +test_hashmap_SOURCES = \ + src/test/test-hashmap.c + +test_hashmap_CFLAGS = \ + $(AM_CFLAGS) + +test_hashmap_LDADD = \ + libsystemd-core.la + test_log_SOURCES = \ src/test/test-log.c diff --git a/src/test/test-hashmap.c b/src/test/test-hashmap.c new file mode 100644 index 0000000..610607b --- /dev/null +++ b/src/test/test-hashmap.c @@ -0,0 +1,203 @@ +#include <inttypes.h> +#include "util.h" +#include "hashmap.h" + +#define FOREACH_CONST_CHAR(value, table) \ + for (const char **(value) = (table); (value) && *(value); (value)++) + +static void test_hashmap_foreach_key(void) { + Hashmap *m = hashmap_new(string_hash_func, string_compare_func); + Iterator i; + const char *s; + const char *key_table[] = {"key 1", "key 2", "key 3", "key 4", NULL }; + bool key_found[] = { false, false, false, false }; + + FOREACH_CONST_CHAR(value, key_table) + hashmap_put(m, *value, (void*) (const char*) "blah"); + + HASHMAP_FOREACH_KEY(s, key_table, m, i) { + if (!key_found[0] && streq(*key_table, "key 1")) + key_found[0] = true; + else if (!key_found[1] && streq(*key_table, "key 2")) + key_found[1] = true; + else if (!key_found[2] && streq(*key_table, "key 3")) + key_found[2] = true; + else if (!key_found[3] && streq(*key_table, "fail")) + key_found[3] = true; + } + + assert_se(key_found[0] && key_found[1] && key_found[2] && !key_found[3]); + + hashmap_free(m); +} + +static void test_hashmap_foreach(void) { + Hashmap *m = hashmap_new(string_hash_func, string_compare_func); + Iterator i; + char *s; + char *val1 = strdup("my val1"); + char *val2 = strdup("my val2"); + char *val3 = strdup("my val3"); + char *val4 = strdup("my val4"); + bool value_found[] = { false, false, false, false }; + + hashmap_put(m, "KeyN 1", val1); + hashmap_put(m, "KeyN 2", val2); + hashmap_put(m, "KeyN 3", val3); + hashmap_put(m, "KeyN 4", val4); + + HASHMAP_FOREACH(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(value_found[0] && value_found[1] && value_found[2] && value_found[3]); + + hashmap_free_free(m); +} + +static void test_hashmap_foreach_backwards(void) { + Hashmap *m = hashmap_new(string_hash_func, string_compare_func); + Iterator i; + char *s; + char *val1 = strdup("my val1"); + char *val2 = strdup("my val2"); + char *val3 = strdup("my val3"); + char *val4 = strdup("my val4"); + bool value_found[] = { false, false, false, false }; + + hashmap_put(m, "KeyN 1", val1); + hashmap_put(m, "KeyN 2", val2); + hashmap_put(m, "KeyN 3", val3); + hashmap_put(m, "KeyN 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(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_new(string_hash_func, string_compare_func); + Hashmap *n = hashmap_new(string_hash_func, string_compare_func); + + char *val1 = strdup("my val1"); + char *val2 = strdup("my val2"); + char *val3 = strdup("my val3"); + char *val4 = strdup("my val4"); + + hashmap_put(m, "KeyN 1", val1); + hashmap_put(m, "KeyN 2", val2); + hashmap_put(n, "KeyN 3", val3); + hashmap_put(n, "KeyN 4", val4); + + assert_se(hashmap_merge(m, n) == 0); + assert_se(hashmap_contains(m, "KeyN 3")); + assert_se(hashmap_contains(m, "KeyN 4")); + + hashmap_free_free(m); +} + +static void test_hashmap_contains(void) { + Hashmap *m = hashmap_new(string_hash_func, string_compare_func); + char *val1 = strdup("my val"); + + assert_se(!hashmap_contains(m, "KeyN 1")); + hashmap_put(m, "KeyN 1", val1); + assert_se(hashmap_contains(m, "KeyN 1")); + + hashmap_free_free(m); +} + +static void test_hashmap_isempty(void) { + Hashmap *m = hashmap_new(string_hash_func, string_compare_func); + char *val1 = strdup("my val"); + + assert_se(hashmap_isempty(m)); + + hashmap_put(m, "KeyN 1", val1); + + assert_se(!hashmap_isempty(m)); + + hashmap_free_free(m); +} + +static void test_hashmap_size(void) { + Hashmap *m = hashmap_new(string_hash_func, string_compare_func); + + char *val1 = strdup("my val"); + char *val2 = strdup("my val"); + char *val3 = strdup("my val"); + char *val4 = strdup("my val"); + + hashmap_put(m, "KeyN 1", val1); + hashmap_put(m, "KeyN 2", val2); + hashmap_put(m, "KeyN 3", val3); + hashmap_put(m, "KeyN 4", val4); + + assert(hashmap_size(m) == 4); + hashmap_free_free(m); +} + +static void test_hashmap_get(void) { + Hashmap *m; + char *r; + char *val = strdup("my val"); + + m = hashmap_new(string_hash_func, string_compare_func); + hashmap_put(m, "KeyN 1", val); + + r = hashmap_get(m, "KeyN 1"); + assert_se(streq(r, val)); + + hashmap_free_free(m); +} + +static void test_uint64_compare_func(void) { + assert_se((uint64_t)trivial_compare_func("a", "a") == (uint64_t)0); + assert_se((uint64_t)trivial_compare_func("a", "b") == (uint64_t)-1); + assert_se((uint64_t)trivial_compare_func("b", "a") == (uint64_t)1); +} + +static void test_trivial_compare_func(void) { + assert_se(trivial_compare_func("a", "a") == 0); + assert_se(trivial_compare_func("a", "b") == -1); + assert_se(trivial_compare_func("b", "a") == 1); +} + +static void test_string_compare_func(void) { + assert_se(!string_compare_func("hello", "world") == 0); + assert_se(string_compare_func("hello", "hello") == 0); +} + +int main(int argc, const char *argv[]) +{ + test_hashmap_foreach(); + test_hashmap_foreach_backwards(); + test_hashmap_foreach_key(); + test_hashmap_merge(); + test_hashmap_contains(); + test_hashmap_isempty(); + test_hashmap_get(); + test_hashmap_size(); + test_uint64_compare_func(); + test_trivial_compare_func(); + test_string_compare_func(); + return 0; +} -- 1.8.2 _______________________________________________ systemd-devel mailing list systemd-devel@lists.freedesktop.org http://lists.freedesktop.org/mailman/listinfo/systemd-devel