forked from awslabs/aws-c-common
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpriority_queue_test.c
169 lines (140 loc) · 7.61 KB
/
priority_queue_test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
* Copyright 2010-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
#include <aws/common/priority_queue.h>
#include <aws/testing/aws_test_harness.h>
#include <stdlib.h>
static int compare_ints(const void* a, const void* b)
{
int arg1 = *(const int*)a;
int arg2 = *(const int*)b;
if (arg1 < arg2) return -1;
if (arg1 > arg2) return 1;
return 0;
}
static int test_priority_queue_preserves_order(struct aws_allocator *alloc, void *ctx) {
struct aws_priority_queue queue;
int err = aws_priority_queue_dynamic_init(&queue, alloc, 10, sizeof(int), compare_ints);
ASSERT_SUCCESS(err, "Priority queue initialization failed with error %d", err);
int first = 45, second = 67, third = 80, fourth = 120, fifth = 10000;
ASSERT_SUCCESS(aws_priority_queue_push(&queue, &third), "Push operation failed for item %d", third);
ASSERT_SUCCESS(aws_priority_queue_push(&queue, &fourth), "Push operation failed for item %d", fourth);
ASSERT_SUCCESS(aws_priority_queue_push(&queue, &second), "Push operation failed for item %d", second);
ASSERT_SUCCESS(aws_priority_queue_push(&queue, &fifth), "Push operation failed for item %d", fifth);
ASSERT_SUCCESS(aws_priority_queue_push(&queue, &first), "Push operation failed for item %d", first);
size_t num_elements = aws_priority_queue_size(&queue);
ASSERT_INT_EQUALS(5, num_elements, "Priority queue size should have been %d but was %d", 5, num_elements);
int pop_val, top_val, *top_val_ptr;
err = aws_priority_queue_top(&queue, (void **)&top_val_ptr);
ASSERT_SUCCESS(err, "Top operation failed with error %d", err);
top_val = *top_val_ptr;
err = aws_priority_queue_pop(&queue, &pop_val);
ASSERT_SUCCESS(err, "Pop operation failed with error %d", err);
ASSERT_INT_EQUALS(first, pop_val, "First element returned should have been %d but was %d", first, pop_val);
ASSERT_INT_EQUALS(pop_val, top_val, "Popped element should have been the top element. expected %d but was %d",
pop_val, top_val);
err = aws_priority_queue_top(&queue, (void **)&top_val_ptr);
ASSERT_SUCCESS(err, "Top operation failed with error %d", err);
top_val = *top_val_ptr;
err = aws_priority_queue_pop(&queue, &pop_val);
ASSERT_SUCCESS(err, "Pop operation failed with error %d", err);
ASSERT_INT_EQUALS(second, pop_val, "Second element returned should have been %d but was %d", second, pop_val);
ASSERT_INT_EQUALS(pop_val, top_val, "Popped element should have been the top element. expected %d but was %d",
pop_val, top_val);
err = aws_priority_queue_top(&queue, (void **)&top_val_ptr);
ASSERT_SUCCESS(err, "Top operation failed with error %d", err);
top_val = *top_val_ptr;
err = aws_priority_queue_pop(&queue, &pop_val);
ASSERT_SUCCESS(err, "Pop operation failed with error %d", err);
ASSERT_INT_EQUALS(third, pop_val, "Third element returned should have been %d but was %d", third, pop_val);
ASSERT_INT_EQUALS(pop_val, top_val, "Popped element should have been the top element. expected %d but was %d",
pop_val, top_val);
err = aws_priority_queue_top(&queue, (void **)&top_val_ptr);
ASSERT_SUCCESS(err, "Top operation failed with error %d", err);
top_val = *top_val_ptr;
err = aws_priority_queue_pop(&queue, &pop_val);
ASSERT_SUCCESS(err, "Pop operation failed with error %d", err);
ASSERT_INT_EQUALS(fourth, pop_val, "Fourth element returned should have been %d but was %d", fourth, pop_val);
ASSERT_INT_EQUALS(pop_val, top_val, "Popped element should have been the top element. expected %d but was %d",
pop_val, top_val);
err = aws_priority_queue_top(&queue, (void **)&top_val_ptr);
ASSERT_SUCCESS(err, "Top operation failed with error %d", err);
top_val = *top_val_ptr;
err = aws_priority_queue_pop(&queue, &pop_val);
ASSERT_SUCCESS(err, "Pop operation failed with error %d", err);
ASSERT_INT_EQUALS(fifth, pop_val, "Fifth element returned should have been %d but was %d", fifth, pop_val);
ASSERT_INT_EQUALS(pop_val, top_val, "Popped element should have been the top element. expected %d but was %d",
pop_val, top_val);
ASSERT_ERROR(AWS_ERROR_PRIORITY_QUEUE_EMPTY, aws_priority_queue_pop(&queue, &pop_val), "Popping from empty queue should result in error");
aws_priority_queue_clean_up(&queue);
return 0;
}
static int test_priority_queue_random_values(struct aws_allocator *alloc, void *ctx) {
enum { SIZE = 20 };
struct aws_priority_queue queue;
int storage[SIZE], err;
aws_priority_queue_static_init(&queue, storage, SIZE, sizeof(int), compare_ints);
int values[SIZE];
srand((unsigned)(uintptr_t)&queue);
for(int i = 0; i < SIZE; i++) {
values[i] = rand() % 1000;
err = aws_priority_queue_push(&queue, &values[i]);
ASSERT_SUCCESS(err, "Push operation failed with error %d", err);
}
qsort(values, SIZE, sizeof(int), compare_ints);
/* pop only half */
for(int i = 0; i < SIZE/2; i++) {
int top;
err = aws_priority_queue_pop(&queue, &top);
ASSERT_SUCCESS(err, "Pop operation failed with error %d", err);
ASSERT_INT_EQUALS(values[i], top, "Elements priority are out of order. Expected: %d Actual %d", values[i], top);
}
/* push new random values in that first half*/
for(int i = 0; i < SIZE/2; i++) {
values[i] = rand() % 1000;
err = aws_priority_queue_push(&queue, &values[i]);
ASSERT_SUCCESS(err, "Push operation failed with error %d", err);
}
/* sort again so we can verify correct order on pop */
qsort(values, SIZE, sizeof(int), compare_ints);
/* pop all the queue */
for(int i = 0; i < SIZE; i++) {
int top;
err = aws_priority_queue_pop(&queue, &top);
ASSERT_SUCCESS(err, "Pop operation failed with error %d", err);
ASSERT_INT_EQUALS(values[i], top, "Elements priority are out of order. Expected: %d Actual %d", values[i], top);
}
aws_priority_queue_clean_up(&queue);
return 0;
}
static int test_priority_queue_size_and_capacity(struct aws_allocator *alloc, void *ctx) {
struct aws_priority_queue queue;
int err = aws_priority_queue_dynamic_init(&queue, alloc, 5, sizeof(int), compare_ints);
ASSERT_SUCCESS(err, "Dynamic init failed with error %d", err);
size_t capacity = aws_priority_queue_capacity(&queue);
ASSERT_INT_EQUALS(5, capacity, "Expected Capacity %d but was %d", 5, capacity);
for(int i = 0; i < 15; i++) {
err = aws_priority_queue_push(&queue, &i);
ASSERT_SUCCESS(err, "Push operation failed with error %d", err);
}
size_t size = aws_priority_queue_size(&queue);
ASSERT_INT_EQUALS(15, size, "Expected Size %d but was %d", 15, capacity);
capacity = aws_priority_queue_capacity(&queue);
ASSERT_INT_EQUALS(20, capacity, "Expected Capacity %d but was %d", 20, capacity);
aws_priority_queue_clean_up(&queue);
return 0;
}
AWS_TEST_CASE(priority_queue_push_pop_order_test, test_priority_queue_preserves_order);
AWS_TEST_CASE(priority_queue_random_values_test, test_priority_queue_random_values);
AWS_TEST_CASE(priority_queue_size_and_capacity_test, test_priority_queue_size_and_capacity);