]> git.ozlabs.org Git - ccan/blob - ccan/coroutine/test/api-1.c
coroutine: Stack allocation
[ccan] / ccan / coroutine / test / api-1.c
1 #include <stdlib.h>
2
3 #include <ccan/coroutine/coroutine.h>
4 #include <ccan/tap/tap.h>
5
6 #define STKSZ           (COROUTINE_MIN_STKSZ + COROUTINE_STK_OVERHEAD)
7
8 static int global = 0;
9
10 static void trivial_fn(void *p)
11 {
12         struct coroutine_state *ret = (struct coroutine_state *)p;
13
14         global = 1;
15
16         coroutine_jump(ret);
17 }
18
19 static void test_trivial(struct coroutine_stack *stack)
20 {
21         struct coroutine_state t, master;
22
23         ok1(stack != NULL);
24         ok1(coroutine_stack_check(stack, NULL) == stack);
25         ok1(coroutine_stack_size(stack) == COROUTINE_MIN_STKSZ);
26
27         if (!COROUTINE_AVAILABLE) {
28                 skip(1, "Coroutines not available");
29                 return;
30         }
31
32         coroutine_init(&t, trivial_fn, &master, stack);
33         coroutine_switch(&master, &t);
34
35         ok1(global == 1);
36 }
37
38
39 static char buf[STKSZ];
40
41 int main(void)
42 {
43         struct coroutine_stack *stack;
44
45         /* This is how many tests you plan to run */
46         plan_tests(2 * 4 + 1);
47
48         stack = coroutine_stack_init(buf, sizeof(buf), 0);
49         test_trivial(stack);
50         coroutine_stack_release(stack, 0);
51         ok1(!coroutine_stack_check(stack, NULL));
52
53         stack = coroutine_stack_alloc(STKSZ, 0);
54         test_trivial(stack);
55         coroutine_stack_release(stack, 0);
56
57         /* This exits depending on whether all tests passed */
58         return exit_status();
59 }