]> git.ozlabs.org Git - ccan/blobdiff - ccan/coroutine/test/api-3.c
rbuf, tools: clean up rbuf usage.
[ccan] / ccan / coroutine / test / api-3.c
index dcc3531250683d49c3ef3d9c52281917e9445598..013e60a54a99f4879b8b3f31b08d898a324aaf6f 100644 (file)
@@ -27,7 +27,6 @@ static void clobber(void *p)
                buf[i] = random() & 0xff;
        }
 
-       s->total = 0;
        for (i = 0; i < sizeof(buf); i++) {
                s->total += buf[i];
        }
@@ -39,6 +38,11 @@ static void test_metadata(struct coroutine_stack *stack)
 {
        struct metadata *meta;
 
+       ok1(stack != NULL);
+       ok1(coroutine_stack_check(stack, NULL) == stack);
+       ok1(coroutine_stack_size(stack)
+           == BUFSIZE - COROUTINE_STK_OVERHEAD - sizeof(struct metadata));
+
        meta = coroutine_stack_to_metadata(stack, sizeof(*meta));
        ok1(coroutine_stack_from_metadata(meta, sizeof(*meta)) == stack);
 
@@ -48,6 +52,7 @@ static void test_metadata(struct coroutine_stack *stack)
        if (COROUTINE_AVAILABLE) {
                struct coroutine_state t;
                struct state s = {
+                       .total = 0,
                };
 
                coroutine_init(&t, clobber, &s, stack);
@@ -64,25 +69,27 @@ static void test_metadata(struct coroutine_stack *stack)
 
 int main(void)
 {
-       char buf[BUFSIZE];
+       char *buf;
        struct coroutine_stack *stack;
 
        /* This is how many tests you plan to run */
-       plan_tests(9);
+       plan_tests(1 + 2 * 9);
 
        /* Fix seed so we get consistent, though pseudo-random results */       
        srandom(0);
 
-       stack = coroutine_stack_init(buf, sizeof(buf), sizeof(struct metadata));
-       ok1(stack != NULL);
-       ok1(coroutine_stack_check(stack, NULL) == stack);
-       ok1(coroutine_stack_size(stack)
-           == BUFSIZE - COROUTINE_STK_OVERHEAD - sizeof(struct metadata));
-
+       stack = coroutine_stack_alloc(BUFSIZE, sizeof(struct metadata));
        test_metadata(stack);
+       coroutine_stack_release(stack, sizeof(struct metadata));
 
+       buf = malloc(BUFSIZE);
+       ok1(buf != NULL);
+       stack = coroutine_stack_init(buf, BUFSIZE, sizeof(struct metadata));
+       test_metadata(stack);
        coroutine_stack_release(stack, sizeof(struct metadata));
 
+       free(buf);
+
        /* This exits depending on whether all tests passed */
        return exit_status();
 }