Debian Codegraph @

crt/aws-c-http/tests/test_random_access_set.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/string.h>
7 #include <aws/http/private/random_access_set.h>
8
9 #include <aws/testing/aws_test_harness.h>
10
11 static uint64_t s_hash_string_ptr(const void *item) {
12 const struct aws_string *str = *(const struct aws_string **)item;
13 return aws_hash_string((void *)str);
14 }
15
16 static bool s_hash_string_ptr_eq(const void *a, const void *b) {
17 const struct aws_string *str_a = *(const struct aws_string **)a;
18 const struct aws_string *str_b = *(const struct aws_string **)b;
19 return aws_string_eq(str_a, str_b);
20 }
21
22 static int s_random_access_set_sanitize_fn(struct aws_allocator *allocator, void *ctx) {
23 (void)ctx;
24
25 struct aws_random_access_set list_with_map;
26 ASSERT_SUCCESS(aws_random_access_set_init(&list_with_map, allocator, s_hash_string_ptr, aws_ptr_eq, NULL, 0));
27 aws_random_access_set_clean_up(&list_with_map);
28 return AWS_OP_SUCCESS;
29 }
30
31 AWS_TEST_CASE(random_access_set_sanitize, s_random_access_set_sanitize_fn)
32
33 static int s_random_access_set_insert_fn(struct aws_allocator *allocator, void *ctx) {
34 (void)ctx;
35 AWS_STATIC_STRING_FROM_LITERAL(foo, "foo");
36 AWS_STATIC_STRING_FROM_LITERAL(bar, "bar");
37 AWS_STATIC_STRING_FROM_LITERAL(foobar, "foobar");
38
39 struct aws_random_access_set list_with_map;
40 /* With only 1 initial element. */
41 ASSERT_SUCCESS(
42 aws_random_access_set_init(&list_with_map, allocator, s_hash_string_ptr, s_hash_string_ptr_eq, NULL, 1));
43 bool added = true;
44 ASSERT_SUCCESS(aws_random_access_set_add(&list_with_map, &foobar, &added));
45 ASSERT_TRUE(added);
46 ASSERT_SUCCESS(aws_random_access_set_add(&list_with_map, &bar, &added));
47 ASSERT_TRUE(added);
48 ASSERT_SUCCESS(aws_random_access_set_add(&list_with_map, &foo, &added));
49 ASSERT_TRUE(added);
50
51 /* You cannot have duplicates */
52 ASSERT_SUCCESS(aws_random_access_set_add(&list_with_map, &foobar, &added));
53 ASSERT_FALSE(added);
54
55 /* Check the size */
56 ASSERT_UINT_EQUALS(aws_random_access_set_get_size(&list_with_map), 3);
57
58 aws_random_access_set_clean_up(&list_with_map);
59 return AWS_OP_SUCCESS;
60 }
61
62 AWS_TEST_CASE(random_access_set_insert, s_random_access_set_insert_fn)
63
64 static int s_random_access_set_get_random_fn(struct aws_allocator *allocator, void *ctx) {
65 (void)ctx;
66 AWS_STATIC_STRING_FROM_LITERAL(foo, "foo");
67
68 struct aws_random_access_set list_with_map;
69 /* Insert a pointer of pointer of string to the structure */
70 ASSERT_SUCCESS(
71 aws_random_access_set_init(&list_with_map, allocator, s_hash_string_ptr, s_hash_string_ptr_eq, NULL, 1));
72 /* Get the pointer of pointer to the string from the struct */
73 struct aws_string **left_element = NULL;
74 /* Fail to get any, when there is nothing in it. */
75 ASSERT_FAILS(aws_random_access_set_random_get_ptr(&list_with_map, (void **)&left_element));
76 bool added = false;
77 ASSERT_SUCCESS(aws_random_access_set_add(&list_with_map, &foo, &added));
78 ASSERT_TRUE(added);
79
80 /* Check the size */
81 ASSERT_UINT_EQUALS(aws_random_access_set_get_size(&list_with_map), 1);
82 ASSERT_SUCCESS(aws_random_access_set_random_get_ptr(&list_with_map, (void **)&left_element));
83 ASSERT_TRUE(aws_string_eq(*left_element, foo));
84
85 aws_random_access_set_clean_up(&list_with_map);
86 return AWS_OP_SUCCESS;
87 }
88
89 AWS_TEST_CASE(random_access_set_get_random, s_random_access_set_get_random_fn)
90
91 static int s_random_access_set_exist_fn(struct aws_allocator *allocator, void *ctx) {
92 (void)ctx;
93 AWS_STATIC_STRING_FROM_LITERAL(foo, "foo");
94 AWS_STATIC_STRING_FROM_LITERAL(bar, "bar");
95
96 struct aws_random_access_set list_with_map;
97 ASSERT_SUCCESS(
98 aws_random_access_set_init(&list_with_map, allocator, s_hash_string_ptr, s_hash_string_ptr_eq, NULL, 1));
99 bool added = false;
100 ASSERT_SUCCESS(aws_random_access_set_add(&list_with_map, &foo, &added));
101 ASSERT_TRUE(added);
102
103 bool exist = false;
104 ASSERT_SUCCESS(aws_random_access_set_exist(&list_with_map, &foo, &exist));
105 ASSERT_TRUE(exist);
106
107 ASSERT_SUCCESS(aws_random_access_set_exist(&list_with_map, &bar, &exist));
108 ASSERT_FALSE(exist);
109
110 aws_random_access_set_clean_up(&list_with_map);
111 return AWS_OP_SUCCESS;
112 }
113
114 AWS_TEST_CASE(random_access_set_exist, s_random_access_set_exist_fn)
115
116 static int s_random_access_set_remove_fn(struct aws_allocator *allocator, void *ctx) {
117 (void)ctx;
118 (void)ctx;
119 AWS_STATIC_STRING_FROM_LITERAL(foo, "foo");
120 AWS_STATIC_STRING_FROM_LITERAL(bar, "bar");
121 AWS_STATIC_STRING_FROM_LITERAL(foobar, "foobar");
122
123 struct aws_random_access_set list_with_map;
124 /* With only 1 initial element. */
125 ASSERT_SUCCESS(
126 aws_random_access_set_init(&list_with_map, allocator, aws_hash_string, aws_hash_callback_string_eq, NULL, 1));
127 bool added = false;
128 ASSERT_SUCCESS(aws_random_access_set_add(&list_with_map, bar, &added));
129 ASSERT_TRUE(added);
130 ASSERT_SUCCESS(aws_random_access_set_add(&list_with_map, foobar, &added));
131 ASSERT_TRUE(added);
132 ASSERT_SUCCESS(aws_random_access_set_add(&list_with_map, foo, &added));
133 ASSERT_TRUE(added);
134
135 ASSERT_SUCCESS(aws_random_access_set_remove(&list_with_map, foo));
136 /* Check the size */
137 ASSERT_UINT_EQUALS(aws_random_access_set_get_size(&list_with_map), 2);
138
139 /* Should success and do nothing */
140 ASSERT_SUCCESS(aws_random_access_set_remove(&list_with_map, foo));
141
142 /* Remove all beside foobar, so, if we get one random, it will be foobar */
143 ASSERT_SUCCESS(aws_random_access_set_remove(&list_with_map, bar));
144 ASSERT_UINT_EQUALS(aws_random_access_set_get_size(&list_with_map), 1);
145 struct aws_string *left_element = NULL;
146 ASSERT_SUCCESS(aws_random_access_set_random_get_ptr(&list_with_map, (void **)&left_element));
147 ASSERT_TRUE(aws_string_eq(left_element, foobar));
148
149 /* Remove last thing and make sure everything should still work */
150 ASSERT_SUCCESS(aws_random_access_set_remove(&list_with_map, foobar));
151 ASSERT_UINT_EQUALS(aws_random_access_set_get_size(&list_with_map), 0);
152 ASSERT_SUCCESS(aws_random_access_set_add(&list_with_map, foo, &added));
153 ASSERT_TRUE(added);
154 ASSERT_UINT_EQUALS(aws_random_access_set_get_size(&list_with_map), 1);
155 ASSERT_SUCCESS(aws_random_access_set_random_get_ptr(&list_with_map, (void **)&left_element));
156 ASSERT_TRUE(aws_string_eq(left_element, foo));
157
158 aws_random_access_set_clean_up(&list_with_map);
159 return AWS_OP_SUCCESS;
160 }
161
162 AWS_TEST_CASE(random_access_set_remove, s_random_access_set_remove_fn)
163
164 static void s_aws_string_destroy_callback(void *key) {
165 struct aws_string *str = *(struct aws_string **)key;
166 aws_string_destroy(str);
167 }
168
169 static int s_random_access_set_owns_element_fn(struct aws_allocator *allocator, void *ctx) {
170 (void)ctx;
171 /* If we copy the aws string itself, the underlying data will be copied as a pointer, if the data is
172 * less than the size of a pointer, we will be fine. The long string will test against it. */
173 struct aws_string *foo = aws_string_new_from_c_str(allocator, "foo123456");
174 struct aws_string *bar = aws_string_new_from_c_str(allocator, "bar7894156132121");
175 struct aws_string *foobar = aws_string_new_from_c_str(allocator, "foobar970712389709123");
176
177 struct aws_random_access_set list_with_map;
178 /* With only 1 initial element. Add clean up for the string */
179 ASSERT_SUCCESS(aws_random_access_set_init(
180 &list_with_map, allocator, s_hash_string_ptr, s_hash_string_ptr_eq, s_aws_string_destroy_callback, 1));
181 bool added = false;
182 ASSERT_SUCCESS(aws_random_access_set_add(&list_with_map, &foobar, &added));
183 ASSERT_TRUE(added);
184 ASSERT_SUCCESS(aws_random_access_set_add(&list_with_map, &bar, &added));
185 ASSERT_TRUE(added);
186 ASSERT_SUCCESS(aws_random_access_set_add(&list_with_map, &foo, &added));
187 ASSERT_TRUE(added);
188
189 /* You cannot have duplicates */
190 ASSERT_SUCCESS(aws_random_access_set_add(&list_with_map, &foobar, &added));
191 ASSERT_FALSE(added);
192
193 ASSERT_SUCCESS(aws_random_access_set_remove(&list_with_map, &foo));
194 ASSERT_SUCCESS(aws_random_access_set_remove(&list_with_map, &foobar));
195
196 /* Check the size */
197 ASSERT_UINT_EQUALS(aws_random_access_set_get_size(&list_with_map), 1);
198
199 aws_random_access_set_clean_up(&list_with_map);
200 return AWS_OP_SUCCESS;
201 }
202
203 AWS_TEST_CASE(random_access_set_owns_element, s_random_access_set_owns_element_fn)