diff options
| author | Tom Harley | 2020-01-24 05:15:35 +0000 |
|---|---|---|
| committer | Tom Harley | 2020-01-24 05:15:35 +0000 |
| commit | c9f0de12be31eea463bf022f3eee76490b30f8dc (patch) | |
| tree | a80718ff00fa2dcb1c52c2af6828feffc7f89d8b /test | |
| download | parapat-c9f0de12be31eea463bf022f3eee76490b30f8dc.tar.gz parapat-c9f0de12be31eea463bf022f3eee76490b30f8dc.zip | |
Diffstat (limited to 'test')
| -rw-r--r-- | test/Makefile | 23 | ||||
| -rw-r--r-- | test/farm-tests.c | 89 | ||||
| -rw-r--r-- | test/fib.c | 59 | ||||
| -rw-r--r-- | test/minunit.h | 17 | ||||
| -rw-r--r-- | test/pipeline-tests.c | 83 | ||||
| -rw-r--r-- | test/test.c | 55 |
6 files changed, 326 insertions, 0 deletions
diff --git a/test/Makefile b/test/Makefile new file mode 100644 index 0000000..6605f50 --- /dev/null +++ b/test/Makefile @@ -0,0 +1,23 @@ +CFLAGS = -Werror -Wall -Wextra -Wpedantic -std=c89 -ggdb3 -O1 -L.. -I.. +LDLIBS = -lpthread -lparapat +OUTPUT_OPTION := -MMD -MP $(OUTPUT_OPTION) +TRGT = farm-tests pipeline-tests fib + +LD_LIBRARY_PATH := $(LD_LIBRARY_PATH):$(PWD)/.. + +.PHONY: default test clean + +default: test + +test: test.time + +%-tests: ../libparapat.so + +test.time: $(TRGT) + for x in $(TRGT) ; do ./$$x ; done + touch test.time + +clean: + $(RM) *.d *.o $(TRGT) + +-include $(wildcard *.d) diff --git a/test/farm-tests.c b/test/farm-tests.c new file mode 100644 index 0000000..470aa7d --- /dev/null +++ b/test/farm-tests.c @@ -0,0 +1,89 @@ +#include <stdlib.h> +#include <stdio.h> + +#include "minunit.h" + +#include "parapat.h" + +#define unused(x) (x = x) + +int tests_run = 0; + +int x = 1; + +int test_emitter(void **v, void *_) { + int *p = malloc(sizeof *p); + unused(_); + *p = x++; + *v = p; + return x > 10; +} + +void *test_worker(void *v, void *_) { + int *p = v; + unused(_); + *p *= 2; + return v; +} + +void *test_collector(void *l, void *r, void *_) { + int *p = l; + int *q = r; + unused(_); + if (!p) { + p = malloc(sizeof *p); + *p = 0; + } + *p += *q; + /* free(q); */ + return p; +} + +char *farm_simple() { + int a, *res; + x = 1; + a = 0; + res = create_farm(test_emitter, test_worker, test_collector, 5, NULL, NULL, + &a); + + mu_assert("error, *res != 110", *res == 110); + return 0; +} + +/* char *farm_one() { */ +/* int a, *res; */ +/* x = 1; */ +/* a = 0; */ +/* res = create_farm(test_emitter, test_worker, test_collector, 1, NULL, NULL, */ +/* &a); */ + +/* mu_assert("error, *res != 110", *res == 110); */ +/* return 0; */ +/* } */ + +/* char *farm_zero() { */ +/* void *res = create_farm(test_emitter, test_worker, test_collector, 0, NULL, */ +/* NULL, NULL); */ + +/* mu_assert("error, res != NULL", res == NULL); */ +/* return 0; */ +/* } */ + +static char *all_tests() { + mu_run_test(farm_simple); + /* mu_run_test(farm_one); */ + /* mu_run_test(farm_zero); */ + return 0; +} + +int main() { + char *error = all_tests(); + if (error) { + printf("%s\n", error); + } else { + printf("ALL TESTS PASSED\n"); + } + printf("Tests run: %d\n", tests_run); + + return error != 0; +} diff --git a/test/fib.c b/test/fib.c new file mode 100644 index 0000000..65bae54 --- /dev/null +++ b/test/fib.c @@ -0,0 +1,59 @@ +#include <stdio.h> +#include <stdlib.h> + +#include "parapat.h" + +#define unused(x) (x = x) + +int fib(int n) { + int i, Fnew, Fold, temp,ans; + + Fnew = 1; Fold = 0; + for (i = 2; i <= n; i++) { + temp = Fnew; + Fnew = Fnew + Fold; + Fold = temp; + } + ans = Fnew; + return ans; +} + +int payload1(int i) { + unused(i); + return fib(900090000); +} + +int payload2(int j) { + unused(j); + return fib(900090000); +} + +int emit(void **out, void *v) { + int *q; + int *p = v; + *out = malloc(sizeof (int)); + q = *out; + *q = (*p)++; + return *p >= 100; +} + +void *fibw(void *v, void *a) { + int *p = v; + int x = payload1(*p); + unused(a); + *p = payload2(x); + return v; +} + +void *printer(void *l, void *r, void *a) { + unused(l); + unused(r); + unused(a); + return NULL; +} + +int main() { + int i = 0; + create_farm(emit, fibw, printer, 100, &i, NULL, NULL); + return 0; +} diff --git a/test/minunit.h b/test/minunit.h new file mode 100644 index 0000000..a199bf2 --- /dev/null +++ b/test/minunit.h @@ -0,0 +1,17 @@ +/* + * MinUnit comes from a tech-note by Jera Design: + * http://www.jera.com/techinfo/jtns/jtn002.html + */ + +#define mu_assert(message, test) do { \ + if (!(test)) return message; \ +} while (0) + +#define mu_run_test(test) do { \ + char *message; \ + message = test(); \ + tests_run++; \ + if (message) return message; \ +} while (0) + +extern int tests_run; diff --git a/test/pipeline-tests.c b/test/pipeline-tests.c new file mode 100644 index 0000000..2d446a5 --- /dev/null +++ b/test/pipeline-tests.c @@ -0,0 +1,83 @@ +#include <stdlib.h> +#include <stdio.h> + +#include "minunit.h" + +#include "parapat.h" + +#define unused(x) (x = x) + +int tests_run = 0; + +int x = 1; + +int test_emitter(void **v, void *_) { + int *p = malloc(sizeof *p); + unused(_); + *p = x++; + *v = p; + return x > 3; +} + +void *test_worker(void *v, void *_) { + int *p = v; + unused(_); + *p *= 2; + return v; +} + +void *test_collector(void *l, void *r, void *_) { + int *p = l; + int *q = r; + unused(_); + if (!p) { + p = malloc(sizeof *p); + *p = 0; + } + *p += *q; + free(q); + return p; +} + +char *pipeline_simple() { + int a, *res; + worker *wfns[] = { test_worker, test_worker, test_worker, test_worker }; + x = 1; + a = 0; + res = create_pipeline(test_emitter, wfns, 4, test_collector, NULL, NULL, + &a); + + mu_assert("error, *res != 96", *res == 96); + return 0; +} + +char *pipeline_one() { + int a, *res; + worker *wfns[] = { test_worker }; + x = 1; + a = 0; + res = create_pipeline(test_emitter, wfns, 1, test_collector, NULL, NULL, + &a); + + printf("answer is %d\n", *res); + mu_assert("error, *res != 12", *res == 12); + return 0; +} + +static char *all_tests() { + mu_run_test(pipeline_simple); + mu_run_test(pipeline_one); + return 0; +} + +int main() { + char *error = all_tests(); + if (error) { + printf("%s\n", error); + } else { + printf("ALL TESTS PASSED\n"); + } + printf("Tests run: %d\n", tests_run); + + return error != 0; +} diff --git a/test/test.c b/test/test.c new file mode 100644 index 0000000..8f51f62 --- /dev/null +++ b/test/test.c @@ -0,0 +1,55 @@ +#include <stdio.h> + +#include "parapat.h" + +typedef struct { + int v; +} arg_t; + +void *test_work(void *v) { + arg_t *a = v; + a->v *= 2; + return a; +} + +void *test_fold(void *a, void *b) { + arg_t *x = a, *y = b; + + x->v += y->v; + + return x; +} + +int main() { + int i; + arg_t a, *res = &a; + + arg_t args[10]; + arg_t *ptrs[10]; + + work *fns[4] = { + &test_work, + &test_work, + &test_work, + &test_work + }; + + pipeline *pl; + + for (i = 0; i < 10; ++i) { + args[i].v = i; + ptrs[i] = &args[i]; + } + + a.v = 0; + + res = create_farm(&test_work, &test_fold, 10, (void **) &ptrs[0]); + printf("after farm: %d\n", res->v); + + pl = create_pipeline(fns, 4); + push(pl->in, res); + res = pop(pl->out); + printf("after pipeline: %d\n", res->v); + + return 0; +} |
