-
Notifications
You must be signed in to change notification settings - Fork 5
/
koishi_test.c
140 lines (121 loc) · 3.56 KB
/
koishi_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
#undef NDEBUG
#include <koishi.h>
#include <stdio.h>
#include <stdint.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
void *cofunc1(void *data) {
assert(koishi_state(koishi_active()) == KOISHI_RUNNING);
char *str = data;
printf("C: start coroutine (got %s)\n", str);
printf("C: yielding 1\n");
str = koishi_yield("Reimu");
printf("C: resumed 1 (got %s)\n", str);
printf("C: yielding 2\n");
str = koishi_yield("Marisa");
printf("C: resumed 2 (got %s)\n", str);
printf("C: yielding 3\n");
str = koishi_yield("Youmu");
printf("C: resumed 3 (got %s)\n", str);
printf("C: done\n");
koishi_die("Bye");
// return "Bye";
}
void *cofunc_nested(void *data) {
koishi_coroutine_t *caller = data;
assert(koishi_state(caller) == KOISHI_IDLE);
assert(koishi_state(koishi_active()) == KOISHI_RUNNING);
return (void*)42;
}
void *cofunc2(void *data) {
assert(koishi_state(koishi_active()) == KOISHI_RUNNING);
koishi_coroutine_t co, *c2 = &co;
koishi_init(c2, 0, cofunc_nested);
int nested_return = (intptr_t)koishi_resume(c2, koishi_active());
assert(koishi_state(koishi_active()) == KOISHI_RUNNING);
assert(nested_return == 42);
koishi_deinit(c2);
intptr_t i = 0;
printf("C: start coroutine\n");
while(1) {
i += (intptr_t)data;
i = (2 * i) * (3 * i);
printf("C: yielding %zi\n", (size_t)i);
assert(koishi_state(koishi_active()) == KOISHI_RUNNING);
koishi_yield((void*)i);
assert(koishi_state(koishi_active()) == KOISHI_RUNNING);
}
return NULL;
}
void test1(koishi_coroutine_t *c) {
char *str;
koishi_init(c, 128 * 1024, cofunc1);
printf("O: created coroutine\n");
printf("O: resume 1\n");
str = koishi_resume(c, "Hello");
printf("O: post yield 1 (got %s)\n", str);
assert(!strcmp(str, "Reimu"));
}
void test2(koishi_coroutine_t *c) {
intptr_t i;
koishi_recycle(c, cofunc2);
assert(koishi_state(c) == KOISHI_SUSPENDED);
printf("O: recycled coroutine\n");
printf("O: resume 1\n");
i = (intptr_t)koishi_resume(c, (void*)1);
printf("O: post yield 1 (got %zi)\n", (size_t)i);
printf("O: resume 2\n");
i = (intptr_t)koishi_resume(c, (void*)2);
printf("O: post yield 2 (got %zi)\n", (size_t)i);
assert(koishi_state(c) == KOISHI_SUSPENDED);
}
void *cancelled_caller_test_inner(void *data) {
koishi_coroutine_t *outer = data;
koishi_kill(outer, (void*)42);
koishi_yield(NULL);
return NULL;
}
void *cancelled_caller_test_outer(void *data) {
koishi_coroutine_t *inner = data;
koishi_resume(inner, koishi_active());
abort(); // unreachable
}
void cancelled_caller_test(int *result) {
koishi_coroutine_t inner, outer;
koishi_init(&inner, 0, cancelled_caller_test_inner);
koishi_init(&outer, 0, cancelled_caller_test_outer);
*result = (intptr_t)koishi_resume(&outer, &inner);
koishi_deinit(&inner);
koishi_deinit(&outer);
}
int main(int argc, char **argv) {
if(argc != 1) {
printf("%s takes no arguments.\n", argv[0]);
return 1;
}
assert(koishi_state(koishi_active()) == KOISHI_RUNNING);
char *str;
koishi_coroutine_t co, *c = &co;
test1(&co);
printf("O: resume 2\n");
str = koishi_resume(c, "Hakurei");
printf("O: post yield 2 (got %s)\n", str);
assert(!strcmp(str, "Marisa"));
printf("O: resume 3\n");
str = koishi_resume(c, "Kirisame");
printf("O: post yield 3 (got %s)\n", str);
assert(!strcmp(str, "Youmu"));
printf("O: resume 4\n");
str = koishi_resume(c, "Konpaku");
printf("O: done (got %s)\n", str);
assert(koishi_state(c) == KOISHI_DEAD);
assert(koishi_state(koishi_active()) == KOISHI_RUNNING);
test2(&co);
koishi_deinit(c);
int result = 0;
cancelled_caller_test(&result);
assert(result == 42);
printf("Done\n");
return 0;
}