From 6c11d4503758079f29ce2b0b0509d2a128ada662 Mon Sep 17 00:00:00 2001 From: Mitya Selivanov <0x7fffff@guattari.ru> Date: Sat, 6 Aug 2022 17:25:22 +0400 Subject: test --- source/kit/allocator.c | 10 +- source/kit/async_function.h | 374 ++++++++++++++++++++++---------------- source/kit_test/run_tests.c | 24 +-- source/kit_test/test.h | 42 +++-- source/test/unittests/main.test.c | 2 +- 5 files changed, 262 insertions(+), 190 deletions(-) (limited to 'source') diff --git a/source/kit/allocator.c b/source/kit/allocator.c index fbad11f..115807c 100644 --- a/source/kit/allocator.c +++ b/source/kit/allocator.c @@ -1,13 +1,21 @@ #include "allocator.h" -#include +#ifndef KIT_DISABLE_SYSTEM_MALLOC +# include +#endif static void *allocate(void *_, size_t size) { +#ifndef KIT_DISABLE_SYSTEM_MALLOC return malloc(size); +#else + return NULL; +#endif } static void deallocate(void *_, void *pointer) { +#ifndef KIT_DISABLE_SYSTEM_MALLOC free(pointer); +#endif } struct kit_allocator kit_alloc_default() { diff --git a/source/kit/async_function.h b/source/kit/async_function.h index 69b9453..ec870bb 100644 --- a/source/kit/async_function.h +++ b/source/kit/async_function.h @@ -5,227 +5,285 @@ extern "C" { #endif -enum af_request { - af_request_execute = 0, - af_request_resume = 1, - af_request_join = 2, - af_request_resume_and_join = 3 +enum kit_af_request { + kit_af_request_execute = 0, + kit_af_request_resume = 1, + kit_af_request_join = 2, + kit_af_request_resume_and_join = 3 }; typedef struct { int _; -} af_void; +} kit_af_void; -typedef void (*af_state_machine)(void *self_void_, int request_); -typedef void (*af_execute)(void *state, void *coro_state, - int request); +typedef void (*kit_af_state_machine)(void *self_void_, int request_); +typedef void (*kit_af_execute)(void *state, void *coro_state, + int request); typedef struct { - long long _internal; - void *state; - af_execute execute; -} af_execution_context; + long long _internal; + void *state; + kit_af_execute execute; +} kit_af_execution_context; typedef struct { - int _index; - af_state_machine _state_machine; - af_execution_context _context; -} af_type_void; - -#ifndef AF_DISABLE_SELF_SHORTCUT -# define af self-> -#endif - -#define AF_INTERNAL(coro_) (*((af_type_void *) (coro_))) - -#define AF_STATE(ret_type_, name_, ...) \ - struct name_##_coro_state_ { \ - int _index; \ - af_state_machine _state_machine; \ - af_execution_context _context; \ - ret_type_ return_value; \ - __VA_ARGS__ \ + int _index; + kit_af_state_machine _state_machine; + kit_af_execution_context _context; +} kit_af_type_void; + +#define KIT_AF_INTERNAL(coro_) (*((kit_af_type_void *) (coro_))) + +#define KIT_AF_STATE(ret_type_, name_, ...) \ + struct name_##_coro_state_ { \ + int _index; \ + kit_af_state_machine _state_machine; \ + kit_af_execution_context _context; \ + ret_type_ return_value; \ + __VA_ARGS__ \ } -#define AF_DECL(name_) \ +#define KIT_AF_DECL(name_) \ void name_##_coro_(void *self_void_, int request_) -#define CORO_IMPL(name_) \ - AF_DECL(name_) { \ - struct name_##_coro_state_ *self = \ - (struct name_##_coro_state_ *) self_void_; \ - if (request_ != af_request_execute) { \ - if (self->_context.execute != NULL) \ - self->_context.execute(self->_context.state, self_void_, \ - request_); \ - else if (request_ == af_request_join || \ - request_ == af_request_resume_and_join) \ - self->_state_machine(self_void_, af_request_execute); \ - return; \ - } \ - switch (self->_index) { \ +#define KIT_CORO_IMPL(name_) \ + KIT_AF_DECL(name_) { \ + struct name_##_coro_state_ *self = \ + (struct name_##_coro_state_ *) self_void_; \ + if (request_ != kit_af_request_execute) { \ + if (self->_context.execute != NULL) \ + self->_context.execute(self->_context.state, self_void_, \ + request_); \ + else if (request_ == kit_af_request_join || \ + request_ == kit_af_request_resume_and_join) \ + self->_state_machine(self_void_, kit_af_request_execute); \ + return; \ + } \ + switch (self->_index) { \ case 0:; -#define AF_LINE() __LINE__ +#define KIT_AF_LINE_() __LINE__ -#define CORO_END \ +#define KIT_CORO_END \ } \ self->_index = -1; \ } -#define CORO_DECL(ret_type_, name_, ...) \ - AF_STATE(ret_type_, name_, __VA_ARGS__); \ - AF_DECL(name_) +#define KIT_CORO_DECL(ret_type_, name_, ...) \ + KIT_AF_STATE(ret_type_, name_, __VA_ARGS__); \ + KIT_AF_DECL(name_) -#define CORO(ret_type_, name_, ...) \ - AF_STATE(ret_type_, name_, __VA_ARGS__); \ - CORO_IMPL(name_) +#define KIT_CORO(ret_type_, name_, ...) \ + KIT_AF_STATE(ret_type_, name_, __VA_ARGS__); \ + KIT_CORO_IMPL(name_) -#define CORO_DECL_VOID(name_, ...) \ - CORO_DECL(af_void, name_, __VA_ARGS__) +#define KIT_CORO_DECL_VOID(name_, ...) \ + KIT_CORO_DECL(kit_af_void, name_, __VA_ARGS__) -#define CORO_VOID(name_, ...) CORO(af_void, name_, __VA_ARGS__) +#define KIT_CORO_VOID(name_, ...) \ + KIT_CORO(kit_af_void, name_, __VA_ARGS__) -#define AF_YIELD(...) \ - { \ - self->_index = AF_LINE(); \ - self->return_value = __VA_ARGS__; \ - return; \ - case AF_LINE():; \ +#define KIT_AF_YIELD(...) \ + { \ + self->_index = KIT_AF_LINE_(); \ + self->return_value = __VA_ARGS__; \ + return; \ + case KIT_AF_LINE_():; \ } -#define AF_YIELD_VOID \ - { \ - self->_index = AF_LINE(); \ - return; \ - case AF_LINE():; \ +#define KIT_AF_YIELD_VOID \ + { \ + self->_index = KIT_AF_LINE_(); \ + return; \ + case KIT_AF_LINE_():; \ } -#define AF_RETURN(...) \ +#define KIT_AF_RETURN(...) \ { \ self->_index = -1; \ self->return_value = __VA_ARGS__; \ return; \ } -#define AF_RETURN_VOID \ - { \ - self->_index = -1; \ - return; \ +#define KIT_AF_RETURN_VOID \ + { \ + self->_index = -1; \ + return; \ } -#define AF_AWAIT(promise_) \ - { \ - case AF_LINE(): \ - if ((promise_)._index != -1) { \ - self->_index = AF_LINE(); \ - (promise_)._state_machine(&(promise_), \ - af_request_resume_and_join); \ - } \ - if ((promise_)._index != -1) \ - return; \ +#define KIT_AF_AWAIT(promise_) \ + { \ + case KIT_AF_LINE_(): \ + if ((promise_)._index != -1) { \ + self->_index = KIT_AF_LINE_(); \ + (promise_)._state_machine(&(promise_), \ + kit_af_request_resume_and_join); \ + } \ + if ((promise_)._index != -1) \ + return; \ } -#define AF_YIELD_AWAIT(promise_) \ - { \ - case AF_LINE(): \ - if ((promise_)._index != -1) { \ - self->_index = AF_LINE(); \ - (promise_)._state_machine(&(promise_), \ - af_request_resume_and_join); \ - self->return_value = (promise_).return_value; \ - return; \ - } \ +#define KIT_AF_YIELD_AWAIT(promise_) \ + { \ + case KIT_AF_LINE_(): \ + if ((promise_)._index != -1) { \ + self->_index = KIT_AF_LINE_(); \ + (promise_)._state_machine(&(promise_), \ + kit_af_request_resume_and_join); \ + self->return_value = (promise_).return_value; \ + return; \ + } \ } -#define AF_TYPE(coro_) struct coro_##_coro_state_ +#define KIT_AF_TYPE(coro_) struct coro_##_coro_state_ -#define AF_INITIAL(coro_) \ +#define KIT_AF_INITIAL(coro_) \ ._index = 0, ._state_machine = coro_##_coro_, \ ._context = { .state = NULL, .execute = NULL } -#define AF_CREATE(promise_, coro_, ...) \ - AF_TYPE(coro_) \ - promise_ = { AF_INITIAL(coro_), __VA_ARGS__ } +#define KIT_AF_CREATE(promise_, coro_, ...) \ + KIT_AF_TYPE(coro_) \ + promise_ = { KIT_AF_INITIAL(coro_), __VA_ARGS__ } -#define AF_INIT(promise_, coro_, ...) \ - { \ - AF_CREATE(af_temp_, coro_, __VA_ARGS__); \ - (promise_) = af_temp_; \ +#define KIT_AF_INIT(promise_, coro_, ...) \ + { \ + KIT_AF_CREATE(kit_af_temp_, coro_, __VA_ARGS__); \ + (promise_) = kit_af_temp_; \ } -#define AF_EXECUTION_CONTEXT(promise_, ...) \ - { \ - af_execution_context af_temp_ = { ._internal = 0, __VA_ARGS__ }; \ - (promise_)._context = af_temp_; \ +#define KIT_AF_EXECUTION_CONTEXT(promise_, ...) \ + { \ + kit_af_execution_context kit_af_temp_ = { ._internal = 0, \ + __VA_ARGS__ }; \ + (promise_)._context = kit_af_temp_; \ } -#define AF_RESUME(promise_) \ - (promise_)._state_machine(&(promise_), af_request_resume) +#define KIT_AF_RESUME(promise_) \ + (promise_)._state_machine(&(promise_), kit_af_request_resume) -#define AF_RESUME_N(promises_, size_) \ - for (int af_index_ = 0; af_index_ < (size_); af_index_++) \ - AF_RESUME((promises_)[af_index_]) +#define KIT_AF_RESUME_N(promises_, size_) \ + for (int kit_af_index_ = 0; kit_af_index_ < (size_); \ + kit_af_index_++) \ + KIT_AF_RESUME((promises_)[kit_af_index_]) -#define AF_JOIN(promise_) \ - ((promise_)._state_machine(&(promise_), af_request_join), \ +#define KIT_AF_JOIN(promise_) \ + ((promise_)._state_machine(&(promise_), kit_af_request_join), \ (promise_).return_value) -#define AF_JOIN_N(promises_, size_) \ - for (int af_index_ = 0; af_index_ < (size_); af_index_++) \ - AF_JOIN((promises_)[af_index_]) +#define KIT_AF_JOIN_N(promises_, size_) \ + for (int kit_af_index_ = 0; kit_af_index_ < (size_); \ + kit_af_index_++) \ + KIT_AF_JOIN((promises_)[kit_af_index_]) -#define AF_RESUME_AND_JOIN(promise_) \ - ((promise_)._state_machine(&(promise_), \ - af_request_resume_and_join), \ +#define KIT_AF_RESUME_AND_JOIN(promise_) \ + ((promise_)._state_machine(&(promise_), \ + kit_af_request_resume_and_join), \ (promise_).return_value) -#define AF_RESUME_AND_JOIN_N(promises_, size_) \ - AF_RESUME_N((promises_), (size_)); \ - AF_JOIN_N((promises_), (size_)) +#define KIT_AF_RESUME_AND_JOIN_N(promises_, size_) \ + KIT_AF_RESUME_N((promises_), (size_)); \ + KIT_AF_JOIN_N((promises_), (size_)) -#define AF_RESUME_ALL(promises_) \ - AF_RESUME_N((promises_), sizeof(promises_) / sizeof((promises_)[0])) +#define KIT_AF_RESUME_ALL(promises_) \ + KIT_AF_RESUME_N((promises_), \ + sizeof(promises_) / sizeof((promises_)[0])) -#define AF_JOIN_ALL(promises_) \ - AF_JOIN_N((promises_), sizeof(promises_) / sizeof((promises_)[0])) - -#define AF_RESUME_AND_JOIN_ALL(promises_) \ - AF_RESUME_AND_JOIN_N((promises_), \ - sizeof(promises_) / sizeof((promises_)[0])) - -#define AF_FINISHED(promise_) ((promise_)._index == -1) - -#define AF_FINISHED_N(return_, promises_, size_) \ - { \ - (return_) = true; \ - for (int af_index_ = 0; af_index_ < (size_); af_index_++) \ - if (!AF_FINISHED((promises_)[af_index_])) { \ - (return_) = false; \ - break; \ - } \ - } - -#define AF_FINISHED_ALL(return_, promises_, size_) \ - AF_FINISHED_N((return_), (promises_), \ +#define KIT_AF_JOIN_ALL(promises_) \ + KIT_AF_JOIN_N((promises_), \ sizeof(promises_) / sizeof((promises_)[0])) -#define AF_AWAIT_N(promises_, size_) \ - { \ - case AF_LINE(): \ - self->_index = AF_LINE(); \ - AF_RESUME_AND_JOIN_N((promises_), (size_)); \ - bool af_done_; \ - AF_FINISHED_N(af_done_, (promises_), (size_)); \ - if (!af_done_) \ - return; \ +#define KIT_AF_RESUME_AND_JOIN_ALL(promises_) \ + KIT_AF_RESUME_AND_JOIN_N((promises_), sizeof(promises_) / \ + sizeof((promises_)[0])) + +#define KIT_AF_FINISHED(promise_) ((promise_)._index == -1) + +#define KIT_AF_FINISHED_N(return_, promises_, size_) \ + { \ + (return_) = 1; \ + for (int kit_af_index_ = 0; kit_af_index_ < (size_); \ + kit_af_index_++) \ + if (!KIT_AF_FINISHED((promises_)[kit_af_index_])) { \ + (return_) = 0; \ + break; \ + } \ } -#define AF_AWAIT_ALL(promises_) \ - AF_AWAIT_N((promises_), sizeof(promises_) / sizeof((promises_)[0])) +#define KIT_AF_FINISHED_ALL(return_, promises_, size_) \ + KIT_AF_FINISHED_N((return_), (promises_), \ + sizeof(promises_) / sizeof((promises_)[0])) + +#define KIT_AF_AWAIT_N(promises_, size_) \ + { \ + case KIT_AF_LINE_(): \ + self->_index = KIT_AF_LINE_(); \ + KIT_AF_RESUME_AND_JOIN_N((promises_), (size_)); \ + _Bool kit_af_done_; \ + KIT_AF_FINISHED_N(kit_af_done_, (promises_), (size_)); \ + if (!kit_af_done_) \ + return; \ + } -#define AF_EXECUTE(coro_state_) \ - AF_INTERNAL(coro_state_)._state_machine(coro, af_request_execute) +#define KIT_AF_AWAIT_ALL(promises_) \ + KIT_AF_AWAIT_N((promises_), \ + sizeof(promises_) / sizeof((promises_)[0])) + +#define KIT_AF_EXECUTE(coro_state_) \ + KIT_AF_INTERNAL(coro_state_) \ + ._state_machine(coro, kit_af_request_execute) + +#ifndef KIT_DISABLE_SHORT_NAMES +# ifndef KIT_DISABLE_AF_SELF_SHORTCUT +# define af self-> +# endif + +# define af_request kit_af_request +# define af_request_execute kit_af_request_execute +# define af_request_resume kit_af_request_resume +# define af_request_join kit_af_request_join +# define af_request_resume_and_join kit_af_request_resume_and_join +# define af_void kit_af_void +# define af_state_machine kit_af_state_machine +# define af_execute kit_af_execute +# define af_execution_context kit_af_execution_context +# define af_type_void kit_af_type_void + +# define AF_INTERNAL KIT_AF_INTERNAL +# define AF_STATE KIT_AF_STATE +# define AF_DECL KIT_AF_DECL +# define CORO_IMPL KIT_CORO_IMPL +# define CORO_END KIT_CORO_END +# define CORO_DECL KIT_CORO_DECL +# define CORO KIT_CORO +# define CORO_DECL_VOID KIT_CORO_DECL_VOID +# define CORO_VOID KIT_CORO_VOID +# define AF_YIELD KIT_AF_YIELD +# define AF_YIELD_VOID KIT_AF_YIELD_VOID +# define AF_RETURN KIT_AF_RETURN +# define AF_RETURN_VOID KIT_AF_RETURN_VOID +# define AF_AWAIT KIT_AF_AWAIT +# define AF_YIELD_AWAIT KIT_AF_YIELD_AWAIT +# define AF_TYPE KIT_AF_TYPE +# define AF_INITIAL KIT_AF_INITIAL +# define AF_CREATE KIT_AF_CREATE +# define AF_INIT KIT_AF_INIT +# define AF_EXECUTION_CONTEXT KIT_AF_EXECUTION_CONTEXT +# define AF_RESUME KIT_AF_RESUME +# define AF_RESUME_N KIT_AF_RESUME_N +# define AF_JOIN KIT_AF_JOIN +# define AF_JOIN_N KIT_AF_JOIN_N +# define AF_RESUME_AND_JOIN KIT_AF_RESUME_AND_JOIN +# define AF_RESUME_AND_JOIN_N KIT_AF_RESUME_AND_JOIN_N +# define AF_RESUME_ALL KIT_AF_RESUME_ALL +# define AF_JOIN_ALL KIT_AF_JOIN_ALL +# define AF_RESUME_AND_JOIN_ALL KIT_AF_RESUME_AND_JOIN_ALL +# define AF_FINISHED KIT_AF_FINISHED +# define AF_FINISHED_N KIT_AF_FINISHED_N +# define AF_FINISHED_ALL KIT_AF_FINISHED_ALL +# define AF_AWAIT_N KIT_AF_AWAIT_N +# define AF_AWAIT_ALL KIT_AF_AWAIT_ALL +# define AF_EXECUTE KIT_AF_EXECUTE +#endif #ifdef __cplusplus } diff --git a/source/kit_test/run_tests.c b/source/kit_test/run_tests.c index 191982a..19fea96 100644 --- a/source/kit_test/run_tests.c +++ b/source/kit_test/run_tests.c @@ -5,7 +5,7 @@ struct kit_tests_list kit_tests_list = { 0 }; -static void report(int i, char const *file, int line, bool ok) { +static void report(int i, char const *file, int line, _Bool ok) { int const n = kit_tests_list.tests[i].assertions++; kit_tests_list.tests[i].file[n] = file; @@ -23,7 +23,7 @@ static long long sec_to_ms(long long sec) { enum code_value { white, yellow, red, green }; -static void color_code(bool term_color, int c) { +static void color_code(_Bool term_color, int c) { if (term_color) { if (c == white) printf("\x1b[37m"); @@ -37,15 +37,15 @@ static void color_code(bool term_color, int c) { } int kit_run_tests(int argc, char **argv) { - int fail_test_count = 0; - int fail_assertion_count = 0; - int total_assertion_count = 0; - int status = 0; - bool term_color = true; + int fail_test_count = 0; + int fail_assertion_count = 0; + int total_assertion_count = 0; + int status = 0; + _Bool term_color = 1; for (int i = 0; i < argc; i++) if (strcmp("--no-term-color", argv[i]) == 0) - term_color = false; + term_color = 0; for (int i = 0; i < kit_tests_list.size; i++) { color_code(term_color, yellow); @@ -63,17 +63,17 @@ int kit_run_tests(int argc, char **argv) { printf("\r"); - bool test_status = true; + _Bool test_status = 1; for (int j = 0; j < kit_tests_list.tests[i].assertions; j++) - if (kit_tests_list.tests[i].status[j] == false) { + if (kit_tests_list.tests[i].status[j] == 0) { fail_assertion_count++; - test_status = false; + test_status = 0; } total_assertion_count += kit_tests_list.tests[i].assertions; - if (test_status == false) { + if (test_status == 0) { color_code(term_color, red); printf("[ RUN ] %s\n", kit_tests_list.tests[i].test_name); printf("[ FAILED ] %s - %d ms\n", diff --git a/source/kit_test/test.h b/source/kit_test/test.h index 09df7e9..a57f768 100644 --- a/source/kit_test/test.h +++ b/source/kit_test/test.h @@ -5,7 +5,6 @@ extern "C" { #endif -#include #include #include @@ -26,7 +25,7 @@ extern "C" { #endif typedef void (*kit_test_report)(int, char const *file, int line, - bool); + _Bool); typedef void (*kit_test_function)(int, kit_test_report); struct kit_test_case { @@ -35,7 +34,7 @@ struct kit_test_case { int assertions; char const *file[KIT_TEST_ASSERTIONS_LIMIT]; int line[KIT_TEST_ASSERTIONS_LIMIT]; - bool status[KIT_TEST_ASSERTIONS_LIMIT]; + _Bool status[KIT_TEST_ASSERTIONS_LIMIT]; }; struct kit_tests_list { @@ -47,48 +46,55 @@ extern struct kit_tests_list kit_tests_list; #ifdef _MSC_VER # pragma section(".CRT$XCU", read) -# define KIT_TEST_ON_START_2(f, p) \ +# define KIT_TEST_ON_START_2_(f, p) \ static void f(void); \ __declspec(allocate(".CRT$XCU")) void (*f##_)(void) = f; \ __pragma(comment(linker, "/include:" p #f "_")) static void f( \ void) # ifdef _WIN64 -# define KIT_TEST_ON_START(f) KIT_TEST_ON_START_2(f, "") +# define KIT_TEST_ON_START_(f) KIT_TEST_ON_START_2_(f, "") # else -# define KIT_TEST_ON_START(f) KIT_TEST_ON_START_2(f, "_") +# define KIT_TEST_ON_START_(f) KIT_TEST_ON_START_2_(f, "_") # endif #else -# define KIT_TEST_ON_START(f) \ +# define KIT_TEST_ON_START_(f) \ static void f(void) __attribute__((constructor)); \ static void f(void) #endif -#define KIT_TEST_CONCAT4(a, b, c, d) a##b##c##d -#define KIT_TEST_CONCAT3(a, b, c) KIT_TEST_CONCAT4(a, b, _, c) +#define KIT_TEST_CONCAT4_(a, b, c, d) a##b##c##d +#define KIT_TEST_CONCAT3_(a, b, c) KIT_TEST_CONCAT4_(a, b, _, c) -#define TEST(name) \ - static void KIT_TEST_CONCAT3(kit_test_run_, __LINE__, \ - KIT_TEST_FILE)(int, kit_test_report); \ - KIT_TEST_ON_START( \ - KIT_TEST_CONCAT3(kit_test_case_, __LINE__, KIT_TEST_FILE)) { \ +#define KIT_TEST(name) \ + static void KIT_TEST_CONCAT3_( \ + kit_test_run_, __LINE__, KIT_TEST_FILE)(int, kit_test_report); \ + KIT_TEST_ON_START_( \ + KIT_TEST_CONCAT3_(kit_test_case_, __LINE__, KIT_TEST_FILE)) { \ int n = kit_tests_list.size; \ if (n < KIT_TESTS_SIZE_LIMIT) { \ kit_tests_list.size++; \ - kit_tests_list.tests[n].test_fn = KIT_TEST_CONCAT3( \ + kit_tests_list.tests[n].test_fn = KIT_TEST_CONCAT3_( \ kit_test_run_, __LINE__, KIT_TEST_FILE); \ strcpy(kit_tests_list.tests[n].test_name, name); \ kit_tests_list.tests[n].assertions = 0; \ } \ } \ - static void KIT_TEST_CONCAT3(kit_test_run_, __LINE__, \ - KIT_TEST_FILE)( \ + static void KIT_TEST_CONCAT3_(kit_test_run_, __LINE__, \ + KIT_TEST_FILE)( \ int kit_test_index_, kit_test_report kit_test_report_) -#define REQUIRE(ok) \ +#define KIT_REQUIRE(ok) \ kit_test_report_(kit_test_index_, __FILE__, __LINE__, (ok)) int kit_run_tests(int argc, char **argv); +#ifndef KIT_DISABLE_SHORT_NAMES +# define TEST KIT_TEST +# define REQUIRE KIT_REQUIRE + +# define run_tests kit_run_tests +#endif + #ifdef __cplusplus } #endif diff --git a/source/test/unittests/main.test.c b/source/test/unittests/main.test.c index b072c67..3ced74c 100644 --- a/source/test/unittests/main.test.c +++ b/source/test/unittests/main.test.c @@ -1,5 +1,5 @@ #include "../../kit_test/test.h" int main(int argc, char **argv) { - return kit_run_tests(argc, argv); + return run_tests(argc, argv); } -- cgit v1.2.3