3 #include <ccan/coroutine/coroutine.h>
4 #include <ccan/tap/tap.h>
7 #define META_MAGIC 0x4d86aa82ec1892f6
15 struct coroutine_state ret;
19 /* Touch a bunch of stack */
20 static void clobber(void *p)
22 struct state *s = (struct state *)p;
23 char buf[BUFSIZE - COROUTINE_MIN_STKSZ];
26 for (i = 0; i < sizeof(buf); i++) {
27 buf[i] = random() & 0xff;
30 for (i = 0; i < sizeof(buf); i++) {
34 coroutine_jump(&s->ret);
37 static void test_metadata(struct coroutine_stack *stack)
39 struct metadata *meta;
42 ok1(coroutine_stack_check(stack, NULL) == stack);
43 ok1(coroutine_stack_size(stack)
44 == BUFSIZE - COROUTINE_STK_OVERHEAD - sizeof(struct metadata));
46 meta = coroutine_stack_to_metadata(stack, sizeof(*meta));
47 ok1(coroutine_stack_from_metadata(meta, sizeof(*meta)) == stack);
49 meta->magic = META_MAGIC;
50 ok1(meta->magic == META_MAGIC);
52 if (COROUTINE_AVAILABLE) {
53 struct coroutine_state t;
58 coroutine_init(&t, clobber, &s, stack);
59 coroutine_switch(&s.ret, &t);
62 skip(1, "Coroutines not available");
65 ok1(coroutine_stack_to_metadata(stack, sizeof(*meta)) == meta);
66 ok1(coroutine_stack_from_metadata(meta, sizeof(*meta)) == stack);
67 ok1(meta->magic == META_MAGIC);
73 struct coroutine_stack *stack;
75 /* This is how many tests you plan to run */
76 plan_tests(1 + 2 * 9);
78 /* Fix seed so we get consistent, though pseudo-random results */
81 stack = coroutine_stack_alloc(BUFSIZE, sizeof(struct metadata));
83 coroutine_stack_release(stack, sizeof(struct metadata));
85 buf = malloc(BUFSIZE);
87 stack = coroutine_stack_init(buf, BUFSIZE, sizeof(struct metadata));
89 coroutine_stack_release(stack, sizeof(struct metadata));
93 /* This exits depending on whether all tests passed */