forked from beanstalkd/beanstalkd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testheap.c
271 lines (230 loc) · 6.27 KB
/
testheap.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#include "dat.h"
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include "ct/ct.h"
void
cttest_heap_insert_one()
{
Heap h = {
.less = job_pri_less,
.setpos = job_setpos,
};
Job *j = make_job(1, 0, 1, 0, 0);
assertf(j, "allocate job");
heapinsert(&h, j);
assertf(h.len == 1, "h should contain one item.");
assertf(j->heap_index == 0, "should match");
assert(heapremove(&h, 0));
job_free(j);
free(h.data);
}
void
cttest_heap_insert_and_remove_one()
{
Heap h = {
.less = job_pri_less,
.setpos = job_setpos,
};
Job *j1 = make_job(1, 0, 1, 0, 0);
assertf(j1, "allocate job");
int r = heapinsert(&h, j1);
assertf(r, "insert should succeed");
Job *got = heapremove(&h, 0);
assertf(got == j1, "j1 should come back out");
assertf(h.len == 0, "h should be empty.");
free(h.data);
job_free(j1);
}
void
cttest_heap_priority()
{
Heap h = {
.less = job_pri_less,
.setpos = job_setpos,
};
Job *j, *j1, *j2, *j3;
j1 = make_job(1, 0, 1, 0, 0);
j2 = make_job(2, 0, 1, 0, 0);
j3 = make_job(3, 0, 1, 0, 0);
assertf(j1, "allocate job");
assertf(j2, "allocate job");
assertf(j3, "allocate job");
int r = heapinsert(&h, j2);
assertf(r, "insert should succeed");
assertf(j2->heap_index == 0, "should match");
r = heapinsert(&h, j3);
assertf(r, "insert should succeed");
assertf(j2->heap_index == 0, "should match");
assertf(j3->heap_index == 1, "should match");
r = heapinsert(&h, j1);
assertf(r, "insert should succeed");
assertf(j1->heap_index == 0, "should match");
assertf(j2->heap_index == 2, "should match");
assertf(j3->heap_index == 1, "should match");
j = heapremove(&h, 0);
assertf(j == j1, "j1 should come out first.");
assertf(j2->heap_index == 0, "should match");
assertf(j3->heap_index == 1, "should match");
j = heapremove(&h, 0);
assertf(j == j2, "j2 should come out second.");
assertf(j3->heap_index == 0, "should match");
j = heapremove(&h, 0);
assertf(j == j3, "j3 should come out third.");
free(h.data);
job_free(j1);
job_free(j2);
job_free(j3);
}
void
cttest_heap_fifo_property()
{
Heap h = {
.less = job_pri_less,
.setpos = job_setpos,
};
Job *j, *j3a, *j3b, *j3c;
j3a = make_job(3, 0, 1, 0, 0);
j3b = make_job(3, 0, 1, 0, 0);
j3c = make_job(3, 0, 1, 0, 0);
assertf(j3a, "allocate job");
assertf(j3b, "allocate job");
assertf(j3c, "allocate job");
int r = heapinsert(&h, j3a);
assertf(r, "insert should succeed");
assertf(h.data[0] == j3a, "j3a should be in pos 0");
assertf(j3a->heap_index == 0, "should match");
r = heapinsert(&h, j3b);
assertf(r, "insert should succeed");
assertf(h.data[1] == j3b, "j3b should be in pos 1");
assertf(j3a->heap_index == 0, "should match");
assertf(j3b->heap_index == 1, "should match");
r = heapinsert(&h, j3c);
assertf(r, "insert should succeed");
assertf(h.data[2] == j3c, "j3c should be in pos 2");
assertf(j3a->heap_index == 0, "should match");
assertf(j3b->heap_index == 1, "should match");
assertf(j3c->heap_index == 2, "should match");
j = heapremove(&h, 0);
assertf(j == j3a, "j3a should come out first.");
assertf(j3b->heap_index == 0, "should match");
assertf(j3c->heap_index == 1, "should match");
j = heapremove(&h, 0);
assertf(j == j3b, "j3b should come out second.");
assertf(j3c->heap_index == 0, "should match");
j = heapremove(&h, 0);
assertf(j == j3c, "j3c should come out third.");
free(h.data);
job_free(j3a);
job_free(j3b);
job_free(j3c);
}
void
cttest_heap_many_jobs()
{
Heap h = {
.less = job_pri_less,
.setpos = job_setpos,
};
const int n = 20;
Job *j;
int i;
for (i = 0; i < n; i++) {
j = make_job(1 + rand() % 8192, 0, 1, 0, 0);
assertf(j, "allocation");
int r = heapinsert(&h, j);
assertf(r, "heapinsert");
}
uint last_pri = 0;
for (i = 0; i < n; i++) {
j = heapremove(&h, 0);
assertf(j->r.pri >= last_pri, "should come out in order");
last_pri = j->r.pri;
assert(j);
job_free(j);
}
free(h.data);
}
void
cttest_heap_remove_k()
{
Heap h = {
.less = job_pri_less,
.setpos = job_setpos,
};
const int n = 50;
const int mid = 25;
int c, i;
for (c = 0; c < 50; c++) {
for (i = 0; i < n; i++) {
Job *j = make_job(1 + rand() % 8192, 0, 1, 0, 0);
assertf(j, "allocation");
int r = heapinsert(&h, j);
assertf(r, "heapinsert");
}
/* remove one from the middle */
Job *j0 = heapremove(&h, mid);
assertf(j0, "j0 should not be NULL");
job_free(j0);
/* now make sure the rest are still a valid heap */
uint last_pri = 0;
for (i = 1; i < n; i++) {
Job *j = heapremove(&h, 0);
assertf(j->r.pri >= last_pri, "should come out in order");
last_pri = j->r.pri;
assertf(j, "j should not be NULL");
job_free(j);
}
}
free(h.data);
}
void
ctbench_heap_insert(int n)
{
Job **j = calloc(n, sizeof *j);
int i;
for (i = 0; i < n; i++) {
j[i] = make_job(1, 0, 1, 0, 0);
assert(j[i]);
j[i]->r.pri = -j[i]->r.id;
}
Heap h = {
.less = job_pri_less,
.setpos = job_setpos,
};
ctresettimer();
for (i = 0; i < n; i++) {
heapinsert(&h, j[i]);
}
ctstoptimer();
for (i = 0; i < n; i++)
job_free(heapremove(&h, 0));
free(h.data);
free(j);
}
void
ctbench_heap_remove(int n)
{
Heap h = {
.less = job_pri_less,
.setpos = job_setpos,
};
int i;
for (i = 0; i < n; i++) {
Job *j = make_job(1, 0, 1, 0, 0);
assertf(j, "allocate job");
heapinsert(&h, j);
}
Job **jj = calloc(n, sizeof(Job *)); // temp storage to deallocate jobs later
ctresettimer();
for (i = 0; i < n; i++) {
jj[i] = (Job *)heapremove(&h, 0);
}
ctstoptimer();
free(h.data);
for (i = 0; i < n; i++)
job_free(jj[i]);
free(jj);
}