From 2dc8bef8bc4a8e48996a73268e48ce014d616509 Mon Sep 17 00:00:00 2001 From: Dmitry Simonenko Date: Mon, 15 May 2017 16:55:10 +0300 Subject: [PATCH] machinarium: add condition tests --- test-suite/machinarium_test.c | 4 +++ test-suite/makefile | 4 ++- test-suite/test_condition0.c | 53 +++++++++++++++++++++++++++++++++++ test-suite/test_condition1.c | 51 +++++++++++++++++++++++++++++++++ 4 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 test-suite/test_condition0.c create mode 100644 test-suite/test_condition1.c diff --git a/test-suite/machinarium_test.c b/test-suite/machinarium_test.c index 5f091035..2ab79027 100644 --- a/test-suite/machinarium_test.c +++ b/test-suite/machinarium_test.c @@ -17,6 +17,8 @@ extern void test_sleep_cancel0(void); extern void test_sleep_cancel1(void); extern void test_wait(void); +extern void test_condition0(void); +extern void test_condition1(void); int main(int argc, char *argv[]) @@ -28,5 +30,7 @@ main(int argc, char *argv[]) machinarium_test(test_sleep_cancel0); machinarium_test(test_sleep_cancel1); machinarium_test(test_wait); + machinarium_test(test_condition0); + machinarium_test(test_condition1); return 0; } diff --git a/test-suite/makefile b/test-suite/makefile index d456304b..086bbd9f 100644 --- a/test-suite/makefile +++ b/test-suite/makefile @@ -11,7 +11,9 @@ OBJECTS = machinarium_test.o \ test_sleep_yield.o \ test_sleep_cancel0.o \ test_sleep_cancel1.o \ - test_wait.o + test_wait.o \ + test_condition0.o \ + test_condition1.o all: clean $(OBJECTS) machinarium_test machinarium_test: $(CC) $(OBJECTS) $(LFLAGS) -o machinarium_test diff --git a/test-suite/test_condition0.c b/test-suite/test_condition0.c new file mode 100644 index 00000000..b93881ce --- /dev/null +++ b/test-suite/test_condition0.c @@ -0,0 +1,53 @@ + +/* + * machinarium. + * + * Cooperative multitasking engine. +*/ + +#include +#include + +static void +test_condition_fiber(void *arg) +{ + machine_t machine = arg; + int rc = machine_condition(machine, 1000); + test(rc == 0); +} + +static void +test_waiter(void *arg) +{ + machine_t machine = arg; + + int64_t a; + a = machine_create_fiber(machine, test_condition_fiber, machine); + test(a != -1); + + machine_sleep(machine, 0); + + int rc; + rc = machine_signal(machine, a); + test(rc != -1); + + machine_sleep(machine, 0); + + machine_stop(machine); +} + +void +test_condition0(void) +{ + machine_t machine = machine_create(); + test(machine != NULL); + + int rc; + rc = machine_create_fiber(machine, test_waiter, machine); + test(rc != -1); + + machine_start(machine); + + rc = machine_free(machine); + test(rc != -1); +} diff --git a/test-suite/test_condition1.c b/test-suite/test_condition1.c new file mode 100644 index 00000000..1a5847f1 --- /dev/null +++ b/test-suite/test_condition1.c @@ -0,0 +1,51 @@ + +/* + * machinarium. + * + * Cooperative multitasking engine. +*/ + +#include +#include + +static void +test_condition_fiber(void *arg) +{ + machine_t machine = arg; + int rc = machine_condition(machine, 1); + test(rc == -1); +} + +static void +test_waiter(void *arg) +{ + machine_t machine = arg; + + int64_t a; + a = machine_create_fiber(machine, test_condition_fiber, machine); + test(a != -1); + + machine_sleep(machine, 100); + + int rc; + rc = machine_signal(machine, a); + test(rc == -1); + + machine_stop(machine); +} + +void +test_condition1(void) +{ + machine_t machine = machine_create(); + test(machine != NULL); + + int rc; + rc = machine_create_fiber(machine, test_waiter, machine); + test(rc != -1); + + machine_start(machine); + + rc = machine_free(machine); + test(rc != -1); +}