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