aboutsummaryrefslogtreecommitdiff
path: root/src/base/test.c
blob: a29be1d1485b13177093b333efbb2b3d1da2ab37 (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include <u.h>
#include <base.h>
#include <base/macro/map.h>

#include <time.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;

    // Need to copy relevant variables onto the local stack
    // Data is volatile.
    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;
}

error
test·coro()
{
    int i;
    Coro *c[4];
    uintptr d;

    printf("Starting singleton test\n");

    for (i = 0; i < arrlen(c); i++) {
        c[i] = coro·make(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·make(4096, &sequence);
    cur    = *seq;

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

int
less(void* a, void* b)
{
    int ai, bi;
    ai = *(int*)a;
    bi = *(int*)b;

    return ai - bi;
}

error
test·sort()
{
    clock_t t;
    int i, test[10000];
    for (i = 0; i < arrlen(test); i++) {
        test[i] = rand();
    }

    t = clock();
    sort·int(arrlen(test), test);
    t = clock() - t;
    printf("inlined code took %f ms to execute\n", 1000.*t/CLOCKS_PER_SEC);

    for (i = 0; i < arrlen(test); i++) {
        test[i] = rand();
    }

    t = clock();
    qsort(test, arrlen(test), sizeof(int), (int (*)(const void *, const void *))less);
    t = clock() - t;
    printf("std qsort code took %f ms to execute\n", 1000.*t/CLOCKS_PER_SEC);

    /*
    for (i = 1; i < arrlen(test); i++) {
        if (test[i] >= test[i-1]) {
            printf("%d is less that %d\n", test[i], test[i-1]);
        } else {
            printf("ERROR: %d is NOT less that %d\n", test[i], test[i-1]);
        }
    }
    */

    return 0;
}

error
main()
{
    error err;
#if 0
    if (err = test·coro(), err) {
        errorf("test fail: coroutine");
    }
#endif
    if (err = test·sort(), err) {
        errorf("test fail: coroutine");
    }
}