Debian Codegraph @

tests/isc/hashmap_test.c

raw
1 /*
2 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3 *
4 * SPDX-License-Identifier: MPL-2.0
5 *
6 * This Source Code Form is subject to the terms of the Mozilla Public
7 * License, v. 2.0. If a copy of the MPL was not distributed with this
8 * file, you can obtain one at https://mozilla.org/MPL/2.0/.
9 *
10 * See the COPYRIGHT file distributed with this work for additional
11 * information regarding copyright ownership.
12 */
13
14 #include <inttypes.h>
15 #include <sched.h> /* IWYU pragma: keep */
16 #include <setjmp.h>
17 #include <stdarg.h>
18 #include <stddef.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22
23 #define UNIT_TESTING
24 #include <cmocka.h>
25
26 #include <isc/hash.h>
27 #include <isc/hashmap.h>
28 #include <isc/mem.h>
29 #include <isc/string.h>
30 #include <isc/util.h>
31
32 #include <tests/isc.h>
33
34 /* INCLUDE LAST */
35
36 #define mctx __mctx
37 #include "hashmap.c"
38 #undef mctx
39
40 typedef struct test_node {
41 uint32_t hashval;
42 char key[64];
43 } test_node_t;
44
45 static bool
46 nodes_match(void *node0, const void *key) {
47 struct test_node *node = node0;
48
49 return memcmp(node->key, key, 16) == 0;
50 }
51
52 static bool
53 long_nodes_match(void *node0, const void *key) {
54 struct test_node *node = node0;
55 size_t len = strlen(key);
56
57 return memcmp(node->key, key, len) == 0;
58 }
59
60 static bool
61 upper_nodes_match(void *node0, const void *key) {
62 struct test_node *node = node0;
63
64 return isc_ascii_lowerequal((uint8_t *)node->key, key, 16);
65 }
66
67 static void
68 test_hashmap_full(uint8_t init_bits, uintptr_t count) {
69 isc_hashmap_t *hashmap = NULL;
70 isc_result_t result;
71 test_node_t *nodes, *long_nodes, *upper_nodes;
72
73 nodes = isc_mem_cget(mctx, count, sizeof(nodes[0]));
74 long_nodes = isc_mem_cget(mctx, count, sizeof(nodes[0]));
75 upper_nodes = isc_mem_cget(mctx, count, sizeof(nodes[0]));
76
77 isc_hashmap_create(mctx, init_bits, &hashmap);
78 assert_non_null(hashmap);
79
80 /*
81 * Note: snprintf() is followed with strlcat()
82 * to ensure we are always filling the 16 byte key.
83 */
84 for (size_t i = 0; i < count; i++) {
85 /* short keys */
86 snprintf((char *)nodes[i].key, 16, "%u", (unsigned int)i);
87 strlcat((char *)nodes[i].key, " key of a raw hashmap!!", 16);
88 nodes[i].hashval = isc_hash32(nodes[i].key, 16, true);
89
90 /* long keys */
91 snprintf((char *)long_nodes[i].key, sizeof(long_nodes[i].key),
92 "%u", (unsigned int)i);
93 strlcat((char *)long_nodes[i].key, " key of a raw hashmap!!",
94 sizeof(long_nodes[i].key));
95 long_nodes[i].hashval = isc_hash32(
96 long_nodes[i].key,
97 strlen((const char *)long_nodes[i].key), true);
98
99 /* (some) uppercase keys */
100 snprintf((char *)upper_nodes[i].key, 16, "%u", (unsigned int)i);
101 strlcat((char *)upper_nodes[i].key, " KEY of a raw hashmap!!",
102 16);
103 upper_nodes[i].hashval = isc_hash32(upper_nodes[i].key, 16,
104 false);
105 }
106
107 /* insert short nodes */
108 for (size_t i = 0; i < count; i++) {
109 void *f = NULL;
110 result = isc_hashmap_add(hashmap, nodes[i].hashval, nodes_match,
111 nodes[i].key, &nodes[i], &f);
112 assert_int_equal(result, ISC_R_SUCCESS);
113 assert_ptr_equal(f, NULL);
114 }
115
116 /* check if the short nodes were insert */
117 for (size_t i = 0; i < count; i++) {
118 void *f = NULL;
119 result = isc_hashmap_find(hashmap, nodes[i].hashval,
120 nodes_match, nodes[i].key, &f);
121 assert_int_equal(result, ISC_R_SUCCESS);
122 assert_ptr_equal(&nodes[i], f);
123 }
124
125 /* check for double inserts */
126 for (size_t i = 0; i < count; i++) {
127 void *f = NULL;
128 result = isc_hashmap_add(hashmap, nodes[i].hashval, nodes_match,
129 nodes[i].key, &nodes[i], &f);
130 assert_int_equal(result, ISC_R_EXISTS);
131 assert_ptr_equal(f, &nodes[i]);
132 }
133
134 for (size_t i = 0; i < count; i++) {
135 void *f = NULL;
136 result = isc_hashmap_add(hashmap, long_nodes[i].hashval,
137 long_nodes_match, long_nodes[i].key,
138 &long_nodes[i], &f);
139 assert_int_equal(result, ISC_R_SUCCESS);
140 assert_ptr_equal(f, NULL);
141 }
142
143 for (size_t i = 0; i < count; i++) {
144 void *f = NULL;
145 result = isc_hashmap_find(hashmap, upper_nodes[i].hashval,
146 long_nodes_match, upper_nodes[i].key,
147 &f);
148 assert_int_equal(result, ISC_R_NOTFOUND);
149 assert_null(f);
150 }
151
152 for (size_t i = 0; i < count; i++) {
153 void *f = NULL;
154 result = isc_hashmap_find(hashmap, long_nodes[i].hashval,
155 long_nodes_match, long_nodes[i].key,
156 &f);
157 assert_int_equal(result, ISC_R_SUCCESS);
158 assert_ptr_equal(f, &long_nodes[i]);
159 }
160
161 for (size_t i = 0; i < count; i++) {
162 void *f = NULL;
163 result = isc_hashmap_delete(hashmap, nodes[i].hashval,
164 nodes_match, nodes[i].key);
165 assert_int_equal(result, ISC_R_SUCCESS);
166 result = isc_hashmap_find(hashmap, nodes[i].hashval,
167 nodes_match, nodes[i].key, &f);
168 assert_int_equal(result, ISC_R_NOTFOUND);
169 assert_null(f);
170 }
171
172 for (size_t i = 0; i < count; i++) {
173 void *f = NULL;
174 result = isc_hashmap_add(hashmap, upper_nodes[i].hashval,
175 upper_nodes_match, upper_nodes[i].key,
176 &upper_nodes[i], &f);
177 assert_int_equal(result, ISC_R_SUCCESS);
178 assert_ptr_equal(f, NULL);
179 }
180
181 for (size_t i = 0; i < count; i++) {
182 void *f = NULL;
183 result = isc_hashmap_delete(hashmap, long_nodes[i].hashval,
184 long_nodes_match,
185 long_nodes[i].key);
186 assert_int_equal(result, ISC_R_SUCCESS);
187 result = isc_hashmap_find(hashmap, long_nodes[i].hashval,
188 long_nodes_match, long_nodes[i].key,
189 &f);
190 assert_int_equal(result, ISC_R_NOTFOUND);
191 assert_null(f);
192 }
193
194 for (size_t i = 0; i < count; i++) {
195 void *f = NULL;
196 result = isc_hashmap_find(hashmap, upper_nodes[i].hashval,
197 upper_nodes_match, upper_nodes[i].key,
198 &f);
199 assert_int_equal(result, ISC_R_SUCCESS);
200 assert_ptr_equal(f, &upper_nodes[i]);
201 }
202
203 for (size_t i = 0; i < count; i++) {
204 void *f = NULL;
205 result = isc_hashmap_find(hashmap, nodes[i].hashval,
206 nodes_match, nodes[i].key, &f);
207 assert_int_equal(result, ISC_R_NOTFOUND);
208 assert_null(f);
209 }
210
211 isc_hashmap_destroy(&hashmap);
212 assert_null(hashmap);
213
214 isc_mem_cput(mctx, nodes, count, sizeof(nodes[0]));
215 isc_mem_cput(mctx, long_nodes, count, sizeof(nodes[0]));
216 isc_mem_cput(mctx, upper_nodes, count, sizeof(nodes[0]));
217 }
218
219 #include "hashmap_nodes.h"
220
221 static void
222 test_hashmap_iterator(bool random_data) {
223 isc_hashmap_t *hashmap = NULL;
224 isc_result_t result;
225 isc_hashmap_iter_t *iter = NULL;
226 size_t count = 7600;
227 test_node_t *nodes;
228 bool *seen;
229
230 nodes = isc_mem_cget(mctx, count, sizeof(nodes[0]));
231 seen = isc_mem_cget(mctx, count, sizeof(seen[0]));
232
233 isc_hashmap_create(mctx, HASHMAP_MIN_BITS, &hashmap);
234 assert_non_null(hashmap);
235
236 for (size_t i = 0; i < count; i++) {
237 /* short keys */
238 snprintf((char *)nodes[i].key, 16, "%u", (unsigned int)i);
239 strlcat((char *)nodes[i].key, " key of a raw hashmap!!", 16);
240 if (random_data) {
241 nodes[i].hashval = isc_hash32(nodes[i].key, 16, true);
242 } else {
243 nodes[i].hashval = test_hashvals[i];
244 }
245 }
246
247 for (size_t i = 0; i < count; i++) {
248 void *f = NULL;
249 result = isc_hashmap_add(hashmap, nodes[i].hashval, nodes_match,
250 nodes[i].key, &nodes[i], &f);
251 assert_int_equal(result, ISC_R_SUCCESS);
252 assert_ptr_equal(f, NULL);
253 }
254
255 /* We want to iterate while rehashing is in progress */
256 assert_true(rehashing_in_progress(hashmap));
257
258 memset(seen, 0, count * sizeof(seen[0]));
259 isc_hashmap_iter_create(hashmap, &iter);
260
261 for (result = isc_hashmap_iter_first(iter); result == ISC_R_SUCCESS;
262 result = isc_hashmap_iter_next(iter))
263 {
264 char key[16] = { 0 };
265 ptrdiff_t i;
266 const uint8_t *tkey = NULL;
267 test_node_t *v = NULL;
268
269 isc_hashmap_iter_current(iter, (void *)&v);
270 isc_hashmap_iter_currentkey(iter, &tkey);
271
272 i = v - &nodes[0];
273
274 snprintf(key, 16, "%u", (unsigned int)i);
275 strlcat(key, " key of a raw hashmap!!", 16);
276
277 assert_memory_equal(key, tkey, 16);
278
279 assert_false(seen[i]);
280 seen[i] = true;
281 }
282 assert_int_equal(result, ISC_R_NOMORE);
283 for (size_t i = 0; i < count; i++) {
284 assert_true(seen[i]);
285 }
286
287 /* erase odd */
288 memset(seen, 0, count * sizeof(seen[0]));
289 result = isc_hashmap_iter_first(iter);
290 while (result == ISC_R_SUCCESS) {
291 char key[16] = { 0 };
292 ptrdiff_t i;
293 const uint8_t *tkey = NULL;
294 test_node_t *v = NULL;
295
296 isc_hashmap_iter_current(iter, (void *)&v);
297 isc_hashmap_iter_currentkey(iter, &tkey);
298
299 i = v - nodes;
300 snprintf(key, 16, "%u", (unsigned int)i);
301 strlcat(key, " key of a raw hashmap!!", 16);
302 assert_memory_equal(key, tkey, 16);
303
304 if (i % 2 == 0) {
305 result = isc_hashmap_iter_delcurrent_next(iter);
306 } else {
307 result = isc_hashmap_iter_next(iter);
308 }
309
310 assert_false(seen[i]);
311 seen[i] = true;
312 }
313 assert_int_equal(result, ISC_R_NOMORE);
314 for (size_t i = 0; i < count; i++) {
315 assert_true(seen[i]);
316 }
317
318 /* erase even */
319 memset(seen, 0, count * sizeof(seen[0]));
320 result = isc_hashmap_iter_first(iter);
321 while (result == ISC_R_SUCCESS) {
322 char key[16] = { 0 };
323 ptrdiff_t i;
324 const uint8_t *tkey = NULL;
325 test_node_t *v = NULL;
326
327 isc_hashmap_iter_current(iter, (void *)&v);
328 isc_hashmap_iter_currentkey(iter, &tkey);
329
330 i = v - nodes;
331 snprintf(key, 16, "%u", (unsigned int)i);
332 strlcat(key, " key of a raw hashmap!!", 16);
333 assert_memory_equal(key, tkey, 16);
334
335 if (i % 2 == 1) {
336 result = isc_hashmap_iter_delcurrent_next(iter);
337 } else {
338 result = isc_hashmap_iter_next(iter);
339 }
340 }
341 assert_int_equal(result, ISC_R_NOMORE);
342
343 for (result = isc_hashmap_iter_first(iter); result == ISC_R_SUCCESS;
344 result = isc_hashmap_iter_next(iter))
345 {
346 assert_true(false);
347 }
348 assert_int_equal(result, ISC_R_NOMORE);
349
350 /* Iterator doesn't progress rehashing */
351 assert_true(rehashing_in_progress(hashmap));
352
353 isc_hashmap_iter_destroy(&iter);
354 assert_null(iter);
355
356 isc_hashmap_destroy(&hashmap);
357 assert_null(hashmap);
358
359 isc_mem_cput(mctx, seen, count, sizeof(seen[0]));
360 isc_mem_cput(mctx, nodes, count, sizeof(nodes[0]));
361 }
362
363 /* 1 bit, 120 elements test, full rehashing */
364 ISC_RUN_TEST_IMPL(isc_hashmap_1_120) {
365 test_hashmap_full(1, 120);
366 return;
367 }
368
369 /* 6 bit, 1000 elements test, full rehashing */
370 ISC_RUN_TEST_IMPL(isc_hashmap_6_1000) {
371 test_hashmap_full(6, 1000);
372 return;
373 }
374
375 /* 24 bit, 200K elements test, no rehashing */
376 ISC_RUN_TEST_IMPL(isc_hashmap_24_200000) {
377 test_hashmap_full(24, 200000);
378 return;
379 }
380
381 /* 15 bit, 45K elements test, full rehashing */
382 ISC_RUN_TEST_IMPL(isc_hashmap_1_48000) {
383 test_hashmap_full(1, 48000);
384 return;
385 }
386
387 /* 8 bit, 20k elements test, partial rehashing */
388 ISC_RUN_TEST_IMPL(isc_hashmap_8_20000) {
389 test_hashmap_full(8, 20000);
390 return;
391 }
392
393 /* test hashmap iterator */
394
395 ISC_RUN_TEST_IMPL(isc_hashmap_iterator) {
396 test_hashmap_iterator(true);
397 return;
398 }
399
400 ISC_RUN_TEST_IMPL(isc_hashmap_iterator_static) {
401 test_hashmap_iterator(false);
402 return;
403 }
404
405 ISC_RUN_TEST_IMPL(isc_hashmap_hash_zero_length) {
406 isc_hashmap_t *hashmap = NULL;
407 uint32_t hashval;
408 bool again = false;
409
410 again:
411 isc_hashmap_create(mctx, 1, &hashmap);
412
413 hashval = isc_hash32("", 0, true);
414
415 isc_hashmap_destroy(&hashmap);
416
417 if (hashval == 0 && !again) {
418 /*
419 * We could be extremely unlucky and the siphash could hash the
420 * zero length string to 0, so try one more time.
421 */
422 again = true;
423 goto again;
424 }
425
426 assert_int_not_equal(hashval, 0);
427 }
428
429 static bool
430 case_match(void *node0, const void *key) {
431 struct test_node *node = node0;
432 size_t len = strlen(key);
433
434 return memcmp(node->key, key, len) == 0;
435 }
436
437 static bool
438 nocase_match(void *node0, const void *key) {
439 struct test_node *node = node0;
440 size_t len = strlen(key);
441
442 return isc_ascii_lowerequal((uint8_t *)node->key, key, len);
443 }
444
445 ISC_RUN_TEST_IMPL(isc_hashmap_case) {
446 isc_result_t result;
447 isc_hashmap_t *hashmap = NULL;
448 test_node_t lower = { .key = "isc_hashmap_case" };
449 test_node_t same = { .key = "isc_hashmap_case" };
450 test_node_t upper = { .key = "ISC_HASHMAP_CASE" };
451 test_node_t mixed = { .key = "IsC_hAsHmAp_CaSe" };
452 void *f = NULL;
453
454 isc_hashmap_create(mctx, 1, &hashmap);
455
456 result = isc_hashmap_add(hashmap,
457 isc_hash32(lower.key, strlen(lower.key), true),
458 case_match, lower.key, &lower, NULL);
459 assert_int_equal(result, ISC_R_SUCCESS);
460
461 result = isc_hashmap_add(hashmap,
462 isc_hash32(same.key, strlen(same.key), true),
463 case_match, same.key, &same, NULL);
464 assert_int_equal(result, ISC_R_EXISTS);
465
466 result = isc_hashmap_add(hashmap,
467 isc_hash32(upper.key, strlen(upper.key), true),
468 case_match, upper.key, &upper, NULL);
469 assert_int_equal(result, ISC_R_SUCCESS);
470
471 result = isc_hashmap_find(
472 hashmap, isc_hash32(mixed.key, strlen(mixed.key), true),
473 case_match, mixed.key, &f);
474 assert_int_equal(result, ISC_R_NOTFOUND);
475 assert_ptr_equal(f, NULL);
476
477 isc_hashmap_destroy(&hashmap);
478
479 isc_hashmap_create(mctx, 1, &hashmap);
480
481 result = isc_hashmap_add(
482 hashmap, isc_hash32(lower.key, strlen(lower.key), false),
483 nocase_match, lower.key, &lower, NULL);
484 assert_int_equal(result, ISC_R_SUCCESS);
485
486 result = isc_hashmap_add(hashmap,
487 isc_hash32(same.key, strlen(same.key), false),
488 nocase_match, same.key, &same, NULL);
489 assert_int_equal(result, ISC_R_EXISTS);
490
491 result = isc_hashmap_add(
492 hashmap, isc_hash32(upper.key, strlen(upper.key), false),
493 nocase_match, upper.key, &upper, NULL);
494 assert_int_equal(result, ISC_R_EXISTS);
495
496 result = isc_hashmap_find(
497 hashmap, isc_hash32(mixed.key, strlen(mixed.key), false),
498 nocase_match, mixed.key, &f);
499 assert_int_equal(result, ISC_R_SUCCESS);
500 assert_ptr_equal(f, &lower);
501
502 isc_hashmap_destroy(&hashmap);
503 }
504
505 ISC_TEST_LIST_START
506 ISC_TEST_ENTRY(isc_hashmap_hash_zero_length)
507 ISC_TEST_ENTRY(isc_hashmap_case)
508 ISC_TEST_ENTRY(isc_hashmap_1_120)
509 ISC_TEST_ENTRY(isc_hashmap_6_1000)
510 ISC_TEST_ENTRY(isc_hashmap_24_200000)
511 ISC_TEST_ENTRY(isc_hashmap_1_48000)
512 ISC_TEST_ENTRY(isc_hashmap_8_20000)
513 ISC_TEST_ENTRY(isc_hashmap_iterator)
514 ISC_TEST_ENTRY(isc_hashmap_iterator_static)
515 ISC_TEST_LIST_END
516
517 ISC_TEST_MAIN