]>
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 | |
32a4456c MS |
6 | void test_hashmap_funcs(void); |
7 | void test_ordered_hashmap_funcs(void); | |
9341a4a1 | 8 | |
32a4456c | 9 | static void test_ordered_hashmap_next(void) { |
8f8a5213 MS |
10 | _cleanup_ordered_hashmap_free_ OrderedHashmap *m = NULL; |
11 | int i; | |
12 | ||
32ca2911 ZJS |
13 | log_info("/* %s */", __func__); |
14 | ||
8f8a5213 MS |
15 | assert_se(m = ordered_hashmap_new(NULL)); |
16 | for (i = -2; i <= 2; i++) | |
17 | assert_se(ordered_hashmap_put(m, INT_TO_PTR(i), INT_TO_PTR(i+10)) == 1); | |
18 | for (i = -2; i <= 1; i++) | |
19 | assert_se(ordered_hashmap_next(m, INT_TO_PTR(i)) == INT_TO_PTR(i+11)); | |
20 | assert_se(!ordered_hashmap_next(m, INT_TO_PTR(2))); | |
21 | assert_se(!ordered_hashmap_next(NULL, INT_TO_PTR(1))); | |
22 | assert_se(!ordered_hashmap_next(m, INT_TO_PTR(3))); | |
d06b3a9d RC |
23 | } |
24 | ||
224b0e7a ZJS |
25 | typedef struct Item { |
26 | int seen; | |
27 | } Item; | |
28 | static void item_seen(Item *item) { | |
29 | item->seen++; | |
30 | } | |
31 | ||
32 | static void test_hashmap_free_with_destructor(void) { | |
33 | Hashmap *m; | |
34 | struct Item items[4] = {}; | |
35 | unsigned i; | |
36 | ||
32ca2911 ZJS |
37 | log_info("/* %s */", __func__); |
38 | ||
224b0e7a ZJS |
39 | assert_se(m = hashmap_new(NULL)); |
40 | for (i = 0; i < ELEMENTSOF(items) - 1; i++) | |
41 | assert_se(hashmap_put(m, INT_TO_PTR(i), items + i) == 1); | |
42 | ||
43 | m = hashmap_free_with_destructor(m, item_seen); | |
44 | assert_se(items[0].seen == 1); | |
45 | assert_se(items[1].seen == 1); | |
46 | assert_se(items[2].seen == 1); | |
47 | assert_se(items[3].seen == 0); | |
48 | } | |
49 | ||
9341a4a1 | 50 | static void test_uint64_compare_func(void) { |
8097ab4f ZJS |
51 | const uint64_t a = 0x100, b = 0x101; |
52 | ||
53 | assert_se(uint64_compare_func(&a, &a) == 0); | |
54 | assert_se(uint64_compare_func(&a, &b) == -1); | |
55 | assert_se(uint64_compare_func(&b, &a) == 1); | |
9341a4a1 DB |
56 | } |
57 | ||
58 | static void test_trivial_compare_func(void) { | |
59 | assert_se(trivial_compare_func(INT_TO_PTR('a'), INT_TO_PTR('a')) == 0); | |
60 | assert_se(trivial_compare_func(INT_TO_PTR('a'), INT_TO_PTR('b')) == -1); | |
61 | assert_se(trivial_compare_func(INT_TO_PTR('b'), INT_TO_PTR('a')) == 1); | |
62 | } | |
63 | ||
64 | static void test_string_compare_func(void) { | |
4b3eff61 | 65 | assert_se(string_compare_func("fred", "wilma") != 0); |
9341a4a1 DB |
66 | assert_se(string_compare_func("fred", "fred") == 0); |
67 | } | |
68 | ||
647c7b74 VC |
69 | static void compare_cache(Hashmap *map, IteratedCache *cache) { |
70 | const void **keys = NULL, **values = NULL; | |
71 | unsigned num, idx; | |
72 | Iterator iter; | |
73 | void *k, *v; | |
74 | ||
75 | assert_se(iterated_cache_get(cache, &keys, &values, &num) == 0); | |
76 | assert_se(num == 0 || keys); | |
77 | assert_se(num == 0 || values); | |
78 | ||
79 | idx = 0; | |
80 | HASHMAP_FOREACH_KEY(v, k, map, iter) { | |
81 | assert_se(v == values[idx]); | |
82 | assert_se(k == keys[idx]); | |
83 | ||
84 | idx++; | |
85 | } | |
86 | ||
87 | assert_se(idx == num); | |
88 | } | |
89 | ||
90 | static void test_iterated_cache(void) { | |
91 | Hashmap *m; | |
92 | IteratedCache *c; | |
93 | ||
32ca2911 ZJS |
94 | log_info("/* %s */", __func__); |
95 | ||
647c7b74 VC |
96 | assert_se(m = hashmap_new(NULL)); |
97 | assert_se(c = hashmap_iterated_cache_new(m)); | |
98 | compare_cache(m, c); | |
99 | ||
100 | for (int stage = 0; stage < 100; stage++) { | |
101 | ||
102 | for (int i = 0; i < 100; i++) { | |
103 | int foo = stage * 1000 + i; | |
104 | ||
105 | assert_se(hashmap_put(m, INT_TO_PTR(foo), INT_TO_PTR(foo + 777)) == 1); | |
106 | } | |
107 | ||
108 | compare_cache(m, c); | |
109 | ||
110 | if (!(stage % 10)) { | |
111 | for (int i = 0; i < 100; i++) { | |
112 | int foo = stage * 1000 + i; | |
113 | ||
114 | assert_se(hashmap_remove(m, INT_TO_PTR(foo)) == INT_TO_PTR(foo + 777)); | |
115 | } | |
116 | ||
117 | compare_cache(m, c); | |
118 | } | |
119 | } | |
120 | ||
121 | hashmap_clear(m); | |
122 | compare_cache(m, c); | |
123 | ||
124 | assert_se(hashmap_free(m) == NULL); | |
125 | assert_se(iterated_cache_free(c) == NULL); | |
126 | } | |
127 | ||
46e16b34 | 128 | static void test_path_hashmap(void) { |
3ecdd18f | 129 | _cleanup_hashmap_free_ Hashmap *h = NULL; |
46e16b34 | 130 | |
32ca2911 ZJS |
131 | log_info("/* %s */", __func__); |
132 | ||
46e16b34 LP |
133 | assert_se(h = hashmap_new(&path_hash_ops)); |
134 | ||
135 | assert_se(hashmap_put(h, "foo", INT_TO_PTR(1)) >= 0); | |
136 | assert_se(hashmap_put(h, "/foo", INT_TO_PTR(2)) >= 0); | |
137 | assert_se(hashmap_put(h, "//foo", INT_TO_PTR(3)) == -EEXIST); | |
138 | assert_se(hashmap_put(h, "//foox/", INT_TO_PTR(4)) >= 0); | |
139 | assert_se(hashmap_put(h, "/foox////", INT_TO_PTR(5)) == -EEXIST); | |
140 | assert_se(hashmap_put(h, "foo//////bar/quux//", INT_TO_PTR(6)) >= 0); | |
141 | assert_se(hashmap_put(h, "foo/bar//quux/", INT_TO_PTR(8)) == -EEXIST); | |
142 | ||
143 | assert_se(hashmap_get(h, "foo") == INT_TO_PTR(1)); | |
144 | assert_se(hashmap_get(h, "foo/") == INT_TO_PTR(1)); | |
145 | assert_se(hashmap_get(h, "foo////") == INT_TO_PTR(1)); | |
146 | assert_se(hashmap_get(h, "/foo") == INT_TO_PTR(2)); | |
147 | assert_se(hashmap_get(h, "//foo") == INT_TO_PTR(2)); | |
148 | assert_se(hashmap_get(h, "/////foo////") == INT_TO_PTR(2)); | |
149 | assert_se(hashmap_get(h, "/////foox////") == INT_TO_PTR(4)); | |
150 | assert_se(hashmap_get(h, "/foox/") == INT_TO_PTR(4)); | |
151 | assert_se(hashmap_get(h, "/foox") == INT_TO_PTR(4)); | |
152 | assert_se(!hashmap_get(h, "foox")); | |
153 | assert_se(hashmap_get(h, "foo/bar/quux") == INT_TO_PTR(6)); | |
154 | assert_se(hashmap_get(h, "foo////bar////quux/////") == INT_TO_PTR(6)); | |
155 | assert_se(!hashmap_get(h, "/foo////bar////quux/////")); | |
156 | } | |
157 | ||
45fa9e29 | 158 | int main(int argc, const char *argv[]) { |
32a4456c MS |
159 | test_hashmap_funcs(); |
160 | test_ordered_hashmap_funcs(); | |
161 | ||
162 | test_ordered_hashmap_next(); | |
224b0e7a | 163 | test_hashmap_free_with_destructor(); |
9341a4a1 DB |
164 | test_uint64_compare_func(); |
165 | test_trivial_compare_func(); | |
166 | test_string_compare_func(); | |
647c7b74 | 167 | test_iterated_cache(); |
46e16b34 LP |
168 | test_path_hashmap(); |
169 | ||
170 | return 0; | |
9341a4a1 | 171 | } |