| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#include <aws/common/linked_hash_table.h> |
| 17 |
#include <aws/common/string.h> |
| 18 |
|
| 19 |
#include <aws/testing/aws_test_harness.h> |
| 20 |
|
| 21 |
static int s_test_linked_hash_table_preserves_insertion_order_fn(struct aws_allocator *allocator, void *ctx) { |
| 22 |
(void)ctx; |
| 23 |
|
| 24 |
struct aws_linked_hash_table table; |
| 25 |
|
| 26 |
ASSERT_SUCCESS( |
| 27 |
aws_linked_hash_table_init(&table, allocator, aws_hash_c_string, aws_hash_callback_c_str_eq, NULL, NULL, 3)); |
| 28 |
|
| 29 |
const char *first_key = "first"; |
| 30 |
const char *second_key = "second"; |
| 31 |
const char *third_key = "third"; |
| 32 |
const char *fourth_key = "fourth"; |
| 33 |
|
| 34 |
int first = 1; |
| 35 |
int second = 2; |
| 36 |
int third = 3; |
| 37 |
int fourth = 4; |
| 38 |
|
| 39 |
ASSERT_SUCCESS(aws_linked_hash_table_put(&table, first_key, &first)); |
| 40 |
ASSERT_SUCCESS(aws_linked_hash_table_put(&table, second_key, &second)); |
| 41 |
ASSERT_SUCCESS(aws_linked_hash_table_put(&table, third_key, &third)); |
| 42 |
ASSERT_SUCCESS(aws_linked_hash_table_put(&table, fourth_key, &fourth)); |
| 43 |
|
| 44 |
ASSERT_INT_EQUALS(4, aws_linked_hash_table_get_element_count(&table)); |
| 45 |
|
| 46 |
int *value = NULL; |
| 47 |
ASSERT_SUCCESS(aws_linked_hash_table_find(&table, first_key, (void **)&value)); |
| 48 |
ASSERT_NOT_NULL(value); |
| 49 |
ASSERT_INT_EQUALS(first, *value); |
| 50 |
|
| 51 |
ASSERT_SUCCESS(aws_linked_hash_table_find(&table, second_key, (void **)&value)); |
| 52 |
ASSERT_NOT_NULL(value); |
| 53 |
ASSERT_INT_EQUALS(second, *value); |
| 54 |
|
| 55 |
ASSERT_SUCCESS(aws_linked_hash_table_find(&table, third_key, (void **)&value)); |
| 56 |
ASSERT_NOT_NULL(value); |
| 57 |
ASSERT_INT_EQUALS(third, *value); |
| 58 |
|
| 59 |
ASSERT_SUCCESS(aws_linked_hash_table_find(&table, fourth_key, (void **)&value)); |
| 60 |
ASSERT_NOT_NULL(value); |
| 61 |
ASSERT_INT_EQUALS(fourth, *value); |
| 62 |
|
| 63 |
const struct aws_linked_list *list = aws_linked_hash_table_get_iteration_list(&table); |
| 64 |
ASSERT_NOT_NULL(list); |
| 65 |
|
| 66 |
struct aws_linked_list_node *node = aws_linked_list_front(list); |
| 67 |
|
| 68 |
struct aws_linked_hash_table_node *table_node = AWS_CONTAINER_OF(node, struct aws_linked_hash_table_node, node); |
| 69 |
ASSERT_INT_EQUALS(first, *(int *)table_node->value); |
| 70 |
|
| 71 |
node = aws_linked_list_next(node); |
| 72 |
ASSERT_NOT_NULL(node); |
| 73 |
table_node = AWS_CONTAINER_OF(node, struct aws_linked_hash_table_node, node); |
| 74 |
ASSERT_INT_EQUALS(second, *(int *)table_node->value); |
| 75 |
|
| 76 |
node = aws_linked_list_next(node); |
| 77 |
ASSERT_NOT_NULL(node); |
| 78 |
table_node = AWS_CONTAINER_OF(node, struct aws_linked_hash_table_node, node); |
| 79 |
ASSERT_INT_EQUALS(third, *(int *)table_node->value); |
| 80 |
|
| 81 |
node = aws_linked_list_next(node); |
| 82 |
ASSERT_NOT_NULL(node); |
| 83 |
table_node = AWS_CONTAINER_OF(node, struct aws_linked_hash_table_node, node); |
| 84 |
ASSERT_INT_EQUALS(fourth, *(int *)table_node->value); |
| 85 |
|
| 86 |
node = aws_linked_list_next(node); |
| 87 |
ASSERT_PTR_EQUALS(aws_linked_list_end(list), node); |
| 88 |
|
| 89 |
aws_linked_hash_table_clean_up(&table); |
| 90 |
return 0; |
| 91 |
} |
| 92 |
|
| 93 |
AWS_TEST_CASE(test_linked_hash_table_preserves_insertion_order, s_test_linked_hash_table_preserves_insertion_order_fn) |
| 94 |
|
| 95 |
struct linked_hash_table_test_value_element { |
| 96 |
bool value_removed; |
| 97 |
}; |
| 98 |
|
| 99 |
static void s_linked_hash_table_element_value_destroy(void *value) { |
| 100 |
struct linked_hash_table_test_value_element *value_element = value; |
| 101 |
value_element->value_removed = true; |
| 102 |
} |
| 103 |
|
| 104 |
static int s_test_linked_hash_table_entries_cleanup_fn(struct aws_allocator *allocator, void *ctx) { |
| 105 |
(void)ctx; |
| 106 |
|
| 107 |
struct aws_linked_hash_table table; |
| 108 |
|
| 109 |
ASSERT_SUCCESS(aws_linked_hash_table_init( |
| 110 |
&table, |
| 111 |
allocator, |
| 112 |
aws_hash_c_string, |
| 113 |
aws_hash_callback_c_str_eq, |
| 114 |
NULL, |
| 115 |
s_linked_hash_table_element_value_destroy, |
| 116 |
2)); |
| 117 |
|
| 118 |
const char *first_key = "first"; |
| 119 |
const char *second_key = "second"; |
| 120 |
|
| 121 |
struct linked_hash_table_test_value_element first = {.value_removed = false}; |
| 122 |
struct linked_hash_table_test_value_element second = {.value_removed = false}; |
| 123 |
|
| 124 |
ASSERT_SUCCESS(aws_linked_hash_table_put(&table, first_key, &first)); |
| 125 |
ASSERT_SUCCESS(aws_linked_hash_table_put(&table, second_key, &second)); |
| 126 |
ASSERT_INT_EQUALS(2, aws_linked_hash_table_get_element_count(&table)); |
| 127 |
|
| 128 |
ASSERT_SUCCESS(aws_linked_hash_table_remove(&table, second_key)); |
| 129 |
|
| 130 |
ASSERT_TRUE(second.value_removed); |
| 131 |
ASSERT_INT_EQUALS(1, aws_linked_hash_table_get_element_count(&table)); |
| 132 |
|
| 133 |
aws_linked_hash_table_clear(&table); |
| 134 |
ASSERT_INT_EQUALS(0, aws_linked_hash_table_get_element_count(&table)); |
| 135 |
|
| 136 |
ASSERT_TRUE(first.value_removed); |
| 137 |
ASSERT_TRUE(aws_linked_list_empty(aws_linked_hash_table_get_iteration_list(&table))); |
| 138 |
|
| 139 |
aws_linked_hash_table_clean_up(&table); |
| 140 |
return 0; |
| 141 |
} |
| 142 |
|
| 143 |
AWS_TEST_CASE(test_linked_hash_table_entries_cleanup, s_test_linked_hash_table_entries_cleanup_fn) |
| 144 |
|
| 145 |
static int s_test_linked_hash_table_entries_overwrite_fn(struct aws_allocator *allocator, void *ctx) { |
| 146 |
(void)ctx; |
| 147 |
|
| 148 |
struct aws_linked_hash_table table; |
| 149 |
|
| 150 |
ASSERT_SUCCESS(aws_linked_hash_table_init( |
| 151 |
&table, |
| 152 |
allocator, |
| 153 |
aws_hash_c_string, |
| 154 |
aws_hash_callback_c_str_eq, |
| 155 |
NULL, |
| 156 |
s_linked_hash_table_element_value_destroy, |
| 157 |
2)); |
| 158 |
|
| 159 |
const char *first_key = "first"; |
| 160 |
|
| 161 |
struct linked_hash_table_test_value_element first = {.value_removed = false}; |
| 162 |
struct linked_hash_table_test_value_element second = {.value_removed = false}; |
| 163 |
|
| 164 |
ASSERT_SUCCESS(aws_linked_hash_table_put(&table, first_key, &first)); |
| 165 |
ASSERT_SUCCESS(aws_linked_hash_table_put(&table, first_key, &second)); |
| 166 |
ASSERT_INT_EQUALS(1, aws_linked_hash_table_get_element_count(&table)); |
| 167 |
|
| 168 |
ASSERT_TRUE(first.value_removed); |
| 169 |
ASSERT_FALSE(second.value_removed); |
| 170 |
|
| 171 |
struct linked_hash_table_test_value_element *value = NULL; |
| 172 |
ASSERT_SUCCESS(aws_linked_hash_table_find(&table, first_key, (void **)&value)); |
| 173 |
ASSERT_NOT_NULL(value); |
| 174 |
ASSERT_PTR_EQUALS(&second, value); |
| 175 |
|
| 176 |
aws_linked_hash_table_clean_up(&table); |
| 177 |
return 0; |
| 178 |
} |
| 179 |
|
| 180 |
AWS_TEST_CASE(test_linked_hash_table_entries_overwrite, s_test_linked_hash_table_entries_overwrite_fn) |
| 181 |
|
| 182 |
static int s_test_linked_hash_table_entries_overwrite_reference_unequal_fn(struct aws_allocator *allocator, void *ctx) { |
| 183 |
(void)ctx; |
| 184 |
|
| 185 |
struct aws_linked_hash_table table; |
| 186 |
|
| 187 |
ASSERT_SUCCESS(aws_linked_hash_table_init( |
| 188 |
&table, |
| 189 |
allocator, |
| 190 |
aws_hash_string, |
| 191 |
aws_hash_callback_string_eq, |
| 192 |
aws_hash_callback_string_destroy, |
| 193 |
s_linked_hash_table_element_value_destroy, |
| 194 |
2)); |
| 195 |
|
| 196 |
struct aws_string *first_key = aws_string_new_from_c_str(allocator, "first"); |
| 197 |
struct aws_string *first_key_v2 = aws_string_new_from_c_str(allocator, "first"); |
| 198 |
|
| 199 |
struct linked_hash_table_test_value_element first = {.value_removed = false}; |
| 200 |
struct linked_hash_table_test_value_element second = {.value_removed = false}; |
| 201 |
|
| 202 |
ASSERT_SUCCESS(aws_linked_hash_table_put(&table, first_key, &first)); |
| 203 |
ASSERT_SUCCESS(aws_linked_hash_table_put(&table, first_key_v2, &second)); |
| 204 |
ASSERT_INT_EQUALS(1, aws_linked_hash_table_get_element_count(&table)); |
| 205 |
|
| 206 |
ASSERT_TRUE(first.value_removed); |
| 207 |
ASSERT_FALSE(second.value_removed); |
| 208 |
|
| 209 |
struct linked_hash_table_test_value_element *value = NULL; |
| 210 |
struct aws_string *first_key_v3 = aws_string_new_from_c_str(allocator, "first"); |
| 211 |
ASSERT_SUCCESS(aws_linked_hash_table_find(&table, first_key_v3, (void **)&value)); |
| 212 |
ASSERT_NOT_NULL(value); |
| 213 |
ASSERT_PTR_EQUALS(&second, value); |
| 214 |
|
| 215 |
aws_linked_hash_table_clean_up(&table); |
| 216 |
|
| 217 |
aws_string_destroy(first_key_v3); |
| 218 |
|
| 219 |
return 0; |
| 220 |
} |
| 221 |
|
| 222 |
AWS_TEST_CASE( |
| 223 |
test_linked_hash_table_entries_overwrite_reference_unequal, |
| 224 |
s_test_linked_hash_table_entries_overwrite_reference_unequal_fn) |
| 225 |
|
| 226 |
struct backed_cursor_element { |
| 227 |
struct aws_byte_cursor name_cursor; |
| 228 |
struct aws_byte_buf name; |
| 229 |
uint32_t value; |
| 230 |
struct aws_allocator *allocator; |
| 231 |
}; |
| 232 |
|
| 233 |
static void s_backed_cursor_element_value_destroy(void *value) { |
| 234 |
struct backed_cursor_element *element = value; |
| 235 |
|
| 236 |
if (element == NULL) { |
| 237 |
return; |
| 238 |
} |
| 239 |
|
| 240 |
aws_byte_buf_clean_up(&element->name); |
| 241 |
|
| 242 |
aws_mem_release(element->allocator, element); |
| 243 |
} |
| 244 |
|
| 245 |
static struct backed_cursor_element *s_backed_cursor_element_new( |
| 246 |
struct aws_allocator *allocator, |
| 247 |
struct aws_byte_cursor name, |
| 248 |
uint32_t value) { |
| 249 |
struct backed_cursor_element *element = aws_mem_calloc(allocator, 1, sizeof(struct backed_cursor_element)); |
| 250 |
|
| 251 |
element->allocator = allocator; |
| 252 |
element->value = value; |
| 253 |
|
| 254 |
if (aws_byte_buf_init_copy_from_cursor(&element->name, allocator, name)) { |
| 255 |
goto on_error; |
| 256 |
} |
| 257 |
|
| 258 |
element->name_cursor = aws_byte_cursor_from_buf(&element->name); |
| 259 |
|
| 260 |
return element; |
| 261 |
|
| 262 |
on_error: |
| 263 |
|
| 264 |
s_backed_cursor_element_value_destroy(element); |
| 265 |
|
| 266 |
return NULL; |
| 267 |
} |
| 268 |
|
| 269 |
static bool s_cursor_hash_equality_fn(const void *a, const void *b) { |
| 270 |
const struct aws_byte_cursor *a_cursor = a; |
| 271 |
const struct aws_byte_cursor *b_cursor = b; |
| 272 |
|
| 273 |
return aws_byte_cursor_eq(a_cursor, b_cursor); |
| 274 |
} |
| 275 |
|
| 276 |
static int s_test_linked_hash_table_entries_overwrite_backed_cursor_fn(struct aws_allocator *allocator, void *ctx) { |
| 277 |
(void)ctx; |
| 278 |
|
| 279 |
struct aws_linked_hash_table table; |
| 280 |
|
| 281 |
ASSERT_SUCCESS(aws_linked_hash_table_init( |
| 282 |
&table, |
| 283 |
allocator, |
| 284 |
aws_hash_byte_cursor_ptr, |
| 285 |
s_cursor_hash_equality_fn, |
| 286 |
NULL, |
| 287 |
s_backed_cursor_element_value_destroy, |
| 288 |
2)); |
| 289 |
|
| 290 |
struct aws_byte_cursor shared_name = aws_byte_cursor_from_c_str("Hello"); |
| 291 |
struct backed_cursor_element *element1 = s_backed_cursor_element_new(allocator, shared_name, 1); |
| 292 |
ASSERT_SUCCESS(aws_linked_hash_table_put(&table, &element1->name_cursor, element1)); |
| 293 |
|
| 294 |
struct backed_cursor_element *element1_prime = s_backed_cursor_element_new(allocator, shared_name, 2); |
| 295 |
ASSERT_SUCCESS(aws_linked_hash_table_put(&table, &element1_prime->name_cursor, element1_prime)); |
| 296 |
|
| 297 |
ASSERT_INT_EQUALS(1, aws_linked_hash_table_get_element_count(&table)); |
| 298 |
|
| 299 |
void *element = NULL; |
| 300 |
ASSERT_SUCCESS(aws_linked_hash_table_find(&table, &shared_name, (void **)&element)); |
| 301 |
|
| 302 |
ASSERT_NOT_NULL(element); |
| 303 |
struct backed_cursor_element *found_element = element; |
| 304 |
ASSERT_INT_EQUALS(2, found_element->value); |
| 305 |
|
| 306 |
aws_linked_hash_table_clean_up(&table); |
| 307 |
|
| 308 |
return 0; |
| 309 |
} |
| 310 |
|
| 311 |
AWS_TEST_CASE( |
| 312 |
test_linked_hash_table_entries_overwrite_backed_cursor, |
| 313 |
s_test_linked_hash_table_entries_overwrite_backed_cursor_fn) |