Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright 2010-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License").
5 * You may not use this file except in compliance with the License.
6 * A copy of the License is located at
7 *
8 * http://aws.amazon.com/apache2.0
9 *
10 * or in the "license" file accompanying this file. This file is distributed
11 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12 * express or implied. See the License for the specific language governing
13 * permissions and limitations under the License.
14 */
15
16 #include <aws/common/fifo_cache.h>
17 #include <aws/common/lifo_cache.h>
18 #include <aws/common/lru_cache.h>
19 #include <aws/testing/aws_test_harness.h>
20
21 static int s_test_lru_cache_overflow_static_members_fn(struct aws_allocator *allocator, void *ctx) {
22 (void)ctx;
23
24 struct aws_cache *lru_cache =
25 aws_cache_new_lru(allocator, aws_hash_c_string, aws_hash_callback_c_str_eq, NULL, NULL, 3);
26 ASSERT_NOT_NULL(lru_cache);
27
28 const char *first_key = "first";
29 const char *second_key = "second";
30 const char *third_key = "third";
31 const char *fourth_key = "fourth";
32
33 int first = 1;
34 int second = 2;
35 int third = 3;
36 int fourth = 4;
37
38 ASSERT_SUCCESS(aws_cache_put(lru_cache, first_key, &first));
39 ASSERT_SUCCESS(aws_cache_put(lru_cache, second_key, &second));
40 ASSERT_SUCCESS(aws_cache_put(lru_cache, third_key, &third));
41 ASSERT_INT_EQUALS(3, aws_cache_get_element_count(lru_cache));
42
43 int *value = NULL;
44 ASSERT_SUCCESS(aws_cache_find(lru_cache, first_key, (void **)&value));
45 ASSERT_NOT_NULL(value);
46 ASSERT_INT_EQUALS(first, *value);
47
48 ASSERT_SUCCESS(aws_cache_find(lru_cache, second_key, (void **)&value));
49 ASSERT_NOT_NULL(value);
50 ASSERT_INT_EQUALS(second, *value);
51
52 ASSERT_SUCCESS(aws_cache_find(lru_cache, third_key, (void **)&value));
53 ASSERT_NOT_NULL(value);
54 ASSERT_INT_EQUALS(third, *value);
55
56 ASSERT_SUCCESS(aws_cache_put(lru_cache, fourth_key, (void **)&fourth));
57
58 /* make sure the oldest entry was purged. Note, value should now be NULL but
59 * the call succeeds. */
60 ASSERT_SUCCESS(aws_cache_find(lru_cache, first_key, (void **)&value));
61 ASSERT_NULL(value);
62
63 ASSERT_SUCCESS(aws_cache_find(lru_cache, second_key, (void **)&value));
64 ASSERT_NOT_NULL(value);
65 ASSERT_INT_EQUALS(second, *value);
66
67 ASSERT_SUCCESS(aws_cache_find(lru_cache, third_key, (void **)&value));
68 ASSERT_NOT_NULL(value);
69 ASSERT_INT_EQUALS(third, *value);
70
71 ASSERT_SUCCESS(aws_cache_find(lru_cache, fourth_key, (void **)&value));
72 ASSERT_NOT_NULL(value);
73 ASSERT_INT_EQUALS(fourth, *value);
74
75 aws_cache_destroy(lru_cache);
76 return 0;
77 }
78
79 AWS_TEST_CASE(test_lru_cache_overflow_static_members, s_test_lru_cache_overflow_static_members_fn)
80
81 static int s_test_lru_cache_lru_ness_static_members_fn(struct aws_allocator *allocator, void *ctx) {
82 (void)ctx;
83
84 struct aws_cache *lru_cache =
85 aws_cache_new_lru(allocator, aws_hash_c_string, aws_hash_callback_c_str_eq, NULL, NULL, 3);
86 ASSERT_NOT_NULL(lru_cache);
87
88 const char *first_key = "first";
89 const char *second_key = "second";
90 const char *third_key = "third";
91 const char *fourth_key = "fourth";
92
93 int first = 1;
94 int second = 2;
95 int third = 3;
96 int fourth = 4;
97
98 ASSERT_SUCCESS(aws_cache_put(lru_cache, first_key, &first));
99 ASSERT_SUCCESS(aws_cache_put(lru_cache, second_key, &second));
100 ASSERT_SUCCESS(aws_cache_put(lru_cache, third_key, &third));
101 ASSERT_INT_EQUALS(3, aws_cache_get_element_count(lru_cache));
102
103 int *value = NULL;
104 ASSERT_SUCCESS(aws_cache_find(lru_cache, first_key, (void **)&value));
105 ASSERT_NOT_NULL(value);
106 ASSERT_INT_EQUALS(first, *value);
107
108 ASSERT_SUCCESS(aws_cache_find(lru_cache, second_key, (void **)&value));
109 ASSERT_NOT_NULL(value);
110 ASSERT_INT_EQUALS(second, *value);
111
112 ASSERT_SUCCESS(aws_cache_put(lru_cache, fourth_key, (void **)&fourth));
113
114 /* The third element is the LRU element (see above). Note, value should now
115 * be NULL but the call succeeds. */
116 ASSERT_SUCCESS(aws_cache_find(lru_cache, third_key, (void **)&value));
117 ASSERT_NULL(value);
118
119 ASSERT_SUCCESS(aws_cache_find(lru_cache, first_key, (void **)&value));
120 ASSERT_NOT_NULL(value);
121 ASSERT_INT_EQUALS(first, *value);
122
123 ASSERT_SUCCESS(aws_cache_find(lru_cache, second_key, (void **)&value));
124 ASSERT_NOT_NULL(value);
125 ASSERT_INT_EQUALS(second, *value);
126
127 ASSERT_SUCCESS(aws_cache_find(lru_cache, fourth_key, (void **)&value));
128 ASSERT_NOT_NULL(value);
129 ASSERT_INT_EQUALS(fourth, *value);
130
131 aws_cache_destroy(lru_cache);
132 return 0;
133 }
134
135 AWS_TEST_CASE(test_lru_cache_lru_ness_static_members, s_test_lru_cache_lru_ness_static_members_fn)
136
137 static int s_test_lru_cache_element_access_members_fn(struct aws_allocator *allocator, void *ctx) {
138 (void)ctx;
139 struct aws_cache *lru_cache =
140 aws_cache_new_lru(allocator, aws_hash_c_string, aws_hash_callback_c_str_eq, NULL, NULL, 3);
141 ASSERT_NOT_NULL(lru_cache);
142
143 int *value = NULL;
144 ASSERT_NULL(aws_lru_cache_use_lru_element(lru_cache));
145 ASSERT_NULL(aws_lru_cache_get_mru_element(lru_cache));
146
147 const char *first_key = "first";
148 const char *second_key = "second";
149 const char *third_key = "third";
150
151 int first = 1;
152 int second = 2;
153 int third = 3;
154
155 ASSERT_SUCCESS(aws_cache_put(lru_cache, first_key, &first));
156 ASSERT_SUCCESS(aws_cache_put(lru_cache, second_key, &second));
157 ASSERT_SUCCESS(aws_cache_put(lru_cache, third_key, &third));
158 ASSERT_INT_EQUALS(3, aws_cache_get_element_count(lru_cache));
159
160 value = aws_lru_cache_get_mru_element(lru_cache);
161 ASSERT_NOT_NULL(value);
162 ASSERT_INT_EQUALS(third, *value);
163
164 value = aws_lru_cache_use_lru_element(lru_cache);
165 ASSERT_NOT_NULL(value);
166 ASSERT_INT_EQUALS(first, *value);
167
168 value = aws_lru_cache_get_mru_element(lru_cache);
169 ASSERT_NOT_NULL(value);
170 ASSERT_INT_EQUALS(first, *value);
171
172 aws_cache_destroy(lru_cache);
173 return 0;
174 }
175
176 AWS_TEST_CASE(test_lru_cache_element_access_members, s_test_lru_cache_element_access_members_fn)
177
178 static int s_test_fifo_cache_overflow_static_members_fn(struct aws_allocator *allocator, void *ctx) {
179 (void)ctx;
180
181 struct aws_cache *fifo_cache =
182 aws_cache_new_fifo(allocator, aws_hash_c_string, aws_hash_callback_c_str_eq, NULL, NULL, 3);
183 ASSERT_NOT_NULL(fifo_cache);
184
185 const char *first_key = "first";
186 const char *second_key = "second";
187 const char *third_key = "third";
188 const char *fourth_key = "fourth";
189
190 int first = 1;
191 int second = 2;
192 int third = 3;
193 int fourth = 4;
194
195 ASSERT_SUCCESS(aws_cache_put(fifo_cache, first_key, &first));
196 ASSERT_SUCCESS(aws_cache_put(fifo_cache, second_key, &second));
197 ASSERT_SUCCESS(aws_cache_put(fifo_cache, third_key, &third));
198 ASSERT_INT_EQUALS(3, aws_cache_get_element_count(fifo_cache));
199
200 int *value = NULL;
201 ASSERT_SUCCESS(aws_cache_find(fifo_cache, third_key, (void **)&value));
202 ASSERT_NOT_NULL(value);
203 ASSERT_INT_EQUALS(third, *value);
204
205 ASSERT_SUCCESS(aws_cache_find(fifo_cache, second_key, (void **)&value));
206 ASSERT_NOT_NULL(value);
207 ASSERT_INT_EQUALS(second, *value);
208
209 ASSERT_SUCCESS(aws_cache_find(fifo_cache, first_key, (void **)&value));
210 ASSERT_NOT_NULL(value);
211 ASSERT_INT_EQUALS(first, *value);
212
213 ASSERT_SUCCESS(aws_cache_put(fifo_cache, fourth_key, (void **)&fourth));
214
215 /* make sure the oldest entry was purged. Note, value should now be NULL but
216 * the call succeeds. */
217 ASSERT_SUCCESS(aws_cache_find(fifo_cache, first_key, (void **)&value));
218 ASSERT_NULL(value);
219
220 ASSERT_SUCCESS(aws_cache_find(fifo_cache, second_key, (void **)&value));
221 ASSERT_NOT_NULL(value);
222 ASSERT_INT_EQUALS(second, *value);
223
224 ASSERT_SUCCESS(aws_cache_find(fifo_cache, third_key, (void **)&value));
225 ASSERT_NOT_NULL(value);
226 ASSERT_INT_EQUALS(third, *value);
227
228 ASSERT_SUCCESS(aws_cache_find(fifo_cache, fourth_key, (void **)&value));
229 ASSERT_NOT_NULL(value);
230 ASSERT_INT_EQUALS(fourth, *value);
231
232 aws_cache_destroy(fifo_cache);
233 return 0;
234 }
235
236 AWS_TEST_CASE(test_fifo_cache_overflow_static_members, s_test_fifo_cache_overflow_static_members_fn)
237
238 static int s_test_lifo_cache_overflow_static_members_fn(struct aws_allocator *allocator, void *ctx) {
239 (void)ctx;
240
241 struct aws_cache *lifo_cache =
242 aws_cache_new_lifo(allocator, aws_hash_c_string, aws_hash_callback_c_str_eq, NULL, NULL, 3);
243 ASSERT_NOT_NULL(lifo_cache);
244
245 const char *first_key = "first";
246 const char *second_key = "second";
247 const char *third_key = "third";
248 const char *fourth_key = "fourth";
249
250 int first = 1;
251 int second = 2;
252 int third = 3;
253 int fourth = 4;
254
255 ASSERT_SUCCESS(aws_cache_put(lifo_cache, first_key, &first));
256 ASSERT_SUCCESS(aws_cache_put(lifo_cache, second_key, &second));
257 ASSERT_SUCCESS(aws_cache_put(lifo_cache, third_key, &third));
258 ASSERT_INT_EQUALS(3, aws_cache_get_element_count(lifo_cache));
259
260 int *value = NULL;
261 ASSERT_SUCCESS(aws_cache_find(lifo_cache, third_key, (void **)&value));
262 ASSERT_NOT_NULL(value);
263 ASSERT_INT_EQUALS(third, *value);
264
265 ASSERT_SUCCESS(aws_cache_find(lifo_cache, second_key, (void **)&value));
266 ASSERT_NOT_NULL(value);
267 ASSERT_INT_EQUALS(second, *value);
268
269 ASSERT_SUCCESS(aws_cache_find(lifo_cache, first_key, (void **)&value));
270 ASSERT_NOT_NULL(value);
271 ASSERT_INT_EQUALS(first, *value);
272
273 ASSERT_SUCCESS(aws_cache_put(lifo_cache, fourth_key, (void **)&fourth));
274
275 /* make sure the latest entry was purged. Note, value should now be NULL but
276 * the call succeeds. */
277 ASSERT_SUCCESS(aws_cache_find(lifo_cache, third_key, (void **)&value));
278 ASSERT_NULL(value);
279
280 ASSERT_SUCCESS(aws_cache_find(lifo_cache, first_key, (void **)&value));
281 ASSERT_NOT_NULL(value);
282 ASSERT_INT_EQUALS(first, *value);
283
284 ASSERT_SUCCESS(aws_cache_find(lifo_cache, second_key, (void **)&value));
285 ASSERT_NOT_NULL(value);
286 ASSERT_INT_EQUALS(second, *value);
287
288 ASSERT_SUCCESS(aws_cache_find(lifo_cache, fourth_key, (void **)&value));
289 ASSERT_NOT_NULL(value);
290 ASSERT_INT_EQUALS(fourth, *value);
291
292 aws_cache_destroy(lifo_cache);
293 return 0;
294 }
295
296 AWS_TEST_CASE(test_lifo_cache_overflow_static_members, s_test_lifo_cache_overflow_static_members_fn)
297
298 struct cache_test_value_element {
299 bool value_removed;
300 };
301
302 static void s_cache_element_value_destroy(void *value) {
303 struct cache_test_value_element *value_element = value;
304 value_element->value_removed = true;
305 }
306
307 static int s_test_cache_entries_cleanup_fn(struct aws_allocator *allocator, void *ctx) {
308 (void)ctx;
309
310 /* take fifo cache as example, others are the same as it */
311 struct aws_cache *cache = aws_cache_new_fifo(
312 allocator, aws_hash_c_string, aws_hash_callback_c_str_eq, NULL, s_cache_element_value_destroy, 3);
313 ASSERT_NOT_NULL(cache);
314
315 const char *first_key = "first";
316 const char *second_key = "second";
317
318 struct cache_test_value_element first = {.value_removed = false};
319 struct cache_test_value_element second = {.value_removed = false};
320
321 ASSERT_SUCCESS(aws_cache_put(cache, first_key, &first));
322 ASSERT_SUCCESS(aws_cache_put(cache, second_key, &second));
323 ASSERT_INT_EQUALS(2, aws_cache_get_element_count(cache));
324
325 ASSERT_SUCCESS(aws_cache_remove(cache, second_key));
326
327 ASSERT_TRUE(second.value_removed);
328 ASSERT_INT_EQUALS(1, aws_cache_get_element_count(cache));
329
330 aws_cache_clear(cache);
331 ASSERT_INT_EQUALS(0, aws_cache_get_element_count(cache));
332
333 ASSERT_TRUE(first.value_removed);
334
335 aws_cache_destroy(cache);
336 return 0;
337 }
338
339 AWS_TEST_CASE(test_cache_entries_cleanup, s_test_cache_entries_cleanup_fn)
340
341 static int s_test_cache_entries_overwrite_fn(struct aws_allocator *allocator, void *ctx) {
342 (void)ctx;
343 /* take fifo cache as example, others are the same as it */
344 struct aws_cache *cache = aws_cache_new_fifo(
345 allocator, aws_hash_c_string, aws_hash_callback_c_str_eq, NULL, s_cache_element_value_destroy, 3);
346 ASSERT_NOT_NULL(cache);
347
348 const char *first_key = "first";
349
350 struct cache_test_value_element first = {.value_removed = false};
351 struct cache_test_value_element second = {.value_removed = false};
352
353 ASSERT_SUCCESS(aws_cache_put(cache, first_key, &first));
354 ASSERT_SUCCESS(aws_cache_put(cache, first_key, &second));
355 ASSERT_INT_EQUALS(1, aws_cache_get_element_count(cache));
356
357 ASSERT_TRUE(first.value_removed);
358 ASSERT_FALSE(second.value_removed);
359
360 struct cache_test_value_element *value = NULL;
361 ASSERT_SUCCESS(aws_cache_find(cache, first_key, (void **)&value));
362 ASSERT_NOT_NULL(value);
363 ASSERT_PTR_EQUALS(&second, value);
364
365 aws_cache_destroy(cache);
366 return 0;
367 }
368
369 AWS_TEST_CASE(test_cache_entries_overwrite, s_test_cache_entries_overwrite_fn)