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 diag("Wrote random to buffer\n");
33 for (i = 0; i < sizeof(buf); i++) {
37 coroutine_jump(&s->ret);
40 static void test_metadata(struct coroutine_stack *stack)
42 struct metadata *meta;
44 meta = coroutine_stack_to_metadata(stack, sizeof(*meta));
45 ok1(coroutine_stack_from_metadata(meta, sizeof(*meta)) == stack);
47 meta->magic = META_MAGIC;
48 ok1(meta->magic == META_MAGIC);
50 if (COROUTINE_AVAILABLE) {
51 struct coroutine_state t;
55 coroutine_init(&t, clobber, &s, stack);
56 coroutine_switch(&s.ret, &t);
59 skip(1, "Coroutines not available");
62 ok1(coroutine_stack_to_metadata(stack, sizeof(*meta)) == meta);
63 ok1(coroutine_stack_from_metadata(meta, sizeof(*meta)) == stack);
64 ok1(meta->magic == META_MAGIC);
70 struct coroutine_stack *stack;
72 /* This is how many tests you plan to run */
75 /* Fix seed so we get consistent, though pseudo-random results */
78 stack = coroutine_stack_init(buf, sizeof(buf), sizeof(struct metadata));
80 ok1(coroutine_stack_check(stack, NULL) == stack);
81 ok1(coroutine_stack_size(stack)
82 == BUFSIZE - COROUTINE_STK_OVERHEAD - sizeof(struct metadata));
86 coroutine_stack_release(stack, sizeof(struct metadata));
88 /* This exits depending on whether all tests passed */