Debian Codegraph @

tests/isc/ht_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/ht.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 "ht.c"
38 #undef mctx
39
40 static void
41 test_ht_full(uint8_t init_bits, uintptr_t count) {
42 isc_ht_t *ht = NULL;
43 isc_result_t result;
44 uintptr_t i;
45
46 isc_ht_init(&ht, mctx, init_bits, ISC_HT_CASE_SENSITIVE);
47 assert_non_null(ht);
48
49 for (i = 1; i < count; i++) {
50 /*
51 * Note: snprintf() is followed with strlcat()
52 * to ensure we are always filling the 16 byte key.
53 */
54 unsigned char key[16];
55 snprintf((char *)key, sizeof(key), "%u", (unsigned int)i);
56 strlcat((char *)key, " key of a raw hashtable!!", sizeof(key));
57 result = isc_ht_add(ht, key, 16, (void *)i);
58 assert_int_equal(result, ISC_R_SUCCESS);
59 }
60
61 for (i = 1; i < count; i++) {
62 unsigned char key[16];
63 void *f = NULL;
64 snprintf((char *)key, sizeof(key), "%u", (unsigned int)i);
65 strlcat((char *)key, " key of a raw hashtable!!", sizeof(key));
66 result = isc_ht_find(ht, key, 16, &f);
67 assert_int_equal(result, ISC_R_SUCCESS);
68 assert_ptr_equal((void *)i, (void *)f);
69 }
70
71 for (i = 1; i < count; i++) {
72 unsigned char key[16];
73 snprintf((char *)key, sizeof(key), "%u", (unsigned int)i);
74 strlcat((char *)key, " key of a raw hashtable!!", sizeof(key));
75 result = isc_ht_add(ht, key, 16, (void *)i);
76 assert_int_equal(result, ISC_R_EXISTS);
77 }
78
79 for (i = 1; i < count; i++) {
80 char key[64];
81 /*
82 * Note: the key size is now strlen(key) which is bigger
83 * then the keys added above.
84 */
85 snprintf((char *)key, sizeof(key), "%u", (unsigned int)i);
86 strlcat((char *)key, " key of a raw hashtable!!", sizeof(key));
87 result = isc_ht_add(ht, (const unsigned char *)key, strlen(key),
88 (void *)i);
89 assert_int_equal(result, ISC_R_SUCCESS);
90 }
91
92 for (i = 1; i < count; i++) {
93 unsigned char key[16];
94 void *f = NULL;
95 /*
96 * Note: case of KEY is now in capitals,
97 */
98 snprintf((char *)key, sizeof(key), "%u", (unsigned int)i);
99 strlcat((char *)key, " KEY of a raw hashtable!!", sizeof(key));
100 result = isc_ht_find(ht, key, 16, &f);
101 assert_int_equal(result, ISC_R_NOTFOUND);
102 assert_null(f);
103 }
104
105 for (i = 1; i < count; i++) {
106 char key[64];
107 void *f = NULL;
108 snprintf((char *)key, sizeof(key), "%u", (unsigned int)i);
109 strlcat((char *)key, " key of a raw hashtable!!", sizeof(key));
110 result = isc_ht_find(ht, (const unsigned char *)key,
111 strlen(key), &f);
112 assert_int_equal(result, ISC_R_SUCCESS);
113 assert_ptr_equal(f, (void *)i);
114 }
115
116 for (i = 1; i < count; i++) {
117 unsigned char key[16];
118 void *f = NULL;
119 snprintf((char *)key, sizeof(key), "%u", (unsigned int)i);
120 strlcat((char *)key, " key of a raw hashtable!!", sizeof(key));
121 result = isc_ht_delete(ht, key, 16);
122 assert_int_equal(result, ISC_R_SUCCESS);
123 result = isc_ht_find(ht, key, 16, &f);
124 assert_int_equal(result, ISC_R_NOTFOUND);
125 assert_null(f);
126 }
127
128 for (i = 1; i < count; i++) {
129 unsigned char key[16];
130 /*
131 * Note: upper case KEY.
132 */
133 snprintf((char *)key, sizeof(key), "%u", (unsigned int)i);
134 strlcat((char *)key, " KEY of a raw hashtable!!", sizeof(key));
135 result = isc_ht_add(ht, key, 16, (void *)i);
136 assert_int_equal(result, ISC_R_SUCCESS);
137 }
138
139 for (i = 1; i < count; i++) {
140 char key[64];
141 void *f = NULL;
142 snprintf((char *)key, sizeof(key), "%u", (unsigned int)i);
143 strlcat((char *)key, " key of a raw hashtable!!", sizeof(key));
144 result = isc_ht_delete(ht, (const unsigned char *)key,
145 strlen(key));
146 assert_int_equal(result, ISC_R_SUCCESS);
147 result = isc_ht_find(ht, (const unsigned char *)key,
148 strlen(key), &f);
149 assert_int_equal(result, ISC_R_NOTFOUND);
150 assert_null(f);
151 }
152
153 for (i = 1; i < count; i++) {
154 unsigned char key[16];
155 void *f = NULL;
156 /*
157 * Note: case of KEY is now in capitals,
158 */
159 snprintf((char *)key, sizeof(key), "%u", (unsigned int)i);
160 strlcat((char *)key, " KEY of a raw hashtable!!", sizeof(key));
161 result = isc_ht_find(ht, key, 16, &f);
162 assert_int_equal(result, ISC_R_SUCCESS);
163 assert_ptr_equal((void *)i, (void *)f);
164 }
165
166 for (i = 1; i < count; i++) {
167 unsigned char key[16];
168 void *f = NULL;
169 snprintf((char *)key, sizeof(key), "%u", (unsigned int)i);
170 strlcat((char *)key, " key of a raw hashtable!!", sizeof(key));
171 result = isc_ht_find(ht, key, 16, &f);
172 assert_int_equal(result, ISC_R_NOTFOUND);
173 assert_null(f);
174 }
175
176 isc_ht_destroy(&ht);
177 assert_null(ht);
178 }
179
180 static void
181 test_ht_iterator(void) {
182 isc_ht_t *ht = NULL;
183 isc_result_t result;
184 isc_ht_iter_t *iter = NULL;
185 uintptr_t i;
186 uintptr_t count = 7600;
187 uint32_t walked;
188 unsigned char key[16];
189 size_t tksize;
190
191 isc_ht_init(&ht, mctx, HT_MIN_BITS, ISC_HT_CASE_SENSITIVE);
192 assert_non_null(ht);
193 for (i = 1; i <= count; i++) {
194 /*
195 * Note that the string we're snprintfing is always > 16 bytes
196 * so we are always filling the key.
197 */
198 snprintf((char *)key, sizeof(key), "%u", (unsigned int)i);
199 strlcat((char *)key, "key of a raw hashtable!!", sizeof(key));
200 result = isc_ht_add(ht, key, 16, (void *)i);
201 assert_int_equal(result, ISC_R_SUCCESS);
202 }
203
204 /* We want to iterate while rehashing is in progress */
205 assert_true(rehashing_in_progress(ht));
206
207 walked = 0;
208 isc_ht_iter_create(ht, &iter);
209
210 for (result = isc_ht_iter_first(iter); result == ISC_R_SUCCESS;
211 result = isc_ht_iter_next(iter))
212 {
213 unsigned char *tkey = NULL;
214 void *v = NULL;
215
216 isc_ht_iter_current(iter, &v);
217 isc_ht_iter_currentkey(iter, &tkey, &tksize);
218 assert_int_equal(tksize, 16);
219 i = (uintptr_t)v;
220 snprintf((char *)key, sizeof(key), "%u", (unsigned int)i);
221 strlcat((char *)key, "key of a raw hashtable!!", sizeof(key));
222 assert_memory_equal(key, tkey, 16);
223 walked++;
224 }
225 assert_int_equal(walked, count);
226 assert_int_equal(result, ISC_R_NOMORE);
227
228 /* erase odd */
229 walked = 0;
230 result = isc_ht_iter_first(iter);
231 while (result == ISC_R_SUCCESS) {
232 unsigned char *tkey = NULL;
233 void *v = NULL;
234
235 isc_ht_iter_current(iter, &v);
236 isc_ht_iter_currentkey(iter, &tkey, &tksize);
237 assert_int_equal(tksize, 16);
238 i = (uintptr_t)v;
239 snprintf((char *)key, sizeof(key), "%u", (unsigned int)i);
240 strlcat((char *)key, "key of a raw hashtable!!", sizeof(key));
241 assert_memory_equal(key, tkey, 16);
242 if ((uintptr_t)v % 2 == 0) {
243 result = isc_ht_iter_delcurrent_next(iter);
244 } else {
245 result = isc_ht_iter_next(iter);
246 }
247 walked++;
248 }
249 assert_int_equal(result, ISC_R_NOMORE);
250 assert_int_equal(walked, count);
251
252 /* erase even */
253 walked = 0;
254 result = isc_ht_iter_first(iter);
255 while (result == ISC_R_SUCCESS) {
256 unsigned char *tkey = NULL;
257 void *v = NULL;
258
259 isc_ht_iter_current(iter, &v);
260 isc_ht_iter_currentkey(iter, &tkey, &tksize);
261 assert_int_equal(tksize, 16);
262 i = (uintptr_t)v;
263 snprintf((char *)key, sizeof(key), "%u", (unsigned int)i);
264 strlcat((char *)key, "key of a raw hashtable!!", sizeof(key));
265 assert_memory_equal(key, tkey, 16);
266 if ((uintptr_t)v % 2 == 1) {
267 result = isc_ht_iter_delcurrent_next(iter);
268 } else {
269 result = isc_ht_iter_next(iter);
270 }
271 walked++;
272 }
273 assert_int_equal(result, ISC_R_NOMORE);
274 assert_int_equal(walked, count / 2);
275
276 walked = 0;
277 for (result = isc_ht_iter_first(iter); result == ISC_R_SUCCESS;
278 result = isc_ht_iter_next(iter))
279 {
280 walked++;
281 }
282
283 assert_int_equal(result, ISC_R_NOMORE);
284 assert_int_equal(walked, 0);
285
286 /* Iterator doesn't progress rehashing */
287 assert_true(rehashing_in_progress(ht));
288
289 isc_ht_iter_destroy(&iter);
290 assert_null(iter);
291
292 isc_ht_destroy(&ht);
293 assert_null(ht);
294 }
295
296 /* 1 bit, 120 elements test, full rehashing */
297 ISC_RUN_TEST_IMPL(isc_ht_1_120) {
298 test_ht_full(1, 120);
299 return;
300 }
301
302 /* 6 bit, 1000 elements test, full rehashing */
303 ISC_RUN_TEST_IMPL(isc_ht_6_1000) {
304 test_ht_full(6, 1000);
305 return;
306 }
307
308 /* 24 bit, 200K elements test, no rehashing */
309 ISC_RUN_TEST_IMPL(isc_ht_24_200000) {
310 UNUSED(state);
311 test_ht_full(24, 200000);
312 }
313
314 /* 15 bit, 45K elements test, full rehashing */
315 ISC_RUN_TEST_IMPL(isc_ht_1_48000) {
316 UNUSED(state);
317 test_ht_full(1, 48000);
318 }
319
320 /* 8 bit, 20k elements test, partial rehashing */
321 ISC_RUN_TEST_IMPL(isc_ht_8_20000) {
322 UNUSED(state);
323 test_ht_full(8, 20000);
324 }
325
326 /* test hashtable iterator */
327
328 ISC_RUN_TEST_IMPL(isc_ht_iterator) {
329 UNUSED(state);
330 test_ht_iterator();
331 }
332
333 ISC_RUN_TEST_IMPL(isc_ht_case) {
334 isc_ht_t *ht = NULL;
335 void *f = NULL;
336 isc_result_t result = ISC_R_UNSET;
337
338 unsigned char lower[16] = { "test case" };
339 unsigned char same[16] = { "test case" };
340 unsigned char upper[16] = { "TEST CASE" };
341 unsigned char mixed[16] = { "tEsT CaSe" };
342
343 isc_ht_init(&ht, mctx, 8, ISC_HT_CASE_SENSITIVE);
344 assert_non_null(ht);
345
346 result = isc_ht_add(ht, lower, 16, (void *)lower);
347 assert_int_equal(result, ISC_R_SUCCESS);
348
349 result = isc_ht_add(ht, same, 16, (void *)same);
350 assert_int_equal(result, ISC_R_EXISTS);
351
352 result = isc_ht_add(ht, upper, 16, (void *)upper);
353 assert_int_equal(result, ISC_R_SUCCESS);
354
355 result = isc_ht_find(ht, mixed, 16, &f);
356 assert_int_equal(result, ISC_R_NOTFOUND);
357 assert_null(f);
358
359 isc_ht_destroy(&ht);
360 assert_null(ht);
361
362 isc_ht_init(&ht, mctx, 8, ISC_HT_CASE_INSENSITIVE);
363 assert_non_null(ht);
364
365 result = isc_ht_add(ht, lower, 16, (void *)lower);
366 assert_int_equal(result, ISC_R_SUCCESS);
367
368 result = isc_ht_add(ht, same, 16, (void *)same);
369 assert_int_equal(result, ISC_R_EXISTS);
370
371 result = isc_ht_add(ht, upper, 16, (void *)upper);
372 assert_int_equal(result, ISC_R_EXISTS);
373
374 result = isc_ht_find(ht, mixed, 16, &f);
375 assert_int_equal(result, ISC_R_SUCCESS);
376 assert_ptr_equal(f, &lower);
377
378 isc_ht_destroy(&ht);
379 assert_null(ht);
380 }
381
382 ISC_TEST_LIST_START
383 ISC_TEST_ENTRY(isc_ht_case)
384 ISC_TEST_ENTRY(isc_ht_1_120)
385 ISC_TEST_ENTRY(isc_ht_6_1000)
386 ISC_TEST_ENTRY(isc_ht_24_200000)
387 ISC_TEST_ENTRY(isc_ht_1_48000)
388 ISC_TEST_ENTRY(isc_ht_8_20000)
389 ISC_TEST_ENTRY(isc_ht_iterator)
390 ISC_TEST_LIST_END
391
392 ISC_TEST_MAIN