]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
9341a4a1 | 2 | |
9341a4a1 | 3 | #include "hashmap.h" |
25b3e2a8 | 4 | #include "string-util.h" |
cf0fbc49 | 5 | #include "util.h" |
9341a4a1 | 6 | |
8872c3a3 ZJS |
7 | unsigned custom_counter = 0; |
8 | static void custom_destruct(void* p) { | |
9 | custom_counter--; | |
10 | free(p); | |
11 | } | |
12 | ||
13 | DEFINE_HASH_OPS_FULL(boring_hash_ops, char, string_hash_func, string_compare_func, free, char, free); | |
14 | DEFINE_HASH_OPS_FULL(custom_hash_ops, char, string_hash_func, string_compare_func, custom_destruct, char, custom_destruct); | |
15 | ||
32a4456c MS |
16 | void test_hashmap_funcs(void); |
17 | void test_ordered_hashmap_funcs(void); | |
9341a4a1 | 18 | |
32a4456c | 19 | static void test_ordered_hashmap_next(void) { |
8f8a5213 MS |
20 | _cleanup_ordered_hashmap_free_ OrderedHashmap *m = NULL; |
21 | int i; | |
22 | ||
32ca2911 ZJS |
23 | log_info("/* %s */", __func__); |
24 | ||
8f8a5213 MS |
25 | assert_se(m = ordered_hashmap_new(NULL)); |
26 | for (i = -2; i <= 2; i++) | |
27 | assert_se(ordered_hashmap_put(m, INT_TO_PTR(i), INT_TO_PTR(i+10)) == 1); | |
28 | for (i = -2; i <= 1; i++) | |
29 | assert_se(ordered_hashmap_next(m, INT_TO_PTR(i)) == INT_TO_PTR(i+11)); | |
30 | assert_se(!ordered_hashmap_next(m, INT_TO_PTR(2))); | |
31 | assert_se(!ordered_hashmap_next(NULL, INT_TO_PTR(1))); | |
32 | assert_se(!ordered_hashmap_next(m, INT_TO_PTR(3))); | |
d06b3a9d RC |
33 | } |
34 | ||
9341a4a1 | 35 | static void test_uint64_compare_func(void) { |
8097ab4f ZJS |
36 | const uint64_t a = 0x100, b = 0x101; |
37 | ||
38 | assert_se(uint64_compare_func(&a, &a) == 0); | |
39 | assert_se(uint64_compare_func(&a, &b) == -1); | |
40 | assert_se(uint64_compare_func(&b, &a) == 1); | |
9341a4a1 DB |
41 | } |
42 | ||
43 | static void test_trivial_compare_func(void) { | |
44 | assert_se(trivial_compare_func(INT_TO_PTR('a'), INT_TO_PTR('a')) == 0); | |
45 | assert_se(trivial_compare_func(INT_TO_PTR('a'), INT_TO_PTR('b')) == -1); | |
46 | assert_se(trivial_compare_func(INT_TO_PTR('b'), INT_TO_PTR('a')) == 1); | |
47 | } | |
48 | ||
49 | static void test_string_compare_func(void) { | |
4b3eff61 | 50 | assert_se(string_compare_func("fred", "wilma") != 0); |
9341a4a1 DB |
51 | assert_se(string_compare_func("fred", "fred") == 0); |
52 | } | |
53 | ||
647c7b74 VC |
54 | static void compare_cache(Hashmap *map, IteratedCache *cache) { |
55 | const void **keys = NULL, **values = NULL; | |
56 | unsigned num, idx; | |
57 | Iterator iter; | |
58 | void *k, *v; | |
59 | ||
60 | assert_se(iterated_cache_get(cache, &keys, &values, &num) == 0); | |
61 | assert_se(num == 0 || keys); | |
62 | assert_se(num == 0 || values); | |
63 | ||
64 | idx = 0; | |
65 | HASHMAP_FOREACH_KEY(v, k, map, iter) { | |
66 | assert_se(v == values[idx]); | |
67 | assert_se(k == keys[idx]); | |
68 | ||
69 | idx++; | |
70 | } | |
71 | ||
72 | assert_se(idx == num); | |
73 | } | |
74 | ||
75 | static void test_iterated_cache(void) { | |
76 | Hashmap *m; | |
77 | IteratedCache *c; | |
78 | ||
32ca2911 ZJS |
79 | log_info("/* %s */", __func__); |
80 | ||
647c7b74 VC |
81 | assert_se(m = hashmap_new(NULL)); |
82 | assert_se(c = hashmap_iterated_cache_new(m)); | |
83 | compare_cache(m, c); | |
84 | ||
85 | for (int stage = 0; stage < 100; stage++) { | |
86 | ||
87 | for (int i = 0; i < 100; i++) { | |
88 | int foo = stage * 1000 + i; | |
89 | ||
90 | assert_se(hashmap_put(m, INT_TO_PTR(foo), INT_TO_PTR(foo + 777)) == 1); | |
91 | } | |
92 | ||
93 | compare_cache(m, c); | |
94 | ||
95 | if (!(stage % 10)) { | |
96 | for (int i = 0; i < 100; i++) { | |
97 | int foo = stage * 1000 + i; | |
98 | ||
99 | assert_se(hashmap_remove(m, INT_TO_PTR(foo)) == INT_TO_PTR(foo + 777)); | |
100 | } | |
101 | ||
102 | compare_cache(m, c); | |
103 | } | |
104 | } | |
105 | ||
106 | hashmap_clear(m); | |
107 | compare_cache(m, c); | |
108 | ||
109 | assert_se(hashmap_free(m) == NULL); | |
110 | assert_se(iterated_cache_free(c) == NULL); | |
111 | } | |
112 | ||
25b3e2a8 ZJS |
113 | static void test_hashmap_put_strdup(void) { |
114 | _cleanup_hashmap_free_ Hashmap *m = NULL; | |
115 | char *s; | |
116 | ||
117 | /* We don't have ordered_hashmap_put_strdup() yet. If it is added, | |
118 | * these tests should be moved to test-hashmap-plain.c. */ | |
119 | ||
120 | log_info("/* %s */", __func__); | |
121 | ||
122 | assert_se(hashmap_put_strdup(&m, "foo", "bar") == 1); | |
123 | assert_se(hashmap_put_strdup(&m, "foo", "bar") == 0); | |
124 | assert_se(hashmap_put_strdup(&m, "foo", "BAR") == -EEXIST); | |
125 | assert_se(hashmap_put_strdup(&m, "foo", "bar") == 0); | |
126 | assert_se(hashmap_contains(m, "foo")); | |
127 | ||
128 | s = hashmap_get(m, "foo"); | |
129 | assert_se(streq(s, "bar")); | |
130 | ||
131 | assert_se(hashmap_put_strdup(&m, "xxx", "bar") == 1); | |
132 | assert_se(hashmap_put_strdup(&m, "xxx", "bar") == 0); | |
133 | assert_se(hashmap_put_strdup(&m, "xxx", "BAR") == -EEXIST); | |
134 | assert_se(hashmap_put_strdup(&m, "xxx", "bar") == 0); | |
135 | assert_se(hashmap_contains(m, "xxx")); | |
136 | ||
137 | s = hashmap_get(m, "xxx"); | |
138 | assert_se(streq(s, "bar")); | |
139 | } | |
140 | ||
141 | static void test_hashmap_put_strdup_null(void) { | |
142 | _cleanup_hashmap_free_ Hashmap *m = NULL; | |
143 | char *s; | |
144 | ||
145 | log_info("/* %s */", __func__); | |
146 | ||
147 | assert_se(hashmap_put_strdup(&m, "foo", "bar") == 1); | |
148 | assert_se(hashmap_put_strdup(&m, "foo", "bar") == 0); | |
149 | assert_se(hashmap_put_strdup(&m, "foo", NULL) == -EEXIST); | |
150 | assert_se(hashmap_put_strdup(&m, "foo", "bar") == 0); | |
151 | assert_se(hashmap_contains(m, "foo")); | |
152 | ||
153 | s = hashmap_get(m, "foo"); | |
154 | assert_se(streq(s, "bar")); | |
155 | ||
156 | assert_se(hashmap_put_strdup(&m, "xxx", NULL) == 1); | |
157 | assert_se(hashmap_put_strdup(&m, "xxx", "bar") == -EEXIST); | |
158 | assert_se(hashmap_put_strdup(&m, "xxx", NULL) == 0); | |
159 | assert_se(hashmap_contains(m, "xxx")); | |
160 | ||
161 | s = hashmap_get(m, "xxx"); | |
162 | assert_se(s == NULL); | |
163 | } | |
164 | ||
d578f909 ZJS |
165 | int main(int argc, const char *argv[]) { |
166 | /* This file tests in test-hashmap-plain.c, and tests in test-hashmap-ordered.c, which is generated | |
167 | * from test-hashmap-plain.c. Hashmap tests should be added to test-hashmap-plain.c, and here only if | |
168 | * they don't apply to ordered hashmaps. */ | |
46e16b34 | 169 | |
d578f909 ZJS |
170 | log_parse_environment(); |
171 | log_open(); | |
32ca2911 | 172 | |
32a4456c MS |
173 | test_hashmap_funcs(); |
174 | test_ordered_hashmap_funcs(); | |
175 | ||
d578f909 ZJS |
176 | log_info("/************ non-shared tests ************/"); |
177 | ||
32a4456c | 178 | test_ordered_hashmap_next(); |
9341a4a1 DB |
179 | test_uint64_compare_func(); |
180 | test_trivial_compare_func(); | |
181 | test_string_compare_func(); | |
647c7b74 | 182 | test_iterated_cache(); |
25b3e2a8 ZJS |
183 | test_hashmap_put_strdup(); |
184 | test_hashmap_put_strdup_null(); | |
46e16b34 LP |
185 | |
186 | return 0; | |
9341a4a1 | 187 | } |