-
Notifications
You must be signed in to change notification settings - Fork 248
/
heap_test.c
256 lines (200 loc) · 4.98 KB
/
heap_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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#include "sc_heap.h"
#include <assert.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
int example(void)
{
struct data {
int priority;
char *data;
};
struct data n[] = {{1, "first"},
{4, "fourth"},
{5, "fifth"},
{3, "third"},
{2, "second"}};
struct sc_heap_data *elem;
struct sc_heap heap;
sc_heap_init(&heap, 0);
// Min-heap usage
for (int i = 0; i < 5; i++) {
sc_heap_add(&heap, n[i].priority, n[i].data);
}
while ((elem = sc_heap_pop(&heap)) != NULL) {
printf("key = %d, data = %s \n",
(int) elem->key, (char*) elem->data);
}
printf("---------------- \n");
// Max-heap usage, negate when adding into heap and negate back after
// pop :
for (int i = 0; i < 5; i++) {
sc_heap_add(&heap, -(n[i].priority), n[i].data);
}
while ((elem = sc_heap_pop(&heap)) != NULL) {
printf("key = %d, data = %s \n",
(int) elem->key, (char*) elem->data);
}
sc_heap_term(&heap);
return 0;
}
void test1(void)
{
struct sc_heap heap;
struct sc_heap_data *elem;
sc_heap_init(&heap, 0);
sc_heap_add(&heap, 100, "test");
elem = sc_heap_pop(&heap);
assert(elem->key == 100);
assert(strcmp("test", elem->data) == 0);
sc_heap_term(&heap);
sc_heap_add(&heap, 100, "test");
elem = sc_heap_pop(&heap);
assert(elem->key == 100);
assert(strcmp("test", elem->data) == 0);
sc_heap_term(&heap);
assert(sc_heap_init(&heap, SIZE_MAX / 2) == false);
assert(sc_heap_init(&heap, 3) == true);
for (int i = 0; i < 1000; i++) {
assert(sc_heap_add(&heap, i, (void *) (uintptr_t) i) == true);
elem = sc_heap_pop(&heap);
assert(elem != NULL);
assert(elem->key == (intptr_t) elem->data);
}
int64_t arr[] = {1, 0, 4, 5, 7, 9, 8, 6, 3, 2};
for (int i = 0; i < 10; i++) {
assert(sc_heap_add(&heap, arr[i],
(void *) (uintptr_t) (arr[i] * 2)) == true);
}
for (int i = 0; i < 10; i++) {
elem = sc_heap_pop(&heap);
assert(elem != NULL);
assert(elem->key == i);
assert((intptr_t) elem->data == i * 2);
}
sc_heap_term(&heap);
}
void test2(void)
{
static const int64_t arr[] = {1, 0, 4, 5, 7, 9, 8, 6, 3, 2};
struct sc_heap_data *elem;
struct sc_heap heap;
assert(sc_heap_init(&heap, 0) == true);
for (int i = 0; i < 1000; i++) {
assert(sc_heap_add(&heap, i, (void *) (uintptr_t) i) == true);
elem = sc_heap_pop(&heap);
assert(elem != NULL);
assert(elem->key == (intptr_t) elem->data);
assert(elem->key == (intptr_t) i);
}
for (int i = 0; i < 10; i++) {
assert(sc_heap_add(&heap, -arr[i],
(void *) (uintptr_t) (arr[i] * 2)) == true);
}
for (int i = 0; i < 10; i++) {
elem = sc_heap_pop(&heap);
assert(elem != NULL);
assert(-elem->key == 9 - i);
assert((intptr_t) elem->data == (9 - i) * 2);
}
sc_heap_term(&heap);
}
void test3(void)
{
int arr[100];
struct sc_heap_data *elem;
struct sc_heap heap;
assert(sc_heap_init(&heap, 2) == true);
assert(sc_heap_add(&heap, 9, (void *) (uintptr_t) 9) == true);
elem = sc_heap_peek(&heap);
assert(elem != NULL);
assert(elem->key == 9);
assert(elem->data == (void *) (uintptr_t) 9);
elem = sc_heap_pop(&heap);
assert(elem != NULL);
assert(elem->key == 9);
assert(elem->data == (void *) (uintptr_t) 9);
for (int i = 0; i < 100; i++) {
arr[i] = i;
}
for (int i = 0; i < 100; i++) {
int k = arr[i];
arr[i] = arr[(i * 15) % 100];
arr[(i * 15) % 100] = k;
}
for (int i = 0; i < 100; i++) {
assert(sc_heap_add(&heap, i, (void *) (uintptr_t) i) == true);
}
for (int i = 0; i < 100; i++) {
elem = sc_heap_pop(&heap);
assert(elem != NULL);
assert(elem->key == i);
assert(elem->data == (void *) (uintptr_t) i);
}
assert(sc_heap_peek(&heap) == NULL);
assert(sc_heap_pop(&heap) == NULL);
assert(sc_heap_size(&heap) == 0);
assert(sc_heap_add(&heap, 1, NULL) == true);
assert(sc_heap_size(&heap) == 1);
sc_heap_clear(&heap);
assert(sc_heap_size(&heap) == 0);
assert(sc_heap_add(&heap, 1, NULL) == true);
assert(sc_heap_size(&heap) == 1);
sc_heap_term(&heap);
}
#ifdef SC_HAVE_WRAP
bool fail_malloc = false;
void *__real_malloc(size_t n);
void *__wrap_malloc(size_t n)
{
if (fail_malloc) {
return NULL;
}
return __real_malloc(n);
}
bool fail_realloc = false;
void *__real_realloc(void *p, size_t size);
void *__wrap_realloc(void *p, size_t n)
{
if (fail_realloc) {
return NULL;
}
return __real_realloc(p, n);
}
void fail_test(void)
{
struct sc_heap heap;
assert(sc_heap_init(&heap, 2) == true);
size_t count = SC_HEAP_MAX;
bool success = false;
for (size_t i = 0; i < count + 5; i++) {
success = sc_heap_add(&heap, i, (void *) (uintptr_t) i);
}
assert(!success);
sc_heap_term(&heap);
assert(sc_heap_init(&heap, 0) == true);
fail_realloc = true;
assert(sc_heap_add(&heap, 1, NULL) == false);
fail_realloc = false;
sc_heap_term(&heap);
fail_malloc = true;
assert(sc_heap_init(&heap, 1) == false);
fail_malloc = false;
assert(sc_heap_init(&heap, 1) == true);
sc_heap_term(&heap);
}
#else
void fail_test(void)
{
}
#endif
int main(void)
{
example();
fail_test();
test1();
test2();
test3();
return 0;
}