-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathunittests.c
249 lines (195 loc) · 4.35 KB
/
unittests.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
#define _GNU_SOURCE
#include <assert.h>
#include <pthread.h>
#include <stdlib.h>
#include "gc/gc.h"
#include "prioq.h"
#include "common.h"
#define PER_THREAD 30
static pq_t *pq;
int nthreads;
pthread_t *ts;
void *add_thread(void *id);
void *removemin_thread(void *id);
void *invariant_thread(void *id);
/* the different tests */
void test_parallel_add(void);
void test_parallel_del(void);
void test_invariants(void);
typedef void (* test_func_t)(void);
test_func_t tests[] = {
test_parallel_del,
test_parallel_add,
// test_invariants,
NULL
};
void
test_parallel_add()
{
printf("test parallel add, %d threads\n", nthreads);
for (long i = 0; i < nthreads; i ++)
pthread_create (&ts[i], NULL, add_thread, (void *)i);
for (long i = 0; i < nthreads; i ++)
(void)pthread_join (ts[i], NULL);
unsigned long new, old = 0;
for (long i = 0; i < nthreads * PER_THREAD; i++) {
new = (long)deletemin(pq);
assert (old < new);
old = new;
}
printf("OK.\n");
}
void
test_parallel_del()
{
printf("test parallel del, %d threads\n", nthreads);
for (long i = 0; i < nthreads * PER_THREAD; i++)
insert(pq, i+1, (pval_t)i+1);
for (long i = 0; i < nthreads; i ++)
pthread_create (&ts[i], NULL, removemin_thread, (void *)i);
for (long i = 0; i < nthreads; i ++)
(void)pthread_join (ts[i], NULL);
printf("OK.\n");
}
void
check_invariants(pq_t *pq)
{
node_t *cur, *pred;
int cnt = 0;
unsigned long long k = 0;
int i = 0;
/* Bottom level */
/* deleted prefix */
cur = pq->head->next[0];
while (is_marked_ref(cur)) {
pred = get_unmarked_ref(cur);
cur = pred->next[0];
cnt++;
}
pred = cur;
cur = pred->next[0];
while (cur != pq->tail) {
assert(!is_marked_ref(cur));
i = 1;
/* pred and succ at each each level is ordered correctly */
while(i < cur->level && cur->next[i]) {
assert(cur->k < cur->next[i]->k);
i++;
}
assert(cur->k > k);
k = cur->k;
pred = cur;
cur = pred->next[0];
cnt++;
}
/* Higher levels */
k = 0;
for (int i = 31; i > 0; i--) {
cur = get_unmarked_ref(pq->head->next[i]);
while(cur != pq->tail) {
cur = get_unmarked_ref(cur->next[i]);
}
}
}
/* test_invariants control of invariant threads */
volatile int halt = 0, stop = 0, abort_loop = 0;
/* A rough way to test that certain invariants always are true.
* Run a certain number of operations, halt, check invariants,
* continue, halt, etc.
* Specifically, it does not check that the invariants hold during
* the execution of an operation.
*/
void
test_invariants()
{
printf("test invariants, %d threads\n", nthreads);
for (long i = 0; i < nthreads * PER_THREAD; i++)
insert(pq, i+1, (pval_t)i + 1);
for (long i = 0; i < nthreads; i ++)
pthread_create (&ts[i], NULL, invariant_thread, (void *)i);
for (int i = 0; i < 200; i++) {
usleep(50000);
halt = 1;
while(stop < nthreads) {
IRMB();
}
printf(".");
fflush(stdout);
check_invariants(pq);
stop = 0;
halt = 0;
IWMB();
}
abort_loop = 1;
for (long i = 0; i < nthreads; i ++)
(void)pthread_join (ts[i], NULL);
printf("\nOK.\n");
}
void
setup (int max_offset)
{
_init_gc_subsystem();
pq = pq_init(max_offset);
}
void
teardown ()
{
pq_destroy(pq);
_destroy_gc_subsystem();
}
int
main(int argc, char **argv)
{
nthreads = 8;
ts = malloc(nthreads * sizeof(pthread_t));
assert(ts);
for(test_func_t *tf = tests; *tf; tf++) {
setup(10);
(*tf)();
teardown();
}
return 0;
}
__thread unsigned short rng[3];
void *
invariant_thread(void *_args)
{
unsigned long id = (unsigned long)_args;
unsigned long elem;
int cnt = 0;
rng_init(rng);
while(!abort_loop) {
if (halt) {
__sync_fetch_and_add(&stop, 1);
while(halt)
IRMB();
}
if (erand48(rng) < 0.5) {
elem = nrand48(rng);
insert(pq, elem+1, (pval_t)elem + 1);
} else {
deletemin(pq);
}
cnt++;
}
return NULL;
}
void *
add_thread(void *id)
{
long base = PER_THREAD * (long)id;
for(int i = 0; i < PER_THREAD; i++)
insert(pq, base+i+1, (pval_t) base+i+1);
return NULL;
}
void *
removemin_thread(void *id)
{
unsigned long v, ov = 0;
for(int i = 0; i < PER_THREAD; i++) {
v = (unsigned long) deletemin(pq);
assert(v > ov);
ov = v;
}
return NULL;
}