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;
41 meta = coroutine_stack_to_metadata(stack, sizeof(*meta));
42 ok1(coroutine_stack_from_metadata(meta, sizeof(*meta)) == stack);
44 meta->magic = META_MAGIC;
45 ok1(meta->magic == META_MAGIC);
47 if (COROUTINE_AVAILABLE) {
48 struct coroutine_state t;
53 coroutine_init(&t, clobber, &s, stack);
54 coroutine_switch(&s.ret, &t);
57 skip(1, "Coroutines not available");
60 ok1(coroutine_stack_to_metadata(stack, sizeof(*meta)) == meta);
61 ok1(coroutine_stack_from_metadata(meta, sizeof(*meta)) == stack);
62 ok1(meta->magic == META_MAGIC);
68 struct coroutine_stack *stack;
70 /* This is how many tests you plan to run */
73 /* Fix seed so we get consistent, though pseudo-random results */
76 stack = coroutine_stack_init(buf, sizeof(buf), sizeof(struct metadata));
78 ok1(coroutine_stack_check(stack, NULL) == stack);
79 ok1(coroutine_stack_size(stack)
80 == BUFSIZE - COROUTINE_STK_OVERHEAD - sizeof(struct metadata));
84 coroutine_stack_release(stack, sizeof(struct metadata));
86 /* This exits depending on whether all tests passed */