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