1
0
mirror of https://github.com/systemd/systemd.git synced 2024-11-06 16:59:03 +03:00

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.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2019-07-03 13:59:30 +02:00
parent 87da87846d
commit d578f909ce
2 changed files with 67 additions and 59 deletions

View File

@ -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();
}

View File

@ -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;
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/////"));
}
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_parse_environment();
log_open();
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;
}