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/>.
21 #include "string-util.h"
25 void test_hashmap_funcs(void);
27 static void test_hashmap_replace(void) {
29 char *val1
, *val2
, *val3
, *val4
, *val5
, *r
;
31 m
= hashmap_new(&string_hash_ops
);
33 val1
= strdup("val1");
35 val2
= strdup("val2");
37 val3
= strdup("val3");
39 val4
= strdup("val4");
41 val5
= strdup("val5");
44 hashmap_put(m
, "key 1", val1
);
45 hashmap_put(m
, "key 2", val2
);
46 hashmap_put(m
, "key 3", val3
);
47 hashmap_put(m
, "key 4", val4
);
49 hashmap_replace(m
, "key 3", val1
);
50 r
= hashmap_get(m
, "key 3");
51 assert_se(streq(r
, "val1"));
53 hashmap_replace(m
, "key 5", val5
);
54 r
= hashmap_get(m
, "key 5");
55 assert_se(streq(r
, "val5"));
65 static void test_hashmap_copy(void) {
67 char *val1
, *val2
, *val3
, *val4
, *r
;
69 val1
= strdup("val1");
71 val2
= strdup("val2");
73 val3
= strdup("val3");
75 val4
= strdup("val4");
78 m
= hashmap_new(&string_hash_ops
);
80 hashmap_put(m
, "key 1", val1
);
81 hashmap_put(m
, "key 2", val2
);
82 hashmap_put(m
, "key 3", val3
);
83 hashmap_put(m
, "key 4", val4
);
85 copy
= hashmap_copy(m
);
87 r
= hashmap_get(copy
, "key 1");
88 assert_se(streq(r
, "val1"));
89 r
= hashmap_get(copy
, "key 2");
90 assert_se(streq(r
, "val2"));
91 r
= hashmap_get(copy
, "key 3");
92 assert_se(streq(r
, "val3"));
93 r
= hashmap_get(copy
, "key 4");
94 assert_se(streq(r
, "val4"));
96 hashmap_free_free(copy
);
100 static void test_hashmap_get_strv(void) {
103 char *val1
, *val2
, *val3
, *val4
;
105 val1
= strdup("val1");
107 val2
= strdup("val2");
109 val3
= strdup("val3");
111 val4
= strdup("val4");
114 m
= hashmap_new(&string_hash_ops
);
116 hashmap_put(m
, "key 1", val1
);
117 hashmap_put(m
, "key 2", val2
);
118 hashmap_put(m
, "key 3", val3
);
119 hashmap_put(m
, "key 4", val4
);
121 strv
= hashmap_get_strv(m
);
124 strv
= strv_sort(strv
);
127 assert_se(streq(strv
[0], "val1"));
128 assert_se(streq(strv
[1], "val2"));
129 assert_se(streq(strv
[2], "val3"));
130 assert_se(streq(strv
[3], "val4"));
137 static void test_hashmap_move_one(void) {
139 char *val1
, *val2
, *val3
, *val4
, *r
;
141 val1
= strdup("val1");
143 val2
= strdup("val2");
145 val3
= strdup("val3");
147 val4
= strdup("val4");
150 m
= hashmap_new(&string_hash_ops
);
151 n
= hashmap_new(&string_hash_ops
);
153 hashmap_put(m
, "key 1", val1
);
154 hashmap_put(m
, "key 2", val2
);
155 hashmap_put(m
, "key 3", val3
);
156 hashmap_put(m
, "key 4", val4
);
158 assert_se(hashmap_move_one(n
, NULL
, "key 3") == -ENOENT
);
159 assert_se(hashmap_move_one(n
, m
, "key 5") == -ENOENT
);
160 assert_se(hashmap_move_one(n
, m
, "key 3") == 0);
161 assert_se(hashmap_move_one(n
, m
, "key 4") == 0);
163 r
= hashmap_get(n
, "key 3");
164 assert_se(r
&& streq(r
, "val3"));
165 r
= hashmap_get(n
, "key 4");
166 assert_se(r
&& streq(r
, "val4"));
167 r
= hashmap_get(m
, "key 3");
170 assert_se(hashmap_move_one(n
, m
, "key 3") == -EEXIST
);
172 hashmap_free_free(m
);
173 hashmap_free_free(n
);
176 static void test_hashmap_move(void) {
178 char *val1
, *val2
, *val3
, *val4
, *r
;
180 val1
= strdup("val1");
182 val2
= strdup("val2");
184 val3
= strdup("val3");
186 val4
= strdup("val4");
189 m
= hashmap_new(&string_hash_ops
);
190 n
= hashmap_new(&string_hash_ops
);
192 hashmap_put(n
, "key 1", strdup(val1
));
193 hashmap_put(m
, "key 1", val1
);
194 hashmap_put(m
, "key 2", val2
);
195 hashmap_put(m
, "key 3", val3
);
196 hashmap_put(m
, "key 4", val4
);
198 assert_se(hashmap_move(n
, NULL
) == 0);
199 assert_se(hashmap_move(n
, m
) == 0);
201 assert_se(hashmap_size(m
) == 1);
202 r
= hashmap_get(m
, "key 1");
203 assert_se(r
&& streq(r
, "val1"));
205 r
= hashmap_get(n
, "key 1");
206 assert_se(r
&& streq(r
, "val1"));
207 r
= hashmap_get(n
, "key 2");
208 assert_se(r
&& streq(r
, "val2"));
209 r
= hashmap_get(n
, "key 3");
210 assert_se(r
&& streq(r
, "val3"));
211 r
= hashmap_get(n
, "key 4");
212 assert_se(r
&& streq(r
, "val4"));
214 hashmap_free_free(m
);
215 hashmap_free_free(n
);
218 static void test_hashmap_update(void) {
220 char *val1
, *val2
, *r
;
222 m
= hashmap_new(&string_hash_ops
);
223 val1
= strdup("old_value");
225 val2
= strdup("new_value");
228 hashmap_put(m
, "key 1", val1
);
229 r
= hashmap_get(m
, "key 1");
230 assert_se(streq(r
, "old_value"));
232 assert_se(hashmap_update(m
, "key 2", val2
) == -ENOENT
);
233 r
= hashmap_get(m
, "key 1");
234 assert_se(streq(r
, "old_value"));
236 assert_se(hashmap_update(m
, "key 1", val2
) == 0);
237 r
= hashmap_get(m
, "key 1");
238 assert_se(streq(r
, "new_value"));
245 static void test_hashmap_put(void) {
247 int valid_hashmap_put
;
248 void *val1
= (void*) "val 1";
249 void *val2
= (void*) "val 2";
250 _cleanup_free_
char* key1
= NULL
;
252 assert_se(hashmap_ensure_allocated(&m
, &string_hash_ops
) >= 0);
255 valid_hashmap_put
= hashmap_put(m
, "key 1", val1
);
256 assert_se(valid_hashmap_put
== 1);
257 assert_se(hashmap_put(m
, "key 1", val1
) == 0);
258 assert_se(hashmap_put(m
, "key 1", val2
) == -EEXIST
);
259 key1
= strdup("key 1");
260 assert_se(hashmap_put(m
, key1
, val1
) == 0);
261 assert_se(hashmap_put(m
, key1
, val2
) == -EEXIST
);
266 static void test_hashmap_remove(void) {
267 _cleanup_hashmap_free_ Hashmap
*m
= NULL
;
270 r
= hashmap_remove(NULL
, "key 1");
271 assert_se(r
== NULL
);
273 m
= hashmap_new(&string_hash_ops
);
276 r
= hashmap_remove(m
, "no such key");
277 assert_se(r
== NULL
);
279 hashmap_put(m
, "key 1", (void*) "val 1");
280 hashmap_put(m
, "key 2", (void*) "val 2");
282 r
= hashmap_remove(m
, "key 1");
283 assert_se(streq(r
, "val 1"));
285 r
= hashmap_get(m
, "key 2");
286 assert_se(streq(r
, "val 2"));
287 assert_se(!hashmap_get(m
, "key 1"));
290 static void test_hashmap_remove2(void) {
291 _cleanup_hashmap_free_free_free_ Hashmap
*m
= NULL
;
292 char key1
[] = "key 1";
293 char key2
[] = "key 2";
294 char val1
[] = "val 1";
295 char val2
[] = "val 2";
298 r
= hashmap_remove2(NULL
, "key 1", &r2
);
299 assert_se(r
== NULL
);
301 m
= hashmap_new(&string_hash_ops
);
304 r
= hashmap_remove2(m
, "no such key", &r2
);
305 assert_se(r
== NULL
);
307 hashmap_put(m
, strdup(key1
), strdup(val1
));
308 hashmap_put(m
, strdup(key2
), strdup(val2
));
310 r
= hashmap_remove2(m
, key1
, &r2
);
311 assert_se(streq(r
, val1
));
312 assert_se(streq(r2
, key1
));
316 r
= hashmap_get(m
, key2
);
317 assert_se(streq(r
, val2
));
318 assert_se(!hashmap_get(m
, key1
));
321 static void test_hashmap_remove_value(void) {
322 _cleanup_hashmap_free_ Hashmap
*m
= NULL
;
325 r
= hashmap_remove_value(NULL
, "key 1", (void*) "val 1");
326 assert_se(r
== NULL
);
328 m
= hashmap_new(&string_hash_ops
);
331 r
= hashmap_remove_value(m
, "key 1", (void*) "val 1");
332 assert_se(r
== NULL
);
334 hashmap_put(m
, "key 1", (void*) "val 1");
335 hashmap_put(m
, "key 2", (void*) "val 2");
337 r
= hashmap_remove_value(m
, "key 1", (void*) "val 1");
338 assert_se(streq(r
, "val 1"));
340 r
= hashmap_get(m
, "key 2");
341 assert_se(streq(r
, "val 2"));
342 assert_se(!hashmap_get(m
, "key 1"));
344 r
= hashmap_remove_value(m
, "key 2", (void*) "val 1");
345 assert_se(r
== NULL
);
347 r
= hashmap_get(m
, "key 2");
348 assert_se(streq(r
, "val 2"));
349 assert_se(!hashmap_get(m
, "key 1"));
352 static void test_hashmap_remove_and_put(void) {
353 _cleanup_hashmap_free_ Hashmap
*m
= NULL
;
357 m
= hashmap_new(&string_hash_ops
);
360 valid
= hashmap_remove_and_put(m
, "invalid key", "new key", NULL
);
361 assert_se(valid
== -ENOENT
);
363 valid
= hashmap_put(m
, "key 1", (void*) (const char *) "val 1");
364 assert_se(valid
== 1);
366 valid
= hashmap_remove_and_put(NULL
, "key 1", "key 2", (void*) (const char *) "val 2");
367 assert_se(valid
== -ENOENT
);
369 valid
= hashmap_remove_and_put(m
, "key 1", "key 2", (void*) (const char *) "val 2");
370 assert_se(valid
== 0);
372 r
= hashmap_get(m
, "key 2");
373 assert_se(streq(r
, "val 2"));
374 assert_se(!hashmap_get(m
, "key 1"));
376 valid
= hashmap_put(m
, "key 3", (void*) (const char *) "val 3");
377 assert_se(valid
== 1);
378 valid
= hashmap_remove_and_put(m
, "key 3", "key 2", (void*) (const char *) "val 2");
379 assert_se(valid
== -EEXIST
);
382 static void test_hashmap_remove_and_replace(void) {
383 _cleanup_hashmap_free_ Hashmap
*m
= NULL
;
385 void *key1
= UINT_TO_PTR(1);
386 void *key2
= UINT_TO_PTR(2);
387 void *key3
= UINT_TO_PTR(3);
391 m
= hashmap_new(&trivial_hash_ops
);
394 valid
= hashmap_remove_and_replace(m
, key1
, key2
, NULL
);
395 assert_se(valid
== -ENOENT
);
397 valid
= hashmap_put(m
, key1
, key1
);
398 assert_se(valid
== 1);
400 valid
= hashmap_remove_and_replace(NULL
, key1
, key2
, key2
);
401 assert_se(valid
== -ENOENT
);
403 valid
= hashmap_remove_and_replace(m
, key1
, key2
, key2
);
404 assert_se(valid
== 0);
406 r
= hashmap_get(m
, key2
);
407 assert_se(r
== key2
);
408 assert_se(!hashmap_get(m
, key1
));
410 valid
= hashmap_put(m
, key3
, key3
);
411 assert_se(valid
== 1);
412 valid
= hashmap_remove_and_replace(m
, key3
, key2
, key2
);
413 assert_se(valid
== 0);
414 r
= hashmap_get(m
, key2
);
415 assert_se(r
== key2
);
416 assert_se(!hashmap_get(m
, key3
));
418 /* Repeat this test several times to increase the chance of hitting
419 * the less likely case in hashmap_remove_and_replace where it
420 * compensates for the backward shift. */
421 for (i
= 0; i
< 20; i
++) {
424 for (j
= 1; j
< 7; j
++)
425 hashmap_put(m
, UINT_TO_PTR(10*i
+ j
), UINT_TO_PTR(10*i
+ j
));
426 valid
= hashmap_remove_and_replace(m
, UINT_TO_PTR(10*i
+ 1),
427 UINT_TO_PTR(10*i
+ 2),
428 UINT_TO_PTR(10*i
+ 2));
429 assert_se(valid
== 0);
430 assert_se(!hashmap_get(m
, UINT_TO_PTR(10*i
+ 1)));
431 for (j
= 2; j
< 7; j
++) {
432 r
= hashmap_get(m
, UINT_TO_PTR(10*i
+ j
));
433 assert_se(r
== UINT_TO_PTR(10*i
+ j
));
438 static void test_hashmap_ensure_allocated(void) {
442 m
= hashmap_new(&string_hash_ops
);
444 valid_hashmap
= hashmap_ensure_allocated(&m
, &string_hash_ops
);
445 assert_se(valid_hashmap
== 0);
451 static void test_hashmap_foreach_key(void) {
454 bool key_found
[] = { false, false, false, false };
457 static const char key_table
[] =
463 m
= hashmap_new(&string_hash_ops
);
465 NULSTR_FOREACH(key
, key_table
)
466 hashmap_put(m
, key
, (void*) (const char*) "my dummy val");
468 HASHMAP_FOREACH_KEY(s
, key
, m
, i
) {
470 if (!key_found
[0] && streq(key
, "key 1"))
472 else if (!key_found
[1] && streq(key
, "key 2"))
474 else if (!key_found
[2] && streq(key
, "key 3"))
476 else if (!key_found
[3] && streq(key
, "fail"))
481 assert_se(key_found
[0] && key_found
[1] && key_found
[2] && !key_found
[3]);
486 static void test_hashmap_foreach(void) {
489 bool value_found
[] = { false, false, false, false };
490 char *val1
, *val2
, *val3
, *val4
, *s
;
493 val1
= strdup("my val1");
495 val2
= strdup("my val2");
497 val3
= strdup("my val3");
499 val4
= strdup("my val4");
505 HASHMAP_FOREACH(s
, m
, i
)
507 assert_se(count
== 0);
509 m
= hashmap_new(&string_hash_ops
);
512 HASHMAP_FOREACH(s
, m
, i
)
514 assert_se(count
== 0);
516 hashmap_put(m
, "Key 1", val1
);
517 hashmap_put(m
, "Key 2", val2
);
518 hashmap_put(m
, "Key 3", val3
);
519 hashmap_put(m
, "Key 4", val4
);
521 HASHMAP_FOREACH(s
, m
, i
) {
522 if (!value_found
[0] && streq(s
, val1
))
523 value_found
[0] = true;
524 else if (!value_found
[1] && streq(s
, val2
))
525 value_found
[1] = true;
526 else if (!value_found
[2] && streq(s
, val3
))
527 value_found
[2] = true;
528 else if (!value_found
[3] && streq(s
, val4
))
529 value_found
[3] = true;
533 assert_se(value_found
[0] && value_found
[1] && value_found
[2] && value_found
[3]);
535 hashmap_free_free(m
);
538 static void test_hashmap_merge(void) {
541 char *val1
, *val2
, *val3
, *val4
, *r
;
543 val1
= strdup("my val1");
545 val2
= strdup("my val2");
547 val3
= strdup("my val3");
549 val4
= strdup("my val4");
552 n
= hashmap_new(&string_hash_ops
);
553 m
= hashmap_new(&string_hash_ops
);
555 hashmap_put(m
, "Key 1", val1
);
556 hashmap_put(m
, "Key 2", val2
);
557 hashmap_put(n
, "Key 3", val3
);
558 hashmap_put(n
, "Key 4", val4
);
560 assert_se(hashmap_merge(m
, n
) == 0);
561 r
= hashmap_get(m
, "Key 3");
562 assert_se(r
&& streq(r
, "my val3"));
563 r
= hashmap_get(m
, "Key 4");
564 assert_se(r
&& streq(r
, "my val4"));
569 hashmap_free_free(m
);
572 static void test_hashmap_contains(void) {
576 val1
= strdup("my val");
579 m
= hashmap_new(&string_hash_ops
);
581 assert_se(!hashmap_contains(m
, "Key 1"));
582 hashmap_put(m
, "Key 1", val1
);
583 assert_se(hashmap_contains(m
, "Key 1"));
584 assert_se(!hashmap_contains(m
, "Key 2"));
586 assert_se(!hashmap_contains(NULL
, "Key 1"));
589 hashmap_free_free(m
);
592 static void test_hashmap_isempty(void) {
596 val1
= strdup("my val");
599 m
= hashmap_new(&string_hash_ops
);
601 assert_se(hashmap_isempty(m
));
602 hashmap_put(m
, "Key 1", val1
);
603 assert_se(!hashmap_isempty(m
));
606 hashmap_free_free(m
);
609 static void test_hashmap_size(void) {
611 char *val1
, *val2
, *val3
, *val4
;
613 val1
= strdup("my val");
615 val2
= strdup("my val");
617 val3
= strdup("my val");
619 val4
= strdup("my val");
622 assert_se(hashmap_size(NULL
) == 0);
623 assert_se(hashmap_buckets(NULL
) == 0);
625 m
= hashmap_new(&string_hash_ops
);
627 hashmap_put(m
, "Key 1", val1
);
628 hashmap_put(m
, "Key 2", val2
);
629 hashmap_put(m
, "Key 3", val3
);
630 hashmap_put(m
, "Key 4", val4
);
633 assert_se(hashmap_size(m
) == 4);
634 assert_se(hashmap_buckets(m
) >= 4);
635 hashmap_free_free(m
);
638 static void test_hashmap_get(void) {
643 val
= strdup("my val");
646 r
= hashmap_get(NULL
, "Key 1");
647 assert_se(r
== NULL
);
649 m
= hashmap_new(&string_hash_ops
);
651 hashmap_put(m
, "Key 1", val
);
653 r
= hashmap_get(m
, "Key 1");
654 assert_se(streq(r
, val
));
656 r
= hashmap_get(m
, "no such key");
657 assert_se(r
== NULL
);
660 hashmap_free_free(m
);
663 static void test_hashmap_get2(void) {
667 char key_orig
[] = "Key 1";
670 val
= strdup("my val");
673 key_copy
= strdup(key_orig
);
676 r
= hashmap_get2(NULL
, key_orig
, &key_copy
);
677 assert_se(r
== NULL
);
679 m
= hashmap_new(&string_hash_ops
);
681 hashmap_put(m
, key_copy
, val
);
684 r
= hashmap_get2(m
, key_orig
, &key_copy
);
685 assert_se(streq(r
, val
));
686 assert_se(key_orig
!= key_copy
);
687 assert_se(streq(key_orig
, key_copy
));
689 r
= hashmap_get2(m
, "no such key", NULL
);
690 assert_se(r
== NULL
);
693 hashmap_free_free_free(m
);
696 static void crippled_hashmap_func(const void *p
, struct siphash
*state
) {
697 return trivial_hash_func(INT_TO_PTR(PTR_TO_INT(p
) & 0xff), state
);
700 static const struct hash_ops crippled_hashmap_ops
= {
701 .hash
= crippled_hashmap_func
,
702 .compare
= trivial_compare_func
,
705 static void test_hashmap_many(void) {
709 static const struct {
710 const struct hash_ops
*ops
;
713 { .ops
= NULL
, .n_entries
= 1 << 20 },
714 { .ops
= &crippled_hashmap_ops
, .n_entries
= 1 << 14 },
718 for (j
= 0; j
< ELEMENTSOF(tests
); j
++) {
719 assert_se(h
= hashmap_new(tests
[j
].ops
));
721 for (i
= 1; i
< tests
[j
].n_entries
*3; i
+=3) {
722 assert_se(hashmap_put(h
, UINT_TO_PTR(i
), UINT_TO_PTR(i
)) >= 0);
723 assert_se(PTR_TO_UINT(hashmap_get(h
, UINT_TO_PTR(i
))) == i
);
726 for (i
= 1; i
< tests
[j
].n_entries
*3; i
++)
727 assert_se(hashmap_contains(h
, UINT_TO_PTR(i
)) == (i
% 3 == 1));
729 log_info("%u <= %u * 0.8 = %g", hashmap_size(h
), hashmap_buckets(h
), hashmap_buckets(h
) * 0.8);
731 assert_se(hashmap_size(h
) <= hashmap_buckets(h
) * 0.8);
732 assert_se(hashmap_size(h
) == tests
[j
].n_entries
);
734 while (!hashmap_isempty(h
)) {
735 k
= hashmap_first_key(h
);
736 v
= hashmap_remove(h
, k
);
744 static void test_hashmap_first(void) {
745 _cleanup_hashmap_free_ Hashmap
*m
= NULL
;
747 m
= hashmap_new(&string_hash_ops
);
750 assert_se(!hashmap_first(m
));
751 assert_se(hashmap_put(m
, "key 1", (void*) "val 1") == 1);
752 assert_se(streq(hashmap_first(m
), "val 1"));
753 assert_se(hashmap_put(m
, "key 2", (void*) "val 2") == 1);
755 assert_se(streq(hashmap_first(m
), "val 1"));
756 assert_se(hashmap_remove(m
, "key 1"));
757 assert_se(streq(hashmap_first(m
), "val 2"));
761 static void test_hashmap_first_key(void) {
762 _cleanup_hashmap_free_ Hashmap
*m
= NULL
;
764 m
= hashmap_new(&string_hash_ops
);
767 assert_se(!hashmap_first_key(m
));
768 assert_se(hashmap_put(m
, "key 1", NULL
) == 1);
769 assert_se(streq(hashmap_first_key(m
), "key 1"));
770 assert_se(hashmap_put(m
, "key 2", NULL
) == 1);
772 assert_se(streq(hashmap_first_key(m
), "key 1"));
773 assert_se(hashmap_remove(m
, "key 1") == NULL
);
774 assert_se(streq(hashmap_first_key(m
), "key 2"));
778 static void test_hashmap_steal_first_key(void) {
779 _cleanup_hashmap_free_ Hashmap
*m
= NULL
;
781 m
= hashmap_new(&string_hash_ops
);
784 assert_se(!hashmap_steal_first_key(m
));
785 assert_se(hashmap_put(m
, "key 1", NULL
) == 1);
786 assert_se(streq(hashmap_steal_first_key(m
), "key 1"));
788 assert_se(hashmap_isempty(m
));
791 static void test_hashmap_steal_first(void) {
792 _cleanup_hashmap_free_ Hashmap
*m
= NULL
;
796 m
= hashmap_new(&string_hash_ops
);
799 assert_se(hashmap_put(m
, "key 1", (void*) "1") == 1);
800 assert_se(hashmap_put(m
, "key 2", (void*) "22") == 1);
801 assert_se(hashmap_put(m
, "key 3", (void*) "333") == 1);
803 while ((val
= hashmap_steal_first(m
)))
804 seen
[strlen(val
) - 1]++;
806 assert_se(seen
[0] == 1 && seen
[1] == 1 && seen
[2] == 1);
808 assert_se(hashmap_isempty(m
));
811 static void test_hashmap_clear_free_free(void) {
812 _cleanup_hashmap_free_ Hashmap
*m
= NULL
;
814 m
= hashmap_new(&string_hash_ops
);
817 assert_se(hashmap_put(m
, strdup("key 1"), NULL
) == 1);
818 assert_se(hashmap_put(m
, strdup("key 2"), NULL
) == 1);
819 assert_se(hashmap_put(m
, strdup("key 3"), NULL
) == 1);
821 hashmap_clear_free_free(m
);
822 assert_se(hashmap_isempty(m
));
825 static void test_hashmap_reserve(void) {
826 _cleanup_hashmap_free_ Hashmap
*m
= NULL
;
828 m
= hashmap_new(&string_hash_ops
);
830 assert_se(hashmap_reserve(m
, 1) == 0);
831 assert_se(hashmap_buckets(m
) < 1000);
832 assert_se(hashmap_reserve(m
, 1000) == 0);
833 assert_se(hashmap_buckets(m
) >= 1000);
834 assert_se(hashmap_isempty(m
));
836 assert_se(hashmap_put(m
, "key 1", (void*) "val 1") == 1);
838 assert_se(hashmap_reserve(m
, UINT_MAX
) == -ENOMEM
);
839 assert_se(hashmap_reserve(m
, UINT_MAX
- 1) == -ENOMEM
);
842 void test_hashmap_funcs(void) {
844 test_hashmap_get_strv();
845 test_hashmap_move_one();
847 test_hashmap_replace();
848 test_hashmap_update();
850 test_hashmap_remove();
851 test_hashmap_remove2();
852 test_hashmap_remove_value();
853 test_hashmap_remove_and_put();
854 test_hashmap_remove_and_replace();
855 test_hashmap_ensure_allocated();
856 test_hashmap_foreach();
857 test_hashmap_foreach_key();
858 test_hashmap_contains();
859 test_hashmap_merge();
860 test_hashmap_isempty();
865 test_hashmap_first();
866 test_hashmap_first_key();
867 test_hashmap_steal_first_key();
868 test_hashmap_steal_first();
869 test_hashmap_clear_free_free();
870 test_hashmap_reserve();