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"
22 #include "string-util.h"
26 void test_hashmap_funcs(void);
28 static void test_hashmap_replace(void) {
30 char *val1
, *val2
, *val3
, *val4
, *val5
, *r
;
32 m
= hashmap_new(&string_hash_ops
);
34 val1
= strdup("val1");
36 val2
= strdup("val2");
38 val3
= strdup("val3");
40 val4
= strdup("val4");
42 val5
= strdup("val5");
45 hashmap_put(m
, "key 1", val1
);
46 hashmap_put(m
, "key 2", val2
);
47 hashmap_put(m
, "key 3", val3
);
48 hashmap_put(m
, "key 4", val4
);
50 hashmap_replace(m
, "key 3", val1
);
51 r
= hashmap_get(m
, "key 3");
52 assert_se(streq(r
, "val1"));
54 hashmap_replace(m
, "key 5", val5
);
55 r
= hashmap_get(m
, "key 5");
56 assert_se(streq(r
, "val5"));
66 static void test_hashmap_copy(void) {
68 char *val1
, *val2
, *val3
, *val4
, *r
;
70 val1
= strdup("val1");
72 val2
= strdup("val2");
74 val3
= strdup("val3");
76 val4
= strdup("val4");
79 m
= hashmap_new(&string_hash_ops
);
81 hashmap_put(m
, "key 1", val1
);
82 hashmap_put(m
, "key 2", val2
);
83 hashmap_put(m
, "key 3", val3
);
84 hashmap_put(m
, "key 4", val4
);
86 copy
= hashmap_copy(m
);
88 r
= hashmap_get(copy
, "key 1");
89 assert_se(streq(r
, "val1"));
90 r
= hashmap_get(copy
, "key 2");
91 assert_se(streq(r
, "val2"));
92 r
= hashmap_get(copy
, "key 3");
93 assert_se(streq(r
, "val3"));
94 r
= hashmap_get(copy
, "key 4");
95 assert_se(streq(r
, "val4"));
97 hashmap_free_free(copy
);
101 static void test_hashmap_get_strv(void) {
104 char *val1
, *val2
, *val3
, *val4
;
106 val1
= strdup("val1");
108 val2
= strdup("val2");
110 val3
= strdup("val3");
112 val4
= strdup("val4");
115 m
= hashmap_new(&string_hash_ops
);
117 hashmap_put(m
, "key 1", val1
);
118 hashmap_put(m
, "key 2", val2
);
119 hashmap_put(m
, "key 3", val3
);
120 hashmap_put(m
, "key 4", val4
);
122 strv
= hashmap_get_strv(m
);
125 strv
= strv_sort(strv
);
128 assert_se(streq(strv
[0], "val1"));
129 assert_se(streq(strv
[1], "val2"));
130 assert_se(streq(strv
[2], "val3"));
131 assert_se(streq(strv
[3], "val4"));
138 static void test_hashmap_move_one(void) {
140 char *val1
, *val2
, *val3
, *val4
, *r
;
142 val1
= strdup("val1");
144 val2
= strdup("val2");
146 val3
= strdup("val3");
148 val4
= strdup("val4");
151 m
= hashmap_new(&string_hash_ops
);
152 n
= hashmap_new(&string_hash_ops
);
154 hashmap_put(m
, "key 1", val1
);
155 hashmap_put(m
, "key 2", val2
);
156 hashmap_put(m
, "key 3", val3
);
157 hashmap_put(m
, "key 4", val4
);
159 assert_se(hashmap_move_one(n
, NULL
, "key 3") == -ENOENT
);
160 assert_se(hashmap_move_one(n
, m
, "key 5") == -ENOENT
);
161 assert_se(hashmap_move_one(n
, m
, "key 3") == 0);
162 assert_se(hashmap_move_one(n
, m
, "key 4") == 0);
164 r
= hashmap_get(n
, "key 3");
165 assert_se(r
&& streq(r
, "val3"));
166 r
= hashmap_get(n
, "key 4");
167 assert_se(r
&& streq(r
, "val4"));
168 r
= hashmap_get(m
, "key 3");
171 assert_se(hashmap_move_one(n
, m
, "key 3") == -EEXIST
);
173 hashmap_free_free(m
);
174 hashmap_free_free(n
);
177 static void test_hashmap_move(void) {
179 char *val1
, *val2
, *val3
, *val4
, *r
;
181 val1
= strdup("val1");
183 val2
= strdup("val2");
185 val3
= strdup("val3");
187 val4
= strdup("val4");
190 m
= hashmap_new(&string_hash_ops
);
191 n
= hashmap_new(&string_hash_ops
);
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
);
199 assert_se(hashmap_move(n
, NULL
) == 0);
200 assert_se(hashmap_move(n
, m
) == 0);
202 assert_se(hashmap_size(m
) == 1);
203 r
= hashmap_get(m
, "key 1");
204 assert_se(r
&& streq(r
, "val1"));
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"));
215 hashmap_free_free(m
);
216 hashmap_free_free(n
);
219 static void test_hashmap_update(void) {
221 char *val1
, *val2
, *r
;
223 m
= hashmap_new(&string_hash_ops
);
224 val1
= strdup("old_value");
226 val2
= strdup("new_value");
229 hashmap_put(m
, "key 1", val1
);
230 r
= hashmap_get(m
, "key 1");
231 assert_se(streq(r
, "old_value"));
233 assert_se(hashmap_update(m
, "key 2", val2
) == -ENOENT
);
234 r
= hashmap_get(m
, "key 1");
235 assert_se(streq(r
, "old_value"));
237 assert_se(hashmap_update(m
, "key 1", val2
) == 0);
238 r
= hashmap_get(m
, "key 1");
239 assert_se(streq(r
, "new_value"));
246 static void test_hashmap_put(void) {
248 int valid_hashmap_put
;
249 void *val1
= (void*) "val 1";
250 void *val2
= (void*) "val 2";
251 _cleanup_free_
char* key1
= NULL
;
253 assert_se(hashmap_ensure_allocated(&m
, &string_hash_ops
) >= 0);
256 valid_hashmap_put
= hashmap_put(m
, "key 1", val1
);
257 assert_se(valid_hashmap_put
== 1);
258 assert_se(hashmap_put(m
, "key 1", val1
) == 0);
259 assert_se(hashmap_put(m
, "key 1", val2
) == -EEXIST
);
260 key1
= strdup("key 1");
261 assert_se(hashmap_put(m
, key1
, val1
) == 0);
262 assert_se(hashmap_put(m
, key1
, val2
) == -EEXIST
);
267 static void test_hashmap_remove(void) {
268 _cleanup_hashmap_free_ Hashmap
*m
= NULL
;
271 r
= hashmap_remove(NULL
, "key 1");
272 assert_se(r
== NULL
);
274 m
= hashmap_new(&string_hash_ops
);
277 r
= hashmap_remove(m
, "no such key");
278 assert_se(r
== NULL
);
280 hashmap_put(m
, "key 1", (void*) "val 1");
281 hashmap_put(m
, "key 2", (void*) "val 2");
283 r
= hashmap_remove(m
, "key 1");
284 assert_se(streq(r
, "val 1"));
286 r
= hashmap_get(m
, "key 2");
287 assert_se(streq(r
, "val 2"));
288 assert_se(!hashmap_get(m
, "key 1"));
291 static void test_hashmap_remove2(void) {
292 _cleanup_hashmap_free_free_free_ Hashmap
*m
= NULL
;
293 char key1
[] = "key 1";
294 char key2
[] = "key 2";
295 char val1
[] = "val 1";
296 char val2
[] = "val 2";
299 r
= hashmap_remove2(NULL
, "key 1", &r2
);
300 assert_se(r
== NULL
);
302 m
= hashmap_new(&string_hash_ops
);
305 r
= hashmap_remove2(m
, "no such key", &r2
);
306 assert_se(r
== NULL
);
308 hashmap_put(m
, strdup(key1
), strdup(val1
));
309 hashmap_put(m
, strdup(key2
), strdup(val2
));
311 r
= hashmap_remove2(m
, key1
, &r2
);
312 assert_se(streq(r
, val1
));
313 assert_se(streq(r2
, key1
));
317 r
= hashmap_get(m
, key2
);
318 assert_se(streq(r
, val2
));
319 assert_se(!hashmap_get(m
, key1
));
322 static void test_hashmap_remove_value(void) {
323 _cleanup_hashmap_free_ Hashmap
*m
= NULL
;
326 r
= hashmap_remove_value(NULL
, "key 1", (void*) "val 1");
327 assert_se(r
== NULL
);
329 m
= hashmap_new(&string_hash_ops
);
332 r
= hashmap_remove_value(m
, "key 1", (void*) "val 1");
333 assert_se(r
== NULL
);
335 hashmap_put(m
, "key 1", (void*) "val 1");
336 hashmap_put(m
, "key 2", (void*) "val 2");
338 r
= hashmap_remove_value(m
, "key 1", (void*) "val 1");
339 assert_se(streq(r
, "val 1"));
341 r
= hashmap_get(m
, "key 2");
342 assert_se(streq(r
, "val 2"));
343 assert_se(!hashmap_get(m
, "key 1"));
345 r
= hashmap_remove_value(m
, "key 2", (void*) "val 1");
346 assert_se(r
== NULL
);
348 r
= hashmap_get(m
, "key 2");
349 assert_se(streq(r
, "val 2"));
350 assert_se(!hashmap_get(m
, "key 1"));
353 static void test_hashmap_remove_and_put(void) {
354 _cleanup_hashmap_free_ Hashmap
*m
= NULL
;
358 m
= hashmap_new(&string_hash_ops
);
361 valid
= hashmap_remove_and_put(m
, "invalid key", "new key", NULL
);
362 assert_se(valid
== -ENOENT
);
364 valid
= hashmap_put(m
, "key 1", (void*) (const char *) "val 1");
365 assert_se(valid
== 1);
367 valid
= hashmap_remove_and_put(NULL
, "key 1", "key 2", (void*) (const char *) "val 2");
368 assert_se(valid
== -ENOENT
);
370 valid
= hashmap_remove_and_put(m
, "key 1", "key 2", (void*) (const char *) "val 2");
371 assert_se(valid
== 0);
373 r
= hashmap_get(m
, "key 2");
374 assert_se(streq(r
, "val 2"));
375 assert_se(!hashmap_get(m
, "key 1"));
377 valid
= hashmap_put(m
, "key 3", (void*) (const char *) "val 3");
378 assert_se(valid
== 1);
379 valid
= hashmap_remove_and_put(m
, "key 3", "key 2", (void*) (const char *) "val 2");
380 assert_se(valid
== -EEXIST
);
383 static void test_hashmap_remove_and_replace(void) {
384 _cleanup_hashmap_free_ Hashmap
*m
= NULL
;
386 void *key1
= UINT_TO_PTR(1);
387 void *key2
= UINT_TO_PTR(2);
388 void *key3
= UINT_TO_PTR(3);
392 m
= hashmap_new(&trivial_hash_ops
);
395 valid
= hashmap_remove_and_replace(m
, key1
, key2
, NULL
);
396 assert_se(valid
== -ENOENT
);
398 valid
= hashmap_put(m
, key1
, key1
);
399 assert_se(valid
== 1);
401 valid
= hashmap_remove_and_replace(NULL
, key1
, key2
, key2
);
402 assert_se(valid
== -ENOENT
);
404 valid
= hashmap_remove_and_replace(m
, key1
, key2
, key2
);
405 assert_se(valid
== 0);
407 r
= hashmap_get(m
, key2
);
408 assert_se(r
== key2
);
409 assert_se(!hashmap_get(m
, key1
));
411 valid
= hashmap_put(m
, key3
, key3
);
412 assert_se(valid
== 1);
413 valid
= hashmap_remove_and_replace(m
, key3
, key2
, key2
);
414 assert_se(valid
== 0);
415 r
= hashmap_get(m
, key2
);
416 assert_se(r
== key2
);
417 assert_se(!hashmap_get(m
, key3
));
419 /* Repeat this test several times to increase the chance of hitting
420 * the less likely case in hashmap_remove_and_replace where it
421 * compensates for the backward shift. */
422 for (i
= 0; i
< 20; i
++) {
425 for (j
= 1; j
< 7; j
++)
426 hashmap_put(m
, UINT_TO_PTR(10*i
+ j
), UINT_TO_PTR(10*i
+ j
));
427 valid
= hashmap_remove_and_replace(m
, UINT_TO_PTR(10*i
+ 1),
428 UINT_TO_PTR(10*i
+ 2),
429 UINT_TO_PTR(10*i
+ 2));
430 assert_se(valid
== 0);
431 assert_se(!hashmap_get(m
, UINT_TO_PTR(10*i
+ 1)));
432 for (j
= 2; j
< 7; j
++) {
433 r
= hashmap_get(m
, UINT_TO_PTR(10*i
+ j
));
434 assert_se(r
== UINT_TO_PTR(10*i
+ j
));
439 static void test_hashmap_ensure_allocated(void) {
443 m
= hashmap_new(&string_hash_ops
);
445 valid_hashmap
= hashmap_ensure_allocated(&m
, &string_hash_ops
);
446 assert_se(valid_hashmap
== 0);
452 static void test_hashmap_foreach_key(void) {
455 bool key_found
[] = { false, false, false, false };
458 static const char key_table
[] =
464 m
= hashmap_new(&string_hash_ops
);
466 NULSTR_FOREACH(key
, key_table
)
467 hashmap_put(m
, key
, (void*) (const char*) "my dummy val");
469 HASHMAP_FOREACH_KEY(s
, key
, m
, i
) {
471 if (!key_found
[0] && streq(key
, "key 1"))
473 else if (!key_found
[1] && streq(key
, "key 2"))
475 else if (!key_found
[2] && streq(key
, "key 3"))
477 else if (!key_found
[3] && streq(key
, "fail"))
482 assert_se(key_found
[0] && key_found
[1] && key_found
[2] && !key_found
[3]);
487 static void test_hashmap_foreach(void) {
490 bool value_found
[] = { false, false, false, false };
491 char *val1
, *val2
, *val3
, *val4
, *s
;
494 val1
= strdup("my val1");
496 val2
= strdup("my val2");
498 val3
= strdup("my val3");
500 val4
= strdup("my val4");
506 HASHMAP_FOREACH(s
, m
, i
)
508 assert_se(count
== 0);
510 m
= hashmap_new(&string_hash_ops
);
513 HASHMAP_FOREACH(s
, m
, i
)
515 assert_se(count
== 0);
517 hashmap_put(m
, "Key 1", val1
);
518 hashmap_put(m
, "Key 2", val2
);
519 hashmap_put(m
, "Key 3", val3
);
520 hashmap_put(m
, "Key 4", val4
);
522 HASHMAP_FOREACH(s
, m
, i
) {
523 if (!value_found
[0] && streq(s
, val1
))
524 value_found
[0] = true;
525 else if (!value_found
[1] && streq(s
, val2
))
526 value_found
[1] = true;
527 else if (!value_found
[2] && streq(s
, val3
))
528 value_found
[2] = true;
529 else if (!value_found
[3] && streq(s
, val4
))
530 value_found
[3] = true;
534 assert_se(value_found
[0] && value_found
[1] && value_found
[2] && value_found
[3]);
536 hashmap_free_free(m
);
539 static void test_hashmap_merge(void) {
542 char *val1
, *val2
, *val3
, *val4
, *r
;
544 val1
= strdup("my val1");
546 val2
= strdup("my val2");
548 val3
= strdup("my val3");
550 val4
= strdup("my val4");
553 n
= hashmap_new(&string_hash_ops
);
554 m
= hashmap_new(&string_hash_ops
);
556 hashmap_put(m
, "Key 1", val1
);
557 hashmap_put(m
, "Key 2", val2
);
558 hashmap_put(n
, "Key 3", val3
);
559 hashmap_put(n
, "Key 4", val4
);
561 assert_se(hashmap_merge(m
, n
) == 0);
562 r
= hashmap_get(m
, "Key 3");
563 assert_se(r
&& streq(r
, "my val3"));
564 r
= hashmap_get(m
, "Key 4");
565 assert_se(r
&& streq(r
, "my val4"));
570 hashmap_free_free(m
);
573 static void test_hashmap_contains(void) {
577 val1
= strdup("my val");
580 m
= hashmap_new(&string_hash_ops
);
582 assert_se(!hashmap_contains(m
, "Key 1"));
583 hashmap_put(m
, "Key 1", val1
);
584 assert_se(hashmap_contains(m
, "Key 1"));
585 assert_se(!hashmap_contains(m
, "Key 2"));
587 assert_se(!hashmap_contains(NULL
, "Key 1"));
590 hashmap_free_free(m
);
593 static void test_hashmap_isempty(void) {
597 val1
= strdup("my val");
600 m
= hashmap_new(&string_hash_ops
);
602 assert_se(hashmap_isempty(m
));
603 hashmap_put(m
, "Key 1", val1
);
604 assert_se(!hashmap_isempty(m
));
607 hashmap_free_free(m
);
610 static void test_hashmap_size(void) {
612 char *val1
, *val2
, *val3
, *val4
;
614 val1
= strdup("my val");
616 val2
= strdup("my val");
618 val3
= strdup("my val");
620 val4
= strdup("my val");
623 assert_se(hashmap_size(NULL
) == 0);
624 assert_se(hashmap_buckets(NULL
) == 0);
626 m
= hashmap_new(&string_hash_ops
);
628 hashmap_put(m
, "Key 1", val1
);
629 hashmap_put(m
, "Key 2", val2
);
630 hashmap_put(m
, "Key 3", val3
);
631 hashmap_put(m
, "Key 4", val4
);
634 assert_se(hashmap_size(m
) == 4);
635 assert_se(hashmap_buckets(m
) >= 4);
636 hashmap_free_free(m
);
639 static void test_hashmap_get(void) {
644 val
= strdup("my val");
647 r
= hashmap_get(NULL
, "Key 1");
648 assert_se(r
== NULL
);
650 m
= hashmap_new(&string_hash_ops
);
652 hashmap_put(m
, "Key 1", val
);
654 r
= hashmap_get(m
, "Key 1");
655 assert_se(streq(r
, val
));
657 r
= hashmap_get(m
, "no such key");
658 assert_se(r
== NULL
);
661 hashmap_free_free(m
);
664 static void test_hashmap_get2(void) {
668 char key_orig
[] = "Key 1";
671 val
= strdup("my val");
674 key_copy
= strdup(key_orig
);
677 r
= hashmap_get2(NULL
, key_orig
, &key_copy
);
678 assert_se(r
== NULL
);
680 m
= hashmap_new(&string_hash_ops
);
682 hashmap_put(m
, key_copy
, val
);
685 r
= hashmap_get2(m
, key_orig
, &key_copy
);
686 assert_se(streq(r
, val
));
687 assert_se(key_orig
!= key_copy
);
688 assert_se(streq(key_orig
, key_copy
));
690 r
= hashmap_get2(m
, "no such key", NULL
);
691 assert_se(r
== NULL
);
694 hashmap_free_free_free(m
);
697 static void crippled_hashmap_func(const void *p
, struct siphash
*state
) {
698 return trivial_hash_func(INT_TO_PTR(PTR_TO_INT(p
) & 0xff), state
);
701 static const struct hash_ops crippled_hashmap_ops
= {
702 .hash
= crippled_hashmap_func
,
703 .compare
= trivial_compare_func
,
706 static void test_hashmap_many(void) {
710 static const struct {
711 const struct hash_ops
*ops
;
714 { .ops
= NULL
, .n_entries
= 1 << 20 },
715 { .ops
= &crippled_hashmap_ops
, .n_entries
= 1 << 14 },
719 for (j
= 0; j
< ELEMENTSOF(tests
); j
++) {
720 assert_se(h
= hashmap_new(tests
[j
].ops
));
722 for (i
= 1; i
< tests
[j
].n_entries
*3; i
+=3) {
723 assert_se(hashmap_put(h
, UINT_TO_PTR(i
), UINT_TO_PTR(i
)) >= 0);
724 assert_se(PTR_TO_UINT(hashmap_get(h
, UINT_TO_PTR(i
))) == i
);
727 for (i
= 1; i
< tests
[j
].n_entries
*3; i
++)
728 assert_se(hashmap_contains(h
, UINT_TO_PTR(i
)) == (i
% 3 == 1));
730 log_info("%u <= %u * 0.8 = %g", hashmap_size(h
), hashmap_buckets(h
), hashmap_buckets(h
) * 0.8);
732 assert_se(hashmap_size(h
) <= hashmap_buckets(h
) * 0.8);
733 assert_se(hashmap_size(h
) == tests
[j
].n_entries
);
735 while (!hashmap_isempty(h
)) {
736 k
= hashmap_first_key(h
);
737 v
= hashmap_remove(h
, k
);
745 static void test_hashmap_first(void) {
746 _cleanup_hashmap_free_ Hashmap
*m
= NULL
;
748 m
= hashmap_new(&string_hash_ops
);
751 assert_se(!hashmap_first(m
));
752 assert_se(hashmap_put(m
, "key 1", (void*) "val 1") == 1);
753 assert_se(streq(hashmap_first(m
), "val 1"));
754 assert_se(hashmap_put(m
, "key 2", (void*) "val 2") == 1);
756 assert_se(streq(hashmap_first(m
), "val 1"));
757 assert_se(hashmap_remove(m
, "key 1"));
758 assert_se(streq(hashmap_first(m
), "val 2"));
762 static void test_hashmap_first_key(void) {
763 _cleanup_hashmap_free_ Hashmap
*m
= NULL
;
765 m
= hashmap_new(&string_hash_ops
);
768 assert_se(!hashmap_first_key(m
));
769 assert_se(hashmap_put(m
, "key 1", NULL
) == 1);
770 assert_se(streq(hashmap_first_key(m
), "key 1"));
771 assert_se(hashmap_put(m
, "key 2", NULL
) == 1);
773 assert_se(streq(hashmap_first_key(m
), "key 1"));
774 assert_se(hashmap_remove(m
, "key 1") == NULL
);
775 assert_se(streq(hashmap_first_key(m
), "key 2"));
779 static void test_hashmap_steal_first_key(void) {
780 _cleanup_hashmap_free_ Hashmap
*m
= NULL
;
782 m
= hashmap_new(&string_hash_ops
);
785 assert_se(!hashmap_steal_first_key(m
));
786 assert_se(hashmap_put(m
, "key 1", NULL
) == 1);
787 assert_se(streq(hashmap_steal_first_key(m
), "key 1"));
789 assert_se(hashmap_isempty(m
));
792 static void test_hashmap_steal_first(void) {
793 _cleanup_hashmap_free_ Hashmap
*m
= NULL
;
797 m
= hashmap_new(&string_hash_ops
);
800 assert_se(hashmap_put(m
, "key 1", (void*) "1") == 1);
801 assert_se(hashmap_put(m
, "key 2", (void*) "22") == 1);
802 assert_se(hashmap_put(m
, "key 3", (void*) "333") == 1);
804 while ((val
= hashmap_steal_first(m
)))
805 seen
[strlen(val
) - 1]++;
807 assert_se(seen
[0] == 1 && seen
[1] == 1 && seen
[2] == 1);
809 assert_se(hashmap_isempty(m
));
812 static void test_hashmap_clear_free_free(void) {
813 _cleanup_hashmap_free_ Hashmap
*m
= NULL
;
815 m
= hashmap_new(&string_hash_ops
);
818 assert_se(hashmap_put(m
, strdup("key 1"), NULL
) == 1);
819 assert_se(hashmap_put(m
, strdup("key 2"), NULL
) == 1);
820 assert_se(hashmap_put(m
, strdup("key 3"), NULL
) == 1);
822 hashmap_clear_free_free(m
);
823 assert_se(hashmap_isempty(m
));
826 static void test_hashmap_reserve(void) {
827 _cleanup_hashmap_free_ Hashmap
*m
= NULL
;
829 m
= hashmap_new(&string_hash_ops
);
831 assert_se(hashmap_reserve(m
, 1) == 0);
832 assert_se(hashmap_buckets(m
) < 1000);
833 assert_se(hashmap_reserve(m
, 1000) == 0);
834 assert_se(hashmap_buckets(m
) >= 1000);
835 assert_se(hashmap_isempty(m
));
837 assert_se(hashmap_put(m
, "key 1", (void*) "val 1") == 1);
839 assert_se(hashmap_reserve(m
, UINT_MAX
) == -ENOMEM
);
840 assert_se(hashmap_reserve(m
, UINT_MAX
- 1) == -ENOMEM
);
843 void test_hashmap_funcs(void) {
845 test_hashmap_get_strv();
846 test_hashmap_move_one();
848 test_hashmap_replace();
849 test_hashmap_update();
851 test_hashmap_remove();
852 test_hashmap_remove2();
853 test_hashmap_remove_value();
854 test_hashmap_remove_and_put();
855 test_hashmap_remove_and_replace();
856 test_hashmap_ensure_allocated();
857 test_hashmap_foreach();
858 test_hashmap_foreach_key();
859 test_hashmap_contains();
860 test_hashmap_merge();
861 test_hashmap_isempty();
866 test_hashmap_first();
867 test_hashmap_first_key();
868 test_hashmap_steal_first_key();
869 test_hashmap_steal_first();
870 test_hashmap_clear_free_free();
871 test_hashmap_reserve();