diff options
Diffstat (limited to 'stackless_coroutine.c')
-rwxr-xr-x | stackless_coroutine.c | 98 |
1 files changed, 62 insertions, 36 deletions
diff --git a/stackless_coroutine.c b/stackless_coroutine.c index cbd02f9..27a1123 100755 --- a/stackless_coroutine.c +++ b/stackless_coroutine.c @@ -123,7 +123,7 @@ static void stackless_coroutine_dispatch(void *promise) { # pragma GCC diagnostic pop #endif -#define CORO_PROMISE_STRUCT_(Ret_Type_, coro_, ...) \ +#define CORO_PROMISE_STRUCT_(coro_, Ret_Type_, ...) \ struct Coro_Promise_##coro_ { \ CORO_PROMISE_DATA_; \ Ret_Type_ return_value; \ @@ -147,30 +147,30 @@ static void stackless_coroutine_dispatch(void *promise) { self->_index = -1; \ } -#define CORO_DECL(Ret_Type_, coro_, ...) \ - CORO_PROMISE_STRUCT_(Ret_Type_, coro_, __VA_ARGS__); \ +#define CORO_DECL(coro_, Ret_Type_, ...) \ + CORO_PROMISE_STRUCT_(coro_, Ret_Type_, __VA_ARGS__); \ CORO_PROC_DECL_(coro_) -#define CORO_DECL_STATIC(Ret_Type_, coro_, ...) \ - CORO_PROMISE_STRUCT_(Ret_Type_, coro_, __VA_ARGS__); \ +#define CORO_DECL_STATIC(coro_, Ret_Type_, ...) \ + CORO_PROMISE_STRUCT_(coro_, Ret_Type_, __VA_ARGS__); \ static CORO_PROC_DECL_(coro_) -#define CORO(Ret_Type_, coro_, ...) \ - CORO_PROMISE_STRUCT_(Ret_Type_, coro_, __VA_ARGS__); \ +#define CORO(coro_, Ret_Type_, ...) \ + CORO_PROMISE_STRUCT_(coro_, Ret_Type_, __VA_ARGS__); \ CORO_IMPL(coro_) #define CORO_DECL_VOID(coro_, ...) \ - CORO_DECL(Coro_Void_, coro_, __VA_ARGS__) + CORO_DECL(coro_, Coro_Void_, __VA_ARGS__) #define CORO_VOID(coro_, ...) \ - CORO(Coro_Void_, coro_, __VA_ARGS__) + CORO(coro_, Coro_Void_, __VA_ARGS__) -#define CORO_STATIC(Ret_Type_, coro_, ...) \ - CORO_PROMISE_STRUCT_(Ret_Type_, coro_, __VA_ARGS__); \ +#define CORO_STATIC(coro_, Ret_Type_, ...) \ + CORO_PROMISE_STRUCT_(coro_, Ret_Type_, __VA_ARGS__); \ static CORO_IMPL(coro_) #define CORO_STATIC_VOID(coro_, ...) \ - CORO_STATIC(Coro_Void_, coro_, __VA_ARGS__) + CORO_STATIC(coro_, Coro_Void_, __VA_ARGS__) #define coro_resume(promise_) \ stackless_coroutine_dispatch(&(promise_)) @@ -285,11 +285,16 @@ static void stackless_coroutine_dispatch(void *promise) { #define TEST_FILE stackless_coroutine #include "test.c" -CORO_DECL(int, test_foo, ); +CORO_DECL( + test_foo, + int, +); -CORO_IMPL(test_foo) { - async_return(42); -} CORO_END +CORO_IMPL(test_foo) +{ + async_return (42); +} +CORO_END TEST("coroutine init") { Promise_Of(test_foo) promise; @@ -311,12 +316,17 @@ TEST("coroutine execute and return") { REQUIRE(coro_finished(promise)); } -CORO_DECL_STATIC(int, test_bar, ); +CORO_DECL_STATIC( + test_bar, + int, +); -CORO_IMPL(test_bar) { +CORO_IMPL(test_bar) +{ yield_void; - async_return(42); -} CORO_END + async_return (42); +} +CORO_END TEST("coroutine execute two steps") { Promise_Of(test_bar) promise; @@ -327,17 +337,22 @@ TEST("coroutine execute two steps") { REQUIRE(promise.return_value == 42); } -CORO_STATIC(int, test_gen, int i; int min; int max;) { +CORO_STATIC( + test_gen, + int, + int i; int min; int max; +) { for (self->i = self->min; self->i < self->max; self->i++) - yield(self->i); - async_return(self->max); -} CORO_END + yield (self->i); + async_return (self->max); +} +CORO_END TEST("coroutine generator") { - int i; Promise_Of(test_gen) promise; CORO_INIT(promise, test_gen, .min = 10, .max = 15); - for (i = 0; i <= 5; i++) REQUIRE(coro_next(promise) == 10 + i); + for (i32 i = 0; i <= 5; i++) + REQUIRE(coro_next(promise) == 10 + i); } TEST("coroutine status finished") { @@ -350,11 +365,13 @@ TEST("coroutine status finished") { REQUIRE(coro_finished(promise)); } -CORO_STATIC_VOID(test_task, ) { +CORO_STATIC_VOID(test_task, ) +{ yield_void; yield_void; async_return_void; -} CORO_END +} +CORO_END TEST("coroutine task") { Promise_Of(test_task) promise; @@ -367,12 +384,16 @@ TEST("coroutine task") { REQUIRE(coro_finished(promise)); } -CORO_STATIC_VOID(test_nest_task, Promise_Of(test_task) promise;) { +CORO_STATIC_VOID( + test_nest_task, + Promise_Of(test_task) promise; +) { CORO_INIT(self->promise, test_task, ); - await(self->promise); - await(self->promise); - await(self->promise); -} CORO_END + await (self->promise); + await (self->promise); + await (self->promise); +} +CORO_END TEST("coroutine nested task") { Promise_Of(test_nest_task) promise; @@ -385,10 +406,15 @@ TEST("coroutine nested task") { REQUIRE(coro_finished(promise)); } -CORO_STATIC(int, test_nest_generator, Promise_Of(test_gen) promise;) { +CORO_STATIC( + test_nest_generator, + int, + Promise_Of(test_gen) promise; +) { CORO_INIT(self->promise, test_gen, .min = 1, .max = 3); - yield_await(self->promise); -} CORO_END + yield_await (self->promise); +} +CORO_END TEST("coroutine nested generator") { Promise_Of(test_nest_generator) promise; |