]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-hashmap.c
hashmap: introduce hash_ops to make struct Hashmap smaller
[thirdparty/systemd.git] / src / test / test-hashmap.c
CommitLineData
9341a4a1
DB
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 <inttypes.h>
21#include "strv.h"
22#include "util.h"
23#include "hashmap.h"
24
25static void test_hashmap_replace(void) {
26 Hashmap *m;
27 char *val1, *val2, *val3, *val4, *val5, *r;
28
d5099efc 29 m = hashmap_new(&string_hash_ops);
9341a4a1
DB
30
31 val1 = strdup("val1");
32 assert_se(val1);
33 val2 = strdup("val2");
34 assert_se(val2);
35 val3 = strdup("val3");
36 assert_se(val3);
37 val4 = strdup("val4");
38 assert_se(val4);
39 val5 = strdup("val5");
40 assert_se(val5);
41
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);
46
47 hashmap_replace(m, "key 3", val1);
48 r = hashmap_get(m, "key 3");
49 assert_se(streq(r, "val1"));
50
51 hashmap_replace(m, "key 5", val5);
52 r = hashmap_get(m, "key 5");
53 assert_se(streq(r, "val5"));
54
55 free(val1);
56 free(val2);
57 free(val3);
58 free(val4);
59 free(val5);
60 hashmap_free(m);
61}
62
63static void test_hashmap_copy(void) {
64 Hashmap *m, *copy;
65 char *val1, *val2, *val3, *val4, *r;
66
67 val1 = strdup("val1");
68 assert_se(val1);
69 val2 = strdup("val2");
70 assert_se(val2);
71 val3 = strdup("val3");
72 assert_se(val3);
73 val4 = strdup("val4");
74 assert_se(val4);
75
d5099efc 76 m = hashmap_new(&string_hash_ops);
9341a4a1
DB
77
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);
82
83 copy = hashmap_copy(m);
84
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"));
93
94 hashmap_free_free(copy);
95 hashmap_free(m);
96}
97
98static void test_hashmap_get_strv(void) {
99 Hashmap *m;
100 char **strv;
101 char *val1, *val2, *val3, *val4;
102
103 val1 = strdup("val1");
104 assert_se(val1);
105 val2 = strdup("val2");
106 assert_se(val2);
107 val3 = strdup("val3");
108 assert_se(val3);
109 val4 = strdup("val4");
110 assert_se(val4);
111
d5099efc 112 m = hashmap_new(&string_hash_ops);
9341a4a1
DB
113
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);
118
119 strv = hashmap_get_strv(m);
120
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"));
125
126 strv_free(strv);
127
128 hashmap_free(m);
129}
130
131static void test_hashmap_move_one(void) {
132 Hashmap *m, *n;
133 char *val1, *val2, *val3, *val4, *r;
134
135 val1 = strdup("val1");
136 assert_se(val1);
137 val2 = strdup("val2");
138 assert_se(val2);
139 val3 = strdup("val3");
140 assert_se(val3);
141 val4 = strdup("val4");
142 assert_se(val4);
143
d5099efc
MS
144 m = hashmap_new(&string_hash_ops);
145 n = hashmap_new(&string_hash_ops);
9341a4a1
DB
146
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);
151
152 hashmap_move_one(n, m, "key 3");
153 hashmap_move_one(n, m, "key 4");
154
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");
160 assert_se(!r);
161
162
163 hashmap_free_free(m);
164 hashmap_free_free(n);
165}
166
167static void test_hashmap_next(void) {
168 Hashmap *m;
169 char *val1, *val2, *val3, *val4, *r;
170
d5099efc 171 m = hashmap_new(&string_hash_ops);
9341a4a1
DB
172 val1 = strdup("val1");
173 assert_se(val1);
174 val2 = strdup("val2");
175 assert_se(val2);
176 val3 = strdup("val3");
177 assert_se(val3);
178 val4 = strdup("val4");
179 assert_se(val4);
180
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);
185
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");
193 assert_se(!r);
194
195 hashmap_free_free(m);
196}
197
198static void test_hashmap_update(void) {
199 Hashmap *m;
200 char *val1, *val2, *r;
201
d5099efc 202 m = hashmap_new(&string_hash_ops);
9341a4a1
DB
203 val1 = strdup("old_value");
204 assert_se(val1);
205 val2 = strdup("new_value");
206 assert_se(val2);
207
208 hashmap_put(m, "key 1", val1);
209 r = hashmap_get(m, "key 1");
210 assert_se(streq(r, "old_value"));
211
212 hashmap_update(m, "key 1", val2);
213 r = hashmap_get(m, "key 1");
214 assert_se(streq(r, "new_value"));
215
216 free(val1);
217 free(val2);
218 hashmap_free(m);
219}
220
221static void test_hashmap_put(void) {
222 Hashmap *m;
223 int valid_hashmap_put;
224
d5099efc 225 m = hashmap_new(&string_hash_ops);
9341a4a1
DB
226
227 valid_hashmap_put = hashmap_put(m, "key 1", (void*) (const char *) "val 1");
228 assert_se(valid_hashmap_put == 1);
229
230 assert_se(m);
231 hashmap_free(m);
232}
233
d06b3a9d
RC
234static void test_hashmap_remove_and_put(void) {
235 _cleanup_hashmap_free_ Hashmap *m = NULL;
236 int valid;
237 char *r;
238
d5099efc 239 m = hashmap_new(&string_hash_ops);
d06b3a9d
RC
240 assert_se(m);
241
242 valid = hashmap_remove_and_put(m, "unvalid key", "new key", NULL);
243 assert_se(valid < 0);
244
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);
249
250 r = hashmap_get(m, "key 2");
251 assert_se(streq(r, "val 2"));
252 assert_se(!hashmap_get(m, "key 1"));
253
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);
258}
259
9341a4a1
DB
260static void test_hashmap_ensure_allocated(void) {
261 Hashmap *m;
262 int valid_hashmap;
263
d5099efc 264 m = hashmap_new(&string_hash_ops);
9341a4a1 265
d5099efc 266 valid_hashmap = hashmap_ensure_allocated(&m, &string_hash_ops);
9341a4a1
DB
267 assert_se(valid_hashmap == 0);
268
269 assert_se(m);
270 hashmap_free(m);
271}
272
273static void test_hashmap_foreach_key(void) {
274 Hashmap *m;
275 Iterator i;
276 bool key_found[] = { false, false, false, false };
277 const char *s;
278 const char *key;
279 static const char key_table[] =
280 "key 1\0"
281 "key 2\0"
282 "key 3\0"
283 "key 4\0";
284
d5099efc 285 m = hashmap_new(&string_hash_ops);
9341a4a1
DB
286
287 NULSTR_FOREACH(key, key_table)
288 hashmap_put(m, key, (void*) (const char*) "my dummy val");
289
290 HASHMAP_FOREACH_KEY(s, key, m, i) {
291 if (!key_found[0] && streq(key, "key 1"))
292 key_found[0] = true;
293 else if (!key_found[1] && streq(key, "key 2"))
294 key_found[1] = true;
295 else if (!key_found[2] && streq(key, "key 3"))
296 key_found[2] = true;
297 else if (!key_found[3] && streq(key, "fail"))
298 key_found[3] = true;
299 }
300
301 assert_se(m);
302 assert_se(key_found[0] && key_found[1] && key_found[2] && !key_found[3]);
303
304 hashmap_free(m);
305}
306
307static void test_hashmap_foreach(void) {
308 Hashmap *m;
309 Iterator i;
310 bool value_found[] = { false, false, false, false };
311 char *val1, *val2, *val3, *val4, *s;
312
313 val1 = strdup("my val1");
314 assert_se(val1);
315 val2 = strdup("my val2");
316 assert_se(val2);
317 val3 = strdup("my val3");
318 assert_se(val3);
319 val4 = strdup("my val4");
320 assert_se(val4);
321
d5099efc 322 m = hashmap_new(&string_hash_ops);
9341a4a1
DB
323
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);
328
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;
338 }
339
340 assert_se(m);
341 assert_se(value_found[0] && value_found[1] && value_found[2] && value_found[3]);
342
343 hashmap_free_free(m);
344}
345
346static void test_hashmap_foreach_backwards(void) {
347 Hashmap *m;
348 Iterator i;
349 char *val1, *val2, *val3, *val4, *s;
350 bool value_found[] = { false, false, false, false };
351
352 val1 = strdup("my val1");
353 assert_se(val1);
354 val2 = strdup("my val2");
355 assert_se(val2);
356 val3 = strdup("my val3");
357 assert_se(val3);
358 val4 = strdup("my val4");
359 assert_se(val4);
360
d5099efc 361 m = hashmap_new(&string_hash_ops);
9341a4a1
DB
362 hashmap_put(m, "Key 1", val1);
363 hashmap_put(m, "Key 2", val2);
364 hashmap_put(m, "Key 3", val3);
365 hashmap_put(m, "Key 4", val4);
366
367 HASHMAP_FOREACH_BACKWARDS(s, m, i) {
368 if (!value_found[0] && streq(s, val1))
369 value_found[0] = true;
370 else if (!value_found[1] && streq(s, val2))
371 value_found[1] = true;
372 else if (!value_found[2] && streq(s, val3))
373 value_found[2] = true;
374 else if (!value_found[3] && streq(s, val4))
375 value_found[3] = true;
376 }
377
378 assert_se(m);
379 assert_se(value_found[0] && value_found[1] && value_found[2] && value_found[3]);
380
381 hashmap_free_free(m);
382}
383
384static void test_hashmap_merge(void) {
385 Hashmap *m;
386 Hashmap *n;
387 char *val1, *val2, *val3, *val4, *r;
388
389 val1 = strdup("my val1");
390 assert_se(val1);
391 val2 = strdup("my val2");
392 assert_se(val2);
393 val3 = strdup("my val3");
394 assert_se(val3);
395 val4 = strdup("my val4");
396 assert_se(val4);
397
d5099efc
MS
398 n = hashmap_new(&string_hash_ops);
399 m = hashmap_new(&string_hash_ops);
9341a4a1
DB
400
401 hashmap_put(m, "Key 1", val1);
402 hashmap_put(m, "Key 2", val2);
403 hashmap_put(n, "Key 3", val3);
404 hashmap_put(n, "Key 4", val4);
405
406 assert_se(hashmap_merge(m, n) == 0);
407 r = hashmap_get(m, "Key 3");
408 assert_se(r && streq(r, "my val3"));
409 r = hashmap_get(m, "Key 4");
410 assert_se(r && streq(r, "my val4"));
411
412 assert_se(n);
413 assert_se(m);
414 hashmap_free(n);
415 hashmap_free_free(m);
416}
417
418static void test_hashmap_contains(void) {
419 Hashmap *m;
420 char *val1;
421
422 val1 = strdup("my val");
423 assert_se(val1);
424
d5099efc 425 m = hashmap_new(&string_hash_ops);
9341a4a1
DB
426
427 assert_se(!hashmap_contains(m, "Key 1"));
428 hashmap_put(m, "Key 1", val1);
429 assert_se(hashmap_contains(m, "Key 1"));
430
431 assert_se(m);
432 hashmap_free_free(m);
433}
434
435static void test_hashmap_isempty(void) {
436 Hashmap *m;
437 char *val1;
438
439 val1 = strdup("my val");
440 assert_se(val1);
441
d5099efc 442 m = hashmap_new(&string_hash_ops);
9341a4a1
DB
443
444 assert_se(hashmap_isempty(m));
445 hashmap_put(m, "Key 1", val1);
446 assert_se(!hashmap_isempty(m));
447
448 assert_se(m);
449 hashmap_free_free(m);
450}
451
452static void test_hashmap_size(void) {
453 Hashmap *m;
454 char *val1, *val2, *val3, *val4;
455
456 val1 = strdup("my val");
457 assert_se(val1);
458 val2 = strdup("my val");
459 assert_se(val2);
460 val3 = strdup("my val");
461 assert_se(val3);
462 val4 = strdup("my val");
463 assert_se(val4);
464
d5099efc 465 m = hashmap_new(&string_hash_ops);
9341a4a1
DB
466
467 hashmap_put(m, "Key 1", val1);
468 hashmap_put(m, "Key 2", val2);
469 hashmap_put(m, "Key 3", val3);
470 hashmap_put(m, "Key 4", val4);
471
472 assert_se(m);
473 assert_se(hashmap_size(m) == 4);
474 hashmap_free_free(m);
475}
476
477static void test_hashmap_get(void) {
478 Hashmap *m;
479 char *r;
480 char *val;
481
482 val = strdup("my val");
483 assert_se(val);
484
d5099efc 485 m = hashmap_new(&string_hash_ops);
9341a4a1
DB
486
487 hashmap_put(m, "Key 1", val);
488
489 r = hashmap_get(m, "Key 1");
490 assert_se(streq(r, val));
491
492 assert_se(m);
493 hashmap_free_free(m);
494}
495
45fa9e29
LP
496static void test_hashmap_many(void) {
497 Hashmap *h;
498 unsigned i;
499
500#define N_ENTRIES 100000
501
d5099efc 502 assert_se(h = hashmap_new(NULL));
45fa9e29
LP
503
504 for (i = 1; i < N_ENTRIES*3; i+=3) {
505 assert_se(hashmap_put(h, UINT_TO_PTR(i), UINT_TO_PTR(i)) >= 0);
506 assert_se(PTR_TO_UINT(hashmap_get(h, UINT_TO_PTR(i))) == i);
507 }
508
509 for (i = 1; i < N_ENTRIES*3; i++)
510 assert_se(hashmap_contains(h, UINT_TO_PTR(i)) == (i % 3 == 1));
511
512 log_info("%u <= %u * 0.75 = %g", hashmap_size(h), hashmap_buckets(h), hashmap_buckets(h) * 0.75);
513
514 assert_se(hashmap_size(h) <= hashmap_buckets(h) * 0.75);
515 assert_se(hashmap_size(h) == N_ENTRIES);
516
517 hashmap_free(h);
518}
519
d06b3a9d
RC
520static void test_hashmap_first_key(void) {
521 _cleanup_hashmap_free_ Hashmap *m = NULL;
522
d5099efc 523 m = hashmap_new(&string_hash_ops);
d06b3a9d
RC
524 assert_se(m);
525
526 assert_se(!hashmap_first_key(m));
527 assert_se(hashmap_put(m, "key 1", NULL) == 1);
528 assert_se(streq(hashmap_first_key(m), "key 1"));
529 assert_se(hashmap_put(m, "key 2", NULL) == 1);
530 assert_se(streq(hashmap_first_key(m), "key 1"));
531 assert_se(hashmap_remove(m, "key 1") == NULL);
532 assert_se(streq(hashmap_first_key(m), "key 2"));
533}
534
535static void test_hashmap_last(void) {
536 _cleanup_hashmap_free_ Hashmap *m = NULL;
537
d5099efc 538 m = hashmap_new(&string_hash_ops);
d06b3a9d
RC
539 assert_se(m);
540
541 assert_se(!hashmap_last(m));
542 assert_se(hashmap_put(m, "key 1", (void *) (const char *) "val 1") == 1);
543 assert_se(streq(hashmap_last(m), "val 1"));
544 assert_se(hashmap_put(m, "key 2", (void *) (const char *) "bar") == 1);
545 assert_se(streq(hashmap_last(m), "bar"));
546 assert_se(hashmap_remove(m, "key 2"));
547 assert_se(streq(hashmap_last(m), "val 1"));
548}
549
550static void test_hashmap_steal_first_key(void) {
551 _cleanup_hashmap_free_ Hashmap *m = NULL;
552
d5099efc 553 m = hashmap_new(&string_hash_ops);
d06b3a9d
RC
554 assert_se(m);
555
556 assert_se(!hashmap_steal_first_key(m));
557 assert_se(hashmap_put(m, "key 1", NULL) == 1);
558 assert_se(streq(hashmap_steal_first_key(m), "key 1"));
559
560 assert_se(hashmap_isempty(m));
561}
562
563static void test_hashmap_clear_free_free(void) {
564 _cleanup_hashmap_free_ Hashmap *m = NULL;
565
d5099efc 566 m = hashmap_new(&string_hash_ops);
d06b3a9d
RC
567 assert_se(m);
568
569 assert_se(hashmap_put(m, strdup("key 1"), NULL) == 1);
570 assert_se(hashmap_put(m, strdup("key 2"), NULL) == 1);
571 assert_se(hashmap_put(m, strdup("key 3"), NULL) == 1);
572
573 hashmap_clear_free_free(m);
574 assert_se(hashmap_isempty(m));
575}
576
9341a4a1 577static void test_uint64_compare_func(void) {
8097ab4f
ZJS
578 const uint64_t a = 0x100, b = 0x101;
579
580 assert_se(uint64_compare_func(&a, &a) == 0);
581 assert_se(uint64_compare_func(&a, &b) == -1);
582 assert_se(uint64_compare_func(&b, &a) == 1);
9341a4a1
DB
583}
584
585static void test_trivial_compare_func(void) {
586 assert_se(trivial_compare_func(INT_TO_PTR('a'), INT_TO_PTR('a')) == 0);
587 assert_se(trivial_compare_func(INT_TO_PTR('a'), INT_TO_PTR('b')) == -1);
588 assert_se(trivial_compare_func(INT_TO_PTR('b'), INT_TO_PTR('a')) == 1);
589}
590
591static void test_string_compare_func(void) {
592 assert_se(!string_compare_func("fred", "wilma") == 0);
593 assert_se(string_compare_func("fred", "fred") == 0);
594}
595
45fa9e29 596int main(int argc, const char *argv[]) {
9341a4a1
DB
597 test_hashmap_copy();
598 test_hashmap_get_strv();
599 test_hashmap_move_one();
600 test_hashmap_next();
601 test_hashmap_replace();
602 test_hashmap_update();
603 test_hashmap_put();
d06b3a9d 604 test_hashmap_remove_and_put();
9341a4a1
DB
605 test_hashmap_ensure_allocated();
606 test_hashmap_foreach();
607 test_hashmap_foreach_backwards();
608 test_hashmap_foreach_key();
609 test_hashmap_contains();
610 test_hashmap_merge();
611 test_hashmap_isempty();
612 test_hashmap_get();
613 test_hashmap_size();
45fa9e29 614 test_hashmap_many();
d06b3a9d
RC
615 test_hashmap_first_key();
616 test_hashmap_last();
617 test_hashmap_steal_first_key();
618 test_hashmap_clear_free_free();
9341a4a1
DB
619 test_uint64_compare_func();
620 test_trivial_compare_func();
621 test_string_compare_func();
622}