]>
Commit | Line | Data |
---|---|---|
32a4456c MS |
1 | /*** |
2 | This file is part of systemd | |
3 | ||
4 | Copyright 2013 Daniel Buch | |
5 | ||
6 | systemd is free software; you can redistribute it and/or modify it | |
7 | under the terms of the GNU Lesser General Public License as published by | |
8 | the Free Software Foundation; either version 2.1 of the License, or | |
9 | (at your option) any later version. | |
10 | ||
11 | systemd is distributed in the hope that it will be useful, but | |
12 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | Lesser General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU Lesser General Public License | |
17 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
18 | ***/ | |
19 | ||
b5efdb8a | 20 | #include "alloc-util.h" |
3d14a300 | 21 | #include "env-util.h" |
07630cea | 22 | #include "hashmap.h" |
3d14a300 | 23 | #include "log.h" |
07630cea | 24 | #include "string-util.h" |
32a4456c MS |
25 | #include "strv.h" |
26 | #include "util.h" | |
32a4456c | 27 | |
3d14a300 ZJS |
28 | static bool arg_slow = false; |
29 | ||
32a4456c MS |
30 | void test_hashmap_funcs(void); |
31 | ||
32 | static void test_hashmap_replace(void) { | |
33 | Hashmap *m; | |
34 | char *val1, *val2, *val3, *val4, *val5, *r; | |
35 | ||
3d14a300 ZJS |
36 | log_info("%s", __func__); |
37 | ||
32a4456c MS |
38 | m = hashmap_new(&string_hash_ops); |
39 | ||
40 | val1 = strdup("val1"); | |
41 | assert_se(val1); | |
42 | val2 = strdup("val2"); | |
43 | assert_se(val2); | |
44 | val3 = strdup("val3"); | |
45 | assert_se(val3); | |
46 | val4 = strdup("val4"); | |
47 | assert_se(val4); | |
48 | val5 = strdup("val5"); | |
49 | assert_se(val5); | |
50 | ||
51 | hashmap_put(m, "key 1", val1); | |
52 | hashmap_put(m, "key 2", val2); | |
53 | hashmap_put(m, "key 3", val3); | |
54 | hashmap_put(m, "key 4", val4); | |
55 | ||
56 | hashmap_replace(m, "key 3", val1); | |
57 | r = hashmap_get(m, "key 3"); | |
58 | assert_se(streq(r, "val1")); | |
59 | ||
60 | hashmap_replace(m, "key 5", val5); | |
61 | r = hashmap_get(m, "key 5"); | |
62 | assert_se(streq(r, "val5")); | |
63 | ||
64 | free(val1); | |
65 | free(val2); | |
66 | free(val3); | |
67 | free(val4); | |
68 | free(val5); | |
69 | hashmap_free(m); | |
70 | } | |
71 | ||
72 | static void test_hashmap_copy(void) { | |
73 | Hashmap *m, *copy; | |
74 | char *val1, *val2, *val3, *val4, *r; | |
75 | ||
3d14a300 ZJS |
76 | log_info("%s", __func__); |
77 | ||
32a4456c MS |
78 | val1 = strdup("val1"); |
79 | assert_se(val1); | |
80 | val2 = strdup("val2"); | |
81 | assert_se(val2); | |
82 | val3 = strdup("val3"); | |
83 | assert_se(val3); | |
84 | val4 = strdup("val4"); | |
85 | assert_se(val4); | |
86 | ||
87 | m = hashmap_new(&string_hash_ops); | |
88 | ||
89 | hashmap_put(m, "key 1", val1); | |
90 | hashmap_put(m, "key 2", val2); | |
91 | hashmap_put(m, "key 3", val3); | |
92 | hashmap_put(m, "key 4", val4); | |
93 | ||
94 | copy = hashmap_copy(m); | |
95 | ||
96 | r = hashmap_get(copy, "key 1"); | |
97 | assert_se(streq(r, "val1")); | |
98 | r = hashmap_get(copy, "key 2"); | |
99 | assert_se(streq(r, "val2")); | |
100 | r = hashmap_get(copy, "key 3"); | |
101 | assert_se(streq(r, "val3")); | |
102 | r = hashmap_get(copy, "key 4"); | |
103 | assert_se(streq(r, "val4")); | |
104 | ||
105 | hashmap_free_free(copy); | |
106 | hashmap_free(m); | |
107 | } | |
108 | ||
109 | static void test_hashmap_get_strv(void) { | |
110 | Hashmap *m; | |
111 | char **strv; | |
112 | char *val1, *val2, *val3, *val4; | |
113 | ||
3d14a300 ZJS |
114 | log_info("%s", __func__); |
115 | ||
32a4456c MS |
116 | val1 = strdup("val1"); |
117 | assert_se(val1); | |
118 | val2 = strdup("val2"); | |
119 | assert_se(val2); | |
120 | val3 = strdup("val3"); | |
121 | assert_se(val3); | |
122 | val4 = strdup("val4"); | |
123 | assert_se(val4); | |
124 | ||
125 | m = hashmap_new(&string_hash_ops); | |
126 | ||
127 | hashmap_put(m, "key 1", val1); | |
128 | hashmap_put(m, "key 2", val2); | |
129 | hashmap_put(m, "key 3", val3); | |
130 | hashmap_put(m, "key 4", val4); | |
131 | ||
132 | strv = hashmap_get_strv(m); | |
133 | ||
134 | #ifndef ORDERED | |
135 | strv = strv_sort(strv); | |
136 | #endif | |
137 | ||
138 | assert_se(streq(strv[0], "val1")); | |
139 | assert_se(streq(strv[1], "val2")); | |
140 | assert_se(streq(strv[2], "val3")); | |
141 | assert_se(streq(strv[3], "val4")); | |
142 | ||
143 | strv_free(strv); | |
144 | ||
145 | hashmap_free(m); | |
146 | } | |
147 | ||
148 | static void test_hashmap_move_one(void) { | |
149 | Hashmap *m, *n; | |
150 | char *val1, *val2, *val3, *val4, *r; | |
151 | ||
3d14a300 ZJS |
152 | log_info("%s", __func__); |
153 | ||
32a4456c MS |
154 | val1 = strdup("val1"); |
155 | assert_se(val1); | |
156 | val2 = strdup("val2"); | |
157 | assert_se(val2); | |
158 | val3 = strdup("val3"); | |
159 | assert_se(val3); | |
160 | val4 = strdup("val4"); | |
161 | assert_se(val4); | |
162 | ||
163 | m = hashmap_new(&string_hash_ops); | |
164 | n = hashmap_new(&string_hash_ops); | |
165 | ||
166 | hashmap_put(m, "key 1", val1); | |
167 | hashmap_put(m, "key 2", val2); | |
168 | hashmap_put(m, "key 3", val3); | |
169 | hashmap_put(m, "key 4", val4); | |
170 | ||
9ba81d5a MS |
171 | assert_se(hashmap_move_one(n, NULL, "key 3") == -ENOENT); |
172 | assert_se(hashmap_move_one(n, m, "key 5") == -ENOENT); | |
173 | assert_se(hashmap_move_one(n, m, "key 3") == 0); | |
174 | assert_se(hashmap_move_one(n, m, "key 4") == 0); | |
32a4456c MS |
175 | |
176 | r = hashmap_get(n, "key 3"); | |
177 | assert_se(r && streq(r, "val3")); | |
178 | r = hashmap_get(n, "key 4"); | |
179 | assert_se(r && streq(r, "val4")); | |
180 | r = hashmap_get(m, "key 3"); | |
181 | assert_se(!r); | |
182 | ||
9ba81d5a MS |
183 | assert_se(hashmap_move_one(n, m, "key 3") == -EEXIST); |
184 | ||
185 | hashmap_free_free(m); | |
186 | hashmap_free_free(n); | |
187 | } | |
188 | ||
189 | static void test_hashmap_move(void) { | |
190 | Hashmap *m, *n; | |
191 | char *val1, *val2, *val3, *val4, *r; | |
192 | ||
3d14a300 ZJS |
193 | log_info("%s", __func__); |
194 | ||
9ba81d5a MS |
195 | val1 = strdup("val1"); |
196 | assert_se(val1); | |
197 | val2 = strdup("val2"); | |
198 | assert_se(val2); | |
199 | val3 = strdup("val3"); | |
200 | assert_se(val3); | |
201 | val4 = strdup("val4"); | |
202 | assert_se(val4); | |
203 | ||
204 | m = hashmap_new(&string_hash_ops); | |
205 | n = hashmap_new(&string_hash_ops); | |
206 | ||
207 | hashmap_put(n, "key 1", strdup(val1)); | |
208 | hashmap_put(m, "key 1", val1); | |
209 | hashmap_put(m, "key 2", val2); | |
210 | hashmap_put(m, "key 3", val3); | |
211 | hashmap_put(m, "key 4", val4); | |
212 | ||
e80afdb3 MS |
213 | assert_se(hashmap_move(n, NULL) == 0); |
214 | assert_se(hashmap_move(n, m) == 0); | |
9ba81d5a MS |
215 | |
216 | assert_se(hashmap_size(m) == 1); | |
217 | r = hashmap_get(m, "key 1"); | |
218 | assert_se(r && streq(r, "val1")); | |
219 | ||
220 | r = hashmap_get(n, "key 1"); | |
221 | assert_se(r && streq(r, "val1")); | |
222 | r = hashmap_get(n, "key 2"); | |
223 | assert_se(r && streq(r, "val2")); | |
224 | r = hashmap_get(n, "key 3"); | |
225 | assert_se(r && streq(r, "val3")); | |
226 | r = hashmap_get(n, "key 4"); | |
227 | assert_se(r && streq(r, "val4")); | |
32a4456c MS |
228 | |
229 | hashmap_free_free(m); | |
230 | hashmap_free_free(n); | |
231 | } | |
232 | ||
233 | static void test_hashmap_update(void) { | |
234 | Hashmap *m; | |
235 | char *val1, *val2, *r; | |
236 | ||
3d14a300 ZJS |
237 | log_info("%s", __func__); |
238 | ||
32a4456c MS |
239 | m = hashmap_new(&string_hash_ops); |
240 | val1 = strdup("old_value"); | |
241 | assert_se(val1); | |
242 | val2 = strdup("new_value"); | |
243 | assert_se(val2); | |
244 | ||
245 | hashmap_put(m, "key 1", val1); | |
246 | r = hashmap_get(m, "key 1"); | |
247 | assert_se(streq(r, "old_value")); | |
248 | ||
9ba81d5a MS |
249 | assert_se(hashmap_update(m, "key 2", val2) == -ENOENT); |
250 | r = hashmap_get(m, "key 1"); | |
251 | assert_se(streq(r, "old_value")); | |
252 | ||
253 | assert_se(hashmap_update(m, "key 1", val2) == 0); | |
32a4456c MS |
254 | r = hashmap_get(m, "key 1"); |
255 | assert_se(streq(r, "new_value")); | |
256 | ||
257 | free(val1); | |
258 | free(val2); | |
259 | hashmap_free(m); | |
260 | } | |
261 | ||
262 | static void test_hashmap_put(void) { | |
9ba81d5a | 263 | Hashmap *m = NULL; |
32a4456c | 264 | int valid_hashmap_put; |
9ba81d5a | 265 | void *val1 = (void*) "val 1"; |
435fc317 MP |
266 | void *val2 = (void*) "val 2"; |
267 | _cleanup_free_ char* key1 = NULL; | |
32a4456c | 268 | |
3d14a300 ZJS |
269 | log_info("%s", __func__); |
270 | ||
61c81750 | 271 | assert_se(hashmap_ensure_allocated(&m, &string_hash_ops) >= 0); |
9ba81d5a | 272 | assert_se(m); |
32a4456c | 273 | |
9ba81d5a | 274 | valid_hashmap_put = hashmap_put(m, "key 1", val1); |
32a4456c | 275 | assert_se(valid_hashmap_put == 1); |
9ba81d5a | 276 | assert_se(hashmap_put(m, "key 1", val1) == 0); |
435fc317 MP |
277 | assert_se(hashmap_put(m, "key 1", val2) == -EEXIST); |
278 | key1 = strdup("key 1"); | |
279 | assert_se(hashmap_put(m, key1, val1) == 0); | |
280 | assert_se(hashmap_put(m, key1, val2) == -EEXIST); | |
32a4456c | 281 | |
32a4456c MS |
282 | hashmap_free(m); |
283 | } | |
284 | ||
9ba81d5a MS |
285 | static void test_hashmap_remove(void) { |
286 | _cleanup_hashmap_free_ Hashmap *m = NULL; | |
287 | char *r; | |
288 | ||
3d14a300 ZJS |
289 | log_info("%s", __func__); |
290 | ||
9ba81d5a MS |
291 | r = hashmap_remove(NULL, "key 1"); |
292 | assert_se(r == NULL); | |
293 | ||
294 | m = hashmap_new(&string_hash_ops); | |
295 | assert_se(m); | |
296 | ||
297 | r = hashmap_remove(m, "no such key"); | |
298 | assert_se(r == NULL); | |
299 | ||
300 | hashmap_put(m, "key 1", (void*) "val 1"); | |
301 | hashmap_put(m, "key 2", (void*) "val 2"); | |
302 | ||
303 | r = hashmap_remove(m, "key 1"); | |
304 | assert_se(streq(r, "val 1")); | |
305 | ||
306 | r = hashmap_get(m, "key 2"); | |
307 | assert_se(streq(r, "val 2")); | |
308 | assert_se(!hashmap_get(m, "key 1")); | |
309 | } | |
310 | ||
311 | static void test_hashmap_remove2(void) { | |
312 | _cleanup_hashmap_free_free_free_ Hashmap *m = NULL; | |
313 | char key1[] = "key 1"; | |
314 | char key2[] = "key 2"; | |
315 | char val1[] = "val 1"; | |
316 | char val2[] = "val 2"; | |
317 | void *r, *r2; | |
318 | ||
3d14a300 ZJS |
319 | log_info("%s", __func__); |
320 | ||
9ba81d5a MS |
321 | r = hashmap_remove2(NULL, "key 1", &r2); |
322 | assert_se(r == NULL); | |
323 | ||
324 | m = hashmap_new(&string_hash_ops); | |
325 | assert_se(m); | |
326 | ||
327 | r = hashmap_remove2(m, "no such key", &r2); | |
328 | assert_se(r == NULL); | |
329 | ||
330 | hashmap_put(m, strdup(key1), strdup(val1)); | |
331 | hashmap_put(m, strdup(key2), strdup(val2)); | |
332 | ||
333 | r = hashmap_remove2(m, key1, &r2); | |
334 | assert_se(streq(r, val1)); | |
335 | assert_se(streq(r2, key1)); | |
336 | free(r); | |
337 | free(r2); | |
338 | ||
339 | r = hashmap_get(m, key2); | |
340 | assert_se(streq(r, val2)); | |
341 | assert_se(!hashmap_get(m, key1)); | |
342 | } | |
343 | ||
344 | static void test_hashmap_remove_value(void) { | |
345 | _cleanup_hashmap_free_ Hashmap *m = NULL; | |
346 | char *r; | |
347 | ||
d09139e1 ZJS |
348 | char val1[] = "val 1"; |
349 | char val2[] = "val 2"; | |
350 | ||
3d14a300 ZJS |
351 | log_info("%s", __func__); |
352 | ||
d09139e1 | 353 | r = hashmap_remove_value(NULL, "key 1", val1); |
9ba81d5a MS |
354 | assert_se(r == NULL); |
355 | ||
356 | m = hashmap_new(&string_hash_ops); | |
357 | assert_se(m); | |
358 | ||
d09139e1 | 359 | r = hashmap_remove_value(m, "key 1", val1); |
9ba81d5a MS |
360 | assert_se(r == NULL); |
361 | ||
d09139e1 ZJS |
362 | hashmap_put(m, "key 1", val1); |
363 | hashmap_put(m, "key 2", val2); | |
9ba81d5a | 364 | |
d09139e1 | 365 | r = hashmap_remove_value(m, "key 1", val1); |
9ba81d5a MS |
366 | assert_se(streq(r, "val 1")); |
367 | ||
368 | r = hashmap_get(m, "key 2"); | |
369 | assert_se(streq(r, "val 2")); | |
370 | assert_se(!hashmap_get(m, "key 1")); | |
371 | ||
d09139e1 | 372 | r = hashmap_remove_value(m, "key 2", val1); |
9ba81d5a MS |
373 | assert_se(r == NULL); |
374 | ||
375 | r = hashmap_get(m, "key 2"); | |
376 | assert_se(streq(r, "val 2")); | |
377 | assert_se(!hashmap_get(m, "key 1")); | |
378 | } | |
379 | ||
32a4456c MS |
380 | static void test_hashmap_remove_and_put(void) { |
381 | _cleanup_hashmap_free_ Hashmap *m = NULL; | |
382 | int valid; | |
383 | char *r; | |
384 | ||
3d14a300 ZJS |
385 | log_info("%s", __func__); |
386 | ||
32a4456c MS |
387 | m = hashmap_new(&string_hash_ops); |
388 | assert_se(m); | |
389 | ||
9ba81d5a MS |
390 | valid = hashmap_remove_and_put(m, "invalid key", "new key", NULL); |
391 | assert_se(valid == -ENOENT); | |
32a4456c MS |
392 | |
393 | valid = hashmap_put(m, "key 1", (void*) (const char *) "val 1"); | |
394 | assert_se(valid == 1); | |
9ba81d5a MS |
395 | |
396 | valid = hashmap_remove_and_put(NULL, "key 1", "key 2", (void*) (const char *) "val 2"); | |
397 | assert_se(valid == -ENOENT); | |
398 | ||
32a4456c MS |
399 | valid = hashmap_remove_and_put(m, "key 1", "key 2", (void*) (const char *) "val 2"); |
400 | assert_se(valid == 0); | |
401 | ||
402 | r = hashmap_get(m, "key 2"); | |
403 | assert_se(streq(r, "val 2")); | |
404 | assert_se(!hashmap_get(m, "key 1")); | |
405 | ||
406 | valid = hashmap_put(m, "key 3", (void*) (const char *) "val 3"); | |
407 | assert_se(valid == 1); | |
408 | valid = hashmap_remove_and_put(m, "key 3", "key 2", (void*) (const char *) "val 2"); | |
9ba81d5a MS |
409 | assert_se(valid == -EEXIST); |
410 | } | |
411 | ||
412 | static void test_hashmap_remove_and_replace(void) { | |
413 | _cleanup_hashmap_free_ Hashmap *m = NULL; | |
414 | int valid; | |
415 | void *key1 = UINT_TO_PTR(1); | |
416 | void *key2 = UINT_TO_PTR(2); | |
417 | void *key3 = UINT_TO_PTR(3); | |
418 | void *r; | |
e1323fbf | 419 | int i, j; |
9ba81d5a | 420 | |
3d14a300 ZJS |
421 | log_info("%s", __func__); |
422 | ||
9ba81d5a MS |
423 | m = hashmap_new(&trivial_hash_ops); |
424 | assert_se(m); | |
425 | ||
426 | valid = hashmap_remove_and_replace(m, key1, key2, NULL); | |
427 | assert_se(valid == -ENOENT); | |
428 | ||
429 | valid = hashmap_put(m, key1, key1); | |
430 | assert_se(valid == 1); | |
431 | ||
432 | valid = hashmap_remove_and_replace(NULL, key1, key2, key2); | |
433 | assert_se(valid == -ENOENT); | |
434 | ||
435 | valid = hashmap_remove_and_replace(m, key1, key2, key2); | |
436 | assert_se(valid == 0); | |
437 | ||
438 | r = hashmap_get(m, key2); | |
439 | assert_se(r == key2); | |
440 | assert_se(!hashmap_get(m, key1)); | |
441 | ||
442 | valid = hashmap_put(m, key3, key3); | |
443 | assert_se(valid == 1); | |
444 | valid = hashmap_remove_and_replace(m, key3, key2, key2); | |
445 | assert_se(valid == 0); | |
446 | r = hashmap_get(m, key2); | |
447 | assert_se(r == key2); | |
448 | assert_se(!hashmap_get(m, key3)); | |
e1323fbf MS |
449 | |
450 | /* Repeat this test several times to increase the chance of hitting | |
451 | * the less likely case in hashmap_remove_and_replace where it | |
452 | * compensates for the backward shift. */ | |
453 | for (i = 0; i < 20; i++) { | |
454 | hashmap_clear(m); | |
455 | ||
456 | for (j = 1; j < 7; j++) | |
457 | hashmap_put(m, UINT_TO_PTR(10*i + j), UINT_TO_PTR(10*i + j)); | |
458 | valid = hashmap_remove_and_replace(m, UINT_TO_PTR(10*i + 1), | |
459 | UINT_TO_PTR(10*i + 2), | |
460 | UINT_TO_PTR(10*i + 2)); | |
461 | assert_se(valid == 0); | |
462 | assert_se(!hashmap_get(m, UINT_TO_PTR(10*i + 1))); | |
463 | for (j = 2; j < 7; j++) { | |
464 | r = hashmap_get(m, UINT_TO_PTR(10*i + j)); | |
465 | assert_se(r == UINT_TO_PTR(10*i + j)); | |
466 | } | |
467 | } | |
32a4456c MS |
468 | } |
469 | ||
470 | static void test_hashmap_ensure_allocated(void) { | |
471 | Hashmap *m; | |
472 | int valid_hashmap; | |
473 | ||
3d14a300 ZJS |
474 | log_info("%s", __func__); |
475 | ||
32a4456c MS |
476 | m = hashmap_new(&string_hash_ops); |
477 | ||
478 | valid_hashmap = hashmap_ensure_allocated(&m, &string_hash_ops); | |
479 | assert_se(valid_hashmap == 0); | |
480 | ||
481 | assert_se(m); | |
482 | hashmap_free(m); | |
483 | } | |
484 | ||
485 | static void test_hashmap_foreach_key(void) { | |
486 | Hashmap *m; | |
487 | Iterator i; | |
488 | bool key_found[] = { false, false, false, false }; | |
489 | const char *s; | |
490 | const char *key; | |
491 | static const char key_table[] = | |
492 | "key 1\0" | |
493 | "key 2\0" | |
494 | "key 3\0" | |
495 | "key 4\0"; | |
496 | ||
3d14a300 ZJS |
497 | log_info("%s", __func__); |
498 | ||
32a4456c MS |
499 | m = hashmap_new(&string_hash_ops); |
500 | ||
501 | NULSTR_FOREACH(key, key_table) | |
502 | hashmap_put(m, key, (void*) (const char*) "my dummy val"); | |
503 | ||
504 | HASHMAP_FOREACH_KEY(s, key, m, i) { | |
8927b1da | 505 | assert(s); |
32a4456c MS |
506 | if (!key_found[0] && streq(key, "key 1")) |
507 | key_found[0] = true; | |
508 | else if (!key_found[1] && streq(key, "key 2")) | |
509 | key_found[1] = true; | |
510 | else if (!key_found[2] && streq(key, "key 3")) | |
511 | key_found[2] = true; | |
512 | else if (!key_found[3] && streq(key, "fail")) | |
513 | key_found[3] = true; | |
514 | } | |
515 | ||
516 | assert_se(m); | |
517 | assert_se(key_found[0] && key_found[1] && key_found[2] && !key_found[3]); | |
518 | ||
519 | hashmap_free(m); | |
520 | } | |
521 | ||
522 | static void test_hashmap_foreach(void) { | |
523 | Hashmap *m; | |
524 | Iterator i; | |
525 | bool value_found[] = { false, false, false, false }; | |
526 | char *val1, *val2, *val3, *val4, *s; | |
9ba81d5a | 527 | unsigned count; |
32a4456c | 528 | |
3d14a300 ZJS |
529 | log_info("%s", __func__); |
530 | ||
32a4456c MS |
531 | val1 = strdup("my val1"); |
532 | assert_se(val1); | |
533 | val2 = strdup("my val2"); | |
534 | assert_se(val2); | |
535 | val3 = strdup("my val3"); | |
536 | assert_se(val3); | |
537 | val4 = strdup("my val4"); | |
538 | assert_se(val4); | |
539 | ||
9ba81d5a MS |
540 | m = NULL; |
541 | ||
542 | count = 0; | |
543 | HASHMAP_FOREACH(s, m, i) | |
544 | count++; | |
545 | assert_se(count == 0); | |
546 | ||
32a4456c MS |
547 | m = hashmap_new(&string_hash_ops); |
548 | ||
9ba81d5a MS |
549 | count = 0; |
550 | HASHMAP_FOREACH(s, m, i) | |
551 | count++; | |
552 | assert_se(count == 0); | |
553 | ||
32a4456c MS |
554 | hashmap_put(m, "Key 1", val1); |
555 | hashmap_put(m, "Key 2", val2); | |
556 | hashmap_put(m, "Key 3", val3); | |
557 | hashmap_put(m, "Key 4", val4); | |
558 | ||
559 | HASHMAP_FOREACH(s, m, i) { | |
560 | if (!value_found[0] && streq(s, val1)) | |
561 | value_found[0] = true; | |
562 | else if (!value_found[1] && streq(s, val2)) | |
563 | value_found[1] = true; | |
564 | else if (!value_found[2] && streq(s, val3)) | |
565 | value_found[2] = true; | |
566 | else if (!value_found[3] && streq(s, val4)) | |
567 | value_found[3] = true; | |
568 | } | |
569 | ||
570 | assert_se(m); | |
571 | assert_se(value_found[0] && value_found[1] && value_found[2] && value_found[3]); | |
572 | ||
573 | hashmap_free_free(m); | |
574 | } | |
575 | ||
576 | static void test_hashmap_merge(void) { | |
577 | Hashmap *m; | |
578 | Hashmap *n; | |
579 | char *val1, *val2, *val3, *val4, *r; | |
580 | ||
3d14a300 ZJS |
581 | log_info("%s", __func__); |
582 | ||
32a4456c MS |
583 | val1 = strdup("my val1"); |
584 | assert_se(val1); | |
585 | val2 = strdup("my val2"); | |
586 | assert_se(val2); | |
587 | val3 = strdup("my val3"); | |
588 | assert_se(val3); | |
589 | val4 = strdup("my val4"); | |
590 | assert_se(val4); | |
591 | ||
592 | n = hashmap_new(&string_hash_ops); | |
593 | m = hashmap_new(&string_hash_ops); | |
594 | ||
595 | hashmap_put(m, "Key 1", val1); | |
596 | hashmap_put(m, "Key 2", val2); | |
597 | hashmap_put(n, "Key 3", val3); | |
598 | hashmap_put(n, "Key 4", val4); | |
599 | ||
600 | assert_se(hashmap_merge(m, n) == 0); | |
601 | r = hashmap_get(m, "Key 3"); | |
602 | assert_se(r && streq(r, "my val3")); | |
603 | r = hashmap_get(m, "Key 4"); | |
604 | assert_se(r && streq(r, "my val4")); | |
605 | ||
606 | assert_se(n); | |
607 | assert_se(m); | |
608 | hashmap_free(n); | |
609 | hashmap_free_free(m); | |
610 | } | |
611 | ||
612 | static void test_hashmap_contains(void) { | |
613 | Hashmap *m; | |
614 | char *val1; | |
615 | ||
3d14a300 ZJS |
616 | log_info("%s", __func__); |
617 | ||
32a4456c MS |
618 | val1 = strdup("my val"); |
619 | assert_se(val1); | |
620 | ||
621 | m = hashmap_new(&string_hash_ops); | |
622 | ||
623 | assert_se(!hashmap_contains(m, "Key 1")); | |
624 | hashmap_put(m, "Key 1", val1); | |
625 | assert_se(hashmap_contains(m, "Key 1")); | |
9ba81d5a MS |
626 | assert_se(!hashmap_contains(m, "Key 2")); |
627 | ||
628 | assert_se(!hashmap_contains(NULL, "Key 1")); | |
32a4456c MS |
629 | |
630 | assert_se(m); | |
631 | hashmap_free_free(m); | |
632 | } | |
633 | ||
634 | static void test_hashmap_isempty(void) { | |
635 | Hashmap *m; | |
636 | char *val1; | |
637 | ||
3d14a300 ZJS |
638 | log_info("%s", __func__); |
639 | ||
32a4456c MS |
640 | val1 = strdup("my val"); |
641 | assert_se(val1); | |
642 | ||
643 | m = hashmap_new(&string_hash_ops); | |
644 | ||
645 | assert_se(hashmap_isempty(m)); | |
646 | hashmap_put(m, "Key 1", val1); | |
647 | assert_se(!hashmap_isempty(m)); | |
648 | ||
649 | assert_se(m); | |
650 | hashmap_free_free(m); | |
651 | } | |
652 | ||
653 | static void test_hashmap_size(void) { | |
654 | Hashmap *m; | |
655 | char *val1, *val2, *val3, *val4; | |
656 | ||
3d14a300 ZJS |
657 | log_info("%s", __func__); |
658 | ||
32a4456c MS |
659 | val1 = strdup("my val"); |
660 | assert_se(val1); | |
661 | val2 = strdup("my val"); | |
662 | assert_se(val2); | |
663 | val3 = strdup("my val"); | |
664 | assert_se(val3); | |
665 | val4 = strdup("my val"); | |
666 | assert_se(val4); | |
667 | ||
9ba81d5a MS |
668 | assert_se(hashmap_size(NULL) == 0); |
669 | assert_se(hashmap_buckets(NULL) == 0); | |
670 | ||
32a4456c MS |
671 | m = hashmap_new(&string_hash_ops); |
672 | ||
673 | hashmap_put(m, "Key 1", val1); | |
674 | hashmap_put(m, "Key 2", val2); | |
675 | hashmap_put(m, "Key 3", val3); | |
676 | hashmap_put(m, "Key 4", val4); | |
677 | ||
678 | assert_se(m); | |
679 | assert_se(hashmap_size(m) == 4); | |
9ba81d5a | 680 | assert_se(hashmap_buckets(m) >= 4); |
32a4456c MS |
681 | hashmap_free_free(m); |
682 | } | |
683 | ||
684 | static void test_hashmap_get(void) { | |
685 | Hashmap *m; | |
686 | char *r; | |
687 | char *val; | |
688 | ||
3d14a300 ZJS |
689 | log_info("%s", __func__); |
690 | ||
32a4456c MS |
691 | val = strdup("my val"); |
692 | assert_se(val); | |
693 | ||
9ba81d5a MS |
694 | r = hashmap_get(NULL, "Key 1"); |
695 | assert_se(r == NULL); | |
696 | ||
32a4456c MS |
697 | m = hashmap_new(&string_hash_ops); |
698 | ||
699 | hashmap_put(m, "Key 1", val); | |
700 | ||
701 | r = hashmap_get(m, "Key 1"); | |
702 | assert_se(streq(r, val)); | |
703 | ||
9ba81d5a MS |
704 | r = hashmap_get(m, "no such key"); |
705 | assert_se(r == NULL); | |
706 | ||
32a4456c MS |
707 | assert_se(m); |
708 | hashmap_free_free(m); | |
709 | } | |
710 | ||
9ba81d5a MS |
711 | static void test_hashmap_get2(void) { |
712 | Hashmap *m; | |
713 | char *r; | |
714 | char *val; | |
715 | char key_orig[] = "Key 1"; | |
716 | void *key_copy; | |
717 | ||
3d14a300 ZJS |
718 | log_info("%s", __func__); |
719 | ||
9ba81d5a MS |
720 | val = strdup("my val"); |
721 | assert_se(val); | |
722 | ||
723 | key_copy = strdup(key_orig); | |
724 | assert_se(key_copy); | |
725 | ||
726 | r = hashmap_get2(NULL, key_orig, &key_copy); | |
727 | assert_se(r == NULL); | |
728 | ||
729 | m = hashmap_new(&string_hash_ops); | |
730 | ||
731 | hashmap_put(m, key_copy, val); | |
732 | key_copy = NULL; | |
733 | ||
734 | r = hashmap_get2(m, key_orig, &key_copy); | |
735 | assert_se(streq(r, val)); | |
736 | assert_se(key_orig != key_copy); | |
b669934f | 737 | assert_se(streq(key_orig, key_copy)); |
9ba81d5a MS |
738 | |
739 | r = hashmap_get2(m, "no such key", NULL); | |
740 | assert_se(r == NULL); | |
741 | ||
742 | assert_se(m); | |
743 | hashmap_free_free_free(m); | |
744 | } | |
745 | ||
b826ab58 TG |
746 | static void crippled_hashmap_func(const void *p, struct siphash *state) { |
747 | return trivial_hash_func(INT_TO_PTR(PTR_TO_INT(p) & 0xff), state); | |
9ba81d5a MS |
748 | } |
749 | ||
750 | static const struct hash_ops crippled_hashmap_ops = { | |
751 | .hash = crippled_hashmap_func, | |
752 | .compare = trivial_compare_func, | |
753 | }; | |
754 | ||
32a4456c MS |
755 | static void test_hashmap_many(void) { |
756 | Hashmap *h; | |
9ba81d5a MS |
757 | unsigned i, j; |
758 | void *v, *k; | |
3d14a300 | 759 | const struct { |
9ba81d5a MS |
760 | const struct hash_ops *ops; |
761 | unsigned n_entries; | |
762 | } tests[] = { | |
3d14a300 ZJS |
763 | { .ops = NULL, .n_entries = arg_slow ? 1 << 20 : 240 }, |
764 | { .ops = &crippled_hashmap_ops, .n_entries = arg_slow ? 1 << 14 : 140 }, | |
9ba81d5a | 765 | }; |
32a4456c | 766 | |
3d14a300 | 767 | log_info("%s (%s)", __func__, arg_slow ? "slow" : "fast"); |
32a4456c | 768 | |
9ba81d5a MS |
769 | for (j = 0; j < ELEMENTSOF(tests); j++) { |
770 | assert_se(h = hashmap_new(tests[j].ops)); | |
32a4456c | 771 | |
9ba81d5a MS |
772 | for (i = 1; i < tests[j].n_entries*3; i+=3) { |
773 | assert_se(hashmap_put(h, UINT_TO_PTR(i), UINT_TO_PTR(i)) >= 0); | |
774 | assert_se(PTR_TO_UINT(hashmap_get(h, UINT_TO_PTR(i))) == i); | |
775 | } | |
776 | ||
777 | for (i = 1; i < tests[j].n_entries*3; i++) | |
778 | assert_se(hashmap_contains(h, UINT_TO_PTR(i)) == (i % 3 == 1)); | |
32a4456c | 779 | |
ce79279b | 780 | log_info("%u <= %u * 0.8 = %g", hashmap_size(h), hashmap_buckets(h), hashmap_buckets(h) * 0.8); |
32a4456c | 781 | |
ce79279b | 782 | assert_se(hashmap_size(h) <= hashmap_buckets(h) * 0.8); |
9ba81d5a | 783 | assert_se(hashmap_size(h) == tests[j].n_entries); |
32a4456c | 784 | |
9ba81d5a MS |
785 | while (!hashmap_isempty(h)) { |
786 | k = hashmap_first_key(h); | |
787 | v = hashmap_remove(h, k); | |
788 | assert_se(v == k); | |
789 | } | |
32a4456c | 790 | |
9ba81d5a MS |
791 | hashmap_free(h); |
792 | } | |
793 | } | |
794 | ||
795 | static void test_hashmap_first(void) { | |
796 | _cleanup_hashmap_free_ Hashmap *m = NULL; | |
797 | ||
3d14a300 ZJS |
798 | log_info("%s", __func__); |
799 | ||
9ba81d5a MS |
800 | m = hashmap_new(&string_hash_ops); |
801 | assert_se(m); | |
802 | ||
803 | assert_se(!hashmap_first(m)); | |
804 | assert_se(hashmap_put(m, "key 1", (void*) "val 1") == 1); | |
805 | assert_se(streq(hashmap_first(m), "val 1")); | |
806 | assert_se(hashmap_put(m, "key 2", (void*) "val 2") == 1); | |
807 | #ifdef ORDERED | |
808 | assert_se(streq(hashmap_first(m), "val 1")); | |
809 | assert_se(hashmap_remove(m, "key 1")); | |
810 | assert_se(streq(hashmap_first(m), "val 2")); | |
811 | #endif | |
32a4456c MS |
812 | } |
813 | ||
814 | static void test_hashmap_first_key(void) { | |
815 | _cleanup_hashmap_free_ Hashmap *m = NULL; | |
816 | ||
3d14a300 ZJS |
817 | log_info("%s", __func__); |
818 | ||
32a4456c MS |
819 | m = hashmap_new(&string_hash_ops); |
820 | assert_se(m); | |
821 | ||
822 | assert_se(!hashmap_first_key(m)); | |
823 | assert_se(hashmap_put(m, "key 1", NULL) == 1); | |
824 | assert_se(streq(hashmap_first_key(m), "key 1")); | |
825 | assert_se(hashmap_put(m, "key 2", NULL) == 1); | |
826 | #ifdef ORDERED | |
827 | assert_se(streq(hashmap_first_key(m), "key 1")); | |
828 | assert_se(hashmap_remove(m, "key 1") == NULL); | |
829 | assert_se(streq(hashmap_first_key(m), "key 2")); | |
830 | #endif | |
831 | } | |
832 | ||
833 | static void test_hashmap_steal_first_key(void) { | |
834 | _cleanup_hashmap_free_ Hashmap *m = NULL; | |
835 | ||
3d14a300 ZJS |
836 | log_info("%s", __func__); |
837 | ||
32a4456c MS |
838 | m = hashmap_new(&string_hash_ops); |
839 | assert_se(m); | |
840 | ||
841 | assert_se(!hashmap_steal_first_key(m)); | |
842 | assert_se(hashmap_put(m, "key 1", NULL) == 1); | |
843 | assert_se(streq(hashmap_steal_first_key(m), "key 1")); | |
844 | ||
845 | assert_se(hashmap_isempty(m)); | |
846 | } | |
847 | ||
848 | static void test_hashmap_steal_first(void) { | |
849 | _cleanup_hashmap_free_ Hashmap *m = NULL; | |
850 | int seen[3] = {}; | |
851 | char *val; | |
852 | ||
3d14a300 ZJS |
853 | log_info("%s", __func__); |
854 | ||
32a4456c MS |
855 | m = hashmap_new(&string_hash_ops); |
856 | assert_se(m); | |
857 | ||
858 | assert_se(hashmap_put(m, "key 1", (void*) "1") == 1); | |
859 | assert_se(hashmap_put(m, "key 2", (void*) "22") == 1); | |
860 | assert_se(hashmap_put(m, "key 3", (void*) "333") == 1); | |
861 | ||
862 | while ((val = hashmap_steal_first(m))) | |
863 | seen[strlen(val) - 1]++; | |
864 | ||
865 | assert_se(seen[0] == 1 && seen[1] == 1 && seen[2] == 1); | |
866 | ||
867 | assert_se(hashmap_isempty(m)); | |
868 | } | |
869 | ||
870 | static void test_hashmap_clear_free_free(void) { | |
871 | _cleanup_hashmap_free_ Hashmap *m = NULL; | |
872 | ||
3d14a300 ZJS |
873 | log_info("%s", __func__); |
874 | ||
32a4456c MS |
875 | m = hashmap_new(&string_hash_ops); |
876 | assert_se(m); | |
877 | ||
878 | assert_se(hashmap_put(m, strdup("key 1"), NULL) == 1); | |
879 | assert_se(hashmap_put(m, strdup("key 2"), NULL) == 1); | |
880 | assert_se(hashmap_put(m, strdup("key 3"), NULL) == 1); | |
881 | ||
882 | hashmap_clear_free_free(m); | |
883 | assert_se(hashmap_isempty(m)); | |
884 | } | |
885 | ||
8f88aed7 MS |
886 | static void test_hashmap_reserve(void) { |
887 | _cleanup_hashmap_free_ Hashmap *m = NULL; | |
888 | ||
3d14a300 ZJS |
889 | log_info("%s", __func__); |
890 | ||
8f88aed7 MS |
891 | m = hashmap_new(&string_hash_ops); |
892 | ||
893 | assert_se(hashmap_reserve(m, 1) == 0); | |
894 | assert_se(hashmap_buckets(m) < 1000); | |
895 | assert_se(hashmap_reserve(m, 1000) == 0); | |
896 | assert_se(hashmap_buckets(m) >= 1000); | |
897 | assert_se(hashmap_isempty(m)); | |
898 | ||
899 | assert_se(hashmap_put(m, "key 1", (void*) "val 1") == 1); | |
900 | ||
901 | assert_se(hashmap_reserve(m, UINT_MAX) == -ENOMEM); | |
902 | assert_se(hashmap_reserve(m, UINT_MAX - 1) == -ENOMEM); | |
903 | } | |
904 | ||
32a4456c | 905 | void test_hashmap_funcs(void) { |
3d14a300 ZJS |
906 | int r; |
907 | ||
908 | log_parse_environment(); | |
909 | log_open(); | |
910 | ||
911 | r = getenv_bool("SYSTEMD_SLOW_TESTS"); | |
912 | arg_slow = r >= 0 ? r : SYSTEMD_SLOW_TESTS_DEFAULT; | |
913 | ||
32a4456c MS |
914 | test_hashmap_copy(); |
915 | test_hashmap_get_strv(); | |
916 | test_hashmap_move_one(); | |
9ba81d5a | 917 | test_hashmap_move(); |
32a4456c MS |
918 | test_hashmap_replace(); |
919 | test_hashmap_update(); | |
920 | test_hashmap_put(); | |
9ba81d5a MS |
921 | test_hashmap_remove(); |
922 | test_hashmap_remove2(); | |
923 | test_hashmap_remove_value(); | |
32a4456c | 924 | test_hashmap_remove_and_put(); |
9ba81d5a | 925 | test_hashmap_remove_and_replace(); |
32a4456c MS |
926 | test_hashmap_ensure_allocated(); |
927 | test_hashmap_foreach(); | |
928 | test_hashmap_foreach_key(); | |
929 | test_hashmap_contains(); | |
930 | test_hashmap_merge(); | |
931 | test_hashmap_isempty(); | |
932 | test_hashmap_get(); | |
9ba81d5a | 933 | test_hashmap_get2(); |
32a4456c MS |
934 | test_hashmap_size(); |
935 | test_hashmap_many(); | |
9ba81d5a | 936 | test_hashmap_first(); |
32a4456c MS |
937 | test_hashmap_first_key(); |
938 | test_hashmap_steal_first_key(); | |
939 | test_hashmap_steal_first(); | |
940 | test_hashmap_clear_free_free(); | |
8f88aed7 | 941 | test_hashmap_reserve(); |
32a4456c | 942 | } |