11 cttest_heap_insert_one()
18 Job
*j
= make_job(1, 0, 1, 0, 0);
19 assertf(j
, "allocate job");
22 assertf(h
.len
== 1, "h should contain one item.");
23 assertf(j
->heap_index
== 0, "should match");
25 assert(heapremove(&h
, 0));
31 cttest_heap_insert_and_remove_one()
38 Job
*j1
= make_job(1, 0, 1, 0, 0);
39 assertf(j1
, "allocate job");
41 int r
= heapinsert(&h
, j1
);
42 assertf(r
, "insert should succeed");
44 Job
*got
= heapremove(&h
, 0);
45 assertf(got
== j1
, "j1 should come back out");
46 assertf(h
.len
== 0, "h should be empty.");
53 cttest_heap_priority()
59 Job
*j
, *j1
, *j2
, *j3
;
61 j1
= make_job(1, 0, 1, 0, 0);
62 j2
= make_job(2, 0, 1, 0, 0);
63 j3
= make_job(3, 0, 1, 0, 0);
64 assertf(j1
, "allocate job");
65 assertf(j2
, "allocate job");
66 assertf(j3
, "allocate job");
68 int r
= heapinsert(&h
, j2
);
69 assertf(r
, "insert should succeed");
70 assertf(j2
->heap_index
== 0, "should match");
72 r
= heapinsert(&h
, j3
);
73 assertf(r
, "insert should succeed");
74 assertf(j2
->heap_index
== 0, "should match");
75 assertf(j3
->heap_index
== 1, "should match");
77 r
= heapinsert(&h
, j1
);
78 assertf(r
, "insert should succeed");
79 assertf(j1
->heap_index
== 0, "should match");
80 assertf(j2
->heap_index
== 2, "should match");
81 assertf(j3
->heap_index
== 1, "should match");
83 j
= heapremove(&h
, 0);
84 assertf(j
== j1
, "j1 should come out first.");
85 assertf(j2
->heap_index
== 0, "should match");
86 assertf(j3
->heap_index
== 1, "should match");
88 j
= heapremove(&h
, 0);
89 assertf(j
== j2
, "j2 should come out second.");
90 assertf(j3
->heap_index
== 0, "should match");
92 j
= heapremove(&h
, 0);
93 assertf(j
== j3
, "j3 should come out third.");
102 cttest_heap_fifo_property()
105 .less
= job_pri_less
,
106 .setpos
= job_setpos
,
108 Job
*j
, *j3a
, *j3b
, *j3c
;
110 j3a
= make_job(3, 0, 1, 0, 0);
111 j3b
= make_job(3, 0, 1, 0, 0);
112 j3c
= make_job(3, 0, 1, 0, 0);
113 assertf(j3a
, "allocate job");
114 assertf(j3b
, "allocate job");
115 assertf(j3c
, "allocate job");
117 int r
= heapinsert(&h
, j3a
);
118 assertf(r
, "insert should succeed");
119 assertf(h
.data
[0] == j3a
, "j3a should be in pos 0");
120 assertf(j3a
->heap_index
== 0, "should match");
122 r
= heapinsert(&h
, j3b
);
123 assertf(r
, "insert should succeed");
124 assertf(h
.data
[1] == j3b
, "j3b should be in pos 1");
125 assertf(j3a
->heap_index
== 0, "should match");
126 assertf(j3b
->heap_index
== 1, "should match");
128 r
= heapinsert(&h
, j3c
);
129 assertf(r
, "insert should succeed");
130 assertf(h
.data
[2] == j3c
, "j3c should be in pos 2");
131 assertf(j3a
->heap_index
== 0, "should match");
132 assertf(j3b
->heap_index
== 1, "should match");
133 assertf(j3c
->heap_index
== 2, "should match");
135 j
= heapremove(&h
, 0);
136 assertf(j
== j3a
, "j3a should come out first.");
137 assertf(j3b
->heap_index
== 0, "should match");
138 assertf(j3c
->heap_index
== 1, "should match");
140 j
= heapremove(&h
, 0);
141 assertf(j
== j3b
, "j3b should come out second.");
142 assertf(j3c
->heap_index
== 0, "should match");
144 j
= heapremove(&h
, 0);
145 assertf(j
== j3c
, "j3c should come out third.");
154 cttest_heap_many_jobs()
157 .less
= job_pri_less
,
158 .setpos
= job_setpos
,
164 for (i
= 0; i
< n
; i
++) {
165 j
= make_job(1 + rand() % 8192, 0, 1, 0, 0);
166 assertf(j
, "allocation");
167 int r
= heapinsert(&h
, j
);
168 assertf(r
, "heapinsert");
172 for (i
= 0; i
< n
; i
++) {
173 j
= heapremove(&h
, 0);
174 assertf(j
->r
.pri
>= last_pri
, "should come out in order");
183 cttest_heap_remove_k()
186 .less
= job_pri_less
,
187 .setpos
= job_setpos
,
193 for (c
= 0; c
< 50; c
++) {
194 for (i
= 0; i
< n
; i
++) {
195 Job
*j
= make_job(1 + rand() % 8192, 0, 1, 0, 0);
196 assertf(j
, "allocation");
197 int r
= heapinsert(&h
, j
);
198 assertf(r
, "heapinsert");
201 /* remove one from the middle */
202 Job
*j0
= heapremove(&h
, mid
);
203 assertf(j0
, "j0 should not be NULL");
206 /* now make sure the rest are still a valid heap */
208 for (i
= 1; i
< n
; i
++) {
209 Job
*j
= heapremove(&h
, 0);
210 assertf(j
->r
.pri
>= last_pri
, "should come out in order");
212 assertf(j
, "j should not be NULL");
220 ctbench_heap_insert(int n
)
222 Job
**j
= calloc(n
, sizeof *j
);
224 for (i
= 0; i
< n
; i
++) {
225 j
[i
] = make_job(1, 0, 1, 0, 0);
227 j
[i
]->r
.pri
= -j
[i
]->r
.id
;
230 .less
= job_pri_less
,
231 .setpos
= job_setpos
,
235 for (i
= 0; i
< n
; i
++) {
236 heapinsert(&h
, j
[i
]);
240 for (i
= 0; i
< n
; i
++)
241 job_free(heapremove(&h
, 0));
247 ctbench_heap_remove(int n
)
250 .less
= job_pri_less
,
251 .setpos
= job_setpos
,
254 for (i
= 0; i
< n
; i
++) {
255 Job
*j
= make_job(1, 0, 1, 0, 0);
256 assertf(j
, "allocate job");
259 Job
**jj
= calloc(n
, sizeof(Job
*)); // temp storage to deallocate jobs later
262 for (i
= 0; i
< n
; i
++) {
263 jj
[i
] = (Job
*)heapremove(&h
, 0);
268 for (i
= 0; i
< n
; i
++)