1 /* SPDX-License-Identifier: LGPL-2.1+ */
6 void test_hashmap_funcs(void);
7 void test_ordered_hashmap_funcs(void);
9 static void test_ordered_hashmap_next(void) {
10 _cleanup_ordered_hashmap_free_ OrderedHashmap
*m
= NULL
;
13 assert_se(m
= ordered_hashmap_new(NULL
));
14 for (i
= -2; i
<= 2; i
++)
15 assert_se(ordered_hashmap_put(m
, INT_TO_PTR(i
), INT_TO_PTR(i
+10)) == 1);
16 for (i
= -2; i
<= 1; i
++)
17 assert_se(ordered_hashmap_next(m
, INT_TO_PTR(i
)) == INT_TO_PTR(i
+11));
18 assert_se(!ordered_hashmap_next(m
, INT_TO_PTR(2)));
19 assert_se(!ordered_hashmap_next(NULL
, INT_TO_PTR(1)));
20 assert_se(!ordered_hashmap_next(m
, INT_TO_PTR(3)));
26 static void item_seen(Item
*item
) {
30 static void test_hashmap_free_with_destructor(void) {
32 struct Item items
[4] = {};
35 assert_se(m
= hashmap_new(NULL
));
36 for (i
= 0; i
< ELEMENTSOF(items
) - 1; i
++)
37 assert_se(hashmap_put(m
, INT_TO_PTR(i
), items
+ i
) == 1);
39 m
= hashmap_free_with_destructor(m
, item_seen
);
40 assert_se(items
[0].seen
== 1);
41 assert_se(items
[1].seen
== 1);
42 assert_se(items
[2].seen
== 1);
43 assert_se(items
[3].seen
== 0);
46 static void test_uint64_compare_func(void) {
47 const uint64_t a
= 0x100, b
= 0x101;
49 assert_se(uint64_compare_func(&a
, &a
) == 0);
50 assert_se(uint64_compare_func(&a
, &b
) == -1);
51 assert_se(uint64_compare_func(&b
, &a
) == 1);
54 static void test_trivial_compare_func(void) {
55 assert_se(trivial_compare_func(INT_TO_PTR('a'), INT_TO_PTR('a')) == 0);
56 assert_se(trivial_compare_func(INT_TO_PTR('a'), INT_TO_PTR('b')) == -1);
57 assert_se(trivial_compare_func(INT_TO_PTR('b'), INT_TO_PTR('a')) == 1);
60 static void test_string_compare_func(void) {
61 assert_se(string_compare_func("fred", "wilma") != 0);
62 assert_se(string_compare_func("fred", "fred") == 0);
65 static void compare_cache(Hashmap
*map
, IteratedCache
*cache
) {
66 const void **keys
= NULL
, **values
= NULL
;
71 assert_se(iterated_cache_get(cache
, &keys
, &values
, &num
) == 0);
72 assert_se(num
== 0 || keys
);
73 assert_se(num
== 0 || values
);
76 HASHMAP_FOREACH_KEY(v
, k
, map
, iter
) {
77 assert_se(v
== values
[idx
]);
78 assert_se(k
== keys
[idx
]);
83 assert_se(idx
== num
);
86 static void test_iterated_cache(void) {
90 assert_se(m
= hashmap_new(NULL
));
91 assert_se(c
= hashmap_iterated_cache_new(m
));
94 for (int stage
= 0; stage
< 100; stage
++) {
96 for (int i
= 0; i
< 100; i
++) {
97 int foo
= stage
* 1000 + i
;
99 assert_se(hashmap_put(m
, INT_TO_PTR(foo
), INT_TO_PTR(foo
+ 777)) == 1);
105 for (int i
= 0; i
< 100; i
++) {
106 int foo
= stage
* 1000 + i
;
108 assert_se(hashmap_remove(m
, INT_TO_PTR(foo
)) == INT_TO_PTR(foo
+ 777));
118 assert_se(hashmap_free(m
) == NULL
);
119 assert_se(iterated_cache_free(c
) == NULL
);
122 static void test_path_hashmap(void) {
123 _cleanup_hashmap_free_ Hashmap
*h
= NULL
;
125 assert_se(h
= hashmap_new(&path_hash_ops
));
127 assert_se(hashmap_put(h
, "foo", INT_TO_PTR(1)) >= 0);
128 assert_se(hashmap_put(h
, "/foo", INT_TO_PTR(2)) >= 0);
129 assert_se(hashmap_put(h
, "//foo", INT_TO_PTR(3)) == -EEXIST
);
130 assert_se(hashmap_put(h
, "//foox/", INT_TO_PTR(4)) >= 0);
131 assert_se(hashmap_put(h
, "/foox////", INT_TO_PTR(5)) == -EEXIST
);
132 assert_se(hashmap_put(h
, "foo//////bar/quux//", INT_TO_PTR(6)) >= 0);
133 assert_se(hashmap_put(h
, "foo/bar//quux/", INT_TO_PTR(8)) == -EEXIST
);
135 assert_se(hashmap_get(h
, "foo") == INT_TO_PTR(1));
136 assert_se(hashmap_get(h
, "foo/") == INT_TO_PTR(1));
137 assert_se(hashmap_get(h
, "foo////") == INT_TO_PTR(1));
138 assert_se(hashmap_get(h
, "/foo") == INT_TO_PTR(2));
139 assert_se(hashmap_get(h
, "//foo") == INT_TO_PTR(2));
140 assert_se(hashmap_get(h
, "/////foo////") == INT_TO_PTR(2));
141 assert_se(hashmap_get(h
, "/////foox////") == INT_TO_PTR(4));
142 assert_se(hashmap_get(h
, "/foox/") == INT_TO_PTR(4));
143 assert_se(hashmap_get(h
, "/foox") == INT_TO_PTR(4));
144 assert_se(!hashmap_get(h
, "foox"));
145 assert_se(hashmap_get(h
, "foo/bar/quux") == INT_TO_PTR(6));
146 assert_se(hashmap_get(h
, "foo////bar////quux/////") == INT_TO_PTR(6));
147 assert_se(!hashmap_get(h
, "/foo////bar////quux/////"));
150 int main(int argc
, const char *argv
[]) {
151 test_hashmap_funcs();
152 test_ordered_hashmap_funcs();
154 test_ordered_hashmap_next();
155 test_hashmap_free_with_destructor();
156 test_uint64_compare_func();
157 test_trivial_compare_func();
158 test_string_compare_func();
159 test_iterated_cache();