1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd
5 Copyright 2013 Daniel Buch
11 void test_hashmap_funcs(void);
12 void test_ordered_hashmap_funcs(void);
14 static void test_ordered_hashmap_next(void) {
15 _cleanup_ordered_hashmap_free_ OrderedHashmap *m = NULL;
18 assert_se(m = ordered_hashmap_new(NULL));
19 for (i = -2; i <= 2; i++)
20 assert_se(ordered_hashmap_put(m, INT_TO_PTR(i), INT_TO_PTR(i+10)) == 1);
21 for (i = -2; i <= 1; i++)
22 assert_se(ordered_hashmap_next(m, INT_TO_PTR(i)) == INT_TO_PTR(i+11));
23 assert_se(!ordered_hashmap_next(m, INT_TO_PTR(2)));
24 assert_se(!ordered_hashmap_next(NULL, INT_TO_PTR(1)));
25 assert_se(!ordered_hashmap_next(m, INT_TO_PTR(3)));
31 static void item_seen(Item *item) {
35 static void test_hashmap_free_with_destructor(void) {
37 struct Item items[4] = {};
40 assert_se(m = hashmap_new(NULL));
41 for (i = 0; i < ELEMENTSOF(items) - 1; i++)
42 assert_se(hashmap_put(m, INT_TO_PTR(i), items + i) == 1);
44 m = hashmap_free_with_destructor(m, item_seen);
45 assert_se(items[0].seen == 1);
46 assert_se(items[1].seen == 1);
47 assert_se(items[2].seen == 1);
48 assert_se(items[3].seen == 0);
51 static void test_uint64_compare_func(void) {
52 const uint64_t a = 0x100, b = 0x101;
54 assert_se(uint64_compare_func(&a, &a) == 0);
55 assert_se(uint64_compare_func(&a, &b) == -1);
56 assert_se(uint64_compare_func(&b, &a) == 1);
59 static void test_trivial_compare_func(void) {
60 assert_se(trivial_compare_func(INT_TO_PTR('a'), INT_TO_PTR('a')) == 0);
61 assert_se(trivial_compare_func(INT_TO_PTR('a'), INT_TO_PTR('b')) == -1);
62 assert_se(trivial_compare_func(INT_TO_PTR('b'), INT_TO_PTR('a')) == 1);
65 static void test_string_compare_func(void) {
66 assert_se(string_compare_func("fred", "wilma") != 0);
67 assert_se(string_compare_func("fred", "fred") == 0);
70 static void compare_cache(Hashmap *map, IteratedCache *cache) {
71 const void **keys = NULL, **values = NULL;
76 assert_se(iterated_cache_get(cache, &keys, &values, &num) == 0);
77 assert_se(num == 0 || keys);
78 assert_se(num == 0 || values);
81 HASHMAP_FOREACH_KEY(v, k, map, iter) {
82 assert_se(v == values[idx]);
83 assert_se(k == keys[idx]);
88 assert_se(idx == num);
91 static void test_iterated_cache(void) {
95 assert_se(m = hashmap_new(NULL));
96 assert_se(c = hashmap_iterated_cache_new(m));
99 for (int stage = 0; stage < 100; stage++) {
101 for (int i = 0; i < 100; i++) {
102 int foo = stage * 1000 + i;
104 assert_se(hashmap_put(m, INT_TO_PTR(foo), INT_TO_PTR(foo + 777)) == 1);
110 for (int i = 0; i < 100; i++) {
111 int foo = stage * 1000 + i;
113 assert_se(hashmap_remove(m, INT_TO_PTR(foo)) == INT_TO_PTR(foo + 777));
123 assert_se(hashmap_free(m) == NULL);
124 assert_se(iterated_cache_free(c) == NULL);
127 static void test_path_hashmap(void) {
128 _cleanup_hashmap_free_ Hashmap *h = NULL;
130 assert_se(h = hashmap_new(&path_hash_ops));
132 assert_se(hashmap_put(h, "foo", INT_TO_PTR(1)) >= 0);
133 assert_se(hashmap_put(h, "/foo", INT_TO_PTR(2)) >= 0);
134 assert_se(hashmap_put(h, "//foo", INT_TO_PTR(3)) == -EEXIST);
135 assert_se(hashmap_put(h, "//foox/", INT_TO_PTR(4)) >= 0);
136 assert_se(hashmap_put(h, "/foox////", INT_TO_PTR(5)) == -EEXIST);
137 assert_se(hashmap_put(h, "foo//////bar/quux//", INT_TO_PTR(6)) >= 0);
138 assert_se(hashmap_put(h, "foo/bar//quux/", INT_TO_PTR(8)) == -EEXIST);
140 assert_se(hashmap_get(h, "foo") == INT_TO_PTR(1));
141 assert_se(hashmap_get(h, "foo/") == INT_TO_PTR(1));
142 assert_se(hashmap_get(h, "foo////") == INT_TO_PTR(1));
143 assert_se(hashmap_get(h, "/foo") == INT_TO_PTR(2));
144 assert_se(hashmap_get(h, "//foo") == INT_TO_PTR(2));
145 assert_se(hashmap_get(h, "/////foo////") == INT_TO_PTR(2));
146 assert_se(hashmap_get(h, "/////foox////") == INT_TO_PTR(4));
147 assert_se(hashmap_get(h, "/foox/") == INT_TO_PTR(4));
148 assert_se(hashmap_get(h, "/foox") == INT_TO_PTR(4));
149 assert_se(!hashmap_get(h, "foox"));
150 assert_se(hashmap_get(h, "foo/bar/quux") == INT_TO_PTR(6));
151 assert_se(hashmap_get(h, "foo////bar////quux/////") == INT_TO_PTR(6));
152 assert_se(!hashmap_get(h, "/foo////bar////quux/////"));
155 int main(int argc, const char *argv[]) {
156 test_hashmap_funcs();
157 test_ordered_hashmap_funcs();
159 test_ordered_hashmap_next();
160 test_hashmap_free_with_destructor();
161 test_uint64_compare_func();
162 test_trivial_compare_func();
163 test_string_compare_func();
164 test_iterated_cache();