Branch data Line data Source code
1 : : // Copyright 2011-2023 David Robillard <d@drobilla.net>
2 : : // SPDX-License-Identifier: ISC
3 : :
4 : : #undef NDEBUG
5 : :
6 : : #define ZIX_HASH_KEY_TYPE const char
7 : : #define ZIX_HASH_RECORD_TYPE const char
8 : :
9 : : #include "ensure.h"
10 : : #include "failing_allocator.h"
11 : : #include "test_data.h"
12 : :
13 : : #include <zix/allocator.h>
14 : : #include <zix/attributes.h>
15 : : #include <zix/digest.h>
16 : : #include <zix/hash.h>
17 : : #include <zix/status.h>
18 : :
19 : : #include <assert.h>
20 : : #include <inttypes.h>
21 : : #include <stdarg.h>
22 : : #include <stdbool.h>
23 : : #include <stdio.h>
24 : : #include <stdlib.h>
25 : : #include <string.h>
26 : :
27 : : typedef struct {
28 : : ZixHash* hash;
29 : : char* buffer;
30 : : char** strings;
31 : : } TestState;
32 : :
33 : : ZIX_LOG_FUNC(2, 3)
34 : : static int
35 : 41 : test_fail(TestState* const state, const char* fmt, ...)
36 : : {
37 : : va_list args; // NOLINT(cppcoreguidelines-init-variables)
38 : 41 : va_start(args, fmt);
39 : 41 : fprintf(stderr, "error: ");
40 : 41 : vfprintf(stderr, fmt, args);
41 : 41 : va_end(args);
42 : :
43 : 41 : zix_hash_free(state->hash);
44 : 41 : free(state->buffer);
45 : 41 : free(state->strings);
46 : 41 : return EXIT_FAILURE;
47 : : }
48 : :
49 : : ZIX_PURE_FUNC static const char*
50 : 753130 : identity(const char* record)
51 : : {
52 : 753130 : return record;
53 : : }
54 : :
55 : : /// Decent hash function using zix_digest (murmur2)
56 : : ZIX_PURE_FUNC static size_t
57 : 539430 : decent_string_hash(const char* const str)
58 : : {
59 : 539430 : return zix_digest(0U, str, strlen(str));
60 : : }
61 : :
62 : : /// Terrible hash function from K&R first edition
63 : : ZIX_PURE_FUNC static size_t
64 : 35403 : terrible_string_hash(const char* str)
65 : : {
66 : 35403 : size_t hash = 0U;
67 : 35403 : uint8_t c = 0U;
68 : :
69 [ + + ]: 566477 : while ((c = (uint8_t)*str++)) {
70 : 531074 : hash += c;
71 : : }
72 : :
73 : 35403 : return hash;
74 : : }
75 : :
76 : : ZIX_PURE_FUNC static size_t
77 : 35228 : string_hash_aligned(const char* const str)
78 : : {
79 : 35228 : size_t length = strlen(str);
80 : :
81 : 35228 : length = (length + (sizeof(size_t) - 1)) / sizeof(size_t) * sizeof(size_t);
82 : 35228 : return zix_digest_aligned(0U, str, length);
83 : : }
84 : :
85 : : ZIX_PURE_FUNC static size_t
86 : 35053 : string_hash32(const char* const str)
87 : : {
88 : 35053 : return (size_t)zix_digest32(0U, str, strlen(str));
89 : : }
90 : :
91 : : ZIX_PURE_FUNC static size_t
92 : 34878 : string_hash64(const char* const str)
93 : : {
94 : 34878 : return (size_t)zix_digest64(0U, str, strlen(str));
95 : : }
96 : :
97 : : ZIX_PURE_FUNC static size_t
98 : 34703 : string_hash32_aligned(const char* const str)
99 : : {
100 : 34703 : size_t length = strlen(str);
101 : :
102 : 34703 : length = (length + 3U) / 4U * 4U;
103 : 34703 : return (size_t)zix_digest32_aligned(0U, str, length);
104 : : }
105 : :
106 : : #if UINTPTR_MAX >= UINT64_MAX
107 : :
108 : : ZIX_PURE_FUNC static size_t
109 : 34528 : string_hash64_aligned(const char* const str)
110 : : {
111 : 34528 : size_t length = strlen(str);
112 : :
113 : 34528 : length = (length + 7U) / 8U * 8U;
114 : 34528 : return (size_t)zix_digest64_aligned(0U, str, length);
115 : : }
116 : :
117 : : #endif
118 : :
119 : : static bool
120 : 746045 : string_equal(const char* const a, const char* const b)
121 : : {
122 : 746045 : return !strcmp(a, b);
123 : : }
124 : :
125 : : static int
126 : 160 : stress_with(ZixAllocator* const allocator,
127 : : const ZixHashFunc hash_func,
128 : : const size_t n_elems)
129 : : {
130 : 160 : ZixHash* hash = zix_hash_new(allocator, identity, hash_func, string_equal);
131 : 160 : TestState state = {hash, NULL, NULL};
132 [ + + ]: 160 : ENSURE(&state, hash, "Failed to allocate hash\n");
133 : :
134 : : static const size_t string_length = 15;
135 : :
136 : 146 : char* const buffer = (char*)calloc(1, n_elems * (string_length + 1));
137 : 146 : char** const strings = state.strings = (char**)calloc(n_elems, sizeof(char*));
138 : 146 : state.buffer = buffer;
139 : 146 : state.strings = strings;
140 [ + - - + ]: 146 : ENSURE(&state, buffer && state.strings, "Failed to allocate strings\n");
141 : :
142 : 146 : uint32_t seed = 1U;
143 [ + + ]: 3742 : for (size_t i = 0U; i < n_elems; ++i) {
144 : 3596 : strings[i] = buffer + (i * (string_length + 1));
145 [ - + ]: 3596 : assert((uintptr_t)strings[i] % sizeof(size_t) == 0);
146 [ - + ]: 3596 : assert((uintptr_t)strings[i] % sizeof(uint32_t) == 0);
147 : :
148 [ + + ]: 57536 : for (size_t j = 0U; j < string_length; ++j) {
149 : 53940 : seed = lcg32(seed);
150 : 53940 : strings[i][j] = (char)('!' + (seed % 92));
151 : : }
152 : : }
153 : :
154 : : // Insert each string
155 [ + + ]: 3690 : for (size_t i = 0; i < n_elems; ++i) {
156 : 3553 : ZixStatus st = zix_hash_insert(hash, strings[i]);
157 [ + + ]: 3553 : ENSUREV(&state, !st, "Failed to insert `%s'\n", strings[i]);
158 : : }
159 : :
160 : : // Ensure hash size is correct
161 [ - + ]: 137 : ENSUREV(&state,
162 : : zix_hash_size(hash) == n_elems,
163 : : "Hash size %" PRIuPTR " != %" PRIuPTR "\n",
164 : : zix_hash_size(hash),
165 : : n_elems);
166 : :
167 : : // Attempt to insert each string again
168 [ + + ]: 3661 : for (size_t i = 0; i < n_elems; ++i) {
169 : 3524 : ZixStatus st = zix_hash_insert(hash, strings[i]);
170 [ - + ]: 3524 : ENSUREV(&state,
171 : : st == ZIX_STATUS_EXISTS,
172 : : "Double inserted `%s' (%s)\n",
173 : : strings[i],
174 : : zix_strerror(st));
175 : : }
176 : :
177 : : // Search for each string
178 [ + + ]: 3661 : for (size_t i = 0; i < n_elems; ++i) {
179 : 3524 : const char* match = (const char*)zix_hash_find_record(hash, strings[i]);
180 [ - + ]: 3524 : ENSUREV(&state, match, "Failed to find `%s'\n", strings[i]);
181 [ - + ]: 3524 : ENSUREV(&state,
182 : : match == strings[i],
183 : : "Bad match for `%s': `%s'\n",
184 : : strings[i],
185 : : match);
186 : : }
187 : :
188 : : static const char* const not_indexed_string = "__not__indexed__";
189 : :
190 : : // Do a quick smoke test to ensure that false matches don't succeed
191 : 137 : char* const not_indexed = (char*)calloc(1, strlen(not_indexed_string) + 1);
192 [ + - ]: 137 : if (not_indexed) {
193 : 137 : memcpy(not_indexed, not_indexed_string, strlen(not_indexed_string) + 1);
194 : 137 : const char* match = (const char*)zix_hash_find_record(hash, not_indexed);
195 : 137 : free(not_indexed);
196 [ - + ]: 137 : ENSUREV(&state, !match, "Unexpectedly found `%s'\n", not_indexed_string);
197 : : }
198 : :
199 : : // Remove strings
200 [ + + ]: 3635 : for (size_t i = 0; i < n_elems; ++i) {
201 : 3507 : const size_t initial_size = zix_hash_size(hash);
202 : :
203 : : // Remove string
204 : 3507 : const char* removed = NULL;
205 : 3507 : ZixStatus st = zix_hash_remove(hash, strings[i], &removed);
206 [ + + ]: 3507 : ENSUREV(&state, !st, "Failed to remove `%s'\n", strings[i]);
207 : :
208 : : // Ensure the removed value is what we expected
209 [ - + ]: 3498 : ENSUREV(&state,
210 : : removed == strings[i],
211 : : "Removed `%s` instead of `%s`\n",
212 : : removed,
213 : : strings[i]);
214 : :
215 : : // Ensure size is updated
216 [ - + ]: 3498 : ENSURE(&state,
217 : : zix_hash_size(hash) == initial_size - 1,
218 : : "Removing node did not decrease hash size\n");
219 : :
220 : : // Ensure second removal fails
221 : 3498 : st = zix_hash_remove(hash, strings[i], &removed);
222 [ - + ]: 3498 : ENSUREV(&state,
223 : : st == ZIX_STATUS_NOT_FOUND,
224 : : "Unexpectedly removed `%s' twice\n",
225 : : strings[i]);
226 : :
227 : : // Ensure value can no longer be found
228 [ - + ]: 3498 : assert(zix_hash_find(hash, strings[i]) == zix_hash_end(hash));
229 : :
230 : : // Check to ensure remaining strings are still present
231 [ + + ]: 728071 : for (size_t j = i + 1; j < n_elems; ++j) {
232 : 724573 : const char* match = (const char*)zix_hash_find_record(hash, strings[j]);
233 [ - + ]: 724573 : ENSUREV(&state, match, "Failed to find `%s' after remove\n", strings[j]);
234 [ - + ]: 724573 : ENSUREV(&state,
235 : : match == strings[j],
236 : : "Bad match for `%s' after remove\n",
237 : : strings[j]);
238 : : }
239 : : }
240 : :
241 : : // Insert each string again (to check non-empty destruction)
242 [ + + ]: 3528 : for (size_t i = 0; i < n_elems; ++i) {
243 : 3409 : ZixHashInsertPlan plan = zix_hash_plan_insert(hash, strings[i]);
244 : :
245 [ - + ]: 3409 : assert(!zix_hash_record_at(hash, plan));
246 : 3409 : ZixStatus st = zix_hash_insert_at(hash, plan, strings[i]);
247 [ + + ]: 3409 : ENSUREV(&state, !st, "Failed to insert `%s'\n", strings[i]);
248 : : }
249 : :
250 : : // Check key == value (and test zix_hash_foreach)
251 : 119 : size_t n_checked = 0U;
252 [ + + ]: 3499 : for (ZixHashIter i = zix_hash_begin(hash); i != zix_hash_end(hash);
253 : 3380 : i = zix_hash_next(hash, i)) {
254 : 3380 : const char* const string = (const char*)zix_hash_get(hash, i);
255 [ - + ]: 3380 : assert(string);
256 [ - + ]: 3380 : ENSUREV(&state, strlen(string) >= 3, "Corrupt value `%s'\n", string);
257 : :
258 : 3380 : ++n_checked;
259 : : }
260 : :
261 [ - + ]: 119 : ENSURE(&state, n_checked == n_elems, "Check failed\n");
262 : :
263 : 119 : free(strings);
264 : 119 : free(buffer);
265 : 119 : zix_hash_free(hash);
266 : :
267 : 119 : return 0;
268 : : }
269 : :
270 : : static int
271 : 43 : stress(ZixAllocator* const allocator, const size_t n_elems)
272 : : {
273 [ + + + + ]: 75 : if (stress_with(allocator, decent_string_hash, n_elems) ||
274 [ + + ]: 59 : stress_with(allocator, terrible_string_hash, n_elems / 4) ||
275 [ + + ]: 49 : stress_with(allocator, string_hash_aligned, n_elems / 4) ||
276 [ + + ]: 39 : stress_with(allocator, string_hash32, n_elems / 4) ||
277 [ + + ]: 29 : stress_with(allocator, string_hash64, n_elems / 4) ||
278 : 12 : stress_with(allocator, string_hash32_aligned, n_elems / 4)) {
279 : 36 : return 1;
280 : : }
281 : :
282 : : #if UINTPTR_MAX >= UINT64_MAX
283 [ + + ]: 7 : if (stress_with(allocator, string_hash64_aligned, n_elems / 4)) {
284 : 5 : return 1;
285 : : }
286 : : #endif
287 : :
288 : 2 : return 0;
289 : : }
290 : :
291 : : /// Identity hash function for numeric strings for explicitly hitting cases
292 : : ZIX_PURE_FUNC static size_t
293 : 12 : identity_index_hash(const char* const str)
294 : : {
295 : 12 : return strtoul(str, NULL, 10);
296 : : }
297 : :
298 : : static void
299 : 1 : test_all_tombstones(void)
300 : : {
301 : : /* This tests an edge case where a minimum-sized table can be entirely full
302 : : of tombstones. If the search loop is not written carefully, then this can
303 : : result in a hang. This has been seen to occur in real code, though it's
304 : : very hard to hit with a decent hash function. So, we use the above
305 : : degenerate index hashing function to explicitly place elements exactly
306 : : where we want to hit this case. */
307 : :
308 : : #define N_STRINGS 4
309 : :
310 : : static const char* original_strings[N_STRINGS] = {
311 : : "0 a",
312 : : "1 a",
313 : : "2 a",
314 : : "3 a",
315 : : };
316 : :
317 : : static const char* collision_strings[N_STRINGS] = {
318 : : "0 b",
319 : : "1 b",
320 : : "2 b",
321 : : "3 b",
322 : : };
323 : :
324 : 1 : ZixStatus st = ZIX_STATUS_SUCCESS;
325 : : ZixHash* hash =
326 : 1 : zix_hash_new(NULL, identity, identity_index_hash, string_equal);
327 : :
328 : : // Insert each element then immediately remove it
329 [ + + ]: 5 : for (unsigned i = 0U; i < N_STRINGS; ++i) {
330 : 4 : const char* removed = NULL;
331 : :
332 [ - + ]: 4 : assert(!zix_hash_insert(hash, original_strings[i]));
333 [ - + ]: 4 : assert(!zix_hash_remove(hash, original_strings[i], &removed));
334 : : }
335 : :
336 : : // Now the table should be "empty" but contain tombstones
337 [ - + ]: 1 : assert(zix_hash_size(hash) == 0);
338 : :
339 : : // Insert clashing elements which should hit the "all tombstones" case
340 [ + + ]: 5 : for (unsigned i = 0U; i < N_STRINGS; ++i) {
341 [ - + ]: 4 : assert(!zix_hash_insert(hash, collision_strings[i]));
342 [ - + ]: 4 : assert(!st);
343 : : }
344 : :
345 : 1 : zix_hash_free(hash);
346 : :
347 : : #undef N_STRINGS
348 : 1 : }
349 : :
350 : : static void
351 : 1 : test_failed_alloc(void)
352 : : {
353 : 1 : ZixFailingAllocator allocator = zix_failing_allocator();
354 : :
355 : : // Successfully stress test the tree to count the number of allocations
356 [ - + ]: 1 : assert(!stress(&allocator.base, 16));
357 : :
358 : : // Test that each allocation failing is handled gracefully
359 : 1 : const size_t n_new_allocs = zix_failing_allocator_reset(&allocator, 0);
360 [ + + ]: 42 : for (size_t i = 0U; i < n_new_allocs; ++i) {
361 : 41 : zix_failing_allocator_reset(&allocator, i);
362 [ - + ]: 41 : assert(stress(&allocator.base, 16));
363 : : }
364 : 1 : }
365 : :
366 : : int
367 : 1 : main(void)
368 : : {
369 : 1 : zix_hash_free(NULL);
370 : :
371 : 1 : test_all_tombstones();
372 : 1 : test_failed_alloc();
373 : :
374 : : static const size_t n_elems = 1024U;
375 : :
376 : 1 : return stress(NULL, n_elems);
377 : : }
|