]>
Commit | Line | Data |
---|---|---|
32a4456c MS |
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 "strv.h" | |
21 | #include "util.h" | |
22 | #include "hashmap.h" | |
23 | ||
24 | void test_hashmap_funcs(void); | |
25 | ||
26 | static void test_hashmap_replace(void) { | |
27 | Hashmap *m; | |
28 | char *val1, *val2, *val3, *val4, *val5, *r; | |
29 | ||
30 | m = hashmap_new(&string_hash_ops); | |
31 | ||
32 | val1 = strdup("val1"); | |
33 | assert_se(val1); | |
34 | val2 = strdup("val2"); | |
35 | assert_se(val2); | |
36 | val3 = strdup("val3"); | |
37 | assert_se(val3); | |
38 | val4 = strdup("val4"); | |
39 | assert_se(val4); | |
40 | val5 = strdup("val5"); | |
41 | assert_se(val5); | |
42 | ||
43 | hashmap_put(m, "key 1", val1); | |
44 | hashmap_put(m, "key 2", val2); | |
45 | hashmap_put(m, "key 3", val3); | |
46 | hashmap_put(m, "key 4", val4); | |
47 | ||
48 | hashmap_replace(m, "key 3", val1); | |
49 | r = hashmap_get(m, "key 3"); | |
50 | assert_se(streq(r, "val1")); | |
51 | ||
52 | hashmap_replace(m, "key 5", val5); | |
53 | r = hashmap_get(m, "key 5"); | |
54 | assert_se(streq(r, "val5")); | |
55 | ||
56 | free(val1); | |
57 | free(val2); | |
58 | free(val3); | |
59 | free(val4); | |
60 | free(val5); | |
61 | hashmap_free(m); | |
62 | } | |
63 | ||
64 | static void test_hashmap_copy(void) { | |
65 | Hashmap *m, *copy; | |
66 | char *val1, *val2, *val3, *val4, *r; | |
67 | ||
68 | val1 = strdup("val1"); | |
69 | assert_se(val1); | |
70 | val2 = strdup("val2"); | |
71 | assert_se(val2); | |
72 | val3 = strdup("val3"); | |
73 | assert_se(val3); | |
74 | val4 = strdup("val4"); | |
75 | assert_se(val4); | |
76 | ||
77 | m = hashmap_new(&string_hash_ops); | |
78 | ||
79 | hashmap_put(m, "key 1", val1); | |
80 | hashmap_put(m, "key 2", val2); | |
81 | hashmap_put(m, "key 3", val3); | |
82 | hashmap_put(m, "key 4", val4); | |
83 | ||
84 | copy = hashmap_copy(m); | |
85 | ||
86 | r = hashmap_get(copy, "key 1"); | |
87 | assert_se(streq(r, "val1")); | |
88 | r = hashmap_get(copy, "key 2"); | |
89 | assert_se(streq(r, "val2")); | |
90 | r = hashmap_get(copy, "key 3"); | |
91 | assert_se(streq(r, "val3")); | |
92 | r = hashmap_get(copy, "key 4"); | |
93 | assert_se(streq(r, "val4")); | |
94 | ||
95 | hashmap_free_free(copy); | |
96 | hashmap_free(m); | |
97 | } | |
98 | ||
99 | static void test_hashmap_get_strv(void) { | |
100 | Hashmap *m; | |
101 | char **strv; | |
102 | char *val1, *val2, *val3, *val4; | |
103 | ||
104 | val1 = strdup("val1"); | |
105 | assert_se(val1); | |
106 | val2 = strdup("val2"); | |
107 | assert_se(val2); | |
108 | val3 = strdup("val3"); | |
109 | assert_se(val3); | |
110 | val4 = strdup("val4"); | |
111 | assert_se(val4); | |
112 | ||
113 | m = hashmap_new(&string_hash_ops); | |
114 | ||
115 | hashmap_put(m, "key 1", val1); | |
116 | hashmap_put(m, "key 2", val2); | |
117 | hashmap_put(m, "key 3", val3); | |
118 | hashmap_put(m, "key 4", val4); | |
119 | ||
120 | strv = hashmap_get_strv(m); | |
121 | ||
122 | #ifndef ORDERED | |
123 | strv = strv_sort(strv); | |
124 | #endif | |
125 | ||
126 | assert_se(streq(strv[0], "val1")); | |
127 | assert_se(streq(strv[1], "val2")); | |
128 | assert_se(streq(strv[2], "val3")); | |
129 | assert_se(streq(strv[3], "val4")); | |
130 | ||
131 | strv_free(strv); | |
132 | ||
133 | hashmap_free(m); | |
134 | } | |
135 | ||
136 | static void test_hashmap_move_one(void) { | |
137 | Hashmap *m, *n; | |
138 | char *val1, *val2, *val3, *val4, *r; | |
139 | ||
140 | val1 = strdup("val1"); | |
141 | assert_se(val1); | |
142 | val2 = strdup("val2"); | |
143 | assert_se(val2); | |
144 | val3 = strdup("val3"); | |
145 | assert_se(val3); | |
146 | val4 = strdup("val4"); | |
147 | assert_se(val4); | |
148 | ||
149 | m = hashmap_new(&string_hash_ops); | |
150 | n = hashmap_new(&string_hash_ops); | |
151 | ||
152 | hashmap_put(m, "key 1", val1); | |
153 | hashmap_put(m, "key 2", val2); | |
154 | hashmap_put(m, "key 3", val3); | |
155 | hashmap_put(m, "key 4", val4); | |
156 | ||
9ba81d5a MS |
157 | assert_se(hashmap_move_one(n, NULL, "key 3") == -ENOENT); |
158 | assert_se(hashmap_move_one(n, m, "key 5") == -ENOENT); | |
159 | assert_se(hashmap_move_one(n, m, "key 3") == 0); | |
160 | assert_se(hashmap_move_one(n, m, "key 4") == 0); | |
32a4456c MS |
161 | |
162 | r = hashmap_get(n, "key 3"); | |
163 | assert_se(r && streq(r, "val3")); | |
164 | r = hashmap_get(n, "key 4"); | |
165 | assert_se(r && streq(r, "val4")); | |
166 | r = hashmap_get(m, "key 3"); | |
167 | assert_se(!r); | |
168 | ||
9ba81d5a MS |
169 | assert_se(hashmap_move_one(n, m, "key 3") == -EEXIST); |
170 | ||
171 | hashmap_free_free(m); | |
172 | hashmap_free_free(n); | |
173 | } | |
174 | ||
175 | static void test_hashmap_move(void) { | |
176 | Hashmap *m, *n; | |
177 | char *val1, *val2, *val3, *val4, *r; | |
178 | ||
179 | val1 = strdup("val1"); | |
180 | assert_se(val1); | |
181 | val2 = strdup("val2"); | |
182 | assert_se(val2); | |
183 | val3 = strdup("val3"); | |
184 | assert_se(val3); | |
185 | val4 = strdup("val4"); | |
186 | assert_se(val4); | |
187 | ||
188 | m = hashmap_new(&string_hash_ops); | |
189 | n = hashmap_new(&string_hash_ops); | |
190 | ||
191 | hashmap_put(n, "key 1", strdup(val1)); | |
192 | hashmap_put(m, "key 1", val1); | |
193 | hashmap_put(m, "key 2", val2); | |
194 | hashmap_put(m, "key 3", val3); | |
195 | hashmap_put(m, "key 4", val4); | |
196 | ||
e80afdb3 MS |
197 | assert_se(hashmap_move(n, NULL) == 0); |
198 | assert_se(hashmap_move(n, m) == 0); | |
9ba81d5a MS |
199 | |
200 | assert_se(hashmap_size(m) == 1); | |
201 | r = hashmap_get(m, "key 1"); | |
202 | assert_se(r && streq(r, "val1")); | |
203 | ||
204 | r = hashmap_get(n, "key 1"); | |
205 | assert_se(r && streq(r, "val1")); | |
206 | r = hashmap_get(n, "key 2"); | |
207 | assert_se(r && streq(r, "val2")); | |
208 | r = hashmap_get(n, "key 3"); | |
209 | assert_se(r && streq(r, "val3")); | |
210 | r = hashmap_get(n, "key 4"); | |
211 | assert_se(r && streq(r, "val4")); | |
32a4456c MS |
212 | |
213 | hashmap_free_free(m); | |
214 | hashmap_free_free(n); | |
215 | } | |
216 | ||
217 | static void test_hashmap_update(void) { | |
218 | Hashmap *m; | |
219 | char *val1, *val2, *r; | |
220 | ||
221 | m = hashmap_new(&string_hash_ops); | |
222 | val1 = strdup("old_value"); | |
223 | assert_se(val1); | |
224 | val2 = strdup("new_value"); | |
225 | assert_se(val2); | |
226 | ||
227 | hashmap_put(m, "key 1", val1); | |
228 | r = hashmap_get(m, "key 1"); | |
229 | assert_se(streq(r, "old_value")); | |
230 | ||
9ba81d5a MS |
231 | assert_se(hashmap_update(m, "key 2", val2) == -ENOENT); |
232 | r = hashmap_get(m, "key 1"); | |
233 | assert_se(streq(r, "old_value")); | |
234 | ||
235 | assert_se(hashmap_update(m, "key 1", val2) == 0); | |
32a4456c MS |
236 | r = hashmap_get(m, "key 1"); |
237 | assert_se(streq(r, "new_value")); | |
238 | ||
239 | free(val1); | |
240 | free(val2); | |
241 | hashmap_free(m); | |
242 | } | |
243 | ||
244 | static void test_hashmap_put(void) { | |
9ba81d5a | 245 | Hashmap *m = NULL; |
32a4456c | 246 | int valid_hashmap_put; |
9ba81d5a | 247 | void *val1 = (void*) "val 1"; |
435fc317 MP |
248 | void *val2 = (void*) "val 2"; |
249 | _cleanup_free_ char* key1 = NULL; | |
32a4456c | 250 | |
61c81750 | 251 | assert_se(hashmap_ensure_allocated(&m, &string_hash_ops) >= 0); |
9ba81d5a | 252 | assert_se(m); |
32a4456c | 253 | |
9ba81d5a | 254 | valid_hashmap_put = hashmap_put(m, "key 1", val1); |
32a4456c | 255 | assert_se(valid_hashmap_put == 1); |
9ba81d5a | 256 | assert_se(hashmap_put(m, "key 1", val1) == 0); |
435fc317 MP |
257 | assert_se(hashmap_put(m, "key 1", val2) == -EEXIST); |
258 | key1 = strdup("key 1"); | |
259 | assert_se(hashmap_put(m, key1, val1) == 0); | |
260 | assert_se(hashmap_put(m, key1, val2) == -EEXIST); | |
32a4456c | 261 | |
32a4456c MS |
262 | hashmap_free(m); |
263 | } | |
264 | ||
9ba81d5a MS |
265 | static void test_hashmap_remove(void) { |
266 | _cleanup_hashmap_free_ Hashmap *m = NULL; | |
267 | char *r; | |
268 | ||
269 | r = hashmap_remove(NULL, "key 1"); | |
270 | assert_se(r == NULL); | |
271 | ||
272 | m = hashmap_new(&string_hash_ops); | |
273 | assert_se(m); | |
274 | ||
275 | r = hashmap_remove(m, "no such key"); | |
276 | assert_se(r == NULL); | |
277 | ||
278 | hashmap_put(m, "key 1", (void*) "val 1"); | |
279 | hashmap_put(m, "key 2", (void*) "val 2"); | |
280 | ||
281 | r = hashmap_remove(m, "key 1"); | |
282 | assert_se(streq(r, "val 1")); | |
283 | ||
284 | r = hashmap_get(m, "key 2"); | |
285 | assert_se(streq(r, "val 2")); | |
286 | assert_se(!hashmap_get(m, "key 1")); | |
287 | } | |
288 | ||
289 | static void test_hashmap_remove2(void) { | |
290 | _cleanup_hashmap_free_free_free_ Hashmap *m = NULL; | |
291 | char key1[] = "key 1"; | |
292 | char key2[] = "key 2"; | |
293 | char val1[] = "val 1"; | |
294 | char val2[] = "val 2"; | |
295 | void *r, *r2; | |
296 | ||
297 | r = hashmap_remove2(NULL, "key 1", &r2); | |
298 | assert_se(r == NULL); | |
299 | ||
300 | m = hashmap_new(&string_hash_ops); | |
301 | assert_se(m); | |
302 | ||
303 | r = hashmap_remove2(m, "no such key", &r2); | |
304 | assert_se(r == NULL); | |
305 | ||
306 | hashmap_put(m, strdup(key1), strdup(val1)); | |
307 | hashmap_put(m, strdup(key2), strdup(val2)); | |
308 | ||
309 | r = hashmap_remove2(m, key1, &r2); | |
310 | assert_se(streq(r, val1)); | |
311 | assert_se(streq(r2, key1)); | |
312 | free(r); | |
313 | free(r2); | |
314 | ||
315 | r = hashmap_get(m, key2); | |
316 | assert_se(streq(r, val2)); | |
317 | assert_se(!hashmap_get(m, key1)); | |
318 | } | |
319 | ||
320 | static void test_hashmap_remove_value(void) { | |
321 | _cleanup_hashmap_free_ Hashmap *m = NULL; | |
322 | char *r; | |
323 | ||
324 | r = hashmap_remove_value(NULL, "key 1", (void*) "val 1"); | |
325 | assert_se(r == NULL); | |
326 | ||
327 | m = hashmap_new(&string_hash_ops); | |
328 | assert_se(m); | |
329 | ||
330 | r = hashmap_remove_value(m, "key 1", (void*) "val 1"); | |
331 | assert_se(r == NULL); | |
332 | ||
333 | hashmap_put(m, "key 1", (void*) "val 1"); | |
334 | hashmap_put(m, "key 2", (void*) "val 2"); | |
335 | ||
336 | r = hashmap_remove_value(m, "key 1", (void*) "val 1"); | |
337 | assert_se(streq(r, "val 1")); | |
338 | ||
339 | r = hashmap_get(m, "key 2"); | |
340 | assert_se(streq(r, "val 2")); | |
341 | assert_se(!hashmap_get(m, "key 1")); | |
342 | ||
343 | r = hashmap_remove_value(m, "key 2", (void*) "val 1"); | |
344 | assert_se(r == NULL); | |
345 | ||
346 | r = hashmap_get(m, "key 2"); | |
347 | assert_se(streq(r, "val 2")); | |
348 | assert_se(!hashmap_get(m, "key 1")); | |
349 | } | |
350 | ||
32a4456c MS |
351 | static void test_hashmap_remove_and_put(void) { |
352 | _cleanup_hashmap_free_ Hashmap *m = NULL; | |
353 | int valid; | |
354 | char *r; | |
355 | ||
356 | m = hashmap_new(&string_hash_ops); | |
357 | assert_se(m); | |
358 | ||
9ba81d5a MS |
359 | valid = hashmap_remove_and_put(m, "invalid key", "new key", NULL); |
360 | assert_se(valid == -ENOENT); | |
32a4456c MS |
361 | |
362 | valid = hashmap_put(m, "key 1", (void*) (const char *) "val 1"); | |
363 | assert_se(valid == 1); | |
9ba81d5a MS |
364 | |
365 | valid = hashmap_remove_and_put(NULL, "key 1", "key 2", (void*) (const char *) "val 2"); | |
366 | assert_se(valid == -ENOENT); | |
367 | ||
32a4456c MS |
368 | valid = hashmap_remove_and_put(m, "key 1", "key 2", (void*) (const char *) "val 2"); |
369 | assert_se(valid == 0); | |
370 | ||
371 | r = hashmap_get(m, "key 2"); | |
372 | assert_se(streq(r, "val 2")); | |
373 | assert_se(!hashmap_get(m, "key 1")); | |
374 | ||
375 | valid = hashmap_put(m, "key 3", (void*) (const char *) "val 3"); | |
376 | assert_se(valid == 1); | |
377 | valid = hashmap_remove_and_put(m, "key 3", "key 2", (void*) (const char *) "val 2"); | |
9ba81d5a MS |
378 | assert_se(valid == -EEXIST); |
379 | } | |
380 | ||
381 | static void test_hashmap_remove_and_replace(void) { | |
382 | _cleanup_hashmap_free_ Hashmap *m = NULL; | |
383 | int valid; | |
384 | void *key1 = UINT_TO_PTR(1); | |
385 | void *key2 = UINT_TO_PTR(2); | |
386 | void *key3 = UINT_TO_PTR(3); | |
387 | void *r; | |
e1323fbf | 388 | int i, j; |
9ba81d5a MS |
389 | |
390 | m = hashmap_new(&trivial_hash_ops); | |
391 | assert_se(m); | |
392 | ||
393 | valid = hashmap_remove_and_replace(m, key1, key2, NULL); | |
394 | assert_se(valid == -ENOENT); | |
395 | ||
396 | valid = hashmap_put(m, key1, key1); | |
397 | assert_se(valid == 1); | |
398 | ||
399 | valid = hashmap_remove_and_replace(NULL, key1, key2, key2); | |
400 | assert_se(valid == -ENOENT); | |
401 | ||
402 | valid = hashmap_remove_and_replace(m, key1, key2, key2); | |
403 | assert_se(valid == 0); | |
404 | ||
405 | r = hashmap_get(m, key2); | |
406 | assert_se(r == key2); | |
407 | assert_se(!hashmap_get(m, key1)); | |
408 | ||
409 | valid = hashmap_put(m, key3, key3); | |
410 | assert_se(valid == 1); | |
411 | valid = hashmap_remove_and_replace(m, key3, key2, key2); | |
412 | assert_se(valid == 0); | |
413 | r = hashmap_get(m, key2); | |
414 | assert_se(r == key2); | |
415 | assert_se(!hashmap_get(m, key3)); | |
e1323fbf MS |
416 | |
417 | /* Repeat this test several times to increase the chance of hitting | |
418 | * the less likely case in hashmap_remove_and_replace where it | |
419 | * compensates for the backward shift. */ | |
420 | for (i = 0; i < 20; i++) { | |
421 | hashmap_clear(m); | |
422 | ||
423 | for (j = 1; j < 7; j++) | |
424 | hashmap_put(m, UINT_TO_PTR(10*i + j), UINT_TO_PTR(10*i + j)); | |
425 | valid = hashmap_remove_and_replace(m, UINT_TO_PTR(10*i + 1), | |
426 | UINT_TO_PTR(10*i + 2), | |
427 | UINT_TO_PTR(10*i + 2)); | |
428 | assert_se(valid == 0); | |
429 | assert_se(!hashmap_get(m, UINT_TO_PTR(10*i + 1))); | |
430 | for (j = 2; j < 7; j++) { | |
431 | r = hashmap_get(m, UINT_TO_PTR(10*i + j)); | |
432 | assert_se(r == UINT_TO_PTR(10*i + j)); | |
433 | } | |
434 | } | |
32a4456c MS |
435 | } |
436 | ||
437 | static void test_hashmap_ensure_allocated(void) { | |
438 | Hashmap *m; | |
439 | int valid_hashmap; | |
440 | ||
441 | m = hashmap_new(&string_hash_ops); | |
442 | ||
443 | valid_hashmap = hashmap_ensure_allocated(&m, &string_hash_ops); | |
444 | assert_se(valid_hashmap == 0); | |
445 | ||
446 | assert_se(m); | |
447 | hashmap_free(m); | |
448 | } | |
449 | ||
450 | static void test_hashmap_foreach_key(void) { | |
451 | Hashmap *m; | |
452 | Iterator i; | |
453 | bool key_found[] = { false, false, false, false }; | |
454 | const char *s; | |
455 | const char *key; | |
456 | static const char key_table[] = | |
457 | "key 1\0" | |
458 | "key 2\0" | |
459 | "key 3\0" | |
460 | "key 4\0"; | |
461 | ||
462 | m = hashmap_new(&string_hash_ops); | |
463 | ||
464 | NULSTR_FOREACH(key, key_table) | |
465 | hashmap_put(m, key, (void*) (const char*) "my dummy val"); | |
466 | ||
467 | HASHMAP_FOREACH_KEY(s, key, m, i) { | |
8927b1da | 468 | assert(s); |
32a4456c MS |
469 | if (!key_found[0] && streq(key, "key 1")) |
470 | key_found[0] = true; | |
471 | else if (!key_found[1] && streq(key, "key 2")) | |
472 | key_found[1] = true; | |
473 | else if (!key_found[2] && streq(key, "key 3")) | |
474 | key_found[2] = true; | |
475 | else if (!key_found[3] && streq(key, "fail")) | |
476 | key_found[3] = true; | |
477 | } | |
478 | ||
479 | assert_se(m); | |
480 | assert_se(key_found[0] && key_found[1] && key_found[2] && !key_found[3]); | |
481 | ||
482 | hashmap_free(m); | |
483 | } | |
484 | ||
485 | static void test_hashmap_foreach(void) { | |
486 | Hashmap *m; | |
487 | Iterator i; | |
488 | bool value_found[] = { false, false, false, false }; | |
489 | char *val1, *val2, *val3, *val4, *s; | |
9ba81d5a | 490 | unsigned count; |
32a4456c MS |
491 | |
492 | val1 = strdup("my val1"); | |
493 | assert_se(val1); | |
494 | val2 = strdup("my val2"); | |
495 | assert_se(val2); | |
496 | val3 = strdup("my val3"); | |
497 | assert_se(val3); | |
498 | val4 = strdup("my val4"); | |
499 | assert_se(val4); | |
500 | ||
9ba81d5a MS |
501 | m = NULL; |
502 | ||
503 | count = 0; | |
504 | HASHMAP_FOREACH(s, m, i) | |
505 | count++; | |
506 | assert_se(count == 0); | |
507 | ||
32a4456c MS |
508 | m = hashmap_new(&string_hash_ops); |
509 | ||
9ba81d5a MS |
510 | count = 0; |
511 | HASHMAP_FOREACH(s, m, i) | |
512 | count++; | |
513 | assert_se(count == 0); | |
514 | ||
32a4456c MS |
515 | hashmap_put(m, "Key 1", val1); |
516 | hashmap_put(m, "Key 2", val2); | |
517 | hashmap_put(m, "Key 3", val3); | |
518 | hashmap_put(m, "Key 4", val4); | |
519 | ||
520 | HASHMAP_FOREACH(s, m, i) { | |
521 | if (!value_found[0] && streq(s, val1)) | |
522 | value_found[0] = true; | |
523 | else if (!value_found[1] && streq(s, val2)) | |
524 | value_found[1] = true; | |
525 | else if (!value_found[2] && streq(s, val3)) | |
526 | value_found[2] = true; | |
527 | else if (!value_found[3] && streq(s, val4)) | |
528 | value_found[3] = true; | |
529 | } | |
530 | ||
531 | assert_se(m); | |
532 | assert_se(value_found[0] && value_found[1] && value_found[2] && value_found[3]); | |
533 | ||
534 | hashmap_free_free(m); | |
535 | } | |
536 | ||
537 | static void test_hashmap_merge(void) { | |
538 | Hashmap *m; | |
539 | Hashmap *n; | |
540 | char *val1, *val2, *val3, *val4, *r; | |
541 | ||
542 | val1 = strdup("my val1"); | |
543 | assert_se(val1); | |
544 | val2 = strdup("my val2"); | |
545 | assert_se(val2); | |
546 | val3 = strdup("my val3"); | |
547 | assert_se(val3); | |
548 | val4 = strdup("my val4"); | |
549 | assert_se(val4); | |
550 | ||
551 | n = hashmap_new(&string_hash_ops); | |
552 | m = hashmap_new(&string_hash_ops); | |
553 | ||
554 | hashmap_put(m, "Key 1", val1); | |
555 | hashmap_put(m, "Key 2", val2); | |
556 | hashmap_put(n, "Key 3", val3); | |
557 | hashmap_put(n, "Key 4", val4); | |
558 | ||
559 | assert_se(hashmap_merge(m, n) == 0); | |
560 | r = hashmap_get(m, "Key 3"); | |
561 | assert_se(r && streq(r, "my val3")); | |
562 | r = hashmap_get(m, "Key 4"); | |
563 | assert_se(r && streq(r, "my val4")); | |
564 | ||
565 | assert_se(n); | |
566 | assert_se(m); | |
567 | hashmap_free(n); | |
568 | hashmap_free_free(m); | |
569 | } | |
570 | ||
571 | static void test_hashmap_contains(void) { | |
572 | Hashmap *m; | |
573 | char *val1; | |
574 | ||
575 | val1 = strdup("my val"); | |
576 | assert_se(val1); | |
577 | ||
578 | m = hashmap_new(&string_hash_ops); | |
579 | ||
580 | assert_se(!hashmap_contains(m, "Key 1")); | |
581 | hashmap_put(m, "Key 1", val1); | |
582 | assert_se(hashmap_contains(m, "Key 1")); | |
9ba81d5a MS |
583 | assert_se(!hashmap_contains(m, "Key 2")); |
584 | ||
585 | assert_se(!hashmap_contains(NULL, "Key 1")); | |
32a4456c MS |
586 | |
587 | assert_se(m); | |
588 | hashmap_free_free(m); | |
589 | } | |
590 | ||
591 | static void test_hashmap_isempty(void) { | |
592 | Hashmap *m; | |
593 | char *val1; | |
594 | ||
595 | val1 = strdup("my val"); | |
596 | assert_se(val1); | |
597 | ||
598 | m = hashmap_new(&string_hash_ops); | |
599 | ||
600 | assert_se(hashmap_isempty(m)); | |
601 | hashmap_put(m, "Key 1", val1); | |
602 | assert_se(!hashmap_isempty(m)); | |
603 | ||
604 | assert_se(m); | |
605 | hashmap_free_free(m); | |
606 | } | |
607 | ||
608 | static void test_hashmap_size(void) { | |
609 | Hashmap *m; | |
610 | char *val1, *val2, *val3, *val4; | |
611 | ||
612 | val1 = strdup("my val"); | |
613 | assert_se(val1); | |
614 | val2 = strdup("my val"); | |
615 | assert_se(val2); | |
616 | val3 = strdup("my val"); | |
617 | assert_se(val3); | |
618 | val4 = strdup("my val"); | |
619 | assert_se(val4); | |
620 | ||
9ba81d5a MS |
621 | assert_se(hashmap_size(NULL) == 0); |
622 | assert_se(hashmap_buckets(NULL) == 0); | |
623 | ||
32a4456c MS |
624 | m = hashmap_new(&string_hash_ops); |
625 | ||
626 | hashmap_put(m, "Key 1", val1); | |
627 | hashmap_put(m, "Key 2", val2); | |
628 | hashmap_put(m, "Key 3", val3); | |
629 | hashmap_put(m, "Key 4", val4); | |
630 | ||
631 | assert_se(m); | |
632 | assert_se(hashmap_size(m) == 4); | |
9ba81d5a | 633 | assert_se(hashmap_buckets(m) >= 4); |
32a4456c MS |
634 | hashmap_free_free(m); |
635 | } | |
636 | ||
637 | static void test_hashmap_get(void) { | |
638 | Hashmap *m; | |
639 | char *r; | |
640 | char *val; | |
641 | ||
642 | val = strdup("my val"); | |
643 | assert_se(val); | |
644 | ||
9ba81d5a MS |
645 | r = hashmap_get(NULL, "Key 1"); |
646 | assert_se(r == NULL); | |
647 | ||
32a4456c MS |
648 | m = hashmap_new(&string_hash_ops); |
649 | ||
650 | hashmap_put(m, "Key 1", val); | |
651 | ||
652 | r = hashmap_get(m, "Key 1"); | |
653 | assert_se(streq(r, val)); | |
654 | ||
9ba81d5a MS |
655 | r = hashmap_get(m, "no such key"); |
656 | assert_se(r == NULL); | |
657 | ||
32a4456c MS |
658 | assert_se(m); |
659 | hashmap_free_free(m); | |
660 | } | |
661 | ||
9ba81d5a MS |
662 | static void test_hashmap_get2(void) { |
663 | Hashmap *m; | |
664 | char *r; | |
665 | char *val; | |
666 | char key_orig[] = "Key 1"; | |
667 | void *key_copy; | |
668 | ||
669 | val = strdup("my val"); | |
670 | assert_se(val); | |
671 | ||
672 | key_copy = strdup(key_orig); | |
673 | assert_se(key_copy); | |
674 | ||
675 | r = hashmap_get2(NULL, key_orig, &key_copy); | |
676 | assert_se(r == NULL); | |
677 | ||
678 | m = hashmap_new(&string_hash_ops); | |
679 | ||
680 | hashmap_put(m, key_copy, val); | |
681 | key_copy = NULL; | |
682 | ||
683 | r = hashmap_get2(m, key_orig, &key_copy); | |
684 | assert_se(streq(r, val)); | |
685 | assert_se(key_orig != key_copy); | |
b669934f | 686 | assert_se(streq(key_orig, key_copy)); |
9ba81d5a MS |
687 | |
688 | r = hashmap_get2(m, "no such key", NULL); | |
689 | assert_se(r == NULL); | |
690 | ||
691 | assert_se(m); | |
692 | hashmap_free_free_free(m); | |
693 | } | |
694 | ||
b826ab58 TG |
695 | static void crippled_hashmap_func(const void *p, struct siphash *state) { |
696 | return trivial_hash_func(INT_TO_PTR(PTR_TO_INT(p) & 0xff), state); | |
9ba81d5a MS |
697 | } |
698 | ||
699 | static const struct hash_ops crippled_hashmap_ops = { | |
700 | .hash = crippled_hashmap_func, | |
701 | .compare = trivial_compare_func, | |
702 | }; | |
703 | ||
32a4456c MS |
704 | static void test_hashmap_many(void) { |
705 | Hashmap *h; | |
9ba81d5a MS |
706 | unsigned i, j; |
707 | void *v, *k; | |
708 | static const struct { | |
709 | const struct hash_ops *ops; | |
710 | unsigned n_entries; | |
711 | } tests[] = { | |
712 | { .ops = NULL, .n_entries = 1 << 20 }, | |
713 | { .ops = &crippled_hashmap_ops, .n_entries = 1 << 11 }, | |
714 | }; | |
32a4456c | 715 | |
32a4456c | 716 | |
9ba81d5a MS |
717 | for (j = 0; j < ELEMENTSOF(tests); j++) { |
718 | assert_se(h = hashmap_new(tests[j].ops)); | |
32a4456c | 719 | |
9ba81d5a MS |
720 | for (i = 1; i < tests[j].n_entries*3; i+=3) { |
721 | assert_se(hashmap_put(h, UINT_TO_PTR(i), UINT_TO_PTR(i)) >= 0); | |
722 | assert_se(PTR_TO_UINT(hashmap_get(h, UINT_TO_PTR(i))) == i); | |
723 | } | |
724 | ||
725 | for (i = 1; i < tests[j].n_entries*3; i++) | |
726 | assert_se(hashmap_contains(h, UINT_TO_PTR(i)) == (i % 3 == 1)); | |
32a4456c | 727 | |
ce79279b | 728 | log_info("%u <= %u * 0.8 = %g", hashmap_size(h), hashmap_buckets(h), hashmap_buckets(h) * 0.8); |
32a4456c | 729 | |
ce79279b | 730 | assert_se(hashmap_size(h) <= hashmap_buckets(h) * 0.8); |
9ba81d5a | 731 | assert_se(hashmap_size(h) == tests[j].n_entries); |
32a4456c | 732 | |
9ba81d5a MS |
733 | while (!hashmap_isempty(h)) { |
734 | k = hashmap_first_key(h); | |
735 | v = hashmap_remove(h, k); | |
736 | assert_se(v == k); | |
737 | } | |
32a4456c | 738 | |
9ba81d5a MS |
739 | hashmap_free(h); |
740 | } | |
741 | } | |
742 | ||
743 | static void test_hashmap_first(void) { | |
744 | _cleanup_hashmap_free_ Hashmap *m = NULL; | |
745 | ||
746 | m = hashmap_new(&string_hash_ops); | |
747 | assert_se(m); | |
748 | ||
749 | assert_se(!hashmap_first(m)); | |
750 | assert_se(hashmap_put(m, "key 1", (void*) "val 1") == 1); | |
751 | assert_se(streq(hashmap_first(m), "val 1")); | |
752 | assert_se(hashmap_put(m, "key 2", (void*) "val 2") == 1); | |
753 | #ifdef ORDERED | |
754 | assert_se(streq(hashmap_first(m), "val 1")); | |
755 | assert_se(hashmap_remove(m, "key 1")); | |
756 | assert_se(streq(hashmap_first(m), "val 2")); | |
757 | #endif | |
32a4456c MS |
758 | } |
759 | ||
760 | static void test_hashmap_first_key(void) { | |
761 | _cleanup_hashmap_free_ Hashmap *m = NULL; | |
762 | ||
763 | m = hashmap_new(&string_hash_ops); | |
764 | assert_se(m); | |
765 | ||
766 | assert_se(!hashmap_first_key(m)); | |
767 | assert_se(hashmap_put(m, "key 1", NULL) == 1); | |
768 | assert_se(streq(hashmap_first_key(m), "key 1")); | |
769 | assert_se(hashmap_put(m, "key 2", NULL) == 1); | |
770 | #ifdef ORDERED | |
771 | assert_se(streq(hashmap_first_key(m), "key 1")); | |
772 | assert_se(hashmap_remove(m, "key 1") == NULL); | |
773 | assert_se(streq(hashmap_first_key(m), "key 2")); | |
774 | #endif | |
775 | } | |
776 | ||
777 | static void test_hashmap_steal_first_key(void) { | |
778 | _cleanup_hashmap_free_ Hashmap *m = NULL; | |
779 | ||
780 | m = hashmap_new(&string_hash_ops); | |
781 | assert_se(m); | |
782 | ||
783 | assert_se(!hashmap_steal_first_key(m)); | |
784 | assert_se(hashmap_put(m, "key 1", NULL) == 1); | |
785 | assert_se(streq(hashmap_steal_first_key(m), "key 1")); | |
786 | ||
787 | assert_se(hashmap_isempty(m)); | |
788 | } | |
789 | ||
790 | static void test_hashmap_steal_first(void) { | |
791 | _cleanup_hashmap_free_ Hashmap *m = NULL; | |
792 | int seen[3] = {}; | |
793 | char *val; | |
794 | ||
795 | m = hashmap_new(&string_hash_ops); | |
796 | assert_se(m); | |
797 | ||
798 | assert_se(hashmap_put(m, "key 1", (void*) "1") == 1); | |
799 | assert_se(hashmap_put(m, "key 2", (void*) "22") == 1); | |
800 | assert_se(hashmap_put(m, "key 3", (void*) "333") == 1); | |
801 | ||
802 | while ((val = hashmap_steal_first(m))) | |
803 | seen[strlen(val) - 1]++; | |
804 | ||
805 | assert_se(seen[0] == 1 && seen[1] == 1 && seen[2] == 1); | |
806 | ||
807 | assert_se(hashmap_isempty(m)); | |
808 | } | |
809 | ||
810 | static void test_hashmap_clear_free_free(void) { | |
811 | _cleanup_hashmap_free_ Hashmap *m = NULL; | |
812 | ||
813 | m = hashmap_new(&string_hash_ops); | |
814 | assert_se(m); | |
815 | ||
816 | assert_se(hashmap_put(m, strdup("key 1"), NULL) == 1); | |
817 | assert_se(hashmap_put(m, strdup("key 2"), NULL) == 1); | |
818 | assert_se(hashmap_put(m, strdup("key 3"), NULL) == 1); | |
819 | ||
820 | hashmap_clear_free_free(m); | |
821 | assert_se(hashmap_isempty(m)); | |
822 | } | |
823 | ||
8f88aed7 MS |
824 | static void test_hashmap_reserve(void) { |
825 | _cleanup_hashmap_free_ Hashmap *m = NULL; | |
826 | ||
827 | m = hashmap_new(&string_hash_ops); | |
828 | ||
829 | assert_se(hashmap_reserve(m, 1) == 0); | |
830 | assert_se(hashmap_buckets(m) < 1000); | |
831 | assert_se(hashmap_reserve(m, 1000) == 0); | |
832 | assert_se(hashmap_buckets(m) >= 1000); | |
833 | assert_se(hashmap_isempty(m)); | |
834 | ||
835 | assert_se(hashmap_put(m, "key 1", (void*) "val 1") == 1); | |
836 | ||
837 | assert_se(hashmap_reserve(m, UINT_MAX) == -ENOMEM); | |
838 | assert_se(hashmap_reserve(m, UINT_MAX - 1) == -ENOMEM); | |
839 | } | |
840 | ||
32a4456c MS |
841 | void test_hashmap_funcs(void) { |
842 | test_hashmap_copy(); | |
843 | test_hashmap_get_strv(); | |
844 | test_hashmap_move_one(); | |
9ba81d5a | 845 | test_hashmap_move(); |
32a4456c MS |
846 | test_hashmap_replace(); |
847 | test_hashmap_update(); | |
848 | test_hashmap_put(); | |
9ba81d5a MS |
849 | test_hashmap_remove(); |
850 | test_hashmap_remove2(); | |
851 | test_hashmap_remove_value(); | |
32a4456c | 852 | test_hashmap_remove_and_put(); |
9ba81d5a | 853 | test_hashmap_remove_and_replace(); |
32a4456c MS |
854 | test_hashmap_ensure_allocated(); |
855 | test_hashmap_foreach(); | |
856 | test_hashmap_foreach_key(); | |
857 | test_hashmap_contains(); | |
858 | test_hashmap_merge(); | |
859 | test_hashmap_isempty(); | |
860 | test_hashmap_get(); | |
9ba81d5a | 861 | test_hashmap_get2(); |
32a4456c MS |
862 | test_hashmap_size(); |
863 | test_hashmap_many(); | |
9ba81d5a | 864 | test_hashmap_first(); |
32a4456c MS |
865 | test_hashmap_first_key(); |
866 | test_hashmap_steal_first_key(); | |
867 | test_hashmap_steal_first(); | |
868 | test_hashmap_clear_free_free(); | |
8f88aed7 | 869 | test_hashmap_reserve(); |
32a4456c | 870 | } |