summaryrefslogtreecommitdiff
path: root/source/test/unittests
diff options
context:
space:
mode:
authorMitya Selivanov <automainint@guattari.tech>2023-04-08 01:41:32 +0200
committerMitya Selivanov <automainint@guattari.tech>2023-04-08 01:41:32 +0200
commitfa8d01934681195aacea8b95c78f27ca6a660bdd (patch)
tree98c5efa3b6b0cb9a877f244df441f012ff0fbee0 /source/test/unittests
parent4d720c77b3a91d517552e8b991577936f172a5cb (diff)
downloadkit-fa8d01934681195aacea8b95c78f27ca6a660bdd.zip
Simplify async function
Diffstat (limited to 'source/test/unittests')
-rw-r--r--source/test/unittests/async_function.test.c138
1 files changed, 30 insertions, 108 deletions
diff --git a/source/test/unittests/async_function.test.c b/source/test/unittests/async_function.test.c
index 5adc0e7..a10dbff 100644
--- a/source/test/unittests/async_function.test.c
+++ b/source/test/unittests/async_function.test.c
@@ -21,8 +21,9 @@ CORO_IMPL(test_bar) {
CORO_END
STATIC_CORO(int, test_gen, int i; int min; int max;) {
- for (af i = af min; af i < af max; af i++) AF_YIELD(af i);
- AF_RETURN(af max);
+ for (self->i = self->min; self->i < self->max; self->i++)
+ AF_YIELD(self->i);
+ AF_RETURN(self->max);
}
CORO_END
@@ -34,54 +35,19 @@ STATIC_CORO_VOID(test_task, ) {
CORO_END
STATIC_CORO_VOID(test_nest_task, AF_TYPE(test_task) promise;) {
- AF_INIT(af promise, test_task, );
- AF_AWAIT(af promise);
- AF_AWAIT(af promise);
- AF_AWAIT(af promise);
+ AF_INIT(self->promise, test_task, );
+ AF_AWAIT(self->promise);
+ AF_AWAIT(self->promise);
+ AF_AWAIT(self->promise);
}
CORO_END
STATIC_CORO(int, test_nest_generator, AF_TYPE(test_gen) promise;) {
- AF_INIT(af promise, test_gen, .min = 1, .max = 3);
- AF_YIELD_AWAIT(af promise);
+ AF_INIT(self->promise, test_gen, .min = 1, .max = 3);
+ AF_YIELD_AWAIT(self->promise);
}
CORO_END
-STATIC_CORO(int, test_join_multiple, AF_TYPE(test_bar) promises[3];) {
- int i;
- for (i = 0; i < 3; i++)
- AF_INIT(af promises[i], test_bar, .return_value = 0);
- AF_RESUME_AND_JOIN_ALL(af promises);
- AF_RETURN(af promises[0].return_value +
- af promises[1].return_value +
- af promises[2].return_value);
-}
-CORO_END
-
-STATIC_CORO(int, test_await_multiple,
- AF_TYPE(test_bar) promises[3];) {
- int i;
- for (i = 0; i < 3; i++)
- AF_INIT(af promises[i], test_bar, .return_value = 0);
- AF_AWAIT_ALL(af promises);
- AF_RETURN(af promises[0].return_value +
- af promises[1].return_value +
- af promises[2].return_value);
-}
-CORO_END
-
-void test_execute_lazy(void *_, void *coro, int request) {
- if (request == AF_REQUEST_RESUME)
- return;
- AF_EXECUTE(coro);
-}
-
-void test_execute_immediate(void *_, void *coro, int request) {
- if (request == AF_REQUEST_JOIN)
- return;
- AF_EXECUTE(coro);
-}
-
TEST("coroutine create") {
AF_CREATE(promise, test_foo, );
REQUIRE(!AF_FINISHED(promise));
@@ -106,111 +72,67 @@ TEST("coroutine init with value") {
REQUIRE(!AF_FINISHED(promise));
}
-TEST("coroutine resume") {
+TEST("coroutine create with value") {
AF_CREATE(promise, test_foo, .return_value = -1);
- AF_RESUME(promise);
REQUIRE(promise.return_value == -1);
REQUIRE(!AF_FINISHED(promise));
}
-TEST("coroutine resume and join") {
+TEST("coroutine execute and return") {
AF_CREATE(promise, test_foo, );
- REQUIRE(AF_RESUME_AND_JOIN(promise) == 42);
+ REQUIRE(AF_NEXT(promise) == 42);
REQUIRE(AF_FINISHED(promise));
}
-TEST("coroutine resume and join manually") {
- AF_CREATE(promise, test_foo, );
- AF_RESUME(promise);
- REQUIRE(AF_JOIN(promise) == 42);
- REQUIRE(AF_FINISHED(promise));
-}
-
-TEST("coroutine suspend") {
+TEST("coroutine execute two steps") {
AF_CREATE(promise, test_bar, .return_value = 0);
- REQUIRE(AF_RESUME_AND_JOIN(promise) == 0);
- REQUIRE(AF_RESUME_AND_JOIN(promise) == 42);
+ AF_EXECUTE(promise);
+ REQUIRE(promise.return_value == 0);
+ AF_EXECUTE(promise);
+ REQUIRE(promise.return_value == 42);
}
TEST("coroutine generator") {
int i;
AF_CREATE(promise, test_gen, .min = 10, .max = 15);
- for (i = 0; i <= 5; i++)
- REQUIRE(AF_RESUME_AND_JOIN(promise) == 10 + i);
+ for (i = 0; i <= 5; i++) REQUIRE(AF_NEXT(promise) == 10 + i);
}
TEST("coroutine status finished") {
AF_CREATE(promise, test_bar, );
REQUIRE(!AF_FINISHED(promise));
- (void) AF_RESUME_AND_JOIN(promise);
+ AF_EXECUTE(promise);
REQUIRE(!AF_FINISHED(promise));
- (void) AF_RESUME_AND_JOIN(promise);
+ AF_EXECUTE(promise);
REQUIRE(AF_FINISHED(promise));
}
TEST("coroutine task") {
AF_CREATE(promise, test_task, );
- (void) AF_RESUME_AND_JOIN(promise);
+ AF_EXECUTE(promise);
REQUIRE(!AF_FINISHED(promise));
- (void) AF_RESUME_AND_JOIN(promise);
+ AF_EXECUTE(promise);
REQUIRE(!AF_FINISHED(promise));
- (void) AF_RESUME_AND_JOIN(promise);
+ AF_EXECUTE(promise);
REQUIRE(AF_FINISHED(promise));
}
TEST("coroutine nested task") {
AF_CREATE(promise, test_nest_task, );
- (void) AF_RESUME_AND_JOIN(promise);
+ AF_EXECUTE(promise);
REQUIRE(!AF_FINISHED(promise));
- (void) AF_RESUME_AND_JOIN(promise);
+ AF_EXECUTE(promise);
REQUIRE(!AF_FINISHED(promise));
- (void) AF_RESUME_AND_JOIN(promise);
+ AF_EXECUTE(promise);
REQUIRE(AF_FINISHED(promise));
}
TEST("coroutine nested generator") {
AF_CREATE(promise, test_nest_generator, );
- REQUIRE(AF_RESUME_AND_JOIN(promise) == 1);
- REQUIRE(AF_RESUME_AND_JOIN(promise) == 2);
- REQUIRE(AF_RESUME_AND_JOIN(promise) == 3);
+ REQUIRE(AF_NEXT(promise) == 1);
+ REQUIRE(AF_NEXT(promise) == 2);
+ REQUIRE(AF_NEXT(promise) == 3);
REQUIRE(!AF_FINISHED(promise));
- (void) AF_RESUME_AND_JOIN(promise);
- REQUIRE(AF_FINISHED(promise));
-}
-
-TEST("coroutine join multiple") {
- AF_CREATE(promise, test_join_multiple, );
- REQUIRE(AF_RESUME_AND_JOIN(promise) == 0);
- REQUIRE(AF_FINISHED(promise));
-}
-
-TEST("coroutine await multiple") {
- AF_CREATE(promise, test_await_multiple, );
- REQUIRE(AF_RESUME_AND_JOIN(promise) == 0);
- REQUIRE(AF_RESUME_AND_JOIN(promise) == 42 * 3);
- REQUIRE(AF_FINISHED(promise));
-}
-
-TEST("coroutine custom execution context lazy") {
- AF_CREATE(promise, test_foo, .return_value = 0);
- AF_EXECUTION_CONTEXT(promise, .state = NULL,
- .execute = test_execute_lazy);
- AF_RESUME(promise);
- REQUIRE(promise.return_value == 0);
- REQUIRE(!AF_FINISHED(promise));
- (void) AF_JOIN(promise);
- REQUIRE(promise.return_value == 42);
- REQUIRE(AF_FINISHED(promise));
-}
-
-TEST("coroutine custom execution context immediate") {
- AF_CREATE(promise, test_foo, .return_value = 0);
- AF_EXECUTION_CONTEXT(promise, .state = NULL,
- .execute = test_execute_immediate);
- AF_RESUME(promise);
- REQUIRE(promise.return_value == 42);
- REQUIRE(AF_FINISHED(promise));
- (void) AF_JOIN(promise);
- REQUIRE(promise.return_value == 42);
+ AF_EXECUTE(promise);
REQUIRE(AF_FINISHED(promise));
}