]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-hashmap-plain.c
Merge pull request #7388 from keszybz/doc-tweak
[thirdparty/systemd.git] / src / test / test-hashmap-plain.c
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
20 #include "alloc-util.h"
21 #include "env-util.h"
22 #include "hashmap.h"
23 #include "log.h"
24 #include "string-util.h"
25 #include "strv.h"
26 #include "util.h"
27
28 static bool arg_slow = false;
29
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
36 log_info("%s", __func__);
37
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
76 log_info("%s", __func__);
77
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
114 log_info("%s", __func__);
115
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
152 log_info("%s", __func__);
153
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
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);
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
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
193 log_info("%s", __func__);
194
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
213 assert_se(hashmap_move(n, NULL) == 0);
214 assert_se(hashmap_move(n, m) == 0);
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"));
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
237 log_info("%s", __func__);
238
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
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);
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) {
263 Hashmap *m = NULL;
264 int valid_hashmap_put;
265 void *val1 = (void*) "val 1";
266 void *val2 = (void*) "val 2";
267 _cleanup_free_ char* key1 = NULL;
268
269 log_info("%s", __func__);
270
271 assert_se(hashmap_ensure_allocated(&m, &string_hash_ops) >= 0);
272 assert_se(m);
273
274 valid_hashmap_put = hashmap_put(m, "key 1", val1);
275 assert_se(valid_hashmap_put == 1);
276 assert_se(hashmap_put(m, "key 1", val1) == 0);
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);
281
282 hashmap_free(m);
283 }
284
285 static void test_hashmap_remove(void) {
286 _cleanup_hashmap_free_ Hashmap *m = NULL;
287 char *r;
288
289 log_info("%s", __func__);
290
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
319 log_info("%s", __func__);
320
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
348 char val1[] = "val 1";
349 char val2[] = "val 2";
350
351 log_info("%s", __func__);
352
353 r = hashmap_remove_value(NULL, "key 1", val1);
354 assert_se(r == NULL);
355
356 m = hashmap_new(&string_hash_ops);
357 assert_se(m);
358
359 r = hashmap_remove_value(m, "key 1", val1);
360 assert_se(r == NULL);
361
362 hashmap_put(m, "key 1", val1);
363 hashmap_put(m, "key 2", val2);
364
365 r = hashmap_remove_value(m, "key 1", val1);
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
372 r = hashmap_remove_value(m, "key 2", val1);
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
380 static void test_hashmap_remove_and_put(void) {
381 _cleanup_hashmap_free_ Hashmap *m = NULL;
382 int valid;
383 char *r;
384
385 log_info("%s", __func__);
386
387 m = hashmap_new(&string_hash_ops);
388 assert_se(m);
389
390 valid = hashmap_remove_and_put(m, "invalid key", "new key", NULL);
391 assert_se(valid == -ENOENT);
392
393 valid = hashmap_put(m, "key 1", (void*) (const char *) "val 1");
394 assert_se(valid == 1);
395
396 valid = hashmap_remove_and_put(NULL, "key 1", "key 2", (void*) (const char *) "val 2");
397 assert_se(valid == -ENOENT);
398
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");
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;
419 int i, j;
420
421 log_info("%s", __func__);
422
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));
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 }
468 }
469
470 static void test_hashmap_ensure_allocated(void) {
471 Hashmap *m;
472 int valid_hashmap;
473
474 log_info("%s", __func__);
475
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
497 log_info("%s", __func__);
498
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) {
505 assert(s);
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;
527 unsigned count;
528
529 log_info("%s", __func__);
530
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
540 m = NULL;
541
542 count = 0;
543 HASHMAP_FOREACH(s, m, i)
544 count++;
545 assert_se(count == 0);
546
547 m = hashmap_new(&string_hash_ops);
548
549 count = 0;
550 HASHMAP_FOREACH(s, m, i)
551 count++;
552 assert_se(count == 0);
553
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
581 log_info("%s", __func__);
582
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
616 log_info("%s", __func__);
617
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"));
626 assert_se(!hashmap_contains(m, "Key 2"));
627
628 assert_se(!hashmap_contains(NULL, "Key 1"));
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
638 log_info("%s", __func__);
639
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
657 log_info("%s", __func__);
658
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
668 assert_se(hashmap_size(NULL) == 0);
669 assert_se(hashmap_buckets(NULL) == 0);
670
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);
680 assert_se(hashmap_buckets(m) >= 4);
681 hashmap_free_free(m);
682 }
683
684 static void test_hashmap_get(void) {
685 Hashmap *m;
686 char *r;
687 char *val;
688
689 log_info("%s", __func__);
690
691 val = strdup("my val");
692 assert_se(val);
693
694 r = hashmap_get(NULL, "Key 1");
695 assert_se(r == NULL);
696
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
704 r = hashmap_get(m, "no such key");
705 assert_se(r == NULL);
706
707 assert_se(m);
708 hashmap_free_free(m);
709 }
710
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
718 log_info("%s", __func__);
719
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);
737 assert_se(streq(key_orig, key_copy));
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
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);
748 }
749
750 static const struct hash_ops crippled_hashmap_ops = {
751 .hash = crippled_hashmap_func,
752 .compare = trivial_compare_func,
753 };
754
755 static void test_hashmap_many(void) {
756 Hashmap *h;
757 unsigned i, j;
758 void *v, *k;
759 const struct {
760 const struct hash_ops *ops;
761 unsigned n_entries;
762 } tests[] = {
763 { .ops = NULL, .n_entries = arg_slow ? 1 << 20 : 240 },
764 { .ops = &crippled_hashmap_ops, .n_entries = arg_slow ? 1 << 14 : 140 },
765 };
766
767 log_info("%s (%s)", __func__, arg_slow ? "slow" : "fast");
768
769 for (j = 0; j < ELEMENTSOF(tests); j++) {
770 assert_se(h = hashmap_new(tests[j].ops));
771
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));
779
780 log_info("%u <= %u * 0.8 = %g", hashmap_size(h), hashmap_buckets(h), hashmap_buckets(h) * 0.8);
781
782 assert_se(hashmap_size(h) <= hashmap_buckets(h) * 0.8);
783 assert_se(hashmap_size(h) == tests[j].n_entries);
784
785 while (!hashmap_isempty(h)) {
786 k = hashmap_first_key(h);
787 v = hashmap_remove(h, k);
788 assert_se(v == k);
789 }
790
791 hashmap_free(h);
792 }
793 }
794
795 static void test_hashmap_first(void) {
796 _cleanup_hashmap_free_ Hashmap *m = NULL;
797
798 log_info("%s", __func__);
799
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
812 }
813
814 static void test_hashmap_first_key(void) {
815 _cleanup_hashmap_free_ Hashmap *m = NULL;
816
817 log_info("%s", __func__);
818
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
836 log_info("%s", __func__);
837
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
853 log_info("%s", __func__);
854
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
873 log_info("%s", __func__);
874
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
886 static void test_hashmap_reserve(void) {
887 _cleanup_hashmap_free_ Hashmap *m = NULL;
888
889 log_info("%s", __func__);
890
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
905 void test_hashmap_funcs(void) {
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
914 test_hashmap_copy();
915 test_hashmap_get_strv();
916 test_hashmap_move_one();
917 test_hashmap_move();
918 test_hashmap_replace();
919 test_hashmap_update();
920 test_hashmap_put();
921 test_hashmap_remove();
922 test_hashmap_remove2();
923 test_hashmap_remove_value();
924 test_hashmap_remove_and_put();
925 test_hashmap_remove_and_replace();
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();
933 test_hashmap_get2();
934 test_hashmap_size();
935 test_hashmap_many();
936 test_hashmap_first();
937 test_hashmap_first_key();
938 test_hashmap_steal_first_key();
939 test_hashmap_steal_first();
940 test_hashmap_clear_free_free();
941 test_hashmap_reserve();
942 }