1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright © 2013 Daniel Buch
9 void test_hashmap_funcs(void);
10 void test_ordered_hashmap_funcs(void);
12 static void test_ordered_hashmap_next(void) {
13 _cleanup_ordered_hashmap_free_ OrderedHashmap
*m
= NULL
;
16 assert_se(m
= ordered_hashmap_new(NULL
));
17 for (i
= -2; i
<= 2; i
++)
18 assert_se(ordered_hashmap_put(m
, INT_TO_PTR(i
), INT_TO_PTR(i
+10)) == 1);
19 for (i
= -2; i
<= 1; i
++)
20 assert_se(ordered_hashmap_next(m
, INT_TO_PTR(i
)) == INT_TO_PTR(i
+11));
21 assert_se(!ordered_hashmap_next(m
, INT_TO_PTR(2)));
22 assert_se(!ordered_hashmap_next(NULL
, INT_TO_PTR(1)));
23 assert_se(!ordered_hashmap_next(m
, INT_TO_PTR(3)));
29 static void item_seen(Item
*item
) {
33 static void test_hashmap_free_with_destructor(void) {
35 struct Item items
[4] = {};
38 assert_se(m
= hashmap_new(NULL
));
39 for (i
= 0; i
< ELEMENTSOF(items
) - 1; i
++)
40 assert_se(hashmap_put(m
, INT_TO_PTR(i
), items
+ i
) == 1);
42 m
= hashmap_free_with_destructor(m
, item_seen
);
43 assert_se(items
[0].seen
== 1);
44 assert_se(items
[1].seen
== 1);
45 assert_se(items
[2].seen
== 1);
46 assert_se(items
[3].seen
== 0);
49 static void test_uint64_compare_func(void) {
50 const uint64_t a
= 0x100, b
= 0x101;
52 assert_se(uint64_compare_func(&a
, &a
) == 0);
53 assert_se(uint64_compare_func(&a
, &b
) == -1);
54 assert_se(uint64_compare_func(&b
, &a
) == 1);
57 static void test_trivial_compare_func(void) {
58 assert_se(trivial_compare_func(INT_TO_PTR('a'), INT_TO_PTR('a')) == 0);
59 assert_se(trivial_compare_func(INT_TO_PTR('a'), INT_TO_PTR('b')) == -1);
60 assert_se(trivial_compare_func(INT_TO_PTR('b'), INT_TO_PTR('a')) == 1);
63 static void test_string_compare_func(void) {
64 assert_se(string_compare_func("fred", "wilma") != 0);
65 assert_se(string_compare_func("fred", "fred") == 0);
68 static void compare_cache(Hashmap
*map
, IteratedCache
*cache
) {
69 const void **keys
= NULL
, **values
= NULL
;
74 assert_se(iterated_cache_get(cache
, &keys
, &values
, &num
) == 0);
75 assert_se(num
== 0 || keys
);
76 assert_se(num
== 0 || values
);
79 HASHMAP_FOREACH_KEY(v
, k
, map
, iter
) {
80 assert_se(v
== values
[idx
]);
81 assert_se(k
== keys
[idx
]);
86 assert_se(idx
== num
);
89 static void test_iterated_cache(void) {
93 assert_se(m
= hashmap_new(NULL
));
94 assert_se(c
= hashmap_iterated_cache_new(m
));
97 for (int stage
= 0; stage
< 100; stage
++) {
99 for (int i
= 0; i
< 100; i
++) {
100 int foo
= stage
* 1000 + i
;
102 assert_se(hashmap_put(m
, INT_TO_PTR(foo
), INT_TO_PTR(foo
+ 777)) == 1);
108 for (int i
= 0; i
< 100; i
++) {
109 int foo
= stage
* 1000 + i
;
111 assert_se(hashmap_remove(m
, INT_TO_PTR(foo
)) == INT_TO_PTR(foo
+ 777));
121 assert_se(hashmap_free(m
) == NULL
);
122 assert_se(iterated_cache_free(c
) == NULL
);
125 static void test_path_hashmap(void) {
126 _cleanup_hashmap_free_ Hashmap
*h
= NULL
;
128 assert_se(h
= hashmap_new(&path_hash_ops
));
130 assert_se(hashmap_put(h
, "foo", INT_TO_PTR(1)) >= 0);
131 assert_se(hashmap_put(h
, "/foo", INT_TO_PTR(2)) >= 0);
132 assert_se(hashmap_put(h
, "//foo", INT_TO_PTR(3)) == -EEXIST
);
133 assert_se(hashmap_put(h
, "//foox/", INT_TO_PTR(4)) >= 0);
134 assert_se(hashmap_put(h
, "/foox////", INT_TO_PTR(5)) == -EEXIST
);
135 assert_se(hashmap_put(h
, "foo//////bar/quux//", INT_TO_PTR(6)) >= 0);
136 assert_se(hashmap_put(h
, "foo/bar//quux/", INT_TO_PTR(8)) == -EEXIST
);
138 assert_se(hashmap_get(h
, "foo") == INT_TO_PTR(1));
139 assert_se(hashmap_get(h
, "foo/") == INT_TO_PTR(1));
140 assert_se(hashmap_get(h
, "foo////") == INT_TO_PTR(1));
141 assert_se(hashmap_get(h
, "/foo") == INT_TO_PTR(2));
142 assert_se(hashmap_get(h
, "//foo") == INT_TO_PTR(2));
143 assert_se(hashmap_get(h
, "/////foo////") == INT_TO_PTR(2));
144 assert_se(hashmap_get(h
, "/////foox////") == INT_TO_PTR(4));
145 assert_se(hashmap_get(h
, "/foox/") == INT_TO_PTR(4));
146 assert_se(hashmap_get(h
, "/foox") == INT_TO_PTR(4));
147 assert_se(!hashmap_get(h
, "foox"));
148 assert_se(hashmap_get(h
, "foo/bar/quux") == INT_TO_PTR(6));
149 assert_se(hashmap_get(h
, "foo////bar////quux/////") == INT_TO_PTR(6));
150 assert_se(!hashmap_get(h
, "/foo////bar////quux/////"));
153 int main(int argc
, const char *argv
[]) {
154 test_hashmap_funcs();
155 test_ordered_hashmap_funcs();
157 test_ordered_hashmap_next();
158 test_hashmap_free_with_destructor();
159 test_uint64_compare_func();
160 test_trivial_compare_func();
161 test_string_compare_func();
162 test_iterated_cache();