1 /* SPDX-License-Identifier: LGPL-2.1+ */
6 unsigned custom_counter
= 0;
7 static void custom_destruct(void* p
) {
12 DEFINE_HASH_OPS_FULL(boring_hash_ops
, char, string_hash_func
, string_compare_func
, free
, char, free
);
13 DEFINE_HASH_OPS_FULL(custom_hash_ops
, char, string_hash_func
, string_compare_func
, custom_destruct
, char, custom_destruct
);
15 void test_hashmap_funcs(void);
16 void test_ordered_hashmap_funcs(void);
18 static void test_ordered_hashmap_next(void) {
19 _cleanup_ordered_hashmap_free_ OrderedHashmap
*m
= NULL
;
22 log_info("/* %s */", __func__
);
24 assert_se(m
= ordered_hashmap_new(NULL
));
25 for (i
= -2; i
<= 2; i
++)
26 assert_se(ordered_hashmap_put(m
, INT_TO_PTR(i
), INT_TO_PTR(i
+10)) == 1);
27 for (i
= -2; i
<= 1; i
++)
28 assert_se(ordered_hashmap_next(m
, INT_TO_PTR(i
)) == INT_TO_PTR(i
+11));
29 assert_se(!ordered_hashmap_next(m
, INT_TO_PTR(2)));
30 assert_se(!ordered_hashmap_next(NULL
, INT_TO_PTR(1)));
31 assert_se(!ordered_hashmap_next(m
, INT_TO_PTR(3)));
37 static void item_seen(Item
*item
) {
41 static void test_hashmap_free_with_destructor(void) {
43 struct Item items
[4] = {};
46 log_info("/* %s */", __func__
);
48 assert_se(m
= hashmap_new(NULL
));
49 for (i
= 0; i
< ELEMENTSOF(items
) - 1; i
++)
50 assert_se(hashmap_put(m
, INT_TO_PTR(i
), items
+ i
) == 1);
52 m
= hashmap_free_with_destructor(m
, item_seen
);
53 assert_se(items
[0].seen
== 1);
54 assert_se(items
[1].seen
== 1);
55 assert_se(items
[2].seen
== 1);
56 assert_se(items
[3].seen
== 0);
59 static void test_uint64_compare_func(void) {
60 const uint64_t a
= 0x100, b
= 0x101;
62 assert_se(uint64_compare_func(&a
, &a
) == 0);
63 assert_se(uint64_compare_func(&a
, &b
) == -1);
64 assert_se(uint64_compare_func(&b
, &a
) == 1);
67 static void test_trivial_compare_func(void) {
68 assert_se(trivial_compare_func(INT_TO_PTR('a'), INT_TO_PTR('a')) == 0);
69 assert_se(trivial_compare_func(INT_TO_PTR('a'), INT_TO_PTR('b')) == -1);
70 assert_se(trivial_compare_func(INT_TO_PTR('b'), INT_TO_PTR('a')) == 1);
73 static void test_string_compare_func(void) {
74 assert_se(string_compare_func("fred", "wilma") != 0);
75 assert_se(string_compare_func("fred", "fred") == 0);
78 static void compare_cache(Hashmap
*map
, IteratedCache
*cache
) {
79 const void **keys
= NULL
, **values
= NULL
;
84 assert_se(iterated_cache_get(cache
, &keys
, &values
, &num
) == 0);
85 assert_se(num
== 0 || keys
);
86 assert_se(num
== 0 || values
);
89 HASHMAP_FOREACH_KEY(v
, k
, map
, iter
) {
90 assert_se(v
== values
[idx
]);
91 assert_se(k
== keys
[idx
]);
96 assert_se(idx
== num
);
99 static void test_iterated_cache(void) {
103 log_info("/* %s */", __func__
);
105 assert_se(m
= hashmap_new(NULL
));
106 assert_se(c
= hashmap_iterated_cache_new(m
));
109 for (int stage
= 0; stage
< 100; stage
++) {
111 for (int i
= 0; i
< 100; i
++) {
112 int foo
= stage
* 1000 + i
;
114 assert_se(hashmap_put(m
, INT_TO_PTR(foo
), INT_TO_PTR(foo
+ 777)) == 1);
120 for (int i
= 0; i
< 100; i
++) {
121 int foo
= stage
* 1000 + i
;
123 assert_se(hashmap_remove(m
, INT_TO_PTR(foo
)) == INT_TO_PTR(foo
+ 777));
133 assert_se(hashmap_free(m
) == NULL
);
134 assert_se(iterated_cache_free(c
) == NULL
);
137 static void test_path_hashmap(void) {
138 _cleanup_hashmap_free_ Hashmap
*h
= NULL
;
140 log_info("/* %s */", __func__
);
142 assert_se(h
= hashmap_new(&path_hash_ops
));
144 assert_se(hashmap_put(h
, "foo", INT_TO_PTR(1)) >= 0);
145 assert_se(hashmap_put(h
, "/foo", INT_TO_PTR(2)) >= 0);
146 assert_se(hashmap_put(h
, "//foo", INT_TO_PTR(3)) == -EEXIST
);
147 assert_se(hashmap_put(h
, "//foox/", INT_TO_PTR(4)) >= 0);
148 assert_se(hashmap_put(h
, "/foox////", INT_TO_PTR(5)) == -EEXIST
);
149 assert_se(hashmap_put(h
, "foo//////bar/quux//", INT_TO_PTR(6)) >= 0);
150 assert_se(hashmap_put(h
, "foo/bar//quux/", INT_TO_PTR(8)) == -EEXIST
);
152 assert_se(hashmap_get(h
, "foo") == INT_TO_PTR(1));
153 assert_se(hashmap_get(h
, "foo/") == INT_TO_PTR(1));
154 assert_se(hashmap_get(h
, "foo////") == INT_TO_PTR(1));
155 assert_se(hashmap_get(h
, "/foo") == INT_TO_PTR(2));
156 assert_se(hashmap_get(h
, "//foo") == INT_TO_PTR(2));
157 assert_se(hashmap_get(h
, "/////foo////") == INT_TO_PTR(2));
158 assert_se(hashmap_get(h
, "/////foox////") == INT_TO_PTR(4));
159 assert_se(hashmap_get(h
, "/foox/") == INT_TO_PTR(4));
160 assert_se(hashmap_get(h
, "/foox") == INT_TO_PTR(4));
161 assert_se(!hashmap_get(h
, "foox"));
162 assert_se(hashmap_get(h
, "foo/bar/quux") == INT_TO_PTR(6));
163 assert_se(hashmap_get(h
, "foo////bar////quux/////") == INT_TO_PTR(6));
164 assert_se(!hashmap_get(h
, "/foo////bar////quux/////"));
167 int main(int argc
, const char *argv
[]) {
168 test_hashmap_funcs();
169 test_ordered_hashmap_funcs();
171 test_ordered_hashmap_next();
172 test_hashmap_free_with_destructor();
173 test_uint64_compare_func();
174 test_trivial_compare_func();
175 test_string_compare_func();
176 test_iterated_cache();