From 392c9aff947a41e7e0da0b1a9612e174cfa956a7 Mon Sep 17 00:00:00 2001 From: Nicholas Noll Date: Sat, 18 Apr 2020 13:30:03 -0700 Subject: test: added prime sieve test of coroutines --- src/test.c | 103 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 src/test.c (limited to 'src/test.c') diff --git a/src/test.c b/src/test.c new file mode 100644 index 0000000..e9743cb --- /dev/null +++ b/src/test.c @@ -0,0 +1,103 @@ +#include + +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); + } +} -- cgit v1.2.1