@@ -29,8 +29,29 @@ static const char *TEST_VAL_STR_2 = "value 2";
29
29
#define ASSERT_HASH_TABLE_ENTRY_COUNT (map , count ) \
30
30
ASSERT_UINT_EQUALS(count, aws_hash_table_get_entry_count(map), "Hash map should have %d entries", count)
31
31
32
- AWS_TEST_CASE (test_hash_table_put_get , s_test_hash_table_put_get_fn )
33
- static int s_test_hash_table_put_get_fn (struct aws_allocator * allocator , void * ctx ) {
32
+ #define ASSERT_NO_KEY (hash_table , key ) \
33
+ do { \
34
+ AWS_STATIC_STRING_FROM_LITERAL(assert_key, key); \
35
+ struct aws_hash_element *pElem_assert; \
36
+ ASSERT_SUCCESS(aws_hash_table_find((hash_table), (void *)assert_key, &pElem_assert)); \
37
+ ASSERT_NULL(pElem_assert, "Expected key to not be present: " key); \
38
+ } while (0)
39
+
40
+ #define ASSERT_KEY_VALUE (hash_table , key , expected ) \
41
+ do { \
42
+ AWS_STATIC_STRING_FROM_LITERAL(assert_key, key); \
43
+ AWS_STATIC_STRING_FROM_LITERAL(assert_value, expected); \
44
+ struct aws_hash_element *pElem_assert; \
45
+ ASSERT_SUCCESS(aws_hash_table_find((hash_table), (void *)assert_key, &pElem_assert)); \
46
+ ASSERT_NOT_NULL(pElem_assert, "Expected key to be present: " key); \
47
+ ASSERT_TRUE( \
48
+ aws_string_eq(assert_value, (const struct aws_string *)pElem_assert->value), \
49
+ "Expected key \"" key "\" to have value \"" expected "\"; actually had value \"%s\"", \
50
+ aws_string_bytes((const struct aws_string *)pElem_assert->value)); \
51
+ } while (0)
52
+
53
+ AWS_TEST_CASE (test_hash_table_create_find , s_test_hash_table_create_find_fn )
54
+ static int s_test_hash_table_create_find_fn (struct aws_allocator * allocator , void * ctx ) {
34
55
35
56
(void )ctx ;
36
57
@@ -81,8 +102,8 @@ static int s_test_hash_table_put_get_fn(struct aws_allocator *allocator, void *c
81
102
return 0 ;
82
103
}
83
104
84
- AWS_TEST_CASE (test_hash_table_string_put_get , s_test_hash_table_string_put_get_fn )
85
- static int s_test_hash_table_string_put_get_fn (struct aws_allocator * allocator , void * ctx ) {
105
+ AWS_TEST_CASE (test_hash_table_string_create_find , s_test_hash_table_string_create_find_fn )
106
+ static int s_test_hash_table_string_create_find_fn (struct aws_allocator * allocator , void * ctx ) {
86
107
(void )ctx ;
87
108
88
109
struct aws_hash_table hash_table ;
@@ -189,6 +210,86 @@ static int s_test_hash_table_string_put_get_fn(struct aws_allocator *allocator,
189
210
return 0 ;
190
211
}
191
212
213
+ static const void * last_key , * last_value ;
214
+
215
+ static void destroy_key_record (void * key ) {
216
+ last_key = key ;
217
+ }
218
+
219
+ static void destroy_value_record (void * value ) {
220
+ last_value = value ;
221
+ }
222
+
223
+ AWS_TEST_CASE (test_hash_table_put , s_test_hash_table_put_fn )
224
+ static int s_test_hash_table_put_fn (struct aws_allocator * allocator , void * ctx ) {
225
+ (void )ctx ;
226
+
227
+ struct aws_hash_table hash_table ;
228
+ struct aws_hash_element * pElem ;
229
+ int was_created ;
230
+
231
+ int ret = aws_hash_table_init (
232
+ & hash_table , allocator , 10 , aws_hash_string , aws_string_eq , destroy_key_record , destroy_value_record );
233
+ ASSERT_SUCCESS (ret , "Hash Map init should have succeeded." );
234
+
235
+ AWS_STATIC_STRING_FROM_LITERAL (sentinel , "" );
236
+ AWS_STATIC_STRING_FROM_LITERAL (key_a_1 , "a" );
237
+ AWS_STATIC_STRING_FROM_LITERAL (value_b_1 , "b" );
238
+
239
+ ASSERT_NO_KEY (& hash_table , "a" );
240
+ last_key = last_value = sentinel ;
241
+ aws_hash_table_put (& hash_table , key_a_1 , (void * )value_b_1 , & was_created );
242
+ ASSERT_INT_EQUALS (was_created , 1 );
243
+ ASSERT_KEY_VALUE (& hash_table , "a" , "b" );
244
+ /* dtors were not called, even with nulls */
245
+ ASSERT_PTR_EQUALS (last_key , sentinel );
246
+ ASSERT_PTR_EQUALS (last_value , sentinel );
247
+
248
+ AWS_STATIC_STRING_FROM_LITERAL (key_a_2 , "a" );
249
+ AWS_STATIC_STRING_FROM_LITERAL (value_c_1 , "c" );
250
+
251
+ last_key = last_value = NULL ;
252
+ aws_hash_table_put (& hash_table , key_a_2 , (void * )value_c_1 , & was_created );
253
+ ASSERT_INT_EQUALS (was_created , 0 );
254
+ ASSERT_KEY_VALUE (& hash_table , "a" , "c" );
255
+
256
+ ASSERT_SUCCESS (aws_hash_table_find (& hash_table , (void * )key_a_1 , & pElem ));
257
+ ASSERT_PTR_EQUALS (key_a_2 , pElem -> key );
258
+ /* verify dtor was called on the old key ptr */
259
+ ASSERT_PTR_EQUALS (last_key , key_a_1 );
260
+ ASSERT_PTR_EQUALS (last_value , value_b_1 );
261
+
262
+ last_key = last_value = NULL ;
263
+ aws_hash_table_put (& hash_table , key_a_2 , (void * )value_b_1 , NULL );
264
+ ASSERT_KEY_VALUE (& hash_table , "a" , "b" );
265
+
266
+ /* Since the key ptr did not change, it was not destroyed */
267
+ ASSERT_PTR_EQUALS (last_key , NULL );
268
+ /* The value was destroyed however */
269
+ ASSERT_PTR_EQUALS (last_value , value_c_1 );
270
+
271
+ aws_hash_table_clean_up (& hash_table );
272
+
273
+ return 0 ;
274
+ }
275
+ AWS_TEST_CASE (test_hash_table_put_null_dtor , s_test_hash_table_put_null_dtor_fn )
276
+ static int s_test_hash_table_put_null_dtor_fn (struct aws_allocator * allocator , void * ctx ) {
277
+ (void )ctx ;
278
+
279
+ struct aws_hash_table hash_table ;
280
+
281
+ int ret = aws_hash_table_init (& hash_table , allocator , 10 , aws_hash_string , aws_string_eq , NULL , NULL );
282
+ ASSERT_SUCCESS (ret , "Hash Map init should have succeeded." );
283
+
284
+ AWS_STATIC_STRING_FROM_LITERAL (foo , "foo" );
285
+ ASSERT_SUCCESS (aws_hash_table_put (& hash_table , foo , (void * )foo , NULL ));
286
+ ASSERT_SUCCESS (aws_hash_table_put (& hash_table , foo , (void * )foo , NULL ));
287
+
288
+ aws_hash_table_clean_up (& hash_table );
289
+
290
+ return 0 ;
291
+ }
292
+
192
293
AWS_TEST_CASE (test_hash_table_string_clean_up , s_test_hash_table_string_clean_up_fn )
193
294
static int s_test_hash_table_string_clean_up_fn (struct aws_allocator * allocator , void * ctx ) {
194
295
0 commit comments