-
Notifications
You must be signed in to change notification settings - Fork 0
/
pt_test.c
281 lines (243 loc) · 6.23 KB
/
pt_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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#include <assert.h>
#include "pt.h"
/*
* Test reentrant functions with local continuations
*/
static void pt_reentrant(struct pt *pt, int *reent, int *state) {
(*reent)++;
pt_begin(pt);
pt_label(pt, 1);
if (*state == 0) {
*state = 1;
return;
}
pt_label(pt, 2);
if (*state == 1) {
*state = 2;
return;
}
pt_end(pt);
}
static void test_local_continuation() {
int reent = 0;
int state = 0;
struct pt pt = pt_init();
pt_reentrant(&pt, &reent, &state);
assert(pt_status(&pt) == 1 && reent == 1 && state == 1);
pt_reentrant(&pt, &reent, &state);
assert(pt_status(&pt) == 2 && reent == 2 && state == 2);
pt_reentrant(&pt, &reent, &state);
assert(pt_status(&pt) == -1 && reent == 3 && state == 2);
pt_reentrant(&pt, &reent, &state);
assert(pt_status(&pt) == -1 && reent == 4 && state == 2);
}
/*
* Test empty protothread
*/
static void pt_empty(struct pt *pt, int *reent) {
pt_begin(pt);
(*reent)++;
pt_end(pt);
}
static void test_empty() {
int reent = 0;
struct pt pt = pt_init();
pt_empty(&pt, &reent);
assert(reent == 1);
/* shoud not re-enter */
pt_empty(&pt, &reent);
assert(reent == 1);
}
/*
* Test waiting for certain conditions
*/
static void pt_wait_ready(struct pt *pt, int *reent, int ready) {
pt_begin(pt);
(*reent)++;
pt_wait(pt, ready);
(*reent)++;
pt_end(pt);
}
static void test_wait() {
int ready = 0;
int reent = 0;
struct pt pt = pt_init();
pt_wait_ready(&pt, &reent, ready);
assert(reent == 1);
pt_wait_ready(&pt, &reent, ready);
assert(reent == 1);
ready = 1;
pt_wait_ready(&pt, &reent, ready);
assert(reent == 2);
}
/*
* Test protothread explicit yielding
*/
static void pt_yielding(struct pt *pt, int *state) {
pt_begin(pt);
(*state) = 1;
pt_yield(pt);
(*state) = 2;
pt_end(pt);
}
static void test_yield() {
int state = 0;
struct pt pt1 = pt_init();
struct pt pt2 = pt_init();
pt_yielding(&pt1, &state);
assert(pt_status(&pt1) == PT_STATUS_BLOCKED && state == 1);
pt_yielding(&pt2, &state);
assert(pt_status(&pt2) == PT_STATUS_BLOCKED && state == 1);
pt_yielding(&pt1, &state);
assert(pt_status(&pt1) == PT_STATUS_FINISHED && state == 2);
pt_yielding(&pt2, &state);
assert(pt_status(&pt2) == PT_STATUS_FINISHED && state == 2);
}
/*
* Test protothread early termination
*/
static void pt_exiting(struct pt *pt, int *state) {
pt_begin(pt);
(*state) = 1;
pt_exit(pt, PT_STATUS_FINISHED);
(*state) = 2;
pt_end(pt);
}
static void test_exit() {
int state = 0;
struct pt pt = pt_init();
pt_exiting(&pt, &state);
assert(state == 1 && pt_status(&pt) == PT_STATUS_FINISHED);
pt_exiting(&pt, &state);
assert(state == 1 && pt_status(&pt) == PT_STATUS_FINISHED);
}
/*
* Test complex protothread wait loops
*/
static void pt_loop_with_braces(struct pt *pt, int *state, int timeout,
int ready) {
pt_begin(pt);
*state = 1;
pt_loop(pt, !timeout) {
if (ready) {
break;
}
(*state)++;
}
*state = -1;
pt_end(pt);
}
static void pt_loop_without_braces(struct pt *pt, int *state, int timeout,
int ready) {
pt_begin(pt);
*state = 1;
// clang-format off
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdangling-else"
#endif
if (1)
pt_loop(pt, !timeout)
if (ready)
break;
else
(*state)++;
#ifdef __clang__
#pragma clang diagnostic pop
#endif
// clang-format on
*state = -1;
pt_end(pt);
}
static void test_loop() {
int state = 0;
struct pt pt1 = pt_init();
struct pt pt2 = pt_init();
struct pt pt3 = pt_init();
pt_loop_with_braces(&pt1, &state, 0, 0);
assert(state == 2);
pt_loop_with_braces(&pt1, &state, 0, 0);
assert(state == 3);
pt_loop_with_braces(&pt1, &state, 1, 0);
assert(state == -1);
pt_loop_without_braces(&pt2, &state, 0, 0);
assert(state == 2);
pt_loop_without_braces(&pt2, &state, 0, 0);
assert(state == 3);
pt_loop_without_braces(&pt2, &state, 1, 0);
assert(state == -1);
pt_loop_without_braces(&pt3, &state, 0, 0);
assert(state == 2);
pt_loop_without_braces(&pt3, &state, 0, 0);
assert(state == 3);
pt_loop_without_braces(&pt3, &state, 0, 1);
assert(state == -1);
}
struct pair {
int x;
int y;
};
typedef pt_queue(int, 3) int_queue_t;
typedef pt_queue(struct pair, 3) pair_queue_t;
static void pt_add(struct pt *pt, pair_queue_t *pairs, int_queue_t *ints) {
pt_begin(pt);
for (;;) {
pt_wait(pt, !pt_queue_empty(pairs));
struct pair *p = pt_queue_pop(pairs);
pt_wait(pt, !pt_queue_full(ints));
pt_queue_push(ints, p->x + p->y);
}
pt_end(pt);
}
static void test_queues() {
struct pt pt = pt_init();
int_queue_t ints = pt_queue_init();
pair_queue_t pairs = pt_queue_init();
assert(pt_queue_len(&ints) == 3);
assert(pt_queue_cap(&ints) == 0);
assert(pt_queue_empty(&ints));
assert(!pt_queue_full(&ints));
assert(pt_queue_len(&pairs) == 3);
assert(pt_queue_cap(&pairs) == 0);
assert(pt_queue_empty(&pairs));
assert(!pt_queue_full(&pairs));
assert(pt_queue_push(&ints, 1));
assert(!pt_queue_empty(&ints));
assert(!pt_queue_full(&ints));
assert(pt_queue_push(&ints, 3));
assert(pt_queue_len(&ints) == 3 && pt_queue_cap(&ints) == 2);
assert(pt_queue_push(&ints, 5));
assert(!pt_queue_push(&ints, 7));
assert(pt_queue_full(&ints));
assert(*(pt_queue_pop(&ints)) == 1);
assert(!pt_queue_full(&ints));
assert(*(pt_queue_pop(&ints)) == 3);
assert(*(pt_queue_pop(&ints)) == 5);
assert(pt_queue_pop(&ints) == NULL);
assert(!pt_queue_full(&ints));
assert(pt_queue_empty(&ints));
pt_queue_push(&pairs, ((struct pair){1, 2}));
pt_queue_push(&pairs, ((struct pair){7, 5}));
pt_add(&pt, &pairs, &ints);
assert(*(pt_queue_peek(&ints)) == 3);
assert(*(pt_queue_peek(&ints)) == 3);
assert(*(pt_queue_pop(&ints)) == 3);
assert(*(pt_queue_pop(&ints)) == 12);
assert(pt_queue_pop(&ints) == NULL);
assert(pt_queue_peek(&ints) == NULL);
pt_add(&pt, &pairs, &ints);
assert(pt_queue_pop(&ints) == NULL);
pt_queue_push(&pairs, ((struct pair){123, 456}));
pt_add(&pt, &pairs, &ints);
assert(*(pt_queue_pop(&ints)) == 123 + 456);
}
int main() {
test_local_continuation();
test_empty();
test_wait();
test_yield();
test_exit();
test_loop();
test_queues();
return 0;
}