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