Debian Codegraph @

crt/aws-c-common/tests/priority_queue_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/priority_queue.h>
7
8 #include <aws/testing/aws_test_harness.h>
9
10 #include <stdlib.h>
11
12 static int s_compare_ints(const void *a, const void *b) {
13 int arg1 = *(const int *)a;
14 int arg2 = *(const int *)b;
15
16 if (arg1 < arg2) {
17 return -1;
18 }
19 if (arg1 > arg2) {
20 return 1;
21 }
22 return 0;
23 }
24
25 static int s_test_priority_queue_preserves_order(struct aws_allocator *allocator, void *ctx) {
26 (void)ctx;
27
28 struct aws_priority_queue queue;
29
30 int err = aws_priority_queue_init_dynamic(&queue, allocator, 10, sizeof(int), s_compare_ints);
31 ASSERT_SUCCESS(err, "Priority queue initialization failed with error %d", err);
32
33 int first = 45, second = 67, third = 80, fourth = 120, fifth = 10000;
34
35 ASSERT_SUCCESS(aws_priority_queue_push(&queue, &third), "Push operation failed for item %d", third);
36 ASSERT_SUCCESS(aws_priority_queue_push(&queue, &fourth), "Push operation failed for item %d", fourth);
37 ASSERT_SUCCESS(aws_priority_queue_push(&queue, &second), "Push operation failed for item %d", second);
38 ASSERT_SUCCESS(aws_priority_queue_push(&queue, &fifth), "Push operation failed for item %d", fifth);
39 ASSERT_SUCCESS(aws_priority_queue_push(&queue, &first), "Push operation failed for item %d", first);
40
41 size_t num_elements = aws_priority_queue_size(&queue);
42 ASSERT_INT_EQUALS(5, num_elements, "Priority queue size should have been %d but was %d", 5, num_elements);
43
44 int pop_val, top_val, *top_val_ptr;
45 err = aws_priority_queue_top(&queue, (void **)&top_val_ptr);
46 ASSERT_SUCCESS(err, "Top operation failed with error %d", err);
47 top_val = *top_val_ptr;
48 err = aws_priority_queue_pop(&queue, &pop_val);
49 ASSERT_SUCCESS(err, "Pop operation failed with error %d", err);
50 ASSERT_INT_EQUALS(first, pop_val, "First element returned should have been %d but was %d", first, pop_val);
51 ASSERT_INT_EQUALS(
52 pop_val, top_val, "Popped element should have been the top element. expected %d but was %d", pop_val, top_val);
53
54 err = aws_priority_queue_top(&queue, (void **)&top_val_ptr);
55 ASSERT_SUCCESS(err, "Top operation failed with error %d", err);
56 top_val = *top_val_ptr;
57 err = aws_priority_queue_pop(&queue, &pop_val);
58 ASSERT_SUCCESS(err, "Pop operation failed with error %d", err);
59 ASSERT_INT_EQUALS(second, pop_val, "Second element returned should have been %d but was %d", second, pop_val);
60 ASSERT_INT_EQUALS(
61 pop_val, top_val, "Popped element should have been the top element. expected %d but was %d", pop_val, top_val);
62
63 err = aws_priority_queue_top(&queue, (void **)&top_val_ptr);
64 ASSERT_SUCCESS(err, "Top operation failed with error %d", err);
65 top_val = *top_val_ptr;
66 err = aws_priority_queue_pop(&queue, &pop_val);
67 ASSERT_SUCCESS(err, "Pop operation failed with error %d", err);
68 ASSERT_INT_EQUALS(third, pop_val, "Third element returned should have been %d but was %d", third, pop_val);
69 ASSERT_INT_EQUALS(
70 pop_val, top_val, "Popped element should have been the top element. expected %d but was %d", pop_val, top_val);
71
72 err = aws_priority_queue_top(&queue, (void **)&top_val_ptr);
73 ASSERT_SUCCESS(err, "Top operation failed with error %d", err);
74 top_val = *top_val_ptr;
75 err = aws_priority_queue_pop(&queue, &pop_val);
76 ASSERT_SUCCESS(err, "Pop operation failed with error %d", err);
77 ASSERT_INT_EQUALS(fourth, pop_val, "Fourth element returned should have been %d but was %d", fourth, pop_val);
78 ASSERT_INT_EQUALS(
79 pop_val, top_val, "Popped element should have been the top element. expected %d but was %d", pop_val, top_val);
80
81 err = aws_priority_queue_top(&queue, (void **)&top_val_ptr);
82 ASSERT_SUCCESS(err, "Top operation failed with error %d", err);
83 top_val = *top_val_ptr;
84 err = aws_priority_queue_pop(&queue, &pop_val);
85 ASSERT_SUCCESS(err, "Pop operation failed with error %d", err);
86 ASSERT_INT_EQUALS(fifth, pop_val, "Fifth element returned should have been %d but was %d", fifth, pop_val);
87 ASSERT_INT_EQUALS(
88 pop_val, top_val, "Popped element should have been the top element. expected %d but was %d", pop_val, top_val);
89
90 ASSERT_ERROR(
91 AWS_ERROR_PRIORITY_QUEUE_EMPTY,
92 aws_priority_queue_pop(&queue, &pop_val),
93 "Popping from empty queue should result in error");
94
95 aws_priority_queue_clean_up(&queue);
96 return 0;
97 }
98
99 static int s_test_priority_queue_random_values(struct aws_allocator *allocator, void *ctx) {
100 (void)allocator;
101 (void)ctx;
102
103 enum { SIZE = 20 };
104 struct aws_priority_queue queue;
105 int storage[SIZE], err;
106 aws_priority_queue_init_static(&queue, storage, SIZE, sizeof(int), s_compare_ints);
107 int values[SIZE];
108 srand((unsigned)(uintptr_t)&queue);
109 for (int i = 0; i < SIZE; i++) {
110 values[i] = rand() % 1000;
111 err = aws_priority_queue_push(&queue, &values[i]);
112 ASSERT_SUCCESS(err, "Push operation failed with error %d", err);
113 }
114
115 qsort(values, SIZE, sizeof(int), s_compare_ints);
116
117 /* pop only half */
118 for (int i = 0; i < SIZE / 2; i++) {
119 int top;
120 err = aws_priority_queue_pop(&queue, &top);
121 ASSERT_SUCCESS(err, "Pop operation failed with error %d", err);
122 ASSERT_INT_EQUALS(values[i], top, "Elements priority are out of order. Expected: %d Actual %d", values[i], top);
123 }
124
125 /* push new random values in that first half*/
126 for (int i = 0; i < SIZE / 2; i++) {
127 values[i] = rand() % 1000;
128 err = aws_priority_queue_push(&queue, &values[i]);
129 ASSERT_SUCCESS(err, "Push operation failed with error %d", err);
130 }
131
132 /* sort again so we can verify correct order on pop */
133 qsort(values, SIZE, sizeof(int), s_compare_ints);
134 /* pop all the queue */
135 for (int i = 0; i < SIZE; i++) {
136 int top;
137 err = aws_priority_queue_pop(&queue, &top);
138 ASSERT_SUCCESS(err, "Pop operation failed with error %d", err);
139 ASSERT_INT_EQUALS(values[i], top, "Elements priority are out of order. Expected: %d Actual %d", values[i], top);
140 }
141
142 aws_priority_queue_clean_up(&queue);
143
144 return 0;
145 }
146
147 static int s_test_priority_queue_size_and_capacity(struct aws_allocator *allocator, void *ctx) {
148 (void)ctx;
149
150 struct aws_priority_queue queue;
151 int err = aws_priority_queue_init_dynamic(&queue, allocator, 5, sizeof(int), s_compare_ints);
152 ASSERT_SUCCESS(err, "Dynamic init failed with error %d", err);
153 size_t capacity = aws_priority_queue_capacity(&queue);
154 ASSERT_INT_EQUALS(5, capacity, "Expected Capacity %d but was %d", 5, capacity);
155
156 for (int i = 0; i < 15; i++) {
157 err = aws_priority_queue_push(&queue, &i);
158 ASSERT_SUCCESS(err, "Push operation failed with error %d", err);
159 }
160
161 size_t size = aws_priority_queue_size(&queue);
162 ASSERT_INT_EQUALS(15, size, "Expected Size %d but was %d", 15, capacity);
163
164 capacity = aws_priority_queue_capacity(&queue);
165 ASSERT_INT_EQUALS(20, capacity, "Expected Capacity %d but was %d", 20, capacity);
166
167 aws_priority_queue_clean_up(&queue);
168 return 0;
169 }
170
171 #define ADD_ELEMS(pq, ...) \
172 do { \
173 static int ADD_ELEMS_elems[] = {__VA_ARGS__}; \
174 for (size_t ADD_ELEMS_i = 0; ADD_ELEMS_i < sizeof(ADD_ELEMS_elems) / sizeof(*ADD_ELEMS_elems); \
175 ADD_ELEMS_i++) { \
176 ASSERT_SUCCESS(aws_priority_queue_push(&(pq), &ADD_ELEMS_elems[ADD_ELEMS_i])); \
177 } \
178 } while (0)
179
180 #define CHECK_ORDER(pq, ...) \
181 do { \
182 static int CHECK_ORDER_elems[] = {__VA_ARGS__}; \
183 size_t CHECK_ORDER_count = sizeof(CHECK_ORDER_elems) / sizeof(*CHECK_ORDER_elems); \
184 size_t CHECK_ORDER_i = 0; \
185 int CHECK_ORDER_val; \
186 while (aws_priority_queue_pop(&(pq), &CHECK_ORDER_val) == AWS_OP_SUCCESS) { \
187 ASSERT_TRUE(CHECK_ORDER_i < CHECK_ORDER_count); \
188 ASSERT_INT_EQUALS(CHECK_ORDER_val, CHECK_ORDER_elems[CHECK_ORDER_i]); \
189 CHECK_ORDER_i++; \
190 } \
191 ASSERT_INT_EQUALS(CHECK_ORDER_i, CHECK_ORDER_count); \
192 } while (0)
193
194 static int s_test_remove_root(struct aws_allocator *allocator, void *ctx) {
195 (void)ctx;
196
197 struct aws_priority_queue queue;
198 struct aws_priority_queue_node node = {12345};
199 int val = 0;
200 ASSERT_SUCCESS(aws_priority_queue_init_dynamic(&queue, allocator, 16, sizeof(int), s_compare_ints));
201
202 ASSERT_SUCCESS(aws_priority_queue_push_ref(&queue, &val, &node));
203 ADD_ELEMS(queue, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16);
204
205 val = 42;
206 ASSERT_SUCCESS(aws_priority_queue_remove(&queue, &val, &node));
207 ASSERT_INT_EQUALS(val, 0);
208 ASSERT_ERROR(AWS_ERROR_PRIORITY_QUEUE_BAD_NODE, aws_priority_queue_remove(&queue, &val, &node));
209
210 CHECK_ORDER(queue, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16);
211
212 aws_priority_queue_clean_up(&queue);
213
214 return 0;
215 }
216
217 static int s_test_remove_leaf(struct aws_allocator *allocator, void *ctx) {
218 (void)ctx;
219
220 struct aws_priority_queue queue;
221 struct aws_priority_queue_node node = {12345};
222 ASSERT_SUCCESS(aws_priority_queue_init_dynamic(&queue, allocator, 16, sizeof(int), s_compare_ints));
223
224 ADD_ELEMS(queue, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
225
226 int val = 16;
227 ASSERT_SUCCESS(aws_priority_queue_push_ref(&queue, &val, &node));
228
229 val = 42;
230 ASSERT_SUCCESS(aws_priority_queue_remove(&queue, &val, &node));
231 ASSERT_INT_EQUALS(val, 16);
232 ASSERT_ERROR(AWS_ERROR_PRIORITY_QUEUE_BAD_NODE, aws_priority_queue_remove(&queue, &val, &node));
233
234 CHECK_ORDER(queue, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
235
236 aws_priority_queue_clean_up(&queue);
237
238 return 0;
239 }
240
241 /*
242 * Here we force the heap to sift a value up to its parents when removing an interior node.
243 *
244 * 0
245 * 20
246 * 22
247 * 222 <- Removed, swapped with 15
248 * 2222
249 * 2221
250 * 221
251 * 2212
252 * 2211
253 * 21
254 * 212
255 * 2122
256 * 2121
257 * 211
258 * 2112
259 * 2111
260 * 1
261 * 2
262 * 3
263 * 4
264 * 5
265 * 6
266 * 7
267 * 8
268 * 9
269 * 10
270 * 11
271 * 12
272 * 13
273 * 14
274 * 15
275 */
276 static int s_test_remove_interior_sift_up(struct aws_allocator *allocator, void *ctx) {
277 (void)ctx;
278
279 struct aws_priority_queue queue;
280 struct aws_priority_queue_node node = {12345};
281 ASSERT_SUCCESS(aws_priority_queue_init_dynamic(&queue, allocator, 16, sizeof(int), s_compare_ints));
282
283 ADD_ELEMS(queue, 0, 20, 1, 22, 21, 2, 9);
284 int val = 222;
285 ASSERT_SUCCESS(aws_priority_queue_push_ref(&queue, &val, &node));
286 ADD_ELEMS(
287 queue, 221, 212, 211, 3, 6, 10, 13, 2222, 2221, 2212, 2211, 2122, 2121, 2112, 2111, 4, 5, 7, 8, 11, 12, 14, 15);
288
289 val = 42;
290 ASSERT_SUCCESS(aws_priority_queue_remove(&queue, &val, &node));
291 ASSERT_INT_EQUALS(val, 222);
292 ASSERT_ERROR(AWS_ERROR_PRIORITY_QUEUE_BAD_NODE, aws_priority_queue_remove(&queue, &val, &node));
293
294 CHECK_ORDER(
295 queue,
296 0,
297 1,
298 2,
299 3,
300 4,
301 5,
302 6,
303 7,
304 8,
305 9,
306 10,
307 11,
308 12,
309 13,
310 14,
311 15,
312 20,
313 21,
314 22,
315 211,
316 212,
317 221,
318 /* 222, */ 2111,
319 2112,
320 2121,
321 2122,
322 2211,
323 2212,
324 2221,
325 2222);
326
327 aws_priority_queue_clean_up(&queue);
328
329 return 0;
330 }
331
332 /*
333 * Here we force the heap to sift a value down to a leaf when removing an interior node.
334 *
335 * 0
336 * 1 <- Removed, swapped with 30
337 * 2
338 * 3
339 * 4
340 * 5
341 * 6
342 * 7
343 * 8
344 * 9
345 * 10
346 * 11
347 * 12
348 * 13
349 * 14
350 * 15
351 * 16
352 * 17
353 * 18
354 * 19
355 * 20
356 * 21
357 * 22
358 * 23
359 * 24
360 * 25
361 * 26
362 * 27
363 * 28
364 * 29
365 * 30
366 */
367 static int s_test_remove_interior_sift_down(struct aws_allocator *allocator, void *ctx) {
368 (void)ctx;
369
370 struct aws_priority_queue queue;
371 struct aws_priority_queue_node node = {12345};
372 ASSERT_SUCCESS(aws_priority_queue_init_dynamic(&queue, allocator, 16, sizeof(int), s_compare_ints));
373
374 ADD_ELEMS(queue, 0);
375
376 int val = 1;
377 ASSERT_SUCCESS(aws_priority_queue_push_ref(&queue, &val, &node));
378
379 ADD_ELEMS(
380 queue,
381 16,
382 2,
383 9,
384 17,
385 24,
386 3,
387 6,
388 10,
389 13,
390 18,
391 21,
392 25,
393 28,
394 4,
395 5,
396 7,
397 8,
398 11,
399 12,
400 14,
401 15,
402 19,
403 20,
404 22,
405 23,
406 26,
407 27,
408 29,
409 30);
410
411 val = 42;
412 ASSERT_SUCCESS(aws_priority_queue_remove(&queue, &val, &node));
413 ASSERT_INT_EQUALS(val, 1);
414 ASSERT_ERROR(AWS_ERROR_PRIORITY_QUEUE_BAD_NODE, aws_priority_queue_remove(&queue, &val, &node));
415
416 CHECK_ORDER(
417 queue,
418 0,
419 /* 1, */ 2,
420 3,
421 4,
422 5,
423 6,
424 7,
425 8,
426 9,
427 10,
428 11,
429 12,
430 13,
431 14,
432 15,
433 16,
434 17,
435 18,
436 19,
437 20,
438 21,
439 22,
440 23,
441 24,
442 25,
443 26,
444 27,
445 28,
446 29,
447 30);
448
449 aws_priority_queue_clean_up(&queue);
450
451 return 0;
452 }
453
454 #define BACKPOINTER_CLEAR_NODE_COUNT 16
455
456 static int s_priority_queue_clear_backpointers_test(struct aws_allocator *allocator, void *ctx) {
457 (void)ctx;
458
459 struct aws_priority_queue queue;
460
461 ASSERT_SUCCESS(aws_priority_queue_init_dynamic(&queue, allocator, 16, sizeof(int), s_compare_ints));
462
463 struct aws_priority_queue_node queue_nodes[BACKPOINTER_CLEAR_NODE_COUNT];
464
465 for (size_t i = 0; i < BACKPOINTER_CLEAR_NODE_COUNT; ++i) {
466 aws_priority_queue_node_init(&queue_nodes[i]);
467 }
468
469 for (int i = 0; i < BACKPOINTER_CLEAR_NODE_COUNT; i++) {
470 aws_priority_queue_push_ref(&queue, &i, &queue_nodes[i]);
471 }
472
473 for (size_t i = 0; i < BACKPOINTER_CLEAR_NODE_COUNT; ++i) {
474 ASSERT_TRUE(aws_priority_queue_node_is_in_queue(&queue_nodes[i]));
475 }
476
477 aws_priority_queue_clear(&queue);
478 ASSERT_INT_EQUALS(0, aws_priority_queue_size(&queue));
479
480 for (size_t i = 0; i < BACKPOINTER_CLEAR_NODE_COUNT; ++i) {
481 ASSERT_FALSE(aws_priority_queue_node_is_in_queue(&queue_nodes[i]));
482 }
483
484 aws_priority_queue_clean_up(&queue);
485
486 return 0;
487 }
488
489 AWS_TEST_CASE(priority_queue_remove_interior_sift_down_test, s_test_remove_interior_sift_down);
490 AWS_TEST_CASE(priority_queue_remove_interior_sift_up_test, s_test_remove_interior_sift_up);
491 AWS_TEST_CASE(priority_queue_remove_leaf_test, s_test_remove_leaf);
492 AWS_TEST_CASE(priority_queue_remove_root_test, s_test_remove_root);
493 AWS_TEST_CASE(priority_queue_push_pop_order_test, s_test_priority_queue_preserves_order);
494 AWS_TEST_CASE(priority_queue_random_values_test, s_test_priority_queue_random_values);
495 AWS_TEST_CASE(priority_queue_size_and_capacity_test, s_test_priority_queue_size_and_capacity);
496 AWS_TEST_CASE(priority_queue_clear_backpointers_test, s_priority_queue_clear_backpointers_test);