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