forked from tidwall/neco
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_basic.c
221 lines (190 loc) · 5.81 KB
/
test_basic.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
#include <signal.h>
#include "tests.h"
static int64_t lastid = 0;
void co_basic_startv(int argc, void *argv[]) {
assert(argc == 1);
lastid = neco_getid();
int x = *(int*)argv[0];
assert(x == 1977);
}
void co_basic_start(int argc, void *argv[]) {
assert(argc == 1);
int *x = argv[0];
*x = 1977;
#ifdef IS_FAIL_TARGET
neco_fail_neco_malloc_counter = 1;
expect(neco_start(co_basic_start, 0), NECO_NOMEM);
neco_fail_stack_get_counter = 1;
expect(neco_start(co_basic_start, 0), NECO_NOMEM);
// This will trigger a NOMEM on number of arguments over 4
neco_fail_neco_malloc_counter = 2;
expect(neco_start(co_basic_start, 5, 0, 0, 0, 0, 0), NECO_NOMEM);
#endif
expect(neco_startv(co_basic_startv, 1, &(void*){x}), NECO_OK);
assert(neco_lastid() == lastid);
expect(neco_startv(co_basic_startv, 1, &(void*){x}), NECO_OK);
assert(neco_lastid() == lastid);
expect(neco_startv(co_basic_startv, 1, &(void*){x}), NECO_OK);
assert(neco_lastid() == lastid);
}
void test_basic_start(void) {
int x = 0;
expect(neco_start(co_basic_start, 1, &x), NECO_OK);
assert(x == 1977);
// These will fail
#ifdef IS_FAIL_TARGET
for (int i = 1; i <= 500; i++) {
neco_fail_neco_malloc_counter = i;
int ret = neco_start(co_basic_start, 1, &x);
if (ret == NECO_OK) {
break;
}
if (ret != NECO_NOMEM) {
fprintf(stderr, "expected NECO_NOMEM, got %s (iter %d)\n",
neco_strerror(ret), i);
assert(0);
}
}
// neco_fail_neco_malloc_counter = 1;
// expect(neco_start(co_basic_start, 0), NECO_NOMEM);
// neco_fail_neco_malloc_counter = 2;
// expect(neco_start(co_basic_start, 0), NECO_NOMEM);
// neco_fail_neco_malloc_counter = 3;
// expect(neco_start(co_basic_start, 0), NECO_NOMEM);
// neco_fail_neco_malloc_counter = 4;
// expect(neco_start(co_basic_start, 0), NECO_NOMEM);
// neco_fail_neco_malloc_counter = 5;
// expect(neco_start(co_basic_start, 0), NECO_NOMEM);
// neco_fail_neco_malloc_counter = 6;
// expect(neco_start(co_basic_start, 0), NECO_NOMEM);
// neco_fail_neco_malloc_counter = 7;
// expect(neco_start(co_basic_start, 0), NECO_NOMEM);
#endif
expect(neco_start(co_basic_start, -1), NECO_INVAL);
expect(neco_lastid(), NECO_PERM);
}
void co_basic_stats(int argc, void *argv[]) {
assert(argc == 0);
(void)argv;
neco_stats stats;
expect(neco_getstats(&stats), NECO_OK);
assert(stats.coroutines == 1);
}
void test_basic_stats(void) {
neco_stats stats;
expect(neco_getstats(&stats), NECO_PERM);
expect(neco_getstats(0), NECO_INVAL);
assert(neco_start(co_basic_stats, 0) == NECO_OK);
}
void co_sched1(int argc, void *argv[]) {
assert(argc == 2);
char *a = argv[0];
int *i = argv[1];
a[(*i)++] = 'B';
assert(neco_yield() == NECO_OK);
a[(*i)++] = 'D';
}
void co_sched2(int argc, void *argv[]) {
assert(argc == 2);
char *a = argv[0];
int *i = argv[1];
a[(*i)++] = 'E';
assert(neco_yield() == NECO_OK);
a[(*i)++] = 'G';
}
void co_sched(int argc, void *argv[]) {
assert(argc == 2);
char *a = argv[0];
int *i = argv[1];
a[(*i)++] = 'A';
assert(neco_start(co_sched1, 2, a, i) == NECO_OK);
a[(*i)++] = 'C';
assert(neco_start(co_sched2, 2, a, i) == NECO_OK);
a[(*i)++] = 'F';
assert(neco_yield() == NECO_OK);
a[(*i)++] = 'H';
}
void test_basic_sched(void) {
expect(neco_yield(), NECO_PERM);
// Tests the scheduling order.
char a[10];
int i = 0;
assert(neco_start(co_sched, 2, a, &i) == NECO_OK);
a[i++] = '\0';
char exp[] = "ABCDEFGH";
if (strcmp(a, exp) != 0) {
printf("expected '%s' got '%s'\n", exp, a);
abort();
}
}
void co_sleep(int argc, void *argv[]) {
(void)argc; (void)argv;
expect(neco_sleep(-1), NECO_TIMEDOUT);
expect(neco_cancel(neco_getid()), NECO_OK);
expect(neco_sleep(INT64_MAX), NECO_CANCELED);
expect(neco_sleep(INT64_MIN), NECO_TIMEDOUT);
}
void co_sleep_rand_child(int argc, void *argv[]) {
assert(argc == 1);
int64_t dl = *(int64_t*)argv[0];
int nsecs = (int64_t)(rand_double() * (double)(NECO_SECOND/10));
expect(neco_sleep(nsecs), NECO_OK);
expect(neco_sleep_dl(dl), NECO_OK);
}
void co_sleep_rand(int argc, void *argv[]) {
(void)argc; (void)argv;
int N = 100;
int64_t dl = neco_now() + NECO_SECOND/2;
for (int i = 0; i < N; i++) {
expect(neco_start(co_sleep_rand_child, 1, &dl), NECO_OK);
}
}
void test_basic_sleep(void) {
expect(neco_sleep(0), NECO_PERM);
expect(neco_start(co_sleep, 0), NECO_OK);
expect(neco_start(co_sleep_rand, 0), NECO_OK);
}
void test_basic_misc(void) {
assert(neco_starterid() == NECO_PERM);
expect(neco_is_main_thread(), NECO_PERM);
expect(neco_now(), NECO_PERM);
assert(strlen(neco_switch_method()) > 0);
}
static int val = 0;
void rt1(void *arg) {
(*(int*)arg)++;
}
void co_exit(int argc, void *argv[]) {
(void)argc; (void)argv;
val++;
neco_cleanup_push(rt1, &val);
neco_exit();
neco_cleanup_pop(1);
val++;
}
void test_basic_exit(void) {
neco_exit(); // Noop
val = 0;
expect(neco_start(co_exit, 0), NECO_OK);
assert(val == 2);
}
void test_basic_malloc(void) {
void *ptr = neco_malloc(100);
assert(ptr);
ptr = neco_realloc(ptr, 200);
assert(ptr);
neco_free(ptr);
}
int main(int argc, char **argv) {
do_test(test_basic_start);
do_test(test_basic_stats);
do_test(test_basic_sched);
do_test(test_basic_sleep);
do_test(test_basic_exit);
do_test_(test_basic_malloc, false);
do_test(test_basic_misc);
// The next lines test that neco_free and exit_prog operations can be
// reached from outside of a neco context
neco_free(neco_malloc(100));
__neco_exit_prog(0);
}