aboutsummaryrefslogtreecommitdiff
path: root/src/test.c
blob: e9743cbf95edf7315ed0f0556f927a161623677e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <u.h>

uintptr
printtest(coro *c, uintptr d)
{
    printf("--> Recieved %lu\n", d);
    d = coro·yield(c, d+10);
    printf("--> Now %lu\n", d);

    return d;
}

uintptr
sequence(coro *c, uintptr start)
{
    int d = start;
    for (;;) {
        coro·yield(c, d++);
    }

    return d;
}

struct PrimeMsg 
{
    coro *seq;
    int   p;
};

uintptr
filter(coro *c, uintptr data)
{
    int x, p;
    coro *seq;
    struct PrimeMsg *msg;

    msg = (struct PrimeMsg*)data;
    seq = msg->seq; 
    p   = msg->p;

    for (;;) {
        x = coro·yield(seq, x); 
        if (x % p != 0) {
            x = coro·yield(c, x);
        }
    }

    return 0;
}

int
main()
{
    int i;
    coro *c[4];
    uintptr d;

    printf("Starting singleton test\n");

    for (i = 0; i < arrlen(c); i++) {
        c[i] = coro·new(0, &printtest);
    }

    /* Singleton test */
    d = 0;
    for (i = 0; i < 10; i++) {
        d = coro·yield(c[0], d);
    }

    printf("Starting triplet test\n");

    /* Triplet test */
    for (i = 0; i < 10; i++) {
        d = coro·yield(c[1], d);
        d = coro·yield(c[2], d+100);
        d = coro·yield(c[3], d+200);
    }

    for (i = 0; i < arrlen(c); i++) {
        coro·free(c[i]);
    }

    /* Prime sieve */
    printf("Starting prime test\n");
    uintptr num;
    coro *cur, *seq[50];

    num    = 2;
    seq[0] = coro·new(4096, &sequence);
    cur    = *seq;

    num = coro·yield(cur, num);
    for (i = 1; i < arrlen(seq); i++) {
        seq[i] = coro·new(4096, &filter);
        struct PrimeMsg msg = {
            .seq = cur,
            .p   = num,
        };
        cur = seq[i];
        num = coro·yield(cur, (uintptr)&msg);
        printf("--> prime number %lu\n", num);
    }
}