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/>.
25 static void test_hashmap_replace(void) {
27 char *val1, *val2, *val3, *val4, *val5, *r;
29 m = hashmap_new(&string_hash_ops);
31 val1 = strdup("val1");
33 val2 = strdup("val2");
35 val3 = strdup("val3");
37 val4 = strdup("val4");
39 val5 = strdup("val5");
42 hashmap_put(m, "key 1", val1);
43 hashmap_put(m, "key 2", val2);
44 hashmap_put(m, "key 3", val3);
45 hashmap_put(m, "key 4", val4);
47 hashmap_replace(m, "key 3", val1);
48 r = hashmap_get(m, "key 3");
49 assert_se(streq(r, "val1"));
51 hashmap_replace(m, "key 5", val5);
52 r = hashmap_get(m, "key 5");
53 assert_se(streq(r, "val5"));
63 static void test_hashmap_copy(void) {
65 char *val1, *val2, *val3, *val4, *r;
67 val1 = strdup("val1");
69 val2 = strdup("val2");
71 val3 = strdup("val3");
73 val4 = strdup("val4");
76 m = hashmap_new(&string_hash_ops);
78 hashmap_put(m, "key 1", val1);
79 hashmap_put(m, "key 2", val2);
80 hashmap_put(m, "key 3", val3);
81 hashmap_put(m, "key 4", val4);
83 copy = hashmap_copy(m);
85 r = hashmap_get(copy, "key 1");
86 assert_se(streq(r, "val1"));
87 r = hashmap_get(copy, "key 2");
88 assert_se(streq(r, "val2"));
89 r = hashmap_get(copy, "key 3");
90 assert_se(streq(r, "val3"));
91 r = hashmap_get(copy, "key 4");
92 assert_se(streq(r, "val4"));
94 hashmap_free_free(copy);
98 static void test_hashmap_get_strv(void) {
101 char *val1, *val2, *val3, *val4;
103 val1 = strdup("val1");
105 val2 = strdup("val2");
107 val3 = strdup("val3");
109 val4 = strdup("val4");
112 m = hashmap_new(&string_hash_ops);
114 hashmap_put(m, "key 1", val1);
115 hashmap_put(m, "key 2", val2);
116 hashmap_put(m, "key 3", val3);
117 hashmap_put(m, "key 4", val4);
119 strv = hashmap_get_strv(m);
121 assert_se(streq(strv[0], "val1"));
122 assert_se(streq(strv[1], "val2"));
123 assert_se(streq(strv[2], "val3"));
124 assert_se(streq(strv[3], "val4"));
131 static void test_hashmap_move_one(void) {
133 char *val1, *val2, *val3, *val4, *r;
135 val1 = strdup("val1");
137 val2 = strdup("val2");
139 val3 = strdup("val3");
141 val4 = strdup("val4");
144 m = hashmap_new(&string_hash_ops);
145 n = hashmap_new(&string_hash_ops);
147 hashmap_put(m, "key 1", val1);
148 hashmap_put(m, "key 2", val2);
149 hashmap_put(m, "key 3", val3);
150 hashmap_put(m, "key 4", val4);
152 hashmap_move_one(n, m, "key 3");
153 hashmap_move_one(n, m, "key 4");
155 r = hashmap_get(n, "key 3");
156 assert_se(r && streq(r, "val3"));
157 r = hashmap_get(n, "key 4");
158 assert_se(r && streq(r, "val4"));
159 r = hashmap_get(m, "key 3");
163 hashmap_free_free(m);
164 hashmap_free_free(n);
167 static void test_hashmap_next(void) {
169 char *val1, *val2, *val3, *val4, *r;
171 m = hashmap_new(&string_hash_ops);
172 val1 = strdup("val1");
174 val2 = strdup("val2");
176 val3 = strdup("val3");
178 val4 = strdup("val4");
181 hashmap_put(m, "key 1", val1);
182 hashmap_put(m, "key 2", val2);
183 hashmap_put(m, "key 3", val3);
184 hashmap_put(m, "key 4", val4);
186 r = hashmap_next(m, "key 1");
187 assert_se(streq(r, val2));
188 r = hashmap_next(m, "key 2");
189 assert_se(streq(r, val3));
190 r = hashmap_next(m, "key 3");
191 assert_se(streq(r, val4));
192 r = hashmap_next(m, "key 4");
195 hashmap_free_free(m);
198 static void test_hashmap_update(void) {
200 char *val1, *val2, *r;
202 m = hashmap_new(&string_hash_ops);
203 val1 = strdup("old_value");
205 val2 = strdup("new_value");
208 hashmap_put(m, "key 1", val1);
209 r = hashmap_get(m, "key 1");
210 assert_se(streq(r, "old_value"));
212 hashmap_update(m, "key 1", val2);
213 r = hashmap_get(m, "key 1");
214 assert_se(streq(r, "new_value"));
221 static void test_hashmap_put(void) {
223 int valid_hashmap_put;
225 m = hashmap_new(&string_hash_ops);
227 valid_hashmap_put = hashmap_put(m, "key 1", (void*) (const char *) "val 1");
228 assert_se(valid_hashmap_put == 1);
234 static void test_hashmap_remove_and_put(void) {
235 _cleanup_hashmap_free_ Hashmap *m = NULL;
239 m = hashmap_new(&string_hash_ops);
242 valid = hashmap_remove_and_put(m, "unvalid key", "new key", NULL);
243 assert_se(valid < 0);
245 valid = hashmap_put(m, "key 1", (void*) (const char *) "val 1");
246 assert_se(valid == 1);
247 valid = hashmap_remove_and_put(m, "key 1", "key 2", (void*) (const char *) "val 2");
248 assert_se(valid == 0);
250 r = hashmap_get(m, "key 2");
251 assert_se(streq(r, "val 2"));
252 assert_se(!hashmap_get(m, "key 1"));
254 valid = hashmap_put(m, "key 3", (void*) (const char *) "val 3");
255 assert_se(valid == 1);
256 valid = hashmap_remove_and_put(m, "key 3", "key 2", (void*) (const char *) "val 2");
257 assert_se(valid < 0);
260 static void test_hashmap_ensure_allocated(void) {
264 m = hashmap_new(&string_hash_ops);
266 valid_hashmap = hashmap_ensure_allocated(&m, &string_hash_ops);
267 assert_se(valid_hashmap == 0);
273 static void test_hashmap_foreach_key(void) {
276 bool key_found[] = { false, false, false, false };
279 static const char key_table[] =
285 m = hashmap_new(&string_hash_ops);
287 NULSTR_FOREACH(key, key_table)
288 hashmap_put(m, key, (void*) (const char*) "my dummy val");
290 HASHMAP_FOREACH_KEY(s, key, m, i) {
291 if (!key_found[0] && streq(key, "key 1"))
293 else if (!key_found[1] && streq(key, "key 2"))
295 else if (!key_found[2] && streq(key, "key 3"))
297 else if (!key_found[3] && streq(key, "fail"))
302 assert_se(key_found[0] && key_found[1] && key_found[2] && !key_found[3]);
307 static void test_hashmap_foreach(void) {
310 bool value_found[] = { false, false, false, false };
311 char *val1, *val2, *val3, *val4, *s;
313 val1 = strdup("my val1");
315 val2 = strdup("my val2");
317 val3 = strdup("my val3");
319 val4 = strdup("my val4");
322 m = hashmap_new(&string_hash_ops);
324 hashmap_put(m, "Key 1", val1);
325 hashmap_put(m, "Key 2", val2);
326 hashmap_put(m, "Key 3", val3);
327 hashmap_put(m, "Key 4", val4);
329 HASHMAP_FOREACH(s, m, i) {
330 if (!value_found[0] && streq(s, val1))
331 value_found[0] = true;
332 else if (!value_found[1] && streq(s, val2))
333 value_found[1] = true;
334 else if (!value_found[2] && streq(s, val3))
335 value_found[2] = true;
336 else if (!value_found[3] && streq(s, val4))
337 value_found[3] = true;
341 assert_se(value_found[0] && value_found[1] && value_found[2] && value_found[3]);
343 hashmap_free_free(m);
346 static void test_hashmap_merge(void) {
349 char *val1, *val2, *val3, *val4, *r;
351 val1 = strdup("my val1");
353 val2 = strdup("my val2");
355 val3 = strdup("my val3");
357 val4 = strdup("my val4");
360 n = hashmap_new(&string_hash_ops);
361 m = hashmap_new(&string_hash_ops);
363 hashmap_put(m, "Key 1", val1);
364 hashmap_put(m, "Key 2", val2);
365 hashmap_put(n, "Key 3", val3);
366 hashmap_put(n, "Key 4", val4);
368 assert_se(hashmap_merge(m, n) == 0);
369 r = hashmap_get(m, "Key 3");
370 assert_se(r && streq(r, "my val3"));
371 r = hashmap_get(m, "Key 4");
372 assert_se(r && streq(r, "my val4"));
377 hashmap_free_free(m);
380 static void test_hashmap_contains(void) {
384 val1 = strdup("my val");
387 m = hashmap_new(&string_hash_ops);
389 assert_se(!hashmap_contains(m, "Key 1"));
390 hashmap_put(m, "Key 1", val1);
391 assert_se(hashmap_contains(m, "Key 1"));
394 hashmap_free_free(m);
397 static void test_hashmap_isempty(void) {
401 val1 = strdup("my val");
404 m = hashmap_new(&string_hash_ops);
406 assert_se(hashmap_isempty(m));
407 hashmap_put(m, "Key 1", val1);
408 assert_se(!hashmap_isempty(m));
411 hashmap_free_free(m);
414 static void test_hashmap_size(void) {
416 char *val1, *val2, *val3, *val4;
418 val1 = strdup("my val");
420 val2 = strdup("my val");
422 val3 = strdup("my val");
424 val4 = strdup("my val");
427 m = hashmap_new(&string_hash_ops);
429 hashmap_put(m, "Key 1", val1);
430 hashmap_put(m, "Key 2", val2);
431 hashmap_put(m, "Key 3", val3);
432 hashmap_put(m, "Key 4", val4);
435 assert_se(hashmap_size(m) == 4);
436 hashmap_free_free(m);
439 static void test_hashmap_get(void) {
444 val = strdup("my val");
447 m = hashmap_new(&string_hash_ops);
449 hashmap_put(m, "Key 1", val);
451 r = hashmap_get(m, "Key 1");
452 assert_se(streq(r, val));
455 hashmap_free_free(m);
458 static void test_hashmap_many(void) {
462 #define N_ENTRIES 100000
464 assert_se(h = hashmap_new(NULL));
466 for (i = 1; i < N_ENTRIES*3; i+=3) {
467 assert_se(hashmap_put(h, UINT_TO_PTR(i), UINT_TO_PTR(i)) >= 0);
468 assert_se(PTR_TO_UINT(hashmap_get(h, UINT_TO_PTR(i))) == i);
471 for (i = 1; i < N_ENTRIES*3; i++)
472 assert_se(hashmap_contains(h, UINT_TO_PTR(i)) == (i % 3 == 1));
474 log_info("%u <= %u * 0.75 = %g", hashmap_size(h), hashmap_buckets(h), hashmap_buckets(h) * 0.75);
476 assert_se(hashmap_size(h) <= hashmap_buckets(h) * 0.75);
477 assert_se(hashmap_size(h) == N_ENTRIES);
482 static void test_hashmap_first_key(void) {
483 _cleanup_hashmap_free_ Hashmap *m = NULL;
485 m = hashmap_new(&string_hash_ops);
488 assert_se(!hashmap_first_key(m));
489 assert_se(hashmap_put(m, "key 1", NULL) == 1);
490 assert_se(streq(hashmap_first_key(m), "key 1"));
491 assert_se(hashmap_put(m, "key 2", NULL) == 1);
492 assert_se(streq(hashmap_first_key(m), "key 1"));
493 assert_se(hashmap_remove(m, "key 1") == NULL);
494 assert_se(streq(hashmap_first_key(m), "key 2"));
497 static void test_hashmap_steal_first_key(void) {
498 _cleanup_hashmap_free_ Hashmap *m = NULL;
500 m = hashmap_new(&string_hash_ops);
503 assert_se(!hashmap_steal_first_key(m));
504 assert_se(hashmap_put(m, "key 1", NULL) == 1);
505 assert_se(streq(hashmap_steal_first_key(m), "key 1"));
507 assert_se(hashmap_isempty(m));
510 static void test_hashmap_steal_first(void) {
511 _cleanup_hashmap_free_ Hashmap *m = NULL;
515 m = hashmap_new(&string_hash_ops);
518 assert_se(hashmap_put(m, "key 1", (void*) "1") == 1);
519 assert_se(hashmap_put(m, "key 2", (void*) "22") == 1);
520 assert_se(hashmap_put(m, "key 3", (void*) "333") == 1);
522 while ((val = hashmap_steal_first(m)))
523 seen[strlen(val) - 1]++;
525 assert(seen[0] == 1 && seen[1] == 1 && seen[2] == 1);
527 assert_se(hashmap_isempty(m));
530 static void test_hashmap_clear_free_free(void) {
531 _cleanup_hashmap_free_ Hashmap *m = NULL;
533 m = hashmap_new(&string_hash_ops);
536 assert_se(hashmap_put(m, strdup("key 1"), NULL) == 1);
537 assert_se(hashmap_put(m, strdup("key 2"), NULL) == 1);
538 assert_se(hashmap_put(m, strdup("key 3"), NULL) == 1);
540 hashmap_clear_free_free(m);
541 assert_se(hashmap_isempty(m));
544 static void test_uint64_compare_func(void) {
545 const uint64_t a = 0x100, b = 0x101;
547 assert_se(uint64_compare_func(&a, &a) == 0);
548 assert_se(uint64_compare_func(&a, &b) == -1);
549 assert_se(uint64_compare_func(&b, &a) == 1);
552 static void test_trivial_compare_func(void) {
553 assert_se(trivial_compare_func(INT_TO_PTR('a'), INT_TO_PTR('a')) == 0);
554 assert_se(trivial_compare_func(INT_TO_PTR('a'), INT_TO_PTR('b')) == -1);
555 assert_se(trivial_compare_func(INT_TO_PTR('b'), INT_TO_PTR('a')) == 1);
558 static void test_string_compare_func(void) {
559 assert_se(!string_compare_func("fred", "wilma") == 0);
560 assert_se(string_compare_func("fred", "fred") == 0);
563 int main(int argc, const char *argv[]) {
565 test_hashmap_get_strv();
566 test_hashmap_move_one();
568 test_hashmap_replace();
569 test_hashmap_update();
571 test_hashmap_remove_and_put();
572 test_hashmap_ensure_allocated();
573 test_hashmap_foreach();
574 test_hashmap_foreach_key();
575 test_hashmap_contains();
576 test_hashmap_merge();
577 test_hashmap_isempty();
581 test_hashmap_first_key();
582 test_hashmap_steal_first_key();
583 test_hashmap_steal_first();
584 test_hashmap_clear_free_free();
585 test_uint64_compare_func();
586 test_trivial_compare_func();
587 test_string_compare_func();