diff options
author | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2019-07-03 13:59:30 +0200 |
---|---|---|
committer | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2019-07-19 16:51:14 +0200 |
commit | d578f909ce7f74c56be18c8ad189e49e2210daee (patch) | |
tree | 728935ab7778ae36db5210482eb06dc573943d5d | |
parent | 87da87846dbfb07358d01f9d53f1c35ec61b447d (diff) | |
download | systemd-d578f909ce7f74c56be18c8ad189e49e2210daee.tar.gz |
test-hashmap: move tests which should also apply to ordered hashmaps and add comment
Effectively this does two more tests also for ordered hashmaps.
This setup is a bit confusing, let's add a comment.
-rw-r--r-- | src/test/test-hashmap-plain.c | 60 | ||||
-rw-r--r-- | src/test/test-hashmap.c | 64 |
2 files changed, 66 insertions, 58 deletions
diff --git a/src/test/test-hashmap-plain.c b/src/test/test-hashmap-plain.c index 02cc396cf7..57cf89ff53 100644 --- a/src/test/test-hashmap-plain.c +++ b/src/test/test-hashmap-plain.c @@ -831,6 +831,31 @@ static void test_hashmap_free(void) { } } +typedef struct Item { + int seen; +} Item; +static void item_seen(Item *item) { + item->seen++; +} + +static void test_hashmap_free_with_destructor(void) { + Hashmap *m; + struct Item items[4] = {}; + unsigned i; + + log_info("/* %s */", __func__); + + assert_se(m = hashmap_new(NULL)); + for (i = 0; i < ELEMENTSOF(items) - 1; i++) + assert_se(hashmap_put(m, INT_TO_PTR(i), items + i) == 1); + + m = hashmap_free_with_destructor(m, item_seen); + assert_se(items[0].seen == 1); + assert_se(items[1].seen == 1); + assert_se(items[2].seen == 1); + assert_se(items[3].seen == 0); +} + static void test_hashmap_first(void) { _cleanup_hashmap_free_ Hashmap *m = NULL; @@ -978,6 +1003,36 @@ static void test_hashmap_reserve(void) { assert_se(hashmap_reserve(m, UINT_MAX - 1) == -ENOMEM); } +static void test_path_hashmap(void) { + _cleanup_hashmap_free_ Hashmap *h = NULL; + + log_info("/* %s */", __func__); + + assert_se(h = hashmap_new(&path_hash_ops)); + + assert_se(hashmap_put(h, "foo", INT_TO_PTR(1)) >= 0); + assert_se(hashmap_put(h, "/foo", INT_TO_PTR(2)) >= 0); + assert_se(hashmap_put(h, "//foo", INT_TO_PTR(3)) == -EEXIST); + assert_se(hashmap_put(h, "//foox/", INT_TO_PTR(4)) >= 0); + assert_se(hashmap_put(h, "/foox////", INT_TO_PTR(5)) == -EEXIST); + assert_se(hashmap_put(h, "foo//////bar/quux//", INT_TO_PTR(6)) >= 0); + assert_se(hashmap_put(h, "foo/bar//quux/", INT_TO_PTR(8)) == -EEXIST); + + assert_se(hashmap_get(h, "foo") == INT_TO_PTR(1)); + assert_se(hashmap_get(h, "foo/") == INT_TO_PTR(1)); + assert_se(hashmap_get(h, "foo////") == INT_TO_PTR(1)); + assert_se(hashmap_get(h, "/foo") == INT_TO_PTR(2)); + assert_se(hashmap_get(h, "//foo") == INT_TO_PTR(2)); + assert_se(hashmap_get(h, "/////foo////") == INT_TO_PTR(2)); + assert_se(hashmap_get(h, "/////foox////") == INT_TO_PTR(4)); + assert_se(hashmap_get(h, "/foox/") == INT_TO_PTR(4)); + assert_se(hashmap_get(h, "/foox") == INT_TO_PTR(4)); + assert_se(!hashmap_get(h, "foox")); + assert_se(hashmap_get(h, "foo/bar/quux") == INT_TO_PTR(6)); + assert_se(hashmap_get(h, "foo////bar////quux/////") == INT_TO_PTR(6)); + assert_se(!hashmap_get(h, "/foo////bar////quux/////")); +} + static void test_string_strv_hashmap(void) { _cleanup_hashmap_free_ Hashmap *m = NULL; char **s; @@ -1006,8 +1061,7 @@ static void test_string_strv_hashmap(void) { } void test_hashmap_funcs(void) { - log_parse_environment(); - log_open(); + log_info("/************ %s ************/", __func__); test_hashmap_copy(); test_hashmap_get_strv(); @@ -1032,6 +1086,7 @@ void test_hashmap_funcs(void) { test_hashmap_size(); test_hashmap_many(); test_hashmap_free(); + test_hashmap_free_with_destructor(); test_hashmap_first(); test_hashmap_first_key(); test_hashmap_steal_first_key(); @@ -1039,5 +1094,6 @@ void test_hashmap_funcs(void) { test_hashmap_clear_free_free(); test_hashmap_clear_free_with_destructor(); test_hashmap_reserve(); + test_path_hashmap(); test_string_strv_hashmap(); } diff --git a/src/test/test-hashmap.c b/src/test/test-hashmap.c index ee4c0e66db..1a6e8ffa58 100644 --- a/src/test/test-hashmap.c +++ b/src/test/test-hashmap.c @@ -31,31 +31,6 @@ static void test_ordered_hashmap_next(void) { assert_se(!ordered_hashmap_next(m, INT_TO_PTR(3))); } -typedef struct Item { - int seen; -} Item; -static void item_seen(Item *item) { - item->seen++; -} - -static void test_hashmap_free_with_destructor(void) { - Hashmap *m; - struct Item items[4] = {}; - unsigned i; - - log_info("/* %s */", __func__); - - assert_se(m = hashmap_new(NULL)); - for (i = 0; i < ELEMENTSOF(items) - 1; i++) - assert_se(hashmap_put(m, INT_TO_PTR(i), items + i) == 1); - - m = hashmap_free_with_destructor(m, item_seen); - assert_se(items[0].seen == 1); - assert_se(items[1].seen == 1); - assert_se(items[2].seen == 1); - assert_se(items[3].seen == 0); -} - static void test_uint64_compare_func(void) { const uint64_t a = 0x100, b = 0x101; @@ -134,47 +109,24 @@ static void test_iterated_cache(void) { assert_se(iterated_cache_free(c) == NULL); } -static void test_path_hashmap(void) { - _cleanup_hashmap_free_ Hashmap *h = NULL; +int main(int argc, const char *argv[]) { + /* This file tests in test-hashmap-plain.c, and tests in test-hashmap-ordered.c, which is generated + * from test-hashmap-plain.c. Hashmap tests should be added to test-hashmap-plain.c, and here only if + * they don't apply to ordered hashmaps. */ - log_info("/* %s */", __func__); + log_parse_environment(); + log_open(); - assert_se(h = hashmap_new(&path_hash_ops)); - - assert_se(hashmap_put(h, "foo", INT_TO_PTR(1)) >= 0); - assert_se(hashmap_put(h, "/foo", INT_TO_PTR(2)) >= 0); - assert_se(hashmap_put(h, "//foo", INT_TO_PTR(3)) == -EEXIST); - assert_se(hashmap_put(h, "//foox/", INT_TO_PTR(4)) >= 0); - assert_se(hashmap_put(h, "/foox////", INT_TO_PTR(5)) == -EEXIST); - assert_se(hashmap_put(h, "foo//////bar/quux//", INT_TO_PTR(6)) >= 0); - assert_se(hashmap_put(h, "foo/bar//quux/", INT_TO_PTR(8)) == -EEXIST); - - assert_se(hashmap_get(h, "foo") == INT_TO_PTR(1)); - assert_se(hashmap_get(h, "foo/") == INT_TO_PTR(1)); - assert_se(hashmap_get(h, "foo////") == INT_TO_PTR(1)); - assert_se(hashmap_get(h, "/foo") == INT_TO_PTR(2)); - assert_se(hashmap_get(h, "//foo") == INT_TO_PTR(2)); - assert_se(hashmap_get(h, "/////foo////") == INT_TO_PTR(2)); - assert_se(hashmap_get(h, "/////foox////") == INT_TO_PTR(4)); - assert_se(hashmap_get(h, "/foox/") == INT_TO_PTR(4)); - assert_se(hashmap_get(h, "/foox") == INT_TO_PTR(4)); - assert_se(!hashmap_get(h, "foox")); - assert_se(hashmap_get(h, "foo/bar/quux") == INT_TO_PTR(6)); - assert_se(hashmap_get(h, "foo////bar////quux/////") == INT_TO_PTR(6)); - assert_se(!hashmap_get(h, "/foo////bar////quux/////")); -} - -int main(int argc, const char *argv[]) { test_hashmap_funcs(); test_ordered_hashmap_funcs(); + log_info("/************ non-shared tests ************/"); + test_ordered_hashmap_next(); - test_hashmap_free_with_destructor(); test_uint64_compare_func(); test_trivial_compare_func(); test_string_compare_func(); test_iterated_cache(); - test_path_hashmap(); return 0; } |