]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
9341a4a1 | 2 | |
9341a4a1 | 3 | #include "hashmap.h" |
cf0fbc49 | 4 | #include "util.h" |
9341a4a1 | 5 | |
8872c3a3 ZJS |
6 | unsigned custom_counter = 0; |
7 | static void custom_destruct(void* p) { | |
8 | custom_counter--; | |
9 | free(p); | |
10 | } | |
11 | ||
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); | |
14 | ||
32a4456c MS |
15 | void test_hashmap_funcs(void); |
16 | void test_ordered_hashmap_funcs(void); | |
9341a4a1 | 17 | |
32a4456c | 18 | static void test_ordered_hashmap_next(void) { |
8f8a5213 MS |
19 | _cleanup_ordered_hashmap_free_ OrderedHashmap *m = NULL; |
20 | int i; | |
21 | ||
32ca2911 ZJS |
22 | log_info("/* %s */", __func__); |
23 | ||
8f8a5213 MS |
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))); | |
d06b3a9d RC |
32 | } |
33 | ||
224b0e7a ZJS |
34 | typedef struct Item { |
35 | int seen; | |
36 | } Item; | |
37 | static void item_seen(Item *item) { | |
38 | item->seen++; | |
39 | } | |
40 | ||
41 | static void test_hashmap_free_with_destructor(void) { | |
42 | Hashmap *m; | |
43 | struct Item items[4] = {}; | |
44 | unsigned i; | |
45 | ||
32ca2911 ZJS |
46 | log_info("/* %s */", __func__); |
47 | ||
224b0e7a ZJS |
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); | |
51 | ||
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); | |
57 | } | |
58 | ||
9341a4a1 | 59 | static void test_uint64_compare_func(void) { |
8097ab4f ZJS |
60 | const uint64_t a = 0x100, b = 0x101; |
61 | ||
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); | |
9341a4a1 DB |
65 | } |
66 | ||
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); | |
71 | } | |
72 | ||
73 | static void test_string_compare_func(void) { | |
4b3eff61 | 74 | assert_se(string_compare_func("fred", "wilma") != 0); |
9341a4a1 DB |
75 | assert_se(string_compare_func("fred", "fred") == 0); |
76 | } | |
77 | ||
647c7b74 VC |
78 | static void compare_cache(Hashmap *map, IteratedCache *cache) { |
79 | const void **keys = NULL, **values = NULL; | |
80 | unsigned num, idx; | |
81 | Iterator iter; | |
82 | void *k, *v; | |
83 | ||
84 | assert_se(iterated_cache_get(cache, &keys, &values, &num) == 0); | |
85 | assert_se(num == 0 || keys); | |
86 | assert_se(num == 0 || values); | |
87 | ||
88 | idx = 0; | |
89 | HASHMAP_FOREACH_KEY(v, k, map, iter) { | |
90 | assert_se(v == values[idx]); | |
91 | assert_se(k == keys[idx]); | |
92 | ||
93 | idx++; | |
94 | } | |
95 | ||
96 | assert_se(idx == num); | |
97 | } | |
98 | ||
99 | static void test_iterated_cache(void) { | |
100 | Hashmap *m; | |
101 | IteratedCache *c; | |
102 | ||
32ca2911 ZJS |
103 | log_info("/* %s */", __func__); |
104 | ||
647c7b74 VC |
105 | assert_se(m = hashmap_new(NULL)); |
106 | assert_se(c = hashmap_iterated_cache_new(m)); | |
107 | compare_cache(m, c); | |
108 | ||
109 | for (int stage = 0; stage < 100; stage++) { | |
110 | ||
111 | for (int i = 0; i < 100; i++) { | |
112 | int foo = stage * 1000 + i; | |
113 | ||
114 | assert_se(hashmap_put(m, INT_TO_PTR(foo), INT_TO_PTR(foo + 777)) == 1); | |
115 | } | |
116 | ||
117 | compare_cache(m, c); | |
118 | ||
119 | if (!(stage % 10)) { | |
120 | for (int i = 0; i < 100; i++) { | |
121 | int foo = stage * 1000 + i; | |
122 | ||
123 | assert_se(hashmap_remove(m, INT_TO_PTR(foo)) == INT_TO_PTR(foo + 777)); | |
124 | } | |
125 | ||
126 | compare_cache(m, c); | |
127 | } | |
128 | } | |
129 | ||
130 | hashmap_clear(m); | |
131 | compare_cache(m, c); | |
132 | ||
133 | assert_se(hashmap_free(m) == NULL); | |
134 | assert_se(iterated_cache_free(c) == NULL); | |
135 | } | |
136 | ||
46e16b34 | 137 | static void test_path_hashmap(void) { |
3ecdd18f | 138 | _cleanup_hashmap_free_ Hashmap *h = NULL; |
46e16b34 | 139 | |
32ca2911 ZJS |
140 | log_info("/* %s */", __func__); |
141 | ||
46e16b34 LP |
142 | assert_se(h = hashmap_new(&path_hash_ops)); |
143 | ||
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); | |
151 | ||
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/////")); | |
165 | } | |
166 | ||
45fa9e29 | 167 | int main(int argc, const char *argv[]) { |
32a4456c MS |
168 | test_hashmap_funcs(); |
169 | test_ordered_hashmap_funcs(); | |
170 | ||
171 | test_ordered_hashmap_next(); | |
224b0e7a | 172 | test_hashmap_free_with_destructor(); |
9341a4a1 DB |
173 | test_uint64_compare_func(); |
174 | test_trivial_compare_func(); | |
175 | test_string_compare_func(); | |
647c7b74 | 176 | test_iterated_cache(); |
46e16b34 LP |
177 | test_path_hashmap(); |
178 | ||
179 | return 0; | |
9341a4a1 | 180 | } |