2 This file is part of systemd
4 Copyright 2013 Daniel Buch
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.
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.
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/>.
20 #include "alloc-util.h"
24 #include "string-util.h"
28 static bool arg_slow
= false;
30 void test_hashmap_funcs(void);
32 static void test_hashmap_replace(void) {
34 char *val1
, *val2
, *val3
, *val4
, *val5
, *r
;
36 log_info("%s", __func__
);
38 m
= hashmap_new(&string_hash_ops
);
40 val1
= strdup("val1");
42 val2
= strdup("val2");
44 val3
= strdup("val3");
46 val4
= strdup("val4");
48 val5
= strdup("val5");
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
);
56 hashmap_replace(m
, "key 3", val1
);
57 r
= hashmap_get(m
, "key 3");
58 assert_se(streq(r
, "val1"));
60 hashmap_replace(m
, "key 5", val5
);
61 r
= hashmap_get(m
, "key 5");
62 assert_se(streq(r
, "val5"));
72 static void test_hashmap_copy(void) {
74 char *val1
, *val2
, *val3
, *val4
, *r
;
76 log_info("%s", __func__
);
78 val1
= strdup("val1");
80 val2
= strdup("val2");
82 val3
= strdup("val3");
84 val4
= strdup("val4");
87 m
= hashmap_new(&string_hash_ops
);
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
);
94 copy
= hashmap_copy(m
);
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"));
105 hashmap_free_free(copy
);
109 static void test_hashmap_get_strv(void) {
112 char *val1
, *val2
, *val3
, *val4
;
114 log_info("%s", __func__
);
116 val1
= strdup("val1");
118 val2
= strdup("val2");
120 val3
= strdup("val3");
122 val4
= strdup("val4");
125 m
= hashmap_new(&string_hash_ops
);
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
);
132 strv
= hashmap_get_strv(m
);
135 strv
= strv_sort(strv
);
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"));
148 static void test_hashmap_move_one(void) {
150 char *val1
, *val2
, *val3
, *val4
, *r
;
152 log_info("%s", __func__
);
154 val1
= strdup("val1");
156 val2
= strdup("val2");
158 val3
= strdup("val3");
160 val4
= strdup("val4");
163 m
= hashmap_new(&string_hash_ops
);
164 n
= hashmap_new(&string_hash_ops
);
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
);
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);
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");
183 assert_se(hashmap_move_one(n
, m
, "key 3") == -EEXIST
);
185 hashmap_free_free(m
);
186 hashmap_free_free(n
);
189 static void test_hashmap_move(void) {
191 char *val1
, *val2
, *val3
, *val4
, *r
;
193 log_info("%s", __func__
);
195 val1
= strdup("val1");
197 val2
= strdup("val2");
199 val3
= strdup("val3");
201 val4
= strdup("val4");
204 m
= hashmap_new(&string_hash_ops
);
205 n
= hashmap_new(&string_hash_ops
);
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
);
213 assert_se(hashmap_move(n
, NULL
) == 0);
214 assert_se(hashmap_move(n
, m
) == 0);
216 assert_se(hashmap_size(m
) == 1);
217 r
= hashmap_get(m
, "key 1");
218 assert_se(r
&& streq(r
, "val1"));
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"));
229 hashmap_free_free(m
);
230 hashmap_free_free(n
);
233 static void test_hashmap_update(void) {
235 char *val1
, *val2
, *r
;
237 log_info("%s", __func__
);
239 m
= hashmap_new(&string_hash_ops
);
240 val1
= strdup("old_value");
242 val2
= strdup("new_value");
245 hashmap_put(m
, "key 1", val1
);
246 r
= hashmap_get(m
, "key 1");
247 assert_se(streq(r
, "old_value"));
249 assert_se(hashmap_update(m
, "key 2", val2
) == -ENOENT
);
250 r
= hashmap_get(m
, "key 1");
251 assert_se(streq(r
, "old_value"));
253 assert_se(hashmap_update(m
, "key 1", val2
) == 0);
254 r
= hashmap_get(m
, "key 1");
255 assert_se(streq(r
, "new_value"));
262 static void test_hashmap_put(void) {
264 int valid_hashmap_put
;
265 void *val1
= (void*) "val 1";
266 void *val2
= (void*) "val 2";
267 _cleanup_free_
char* key1
= NULL
;
269 log_info("%s", __func__
);
271 assert_se(hashmap_ensure_allocated(&m
, &string_hash_ops
) >= 0);
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
);
285 static void test_hashmap_remove(void) {
286 _cleanup_hashmap_free_ Hashmap
*m
= NULL
;
289 log_info("%s", __func__
);
291 r
= hashmap_remove(NULL
, "key 1");
292 assert_se(r
== NULL
);
294 m
= hashmap_new(&string_hash_ops
);
297 r
= hashmap_remove(m
, "no such key");
298 assert_se(r
== NULL
);
300 hashmap_put(m
, "key 1", (void*) "val 1");
301 hashmap_put(m
, "key 2", (void*) "val 2");
303 r
= hashmap_remove(m
, "key 1");
304 assert_se(streq(r
, "val 1"));
306 r
= hashmap_get(m
, "key 2");
307 assert_se(streq(r
, "val 2"));
308 assert_se(!hashmap_get(m
, "key 1"));
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";
319 log_info("%s", __func__
);
321 r
= hashmap_remove2(NULL
, "key 1", &r2
);
322 assert_se(r
== NULL
);
324 m
= hashmap_new(&string_hash_ops
);
327 r
= hashmap_remove2(m
, "no such key", &r2
);
328 assert_se(r
== NULL
);
330 hashmap_put(m
, strdup(key1
), strdup(val1
));
331 hashmap_put(m
, strdup(key2
), strdup(val2
));
333 r
= hashmap_remove2(m
, key1
, &r2
);
334 assert_se(streq(r
, val1
));
335 assert_se(streq(r2
, key1
));
339 r
= hashmap_get(m
, key2
);
340 assert_se(streq(r
, val2
));
341 assert_se(!hashmap_get(m
, key1
));
344 static void test_hashmap_remove_value(void) {
345 _cleanup_hashmap_free_ Hashmap
*m
= NULL
;
348 char val1
[] = "val 1";
349 char val2
[] = "val 2";
351 log_info("%s", __func__
);
353 r
= hashmap_remove_value(NULL
, "key 1", val1
);
354 assert_se(r
== NULL
);
356 m
= hashmap_new(&string_hash_ops
);
359 r
= hashmap_remove_value(m
, "key 1", val1
);
360 assert_se(r
== NULL
);
362 hashmap_put(m
, "key 1", val1
);
363 hashmap_put(m
, "key 2", val2
);
365 r
= hashmap_remove_value(m
, "key 1", val1
);
366 assert_se(streq(r
, "val 1"));
368 r
= hashmap_get(m
, "key 2");
369 assert_se(streq(r
, "val 2"));
370 assert_se(!hashmap_get(m
, "key 1"));
372 r
= hashmap_remove_value(m
, "key 2", val1
);
373 assert_se(r
== NULL
);
375 r
= hashmap_get(m
, "key 2");
376 assert_se(streq(r
, "val 2"));
377 assert_se(!hashmap_get(m
, "key 1"));
380 static void test_hashmap_remove_and_put(void) {
381 _cleanup_hashmap_free_ Hashmap
*m
= NULL
;
385 log_info("%s", __func__
);
387 m
= hashmap_new(&string_hash_ops
);
390 valid
= hashmap_remove_and_put(m
, "invalid key", "new key", NULL
);
391 assert_se(valid
== -ENOENT
);
393 valid
= hashmap_put(m
, "key 1", (void*) (const char *) "val 1");
394 assert_se(valid
== 1);
396 valid
= hashmap_remove_and_put(NULL
, "key 1", "key 2", (void*) (const char *) "val 2");
397 assert_se(valid
== -ENOENT
);
399 valid
= hashmap_remove_and_put(m
, "key 1", "key 2", (void*) (const char *) "val 2");
400 assert_se(valid
== 0);
402 r
= hashmap_get(m
, "key 2");
403 assert_se(streq(r
, "val 2"));
404 assert_se(!hashmap_get(m
, "key 1"));
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
);
412 static void test_hashmap_remove_and_replace(void) {
413 _cleanup_hashmap_free_ Hashmap
*m
= NULL
;
415 void *key1
= UINT_TO_PTR(1);
416 void *key2
= UINT_TO_PTR(2);
417 void *key3
= UINT_TO_PTR(3);
421 log_info("%s", __func__
);
423 m
= hashmap_new(&trivial_hash_ops
);
426 valid
= hashmap_remove_and_replace(m
, key1
, key2
, NULL
);
427 assert_se(valid
== -ENOENT
);
429 valid
= hashmap_put(m
, key1
, key1
);
430 assert_se(valid
== 1);
432 valid
= hashmap_remove_and_replace(NULL
, key1
, key2
, key2
);
433 assert_se(valid
== -ENOENT
);
435 valid
= hashmap_remove_and_replace(m
, key1
, key2
, key2
);
436 assert_se(valid
== 0);
438 r
= hashmap_get(m
, key2
);
439 assert_se(r
== key2
);
440 assert_se(!hashmap_get(m
, key1
));
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
));
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
++) {
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
));
470 static void test_hashmap_ensure_allocated(void) {
474 log_info("%s", __func__
);
476 m
= hashmap_new(&string_hash_ops
);
478 valid_hashmap
= hashmap_ensure_allocated(&m
, &string_hash_ops
);
479 assert_se(valid_hashmap
== 0);
485 static void test_hashmap_foreach_key(void) {
488 bool key_found
[] = { false, false, false, false };
491 static const char key_table
[] =
497 log_info("%s", __func__
);
499 m
= hashmap_new(&string_hash_ops
);
501 NULSTR_FOREACH(key
, key_table
)
502 hashmap_put(m
, key
, (void*) (const char*) "my dummy val");
504 HASHMAP_FOREACH_KEY(s
, key
, m
, i
) {
506 if (!key_found
[0] && streq(key
, "key 1"))
508 else if (!key_found
[1] && streq(key
, "key 2"))
510 else if (!key_found
[2] && streq(key
, "key 3"))
512 else if (!key_found
[3] && streq(key
, "fail"))
517 assert_se(key_found
[0] && key_found
[1] && key_found
[2] && !key_found
[3]);
522 static void test_hashmap_foreach(void) {
525 bool value_found
[] = { false, false, false, false };
526 char *val1
, *val2
, *val3
, *val4
, *s
;
529 log_info("%s", __func__
);
531 val1
= strdup("my val1");
533 val2
= strdup("my val2");
535 val3
= strdup("my val3");
537 val4
= strdup("my val4");
543 HASHMAP_FOREACH(s
, m
, i
)
545 assert_se(count
== 0);
547 m
= hashmap_new(&string_hash_ops
);
550 HASHMAP_FOREACH(s
, m
, i
)
552 assert_se(count
== 0);
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
);
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;
571 assert_se(value_found
[0] && value_found
[1] && value_found
[2] && value_found
[3]);
573 hashmap_free_free(m
);
576 static void test_hashmap_merge(void) {
579 char *val1
, *val2
, *val3
, *val4
, *r
;
581 log_info("%s", __func__
);
583 val1
= strdup("my val1");
585 val2
= strdup("my val2");
587 val3
= strdup("my val3");
589 val4
= strdup("my val4");
592 n
= hashmap_new(&string_hash_ops
);
593 m
= hashmap_new(&string_hash_ops
);
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
);
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"));
609 hashmap_free_free(m
);
612 static void test_hashmap_contains(void) {
616 log_info("%s", __func__
);
618 val1
= strdup("my val");
621 m
= hashmap_new(&string_hash_ops
);
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"));
628 assert_se(!hashmap_contains(NULL
, "Key 1"));
631 hashmap_free_free(m
);
634 static void test_hashmap_isempty(void) {
638 log_info("%s", __func__
);
640 val1
= strdup("my val");
643 m
= hashmap_new(&string_hash_ops
);
645 assert_se(hashmap_isempty(m
));
646 hashmap_put(m
, "Key 1", val1
);
647 assert_se(!hashmap_isempty(m
));
650 hashmap_free_free(m
);
653 static void test_hashmap_size(void) {
655 char *val1
, *val2
, *val3
, *val4
;
657 log_info("%s", __func__
);
659 val1
= strdup("my val");
661 val2
= strdup("my val");
663 val3
= strdup("my val");
665 val4
= strdup("my val");
668 assert_se(hashmap_size(NULL
) == 0);
669 assert_se(hashmap_buckets(NULL
) == 0);
671 m
= hashmap_new(&string_hash_ops
);
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
);
679 assert_se(hashmap_size(m
) == 4);
680 assert_se(hashmap_buckets(m
) >= 4);
681 hashmap_free_free(m
);
684 static void test_hashmap_get(void) {
689 log_info("%s", __func__
);
691 val
= strdup("my val");
694 r
= hashmap_get(NULL
, "Key 1");
695 assert_se(r
== NULL
);
697 m
= hashmap_new(&string_hash_ops
);
699 hashmap_put(m
, "Key 1", val
);
701 r
= hashmap_get(m
, "Key 1");
702 assert_se(streq(r
, val
));
704 r
= hashmap_get(m
, "no such key");
705 assert_se(r
== NULL
);
708 hashmap_free_free(m
);
711 static void test_hashmap_get2(void) {
715 char key_orig
[] = "Key 1";
718 log_info("%s", __func__
);
720 val
= strdup("my val");
723 key_copy
= strdup(key_orig
);
726 r
= hashmap_get2(NULL
, key_orig
, &key_copy
);
727 assert_se(r
== NULL
);
729 m
= hashmap_new(&string_hash_ops
);
731 hashmap_put(m
, key_copy
, val
);
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
));
739 r
= hashmap_get2(m
, "no such key", NULL
);
740 assert_se(r
== NULL
);
743 hashmap_free_free_free(m
);
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
);
750 static const struct hash_ops crippled_hashmap_ops
= {
751 .hash
= crippled_hashmap_func
,
752 .compare
= trivial_compare_func
,
755 static void test_hashmap_many(void) {
760 const struct hash_ops
*ops
;
763 { .ops
= NULL
, .n_entries
= arg_slow
? 1 << 20 : 240 },
764 { .ops
= &crippled_hashmap_ops
, .n_entries
= arg_slow
? 1 << 14 : 140 },
767 log_info("%s (%s)", __func__
, arg_slow
? "slow" : "fast");
769 for (j
= 0; j
< ELEMENTSOF(tests
); j
++) {
770 assert_se(h
= hashmap_new(tests
[j
].ops
));
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
);
777 for (i
= 1; i
< tests
[j
].n_entries
*3; i
++)
778 assert_se(hashmap_contains(h
, UINT_TO_PTR(i
)) == (i
% 3 == 1));
780 log_info("%u <= %u * 0.8 = %g", hashmap_size(h
), hashmap_buckets(h
), hashmap_buckets(h
) * 0.8);
782 assert_se(hashmap_size(h
) <= hashmap_buckets(h
) * 0.8);
783 assert_se(hashmap_size(h
) == tests
[j
].n_entries
);
785 while (!hashmap_isempty(h
)) {
786 k
= hashmap_first_key(h
);
787 v
= hashmap_remove(h
, k
);
795 static void test_hashmap_first(void) {
796 _cleanup_hashmap_free_ Hashmap
*m
= NULL
;
798 log_info("%s", __func__
);
800 m
= hashmap_new(&string_hash_ops
);
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);
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"));
814 static void test_hashmap_first_key(void) {
815 _cleanup_hashmap_free_ Hashmap
*m
= NULL
;
817 log_info("%s", __func__
);
819 m
= hashmap_new(&string_hash_ops
);
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);
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"));
833 static void test_hashmap_steal_first_key(void) {
834 _cleanup_hashmap_free_ Hashmap
*m
= NULL
;
836 log_info("%s", __func__
);
838 m
= hashmap_new(&string_hash_ops
);
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"));
845 assert_se(hashmap_isempty(m
));
848 static void test_hashmap_steal_first(void) {
849 _cleanup_hashmap_free_ Hashmap
*m
= NULL
;
853 log_info("%s", __func__
);
855 m
= hashmap_new(&string_hash_ops
);
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);
862 while ((val
= hashmap_steal_first(m
)))
863 seen
[strlen(val
) - 1]++;
865 assert_se(seen
[0] == 1 && seen
[1] == 1 && seen
[2] == 1);
867 assert_se(hashmap_isempty(m
));
870 static void test_hashmap_clear_free_free(void) {
871 _cleanup_hashmap_free_ Hashmap
*m
= NULL
;
873 log_info("%s", __func__
);
875 m
= hashmap_new(&string_hash_ops
);
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);
882 hashmap_clear_free_free(m
);
883 assert_se(hashmap_isempty(m
));
886 static void test_hashmap_reserve(void) {
887 _cleanup_hashmap_free_ Hashmap
*m
= NULL
;
889 log_info("%s", __func__
);
891 m
= hashmap_new(&string_hash_ops
);
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
));
899 assert_se(hashmap_put(m
, "key 1", (void*) "val 1") == 1);
901 assert_se(hashmap_reserve(m
, UINT_MAX
) == -ENOMEM
);
902 assert_se(hashmap_reserve(m
, UINT_MAX
- 1) == -ENOMEM
);
905 void test_hashmap_funcs(void) {
908 log_parse_environment();
911 r
= getenv_bool("SYSTEMD_SLOW_TESTS");
912 arg_slow
= r
>= 0 ? r
: SYSTEMD_SLOW_TESTS_DEFAULT
;
915 test_hashmap_get_strv();
916 test_hashmap_move_one();
918 test_hashmap_replace();
919 test_hashmap_update();
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();
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();