Debian Codegraph @

crt/aws-c-common/tests/hash_table_test.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5
6 #include <aws/common/hash_table.h>
7
8 #include <aws/common/clock.h>
9 #include <aws/common/string.h>
10 #include <aws/testing/aws_test_harness.h>
11 #include <stdio.h>
12
13 static const char *TEST_STR_1 = "test 1";
14 static const char *TEST_STR_2 = "test 2";
15
16 static const char *TEST_VAL_STR_1 = "value 1";
17 static const char *TEST_VAL_STR_2 = "value 2";
18
19 #define ASSERT_HASH_TABLE_ENTRY_COUNT(map, count) \
20 ASSERT_UINT_EQUALS(count, aws_hash_table_get_entry_count(map), "Hash map should have %d entries", count)
21
22 #define ASSERT_NO_KEY(hash_table, key) \
23 do { \
24 AWS_STATIC_STRING_FROM_LITERAL(assert_key, key); \
25 struct aws_hash_element *pElem_assert; \
26 ASSERT_SUCCESS(aws_hash_table_find((hash_table), (void *)assert_key, &pElem_assert)); \
27 ASSERT_NULL(pElem_assert, "Expected key to not be present: " key); \
28 } while (0)
29
30 #define ASSERT_KEY_VALUE(hash_table, key, expected) \
31 do { \
32 AWS_STATIC_STRING_FROM_LITERAL(assert_key, key); \
33 AWS_STATIC_STRING_FROM_LITERAL(assert_value, expected); \
34 struct aws_hash_element *pElem_assert; \
35 ASSERT_SUCCESS(aws_hash_table_find((hash_table), (void *)assert_key, &pElem_assert)); \
36 ASSERT_NOT_NULL(pElem_assert, "Expected key to be present: " key); \
37 ASSERT_TRUE( \
38 aws_string_eq(assert_value, (const struct aws_string *)pElem_assert->value), \
39 "Expected key \"" key "\" to have value \"" expected "\"; actually had value \"%s\"", \
40 aws_string_bytes((const struct aws_string *)pElem_assert->value)); \
41 } while (0)
42
43 AWS_TEST_CASE(test_hash_table_create_find, s_test_hash_table_create_find_fn)
44 static int s_test_hash_table_create_find_fn(struct aws_allocator *allocator, void *ctx) {
45
46 (void)ctx;
47
48 struct aws_hash_table hash_table;
49 int err_code =
50 aws_hash_table_init(&hash_table, allocator, 10, aws_hash_c_string, aws_hash_callback_c_str_eq, NULL, NULL);
51 struct aws_hash_element *pElem;
52 int was_created;
53
54 ASSERT_SUCCESS(err_code, "Hash Map init should have succeeded.");
55 ASSERT_HASH_TABLE_ENTRY_COUNT(&hash_table, 0);
56
57 err_code = aws_hash_table_create(&hash_table, (void *)TEST_STR_1, &pElem, &was_created);
58 ASSERT_SUCCESS(err_code, "Hash Map put should have succeeded.");
59 ASSERT_INT_EQUALS(1, was_created, "Hash Map put should have created a new element.");
60 pElem->value = (void *)TEST_VAL_STR_1;
61
62 ASSERT_HASH_TABLE_ENTRY_COUNT(&hash_table, 1);
63
64 /* Try passing a NULL was_created this time */
65 err_code = aws_hash_table_create(&hash_table, (void *)TEST_STR_2, &pElem, NULL);
66 ASSERT_SUCCESS(err_code, "Hash Map put should have succeeded.");
67 pElem->value = (void *)TEST_VAL_STR_2;
68
69 ASSERT_HASH_TABLE_ENTRY_COUNT(&hash_table, 2);
70
71 err_code = aws_hash_table_find(&hash_table, (void *)TEST_STR_1, &pElem);
72 ASSERT_SUCCESS(err_code, "Hash Map get should have succeeded.");
73 ASSERT_STR_EQUALS(
74 TEST_VAL_STR_1,
75 (const char *)pElem->value,
76 "Returned value for %s, should have been %s",
77 TEST_STR_1,
78 TEST_VAL_STR_1);
79
80 err_code = aws_hash_table_find(&hash_table, (void *)TEST_STR_2, &pElem);
81 ASSERT_SUCCESS(err_code, "Hash Map get should have succeeded.");
82 ASSERT_BIN_ARRAYS_EQUALS(
83 TEST_VAL_STR_2,
84 strlen(TEST_VAL_STR_2) + 1,
85 (const char *)pElem->value,
86 strlen(pElem->value) + 1,
87 "Returned value for %s, should have been %s",
88 TEST_STR_2,
89 TEST_VAL_STR_2);
90
91 ASSERT_HASH_TABLE_ENTRY_COUNT(&hash_table, 2);
92
93 err_code = aws_hash_table_remove_element(&hash_table, pElem);
94 ASSERT_SUCCESS(err_code, "Hash Map remove element should have succeeded.");
95 ASSERT_HASH_TABLE_ENTRY_COUNT(&hash_table, 1);
96
97 aws_hash_table_clean_up(&hash_table);
98 return 0;
99 }
100
101 AWS_TEST_CASE(test_hash_table_string_create_find, s_test_hash_table_string_create_find_fn)
102 static int s_test_hash_table_string_create_find_fn(struct aws_allocator *allocator, void *ctx) {
103 (void)ctx;
104
105 struct aws_hash_table hash_table;
106 struct aws_hash_element *pElem;
107 int was_created;
108
109 int ret = aws_hash_table_init(
110 &hash_table,
111 allocator,
112 10,
113 aws_hash_string,
114 aws_hash_callback_string_eq,
115 aws_hash_callback_string_destroy,
116 aws_hash_callback_string_destroy);
117 ASSERT_SUCCESS(ret, "Hash Map init should have succeeded.");
118
119 /* First element of hash, both key and value are statically allocated strings */
120 AWS_STATIC_STRING_FROM_LITERAL(key_1, "tweedle dee");
121 AWS_STATIC_STRING_FROM_LITERAL(val_1, "tweedle dum");
122
123 /* Second element of hash, only value is dynamically allocated string */
124 AWS_STATIC_STRING_FROM_LITERAL(key_2, "what's for dinner?");
125 const struct aws_string *val_2 = aws_string_new_from_c_str(allocator, "deadbeef");
126
127 /* Third element of hash, only key is dynamically allocated string */
128 uint8_t bytes[] = {0x88, 0x00, 0xaa, 0x13, 0xb7, 0x93, 0x7f, 0xdd, 0xbb, 0x62};
129 const struct aws_string *key_3 = aws_string_new_from_array(allocator, bytes, 10);
130 AWS_STATIC_STRING_FROM_LITERAL(val_3, "hunter2");
131
132 ret = aws_hash_table_create(&hash_table, (void *)key_1, &pElem, &was_created);
133 ASSERT_SUCCESS(ret, "Hash Map put should have succeeded.");
134 ASSERT_INT_EQUALS(1, was_created, "Hash Map put should have created a new element.");
135 pElem->value = (void *)val_1;
136
137 /* Try passing a NULL was_created this time */
138 ret = aws_hash_table_create(&hash_table, (void *)key_2, &pElem, NULL);
139 ASSERT_SUCCESS(ret, "Hash Map put should have succeeded.");
140 pElem->value = (void *)val_2;
141
142 ret = aws_hash_table_create(&hash_table, (void *)key_3, &pElem, NULL);
143 ASSERT_SUCCESS(ret, "Hash Map put should have succeeded.");
144 pElem->value = (void *)val_3;
145
146 ret = aws_hash_table_find(&hash_table, (void *)key_1, &pElem);
147 ASSERT_SUCCESS(ret, "Hash Map get should have succeeded.");
148 ASSERT_BIN_ARRAYS_EQUALS(
149 "tweedle dee",
150 strlen("tweedle dee"),
151 aws_string_bytes(pElem->key),
152 ((struct aws_string *)pElem->key)->len,
153 "Returned key for %s, should have been %s",
154 "tweedle dee",
155 "tweedle dee");
156 ASSERT_BIN_ARRAYS_EQUALS(
157 "tweedle dum",
158 strlen("tweedle dum"),
159 aws_string_bytes(pElem->value),
160 ((struct aws_string *)pElem->value)->len,
161 "Returned value for %s, should have been %s",
162 "tweedle dee",
163 "tweedle dum");
164
165 ret = aws_hash_table_find(&hash_table, (void *)key_2, &pElem);
166 ASSERT_SUCCESS(ret, "Hash Map get should have succeeded.");
167 ASSERT_BIN_ARRAYS_EQUALS(
168 "what's for dinner?",
169 strlen("what's for dinner?"),
170 aws_string_bytes(pElem->key),
171 ((struct aws_string *)pElem->key)->len,
172 "Returned key for %s, should have been %s",
173 "what's for dinner?",
174 "what's for dinner?");
175 ASSERT_BIN_ARRAYS_EQUALS(
176 "deadbeef",
177 strlen("deadbeef"),
178 aws_string_bytes(pElem->value),
179 ((struct aws_string *)pElem->value)->len,
180 "Returned value for %s, should have been %s",
181 "what's for dinner?",
182 "deadbeef");
183
184 ret = aws_hash_table_find(&hash_table, (void *)key_3, &pElem);
185 ASSERT_SUCCESS(ret, "Hash Map get should have succeeded.");
186 ASSERT_BIN_ARRAYS_EQUALS(
187 bytes,
188 10,
189 aws_string_bytes(pElem->key),
190 ((struct aws_string *)pElem->key)->len,
191 "Returned key for %02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx should have been same",
192 bytes[0],
193 bytes[1],
194 bytes[2],
195 bytes[3],
196 bytes[4],
197 bytes[5],
198 bytes[6],
199 bytes[7],
200 bytes[8],
201 bytes[9]);
202 ASSERT_BIN_ARRAYS_EQUALS(
203 "hunter2",
204 strlen("hunter2"),
205 aws_string_bytes(pElem->value),
206 ((struct aws_string *)pElem->value)->len,
207 "Returned value for binary bytes should have been %s",
208 "hunter2");
209
210 aws_string_destroy((struct aws_string *)pElem->key);
211 aws_string_destroy(pElem->value);
212 ret = aws_hash_table_remove_element(&hash_table, pElem);
213 ASSERT_SUCCESS(ret, "Hash Map remove element should have succeeded.");
214 ASSERT_HASH_TABLE_ENTRY_COUNT(&hash_table, 2);
215
216 aws_hash_table_clean_up(&hash_table);
217
218 return 0;
219 }
220
221 static const void *last_key, *last_value;
222
223 static void destroy_key_record(void *key) {
224 last_key = key;
225 }
226
227 static void destroy_value_record(void *value) {
228 last_value = value;
229 }
230
231 AWS_TEST_CASE(test_hash_table_put, s_test_hash_table_put_fn)
232 static int s_test_hash_table_put_fn(struct aws_allocator *allocator, void *ctx) {
233 (void)ctx;
234
235 struct aws_hash_table hash_table;
236 struct aws_hash_element *pElem;
237 int was_created;
238
239 int ret = aws_hash_table_init(
240 &hash_table,
241 allocator,
242 10,
243 aws_hash_string,
244 aws_hash_callback_string_eq,
245 destroy_key_record,
246 destroy_value_record);
247 ASSERT_SUCCESS(ret, "Hash Map init should have succeeded.");
248
249 AWS_STATIC_STRING_FROM_LITERAL(sentinel, "");
250 AWS_STATIC_STRING_FROM_LITERAL(key_a_1, "a");
251 AWS_STATIC_STRING_FROM_LITERAL(value_b_1, "b");
252
253 ASSERT_NO_KEY(&hash_table, "a");
254 last_key = last_value = sentinel;
255 aws_hash_table_put(&hash_table, key_a_1, (void *)value_b_1, &was_created);
256 ASSERT_INT_EQUALS(was_created, 1);
257 ASSERT_KEY_VALUE(&hash_table, "a", "b");
258 /* dtors were not called, even with nulls */
259 ASSERT_PTR_EQUALS(last_key, sentinel);
260 ASSERT_PTR_EQUALS(last_value, sentinel);
261
262 AWS_STATIC_STRING_FROM_LITERAL(key_a_2, "a");
263 AWS_STATIC_STRING_FROM_LITERAL(value_c_1, "c");
264
265 last_key = last_value = NULL;
266 aws_hash_table_put(&hash_table, key_a_2, (void *)value_c_1, &was_created);
267 ASSERT_INT_EQUALS(was_created, 0);
268 ASSERT_KEY_VALUE(&hash_table, "a", "c");
269
270 ASSERT_SUCCESS(aws_hash_table_find(&hash_table, (void *)key_a_1, &pElem));
271 ASSERT_PTR_EQUALS(key_a_2, pElem->key);
272 /* verify dtor was called on the old key ptr */
273 ASSERT_PTR_EQUALS(last_key, key_a_1);
274 ASSERT_PTR_EQUALS(last_value, value_b_1);
275
276 last_key = last_value = NULL;
277 aws_hash_table_put(&hash_table, key_a_2, (void *)value_b_1, NULL);
278 ASSERT_KEY_VALUE(&hash_table, "a", "b");
279
280 /* Since the key ptr did not change, it was not destroyed */
281 ASSERT_PTR_EQUALS(last_key, NULL);
282 /* The value was destroyed however */
283 ASSERT_PTR_EQUALS(last_value, value_c_1);
284
285 aws_hash_table_clean_up(&hash_table);
286
287 return 0;
288 }
289 AWS_TEST_CASE(test_hash_table_put_null_dtor, s_test_hash_table_put_null_dtor_fn)
290 static int s_test_hash_table_put_null_dtor_fn(struct aws_allocator *allocator, void *ctx) {
291 (void)ctx;
292
293 struct aws_hash_table hash_table;
294
295 int ret = aws_hash_table_init(&hash_table, allocator, 10, aws_hash_string, aws_hash_callback_string_eq, NULL, NULL);
296 ASSERT_SUCCESS(ret, "Hash Map init should have succeeded.");
297
298 AWS_STATIC_STRING_FROM_LITERAL(foo, "foo");
299 ASSERT_SUCCESS(aws_hash_table_put(&hash_table, foo, (void *)foo, NULL));
300 ASSERT_SUCCESS(aws_hash_table_put(&hash_table, foo, (void *)foo, NULL));
301
302 aws_hash_table_clean_up(&hash_table);
303
304 return 0;
305 }
306
307 AWS_TEST_CASE(test_hash_table_swap_move, s_test_hash_table_swap_move)
308 static int s_test_hash_table_swap_move(struct aws_allocator *allocator, void *ctx) {
309 (void)ctx;
310
311 AWS_STATIC_STRING_FROM_LITERAL(foo, "foo");
312 AWS_STATIC_STRING_FROM_LITERAL(bar, "bar");
313 AWS_STATIC_STRING_FROM_LITERAL(key, "key");
314
315 struct aws_hash_table table1, table2, tmp;
316
317 ASSERT_SUCCESS(
318 aws_hash_table_init(&table1, allocator, 10, aws_hash_string, aws_hash_callback_string_eq, NULL, NULL));
319 ASSERT_SUCCESS(
320 aws_hash_table_init(&table2, allocator, 10, aws_hash_string, aws_hash_callback_string_eq, NULL, NULL));
321
322 ASSERT_SUCCESS(aws_hash_table_put(&table1, key, (void *)foo, NULL));
323 ASSERT_SUCCESS(aws_hash_table_put(&table2, key, (void *)bar, NULL));
324
325 aws_hash_table_swap(&table1, &table2);
326
327 ASSERT_KEY_VALUE(&table1, "key", "bar");
328 ASSERT_KEY_VALUE(&table2, "key", "foo");
329
330 aws_hash_table_clean_up(&table2);
331
332 ASSERT_KEY_VALUE(&table1, "key", "bar");
333
334 /* Swap is safe with freed/uninitialized tables */
335 aws_hash_table_swap(&table1, &table2);
336 ASSERT_KEY_VALUE(&table2, "key", "bar");
337 memset(&table1, 0xDD, sizeof(table1));
338 aws_hash_table_swap(&table1, &table2);
339 ASSERT_KEY_VALUE(&table1, "key", "bar");
340
341 /* Move is safe with freed/uninitialized destination */
342 aws_hash_table_move(&table2, &table1);
343 ASSERT_KEY_VALUE(&table2, "key", "bar");
344
345 /* After move, source can be cleaned up as a no-op */
346 memcpy(&tmp, &table1, sizeof(table1));
347 aws_hash_table_clean_up(&table1);
348 ASSERT_INT_EQUALS(0, memcmp(&tmp, &table1, sizeof(table1)));
349
350 aws_hash_table_clean_up(&table2);
351
352 return 0;
353 }
354
355 AWS_TEST_CASE(test_hash_table_string_clean_up, s_test_hash_table_string_clean_up_fn)
356 static int s_test_hash_table_string_clean_up_fn(struct aws_allocator *allocator, void *ctx) {
357
358 (void)ctx;
359
360 /* Verify that clean up happens properly when a destructor function is used only on keys or only on values. */
361 struct aws_hash_table hash_table;
362 struct aws_hash_element *pElem;
363 int was_created;
364
365 const struct aws_string *key_1 = aws_string_new_from_c_str(allocator, "Once upon a midnight dreary,");
366 AWS_STATIC_STRING_FROM_LITERAL(val_1, "while I pondered, weak and weary,");
367 const struct aws_string *key_2 = aws_string_new_from_c_str(allocator, "Over many a quaint and curious");
368 AWS_STATIC_STRING_FROM_LITERAL(val_2, "volume of forgotten lore--");
369 const struct aws_string *key_3 = aws_string_new_from_c_str(allocator, "While I nodded, nearly napping,");
370 AWS_STATIC_STRING_FROM_LITERAL(val_3, "suddenly there came a tapping,");
371
372 const struct aws_string *dyn_keys[] = {key_1, key_2, key_3};
373 const struct aws_string *static_vals[] = {val_1, val_2, val_3};
374
375 int ret = aws_hash_table_init(
376 &hash_table,
377 allocator,
378 10,
379 aws_hash_string,
380 aws_hash_callback_string_eq,
381 aws_hash_callback_string_destroy,
382 NULL); /* destroy keys not values */
383 ASSERT_SUCCESS(ret, "Hash Map init should have succeeded.");
384
385 for (int idx = 0; idx < 3; ++idx) {
386 ret = aws_hash_table_create(&hash_table, (void *)dyn_keys[idx], &pElem, &was_created);
387 ASSERT_SUCCESS(ret, "Hash Map put should have succeeded.");
388 ASSERT_INT_EQUALS(1, was_created, "Hash Map put should have created a new element.");
389 pElem->value = (void *)static_vals[idx];
390 }
391
392 aws_hash_table_clean_up(&hash_table);
393
394 AWS_STATIC_STRING_FROM_LITERAL(key_4, "As of some one gently rapping,");
395 const struct aws_string *val_4 = aws_string_new_from_c_str(allocator, "rapping at my chamber door.");
396 AWS_STATIC_STRING_FROM_LITERAL(key_5, "\"'Tis some visitor,\" I muttered,");
397 const struct aws_string *val_5 = aws_string_new_from_c_str(allocator, "\"tapping at my chamber door--");
398 AWS_STATIC_STRING_FROM_LITERAL(key_6, "Only this and nothing more.\"");
399 const struct aws_string *val_6 = aws_string_new_from_c_str(allocator, "from The Raven by Edgar Allan Poe (1845)");
400
401 const struct aws_string *static_keys[] = {key_4, key_5, key_6};
402 const struct aws_string *dyn_vals[] = {val_4, val_5, val_6};
403
404 ret = aws_hash_table_init(
405 &hash_table,
406 allocator,
407 10,
408 aws_hash_string,
409 aws_hash_callback_string_eq,
410 NULL,
411 aws_hash_callback_string_destroy); /* destroy values not keys */
412 ASSERT_SUCCESS(ret, "Hash Map init should have succeeded.");
413
414 for (int idx = 0; idx < 3; ++idx) {
415 ret = aws_hash_table_create(&hash_table, (void *)static_keys[idx], &pElem, &was_created);
416 ASSERT_SUCCESS(ret, "Hash Map put should have succeeded.");
417 ASSERT_INT_EQUALS(1, was_created, "Hash Map put should have created a new element.");
418 pElem->value = (void *)dyn_vals[idx];
419 }
420
421 aws_hash_table_clean_up(&hash_table);
422
423 return 0;
424 }
425
426 static uint64_t hash_collide(const void *a) {
427 (void)a;
428 return 4;
429 }
430
431 AWS_TEST_CASE(test_hash_table_hash_collision, s_test_hash_table_hash_collision_fn)
432 static int s_test_hash_table_hash_collision_fn(struct aws_allocator *allocator, void *ctx) {
433 (void)ctx;
434
435 struct aws_hash_table hash_table;
436 struct aws_hash_element *pElem;
437 int err_code =
438 aws_hash_table_init(&hash_table, allocator, 10, hash_collide, aws_hash_callback_c_str_eq, NULL, NULL);
439
440 ASSERT_SUCCESS(err_code, "Hash Map init should have succeeded.");
441
442 err_code = aws_hash_table_create(&hash_table, (void *)TEST_STR_1, &pElem, NULL);
443 ASSERT_SUCCESS(err_code, "Hash Map put should have succeeded.");
444 pElem->value = (void *)TEST_VAL_STR_1;
445
446 err_code = aws_hash_table_create(&hash_table, (void *)TEST_STR_2, &pElem, NULL);
447 ASSERT_SUCCESS(err_code, "Hash Map put should have succeeded.");
448 pElem->value = (void *)TEST_VAL_STR_2;
449
450 err_code = aws_hash_table_find(&hash_table, (void *)TEST_STR_1, &pElem);
451 ASSERT_SUCCESS(err_code, "Hash Map get should have succeeded.");
452 ASSERT_STR_EQUALS(
453 TEST_VAL_STR_1, pElem->value, "Returned value for %s, should have been %s", TEST_STR_1, TEST_VAL_STR_1);
454
455 err_code = aws_hash_table_find(&hash_table, (void *)TEST_STR_2, &pElem);
456 ASSERT_SUCCESS(err_code, "Hash Map get should have succeeded.");
457 ASSERT_STR_EQUALS(
458 TEST_VAL_STR_2, pElem->value, "Returned value for %s, should have been %s", TEST_STR_2, TEST_VAL_STR_2);
459
460 aws_hash_table_clean_up(&hash_table);
461 return 0;
462 }
463
464 AWS_TEST_CASE(test_hash_table_hash_overwrite, s_test_hash_table_hash_overwrite_fn)
465 static int s_test_hash_table_hash_overwrite_fn(struct aws_allocator *allocator, void *ctx) {
466 (void)ctx;
467
468 struct aws_hash_table hash_table;
469 struct aws_hash_element *pElem;
470 int err_code =
471 aws_hash_table_init(&hash_table, allocator, 10, aws_hash_c_string, aws_hash_callback_c_str_eq, NULL, NULL);
472 int was_created = 42;
473
474 ASSERT_SUCCESS(err_code, "Hash Map init should have succeeded.");
475
476 err_code = aws_hash_table_create(&hash_table, (void *)TEST_STR_1, &pElem, &was_created); //(void *)TEST_VAL_STR_1);
477 ASSERT_SUCCESS(err_code, "Hash Map put should have succeeded.");
478 ASSERT_INT_EQUALS(1, was_created, "Hash Map create should have created a new element.");
479 pElem->value = (void *)TEST_VAL_STR_1;
480
481 err_code = aws_hash_table_create(&hash_table, (void *)TEST_STR_1, &pElem, &was_created);
482 ASSERT_SUCCESS(err_code, "Hash Map put should have succeeded.");
483 ASSERT_INT_EQUALS(0, was_created, "Hash Map create should not have created a new element.");
484 ASSERT_PTR_EQUALS(TEST_VAL_STR_1, pElem->value, "Create should have returned the old value.");
485 pElem->value = (void *)TEST_VAL_STR_2;
486
487 pElem = NULL;
488 err_code = aws_hash_table_find(&hash_table, (void *)TEST_STR_1, &pElem);
489 ASSERT_SUCCESS(err_code, "Hash Map get should have succeeded.");
490 ASSERT_PTR_EQUALS(TEST_VAL_STR_2, pElem->value, "The new value should have been preserved on get");
491
492 aws_hash_table_clean_up(&hash_table);
493 return 0;
494 }
495
496 static void *s_last_removed_key;
497 static void *s_last_removed_value;
498 static int s_key_removal_counter = 0;
499 static int s_value_removal_counter = 0;
500
501 static void s_destroy_key_fn(void *key) {
502 s_last_removed_key = key;
503 ++s_key_removal_counter;
504 }
505 static void s_destroy_value_fn(void *value) {
506 s_last_removed_value = value;
507 ++s_value_removal_counter;
508 }
509
510 static void s_reset_destroy_ck(void) {
511 s_key_removal_counter = 0;
512 s_value_removal_counter = 0;
513 s_last_removed_key = NULL;
514 s_last_removed_value = NULL;
515 }
516
517 AWS_TEST_CASE(test_hash_table_hash_remove, s_test_hash_table_hash_remove_fn)
518 static int s_test_hash_table_hash_remove_fn(struct aws_allocator *allocator, void *ctx) {
519
520 (void)ctx;
521
522 struct aws_hash_table hash_table;
523 struct aws_hash_element *pElem, elem;
524 int err_code = aws_hash_table_init(
525 &hash_table,
526 allocator,
527 10,
528 aws_hash_c_string,
529 aws_hash_callback_c_str_eq,
530 s_destroy_key_fn,
531 s_destroy_value_fn);
532 int was_present = 42;
533
534 s_reset_destroy_ck();
535
536 ASSERT_SUCCESS(err_code, "Hash Map init should have succeeded.");
537
538 err_code = aws_hash_table_create(&hash_table, (void *)TEST_STR_1, NULL, NULL);
539 ASSERT_SUCCESS(err_code, "Hash Map put should have succeeded.");
540
541 err_code = aws_hash_table_create(&hash_table, (void *)TEST_STR_2, &pElem, NULL);
542 ASSERT_SUCCESS(err_code, "Hash Map put should have succeeded.");
543 pElem->value = (void *)TEST_VAL_STR_2;
544
545 /* Create a second time; this should not invoke destroy */
546 err_code = aws_hash_table_create(&hash_table, (void *)TEST_STR_2, &pElem, NULL);
547 ASSERT_SUCCESS(err_code, "Hash Map put should have succeeded.");
548
549 ASSERT_INT_EQUALS(0, s_key_removal_counter, "No keys should be destroyed at this point");
550 ASSERT_INT_EQUALS(0, s_value_removal_counter, "No values should be destroyed at this point");
551
552 err_code = aws_hash_table_remove(&hash_table, (void *)TEST_STR_1, &elem, &was_present);
553 ASSERT_SUCCESS(err_code, "Hash Map remove should have succeeded.");
554 ASSERT_INT_EQUALS(0, s_key_removal_counter, "No keys should be destroyed at this point");
555 ASSERT_INT_EQUALS(0, s_value_removal_counter, "No values should be destroyed at this point");
556 ASSERT_INT_EQUALS(1, was_present, "Item should have been removed");
557
558 err_code = aws_hash_table_find(&hash_table, (void *)TEST_STR_1, &pElem);
559 ASSERT_SUCCESS(err_code, "Find for nonexistent item should still succeed");
560 ASSERT_NULL(pElem, "Expected item to be nonexistent");
561
562 err_code = aws_hash_table_find(&hash_table, (void *)TEST_STR_2, &pElem);
563 ASSERT_SUCCESS(err_code, "Hash Map get should have succeeded.");
564
565 ASSERT_PTR_EQUALS(TEST_VAL_STR_2, pElem->value, "Wrong value returned from second get");
566
567 /* If we delete and discard the element, destroy_fn should be invoked */
568 err_code = aws_hash_table_remove(&hash_table, (void *)TEST_STR_2, NULL, NULL);
569 ASSERT_SUCCESS(err_code, "Remove should have succeeded.");
570 ASSERT_INT_EQUALS(1, s_key_removal_counter, "One key should be destroyed at this point");
571 ASSERT_INT_EQUALS(1, s_value_removal_counter, "One value should be destroyed at this point");
572 ASSERT_PTR_EQUALS(s_last_removed_value, TEST_VAL_STR_2, "Wrong element destroyed");
573
574 /* If we delete an element that's not there, we shouldn't invoke destroy_fn */
575 err_code = aws_hash_table_remove(&hash_table, (void *)TEST_STR_1, NULL, &was_present);
576 ASSERT_SUCCESS(err_code, "Remove still should succeed on nonexistent items");
577 ASSERT_INT_EQUALS(0, was_present, "Remove should indicate item not present");
578 ASSERT_INT_EQUALS(1, s_key_removal_counter, "We shouldn't delete an item if none was found");
579 ASSERT_INT_EQUALS(1, s_value_removal_counter, "We shouldn't delete an item if none was found");
580
581 aws_hash_table_clean_up(&hash_table);
582 return 0;
583 }
584
585 AWS_TEST_CASE(test_hash_table_hash_clear_allows_cleanup, s_test_hash_table_hash_clear_allows_cleanup_fn)
586 static int s_test_hash_table_hash_clear_allows_cleanup_fn(struct aws_allocator *allocator, void *ctx) {
587 (void)ctx;
588
589 struct aws_hash_table hash_table;
590 int err_code = aws_hash_table_init(
591 &hash_table,
592 allocator,
593 10,
594 aws_hash_c_string,
595 aws_hash_callback_c_str_eq,
596 s_destroy_key_fn,
597 s_destroy_value_fn);
598
599 ASSERT_SUCCESS(err_code, "Hash Map init should have succeeded.");
600
601 s_reset_destroy_ck();
602
603 err_code = aws_hash_table_create(&hash_table, (void *)TEST_STR_1, NULL, NULL);
604 ASSERT_SUCCESS(err_code, "Hash Map put should have succeeded.");
605 err_code = aws_hash_table_create(&hash_table, (void *)TEST_STR_2, NULL, NULL);
606 ASSERT_SUCCESS(err_code, "Hash Map put should have succeeded.");
607
608 ASSERT_INT_EQUALS(2, aws_hash_table_get_entry_count(&hash_table));
609 aws_hash_table_clear(&hash_table);
610 ASSERT_INT_EQUALS(2, s_key_removal_counter, "Clear should destroy all keys");
611 ASSERT_INT_EQUALS(2, s_value_removal_counter, "Clear should destroy all values");
612 ASSERT_INT_EQUALS(0, aws_hash_table_get_entry_count(&hash_table));
613
614 struct aws_hash_element *pElem;
615 err_code = aws_hash_table_find(&hash_table, (void *)TEST_STR_1, &pElem);
616 ASSERT_SUCCESS(err_code, "Find should still succeed after clear");
617 ASSERT_NULL(pElem, "Element should not be found");
618
619 s_reset_destroy_ck();
620
621 err_code = aws_hash_table_create(&hash_table, (void *)TEST_STR_1, NULL, NULL);
622 ASSERT_SUCCESS(err_code, "Hash Map put should have succeeded.");
623 err_code = aws_hash_table_create(&hash_table, (void *)TEST_STR_2, NULL, NULL);
624 ASSERT_SUCCESS(err_code, "Hash Map put should have succeeded.");
625
626 aws_hash_table_clean_up(&hash_table);
627 ASSERT_INT_EQUALS(2, s_key_removal_counter, "Cleanup should destroy all keys");
628 ASSERT_INT_EQUALS(2, s_value_removal_counter, "Cleanup should destroy all values");
629
630 return 0;
631 }
632
633 AWS_TEST_CASE(test_hash_table_on_resize_returns_correct_entry, s_test_hash_table_on_resize_returns_correct_entry_fn)
634 static int s_test_hash_table_on_resize_returns_correct_entry_fn(struct aws_allocator *allocator, void *ctx) {
635 (void)ctx;
636
637 struct aws_hash_table hash_table;
638 int err_code = aws_hash_table_init(&hash_table, allocator, 10, aws_hash_ptr, aws_ptr_eq, NULL, NULL);
639
640 ASSERT_SUCCESS(err_code, "Hash Map init should have succeeded.");
641
642 for (int i = 0; i < 20; i++) {
643 struct aws_hash_element *pElem;
644 int was_created;
645 err_code = aws_hash_table_create(&hash_table, (void *)(intptr_t)i, &pElem, &was_created);
646
647 ASSERT_SUCCESS(err_code, "Create should have succeeded");
648 ASSERT_INT_EQUALS(1, was_created, "Create should have created new element");
649 ASSERT_PTR_EQUALS(NULL, pElem->value, "New element should have null value");
650 pElem->value = &hash_table;
651 }
652
653 aws_hash_table_clean_up(&hash_table);
654 return 0;
655 }
656
657 static int s_foreach_cb_tomask(void *context, struct aws_hash_element *p_element) {
658 int *p_mask = context;
659 uintptr_t index = (uintptr_t)p_element->key;
660
661 *p_mask |= (1 << index);
662
663 return AWS_COMMON_HASH_TABLE_ITER_CONTINUE;
664 }
665
666 static int s_foreach_cb_error_and_delete(void *context, struct aws_hash_element *p_element) {
667 (void)context;
668 (void)p_element;
669
670 return AWS_COMMON_HASH_TABLE_ITER_ERROR | AWS_COMMON_HASH_TABLE_ITER_DELETE;
671 }
672
673 static int s_iter_count = 0;
674 static int s_foreach_cb_deltarget(void *context, struct aws_hash_element *p_element) {
675 void **pTarget = context;
676 int rv = AWS_COMMON_HASH_TABLE_ITER_CONTINUE;
677
678 if (p_element->key == *pTarget) {
679 rv |= AWS_COMMON_HASH_TABLE_ITER_DELETE;
680 }
681 s_iter_count++;
682
683 return rv;
684 }
685
686 static int s_foreach_cb_cutoff(void *context, struct aws_hash_element *p_element) {
687 (void)p_element;
688
689 int *p_remain = context;
690 s_iter_count++;
691 if (--*p_remain) {
692 return AWS_COMMON_HASH_TABLE_ITER_CONTINUE;
693 }
694 return 0;
695 }
696 static int s_foreach_cb_cutoff_del(void *context, struct aws_hash_element *p_element) {
697 int *p_remain = context;
698 s_iter_count++;
699 if (--*p_remain) {
700 return AWS_COMMON_HASH_TABLE_ITER_CONTINUE;
701 }
702 *p_remain = (int)(intptr_t)p_element->key;
703 return AWS_COMMON_HASH_TABLE_ITER_DELETE;
704 }
705
706 AWS_TEST_CASE(test_hash_table_foreach, s_test_hash_table_foreach_fn)
707 static int s_test_hash_table_foreach_fn(struct aws_allocator *allocator, void *ctx) {
708 (void)ctx;
709
710 struct aws_hash_table hash_table;
711 ASSERT_SUCCESS(
712 aws_hash_table_init(&hash_table, allocator, 10, aws_hash_ptr, aws_ptr_eq, NULL, NULL), "hash table init");
713
714 for (int i = 0; i < 8; i++) {
715 struct aws_hash_element *pElem;
716 ASSERT_SUCCESS(aws_hash_table_create(&hash_table, (void *)(intptr_t)i, &pElem, NULL), "insert element");
717 pElem->value = NULL;
718 }
719 // delete will not work as long as the error has set
720 ASSERT_FAILS(
721 aws_hash_table_foreach(&hash_table, s_foreach_cb_error_and_delete, NULL), "foreach error from callback");
722 // We should find all eight elements
723 int mask = 0;
724 ASSERT_SUCCESS(aws_hash_table_foreach(&hash_table, s_foreach_cb_tomask, &mask), "foreach invocation");
725 ASSERT_INT_EQUALS(0xff, mask, "bitmask");
726
727 void *target = (void *)(uintptr_t)3;
728 s_iter_count = 0;
729 ASSERT_SUCCESS(aws_hash_table_foreach(&hash_table, s_foreach_cb_deltarget, &target), "foreach invocation");
730 ASSERT_INT_EQUALS(8, s_iter_count, "iteration should not stop when deleting");
731
732 mask = 0;
733 ASSERT_SUCCESS(aws_hash_table_foreach(&hash_table, s_foreach_cb_tomask, &mask), "foreach invocation");
734 ASSERT_INT_EQUALS(0xf7, mask, "element 3 deleted");
735
736 s_iter_count = 0;
737 int remain = 4;
738 ASSERT_SUCCESS(aws_hash_table_foreach(&hash_table, s_foreach_cb_cutoff, &remain), "foreach invocation");
739 ASSERT_INT_EQUALS(0, remain, "no more remaining iterations");
740 ASSERT_INT_EQUALS(4, s_iter_count, "correct iteration count");
741
742 s_iter_count = 0;
743 remain = 4;
744
745 ASSERT_SUCCESS(aws_hash_table_foreach(&hash_table, s_foreach_cb_cutoff_del, &remain), "foreach invocation");
746 ASSERT_INT_EQUALS(4, s_iter_count, "correct iteration count");
747 // we use remain as a side channel to report which element we deleted
748 int expected_mask = 0xf7 & ~(1 << remain);
749
750 mask = 0;
751 ASSERT_SUCCESS(aws_hash_table_foreach(&hash_table, s_foreach_cb_tomask, &mask), "foreach invocation");
752 ASSERT_INT_EQUALS(expected_mask, mask, "stop element deleted");
753
754 aws_hash_table_clean_up(&hash_table);
755
756 return 0;
757 }
758
759 /*
760 * Convenience functions for a hash table which uses uint64_t as keys, and whose
761 * hash function is just the identity function.
762 */
763 static uint64_t s_hash_uint64_identity(const void *a) {
764 return *(uint64_t *)a;
765 }
766
767 static bool s_hash_uint64_eq(const void *a, const void *b) {
768 uint64_t my_a = *(uint64_t *)a;
769 uint64_t my_b = *(uint64_t *)b;
770 return my_a == my_b;
771 }
772
773 AWS_TEST_CASE(test_hash_table_iter, s_test_hash_table_iter_fn)
774 static int s_test_hash_table_iter_fn(struct aws_allocator *allocator, void *ctx) {
775
776 (void)ctx;
777
778 /* Table entries are: (2^0 -> 2^10), (2^1 -> 2^11), (2^2 -> 2^12), ..., (2^9 -> 2^19).
779 * We will iterate through the table and AND all the keys and all the values together
780 * to ensure that we have hit every element of the table.
781 */
782
783 uint64_t powers_of_2[20];
784 uint64_t x = 1;
785 for (int i = 0; i < 20; ++i, x <<= 1) {
786 powers_of_2[i] = x;
787 }
788
789 struct aws_hash_table map;
790 ASSERT_SUCCESS(
791 aws_hash_table_init(&map, allocator, 10, s_hash_uint64_identity, s_hash_uint64_eq, NULL, NULL),
792 "hash table init");
793
794 struct aws_hash_element *elem;
795 for (int i = 0; i < 10; ++i) {
796 int ret = aws_hash_table_create(&map, (void *)(powers_of_2 + i), &elem, NULL);
797 ASSERT_SUCCESS(ret, "Hash Map put should have succeeded.");
798 elem->value = (void *)(powers_of_2 + 10 + i);
799 }
800
801 uint64_t keys_bitflags = 0;
802 uint64_t values_bitflags = 0;
803 int num_elements = 0;
804 for (struct aws_hash_iter iter = aws_hash_iter_begin(&map); !aws_hash_iter_done(&iter); aws_hash_iter_next(&iter)) {
805 uint64_t key = *(const uint64_t *)iter.element.key;
806 uint64_t value = *(uint64_t *)iter.element.value;
807 keys_bitflags |= key;
808 values_bitflags |= value;
809 ++num_elements;
810 }
811 ASSERT_INT_EQUALS(num_elements, 10);
812 ASSERT_UINT_EQUALS(keys_bitflags, 0x3ffULL); // keys are bottom 10 bits
813 ASSERT_UINT_EQUALS(values_bitflags, 0xffc00ULL); // values are next 10 bits
814
815 aws_hash_table_clean_up(&map);
816 return 0;
817 }
818
819 AWS_TEST_CASE(test_hash_table_empty_iter, s_test_hash_table_empty_iter_fn)
820 static int s_test_hash_table_empty_iter_fn(struct aws_allocator *allocator, void *ctx) {
821 (void)ctx;
822
823 struct aws_hash_table map;
824 ASSERT_SUCCESS(aws_hash_table_init(&map, allocator, 10, s_hash_uint64_identity, s_hash_uint64_eq, NULL, NULL));
825
826 struct aws_hash_iter iter = aws_hash_iter_begin(&map);
827 ASSERT_TRUE(aws_hash_iter_done(&iter));
828 aws_hash_iter_next(&iter);
829 ASSERT_TRUE(aws_hash_iter_done(&iter));
830
831 aws_hash_table_clean_up(&map);
832 return 0;
833 }
834
835 AWS_TEST_CASE(test_hash_table_iter_detail, s_test_hash_table_iter_detail)
836 static int s_test_hash_table_iter_detail(struct aws_allocator *allocator, void *ctx) {
837 (void)ctx;
838
839 uint64_t keys[32], vals[32];
840 for (uint64_t i = 0; i < 32; i++) {
841 keys[i] = i;
842 vals[i] = i + 100;
843 }
844
845 struct aws_hash_table map;
846 ASSERT_SUCCESS(aws_hash_table_init(
847 &map, allocator, 10, s_hash_uint64_identity, s_hash_uint64_eq, destroy_key_record, destroy_value_record));
848
849 /*
850 * We'll fill hash table entries as follows:
851 * Slot Value
852 * 0 16
853 * 1 17
854 * 2 18
855 * 3 (empty)
856 * 4 (empty)
857 * 5 5
858 * 6 6
859 * 7 7
860 * 8 8
861 * 9 9
862 * 10 10
863 * 11 11
864 * 12 12
865 * 13 13
866 * 14 14
867 * 15 15
868 */
869 for (size_t i = 5; i <= 18; i++) {
870 ASSERT_SUCCESS(aws_hash_table_put(&map, &keys[i], &vals[i], NULL));
871 }
872
873 /* Verify that we have the correct set of values in the right order, first of all */
874 #define ASSERT_ORDER(iter, ...) \
875 do { \
876 uint64_t expected[] = {__VA_ARGS__}; \
877 size_t count = sizeof(expected) / sizeof(*expected); \
878 for (size_t i = 0; i < count; i++) { \
879 ASSERT_FALSE(aws_hash_iter_done(&(iter))); \
880 ASSERT_INT_EQUALS(expected[i], *(const uint64_t *)(iter).element.key); \
881 ASSERT_INT_EQUALS(expected[i] + 100, *(const uint64_t *)(iter).element.value); \
882 aws_hash_iter_next(&(iter)); \
883 } \
884 } while (0)
885
886 struct aws_hash_iter iter = aws_hash_iter_begin(&map);
887 ASSERT_ORDER(iter, 16, 17, 18, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
888 ASSERT_TRUE(aws_hash_iter_done(&(iter)));
889
890 /* If we delete the very first slot, we expect that we'll see the remaining elements. */
891 iter = aws_hash_iter_begin(&map);
892 last_key = last_value = NULL;
893 aws_hash_iter_delete(&iter, true);
894 aws_hash_iter_next(&iter);
895 /* Since we passed true to delete, we should have destroyed the key and value */
896 ASSERT_PTR_EQUALS(&keys[16], last_key);
897 ASSERT_PTR_EQUALS(&vals[16], last_value);
898 ASSERT_ORDER(iter, 17, 18, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
899
900 /*
901 * If we delete one of the later elements (in this case, 5), the deletion has to wrap
902 * around the hash table. Verify that we don't see the element that wrapped around
903 * (in this case 17) twice.
904 */
905 iter = aws_hash_iter_begin(&map);
906 last_key = last_value = NULL;
907 aws_hash_iter_next(&iter); /* 17 => 18 */
908 aws_hash_iter_next(&iter); /* 18 => 5 */
909
910 aws_hash_iter_delete(&iter, false);
911 ASSERT_NULL(last_key);
912 ASSERT_NULL(last_value);
913
914 aws_hash_iter_next(&iter);
915 ASSERT_ORDER(iter, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
916
917 /* Now verify that we did in fact wrap the element around */
918 iter = aws_hash_iter_begin(&map);
919 ASSERT_ORDER(iter, 17, 18, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
920
921 aws_hash_table_clean_up(&map);
922 #undef ASSERT_ORDER
923
924 return 0;
925 }
926
927 static uint64_t bad_hash_fn(const void *key) {
928 (void)key;
929 return 4; // chosen by fair dice roll
930 // guaranteed to be random
931 }
932
933 static bool everything_is_eq(const void *a, const void *b) {
934 (void)a;
935 (void)b;
936
937 return true;
938 }
939
940 AWS_TEST_CASE(test_hash_table_eq, s_test_hash_table_eq)
941 static int s_test_hash_table_eq(struct aws_allocator *allocator, void *ctx) {
942 (void)ctx;
943
944 struct aws_hash_table table_a, table_b;
945
946 ASSERT_SUCCESS(
947 aws_hash_table_init(&table_a, allocator, 16, aws_hash_string, aws_hash_callback_string_eq, NULL, NULL));
948 ASSERT_SUCCESS(aws_hash_table_init(&table_b, allocator, 16, bad_hash_fn, aws_hash_callback_string_eq, NULL, NULL));
949
950 AWS_STATIC_STRING_FROM_LITERAL(foo_a, "foo");
951 AWS_STATIC_STRING_FROM_LITERAL(foo_b, "foo");
952 AWS_STATIC_STRING_FROM_LITERAL(bar_a, "bar");
953 AWS_STATIC_STRING_FROM_LITERAL(bar_b, "bar");
954 AWS_STATIC_STRING_FROM_LITERAL(quux_a, "quux");
955 AWS_STATIC_STRING_FROM_LITERAL(quux_b, "quux");
956
957 ASSERT_SUCCESS(aws_hash_table_put(&table_a, foo_a, (void *)bar_a, NULL));
958 ASSERT_SUCCESS(aws_hash_table_put(&table_b, foo_b, (void *)bar_b, NULL));
959 ASSERT_SUCCESS(aws_hash_table_put(&table_a, bar_a, (void *)quux_a, NULL));
960 ASSERT_SUCCESS(aws_hash_table_put(&table_b, bar_a, (void *)quux_a, NULL));
961
962 ASSERT_TRUE(aws_hash_table_eq(&table_a, &table_b, aws_hash_callback_string_eq));
963 ASSERT_TRUE(aws_hash_table_eq(&table_a, &table_b, everything_is_eq));
964 ASSERT_FALSE(aws_hash_table_eq(&table_a, &table_b, aws_ptr_eq));
965
966 /* Non-equal: Table B has extra members */
967 ASSERT_SUCCESS(aws_hash_table_put(&table_b, quux_a, (void *)quux_b, NULL));
968 ASSERT_FALSE(aws_hash_table_eq(&table_a, &table_b, aws_hash_callback_string_eq));
969 ASSERT_FALSE(aws_hash_table_eq(&table_a, &table_b, everything_is_eq));
970 ASSERT_FALSE(aws_hash_table_eq(&table_a, &table_b, aws_ptr_eq));
971
972 /* Non-equal: Same number of members, but different keys */
973 ASSERT_SUCCESS(aws_hash_table_remove(&table_b, bar_a, NULL, NULL));
974 ASSERT_FALSE(aws_hash_table_eq(&table_a, &table_b, aws_hash_callback_string_eq));
975 ASSERT_FALSE(aws_hash_table_eq(&table_a, &table_b, everything_is_eq));
976 ASSERT_FALSE(aws_hash_table_eq(&table_a, &table_b, aws_ptr_eq));
977
978 /* Non-equal: Same keys, values differ */
979 ASSERT_SUCCESS(aws_hash_table_remove(&table_b, quux_a, NULL, NULL));
980 ASSERT_SUCCESS(aws_hash_table_put(&table_b, bar_a, (void *)foo_b, NULL));
981 ASSERT_FALSE(aws_hash_table_eq(&table_a, &table_b, aws_hash_callback_string_eq));
982 ASSERT_TRUE(aws_hash_table_eq(&table_a, &table_b, everything_is_eq));
983 ASSERT_FALSE(aws_hash_table_eq(&table_a, &table_b, aws_ptr_eq));
984
985 aws_hash_table_clean_up(&table_b);
986 aws_hash_table_clean_up(&table_a);
987
988 return 0;
989 }
990
991 struct churn_entry {
992 void *key;
993 int original_index;
994 int sorted_index;
995 void *value;
996 int is_removed;
997 };
998
999 static int s_qsort_churn_entry(const void *a, const void *b) {
1000 const struct churn_entry *const *p1 = a, *const *p2 = b;
1001 const struct churn_entry *e1 = *p1, *e2 = *p2;
1002
1003 if (e1->key < e2->key) {
1004 return -1;
1005 }
1006 if (e1->key > e2->key) {
1007 return 1;
1008 }
1009 if (e1->original_index < e2->original_index) {
1010 return -1;
1011 }
1012 if (e1->original_index > e2->original_index) {
1013 return 1;
1014 }
1015 return 0;
1016 }
1017
1018 static long s_timestamp(void) {
1019 uint64_t time = 0;
1020 aws_sys_clock_get_ticks(&time);
1021 return (long)(time / 1000);
1022 }
1023
1024 AWS_TEST_CASE(test_hash_churn, s_test_hash_churn_fn)
1025 static int s_test_hash_churn_fn(struct aws_allocator *allocator, void *ctx) {
1026 (void)ctx;
1027
1028 int i = 0;
1029 struct aws_hash_table hash_table;
1030 const int nentries = 2 * 512 * 1024;
1031 int err_code = aws_hash_table_init(&hash_table, allocator, nentries, aws_hash_ptr, aws_ptr_eq, NULL, NULL);
1032
1033 if (AWS_ERROR_SUCCESS != err_code) {
1034 FAIL("hash table creation failed: %d", err_code);
1035 }
1036
1037 /* Probability that we deliberately try to overwrite.
1038 Note that random collisions can occur, and are not explicitly avoided. */
1039 const double pOverwrite = 0.05;
1040 const double pDelete = 0.05;
1041
1042 /* Create array of "entries" (actions we'll do to the hash table later) */
1043 struct churn_entry *entries = calloc(sizeof(*entries), nentries);
1044 for (i = 0; i < nentries; i++) {
1045 struct churn_entry *e = &entries[i];
1046 e->original_index = i;
1047
1048 int mode = 0; /* 0 = new entry, 1 = overwrite, 2 = delete */
1049
1050 if (i != 0) {
1051 double p = ((double)rand()) / RAND_MAX;
1052 if (p < pOverwrite) {
1053 mode = 1;
1054 } else if (p < pOverwrite + pDelete) {
1055 mode = 2;
1056 }
1057 }
1058
1059 e->is_removed = 0;
1060 if (mode == 0) {
1061 /* new entry: random key */
1062 e->key = (void *)(uintptr_t)rand();
1063 e->value = (void *)(uintptr_t)rand();
1064 } else if (mode == 1) {
1065 /* overwrite entry: use same key as an earlier entry */
1066 e->key = entries[(size_t)rand() % i].key; /* not evenly distributed but close enough */
1067 e->value = (void *)(uintptr_t)rand();
1068 } else if (mode == 2) {
1069 /* delete entry: use same key as an earlier entry */
1070 e->key = entries[(size_t)rand() % i].key; /* not evenly distributed but close enough */
1071 e->value = 0;
1072 e->is_removed = 1;
1073 }
1074 }
1075
1076 /* Create another array with the entries sorted by key (if tied then sort by original index) */
1077 struct churn_entry **sorted_entries = calloc(sizeof(*sorted_entries), nentries);
1078 for (i = 0; i < nentries; i++) {
1079 sorted_entries[i] = &entries[i];
1080 }
1081 qsort(sorted_entries, nentries, sizeof(*sorted_entries), s_qsort_churn_entry);
1082 for (i = 0; i < nentries; i++) {
1083 sorted_entries[i]->sorted_index = i;
1084 }
1085
1086 long start = s_timestamp();
1087
1088 /* Iterate over entries, calling aws_hash_table_remove() or aws_hash_table_create().
1089 * Consult sorted_entries to check whether an item with that key should already be in the table or not. */
1090 for (i = 0; i < nentries; i++) {
1091 if (!(i % 100000)) {
1092 printf("Put progress: %d/%d\n", i, nentries);
1093 }
1094 const struct churn_entry *e = &entries[i];
1095 int sorted_i = e->sorted_index;
1096 if (e->is_removed) {
1097 /* call aws_hash_table_remove() with this entry's key... */
1098 int was_present;
1099 err_code = aws_hash_table_remove(&hash_table, e->key, NULL, &was_present);
1100 ASSERT_SUCCESS(err_code, "Unexpected failure removing element");
1101
1102 if (sorted_i != 0 && sorted_entries[sorted_i - 1]->key == e->key) {
1103 /* if an earlier entry had same key... */
1104 if (sorted_entries[sorted_i - 1]->is_removed) {
1105 /* earlier entry was ALSO marked for deletion, so item should be missing now */
1106 ASSERT_INT_EQUALS(0, was_present, "Expected item to be missing");
1107 } else {
1108 /* earlier entry was inserted, so item should have been present */
1109 ASSERT_INT_EQUALS(1, was_present, "Expected item to be present");
1110 }
1111 } else {
1112 /* no earlier entry with same key, so no chance item was already there */
1113 ASSERT_INT_EQUALS(0, was_present, "Expected item to be missing");
1114 }
1115 } else {
1116 /* call aws_hash_table_create() with this entry's key... */
1117 struct aws_hash_element *pElem;
1118 int was_created;
1119 err_code = aws_hash_table_create(&hash_table, e->key, &pElem, &was_created);
1120 ASSERT_SUCCESS(err_code, "Unexpected failure adding element");
1121
1122 if (sorted_i != 0 && sorted_entries[sorted_i - 1]->key == e->key) {
1123 /* if an earlier entry had same key... */
1124 if (sorted_entries[sorted_i - 1]->is_removed) {
1125 /* earlier entry removed this key, so this entry should create a new one */
1126 ASSERT_INT_EQUALS(1, was_created, "Expected new item to be created");
1127 } else {
1128 /* earlier entry already added this key */
1129 ASSERT_INT_EQUALS(0, was_created, "Expected item to already be present");
1130 }
1131 } else {
1132 /* no earlier entry with same key, so no chance item was already there */
1133 ASSERT_INT_EQUALS(1, was_created, "Expected new item to be created");
1134 }
1135
1136 pElem->value = e->value;
1137 }
1138 }
1139
1140 /* Iterate over sorted_entries. For entries with a given key, the last one
1141 * reflects what should be in the hash table */
1142 for (i = 0; i < nentries; i++) {
1143 if (!(i % 100000)) {
1144 printf("Check progress: %d/%d\n", i, nentries);
1145 }
1146 const struct churn_entry *e = sorted_entries[i];
1147
1148 if (i < nentries - 1 && sorted_entries[i + 1]->key == e->key) {
1149 // overwritten on subsequent step
1150 continue;
1151 }
1152
1153 struct aws_hash_element *pElem;
1154 aws_hash_table_find(&hash_table, e->key, &pElem);
1155
1156 if (e->is_removed) {
1157 ASSERT_NULL(pElem, "expected item to be deleted");
1158 } else {
1159 ASSERT_NOT_NULL(pElem, "expected item to be present");
1160 ASSERT_PTR_EQUALS(e->value, pElem->value, "wrong value for item");
1161 }
1162 }
1163
1164 aws_hash_table_clean_up(&hash_table);
1165
1166 long end = s_timestamp();
1167
1168 free(entries);
1169 free(sorted_entries);
1170
1171 printf("elapsed=%ld us\n", end - start);
1172 return 0;
1173 }
1174
1175 AWS_TEST_CASE(test_hash_table_cleanup_idempotent, s_test_hash_table_cleanup_idempotent_fn)
1176 static int s_test_hash_table_cleanup_idempotent_fn(struct aws_allocator *allocator, void *ctx) {
1177 (void)ctx;
1178
1179 struct aws_hash_table hash_table;
1180 ASSERT_SUCCESS(
1181 aws_hash_table_init(&hash_table, allocator, 10, aws_hash_c_string, aws_hash_callback_c_str_eq, NULL, NULL));
1182
1183 aws_hash_table_clean_up(&hash_table);
1184 aws_hash_table_clean_up(&hash_table);
1185
1186 return 0;
1187 }
1188
1189 struct hash_table_entry {
1190 struct aws_allocator *allocator;
1191 struct aws_byte_cursor key;
1192 };
1193
1194 static void s_hash_table_entry_destroy(void *item) {
1195 struct hash_table_entry *entry = item;
1196 aws_mem_release(entry->allocator, entry);
1197 }
1198
1199 AWS_TEST_CASE(test_hash_table_byte_cursor_create_find, s_test_hash_table_byte_cursor_create_find_fn)
1200 static int s_test_hash_table_byte_cursor_create_find_fn(struct aws_allocator *allocator, void *ctx) {
1201 (void)ctx;
1202
1203 struct aws_hash_table hash_table;
1204 struct aws_hash_element *pElem;
1205 int was_created;
1206
1207 int ret = aws_hash_table_init(
1208 &hash_table,
1209 allocator,
1210 10,
1211 aws_hash_byte_cursor_ptr,
1212 (aws_hash_callback_eq_fn *)aws_byte_cursor_eq,
1213 NULL,
1214 s_hash_table_entry_destroy);
1215 ASSERT_SUCCESS(ret, "Hash Map init should have succeeded.");
1216
1217 /* First element of hash, both key and value are statically allocated
1218 * strings */
1219 AWS_STATIC_STRING_FROM_LITERAL(key_1_str, "tweedle dee");
1220 struct hash_table_entry *val_1 = aws_mem_acquire(allocator, sizeof(struct hash_table_entry));
1221 val_1->allocator = allocator;
1222 val_1->key = aws_byte_cursor_from_string(key_1_str);
1223
1224 /* Second element of hash, only value is dynamically allocated string */
1225 AWS_STATIC_STRING_FROM_LITERAL(key_2_str, "what's for dinner?");
1226 struct hash_table_entry *val_2 = aws_mem_acquire(allocator, sizeof(struct hash_table_entry));
1227 val_2->allocator = allocator;
1228 val_2->key = aws_byte_cursor_from_string(key_2_str);
1229
1230 /* Third element of hash, only key is dynamically allocated string */
1231 uint8_t bytes[] = {0x88, 0x00, 0xaa, 0x13, 0xb7, 0x93, 0x7f, 0xdd, 0xbb, 0x62};
1232 struct aws_string *key_3_str = aws_string_new_from_array(allocator, bytes, 10);
1233 struct hash_table_entry *val_3 = aws_mem_acquire(allocator, sizeof(struct hash_table_entry));
1234 val_3->allocator = allocator;
1235 val_3->key = aws_byte_cursor_from_string(key_3_str);
1236
1237 ret = aws_hash_table_create(&hash_table, (void *)&val_1->key, &pElem, &was_created);
1238 ASSERT_SUCCESS(ret, "Hash Map put should have succeeded.");
1239 ASSERT_INT_EQUALS(1, was_created, "Hash Map put should have created a new element.");
1240 pElem->value = (void *)val_1;
1241
1242 /* Try passing a NULL was_created this time */
1243 ret = aws_hash_table_create(&hash_table, (void *)&val_2->key, &pElem, NULL);
1244 ASSERT_SUCCESS(ret, "Hash Map put should have succeeded.");
1245 pElem->value = (void *)val_2;
1246
1247 ret = aws_hash_table_create(&hash_table, (void *)&val_3->key, &pElem, NULL);
1248 ASSERT_SUCCESS(ret, "Hash Map put should have succeeded.");
1249 pElem->value = (void *)val_3;
1250
1251 ret = aws_hash_table_find(&hash_table, (void *)&val_1->key, &pElem);
1252 ASSERT_SUCCESS(ret, "Hash Map get should have succeeded.");
1253 ASSERT_BIN_ARRAYS_EQUALS(
1254 "tweedle dee",
1255 strlen("tweedle dee"),
1256 ((struct aws_byte_cursor *)pElem->key)->ptr,
1257 ((struct aws_byte_cursor *)pElem->key)->len,
1258 "Returned key for %s, should have been %s",
1259 "tweedle dee",
1260 "tweedle dee");
1261 ASSERT_PTR_EQUALS(val_1, pElem->value);
1262
1263 ret = aws_hash_table_find(&hash_table, (void *)&val_2->key, &pElem);
1264 ASSERT_SUCCESS(ret, "Hash Map get should have succeeded.");
1265 ASSERT_BIN_ARRAYS_EQUALS(
1266 "what's for dinner?",
1267 strlen("what's for dinner?"),
1268 ((struct aws_byte_cursor *)pElem->key)->ptr,
1269 ((struct aws_byte_cursor *)pElem->key)->len,
1270 "Returned key for %s, should have been %s",
1271 "what's for dinner?",
1272 "what's for dinner?");
1273 ASSERT_PTR_EQUALS(val_2, pElem->value);
1274
1275 ret = aws_hash_table_find(&hash_table, (void *)&val_3->key, &pElem);
1276 ASSERT_SUCCESS(ret, "Hash Map get should have succeeded.");
1277 ASSERT_BIN_ARRAYS_EQUALS(
1278 bytes,
1279 10,
1280 ((struct aws_byte_cursor *)pElem->key)->ptr,
1281 ((struct aws_byte_cursor *)pElem->key)->len,
1282 "Returned key for %02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx should have been same",
1283 bytes[0],
1284 bytes[1],
1285 bytes[2],
1286 bytes[3],
1287 bytes[4],
1288 bytes[5],
1289 bytes[6],
1290 bytes[7],
1291 bytes[8],
1292 bytes[9]);
1293 ASSERT_PTR_EQUALS(val_3, pElem->value);
1294 aws_hash_table_clean_up(&hash_table);
1295
1296 aws_string_destroy(key_3_str);
1297
1298 return 0;
1299 }
1300
1301 AWS_TEST_CASE(test_hash_combine, s_test_hash_combine_fn)
1302 static int s_test_hash_combine_fn(struct aws_allocator *allocator, void *ctx) {
1303 (void)allocator;
1304 (void)ctx;
1305
1306 /* We're assuming that the underlying hashing function works well.
1307 * This test just makes sure we hooked it up right for 2 64bit values */
1308
1309 uint64_t a = 0x123456789abcdef;
1310 uint64_t b = 0xfedcba987654321;
1311 uint64_t c = aws_hash_combine(a, b);
1312
1313 /* Sanity check */
1314 ASSERT_TRUE(c != a);
1315 ASSERT_TRUE(c != b);
1316
1317 /* Same inputs gets same results, right? */
1318 ASSERT_UINT_EQUALS(c, aws_hash_combine(a, b));
1319
1320 /* Result spread across all bytes, right? */
1321 uint8_t *c_bytes = (uint8_t *)&c;
1322 for (size_t i = 0; i < sizeof(c); ++i) {
1323 ASSERT_TRUE(c_bytes[i] != 0);
1324 }
1325
1326 /* Hash should NOT be commutative */
1327 ASSERT_TRUE(aws_hash_combine(a, b) != aws_hash_combine(b, a));
1328
1329 return 0;
1330 }