diff --git a/CMakeLists.txt b/CMakeLists.txt index d95bb84..67dfe4e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -133,9 +133,6 @@ find_package(PCRE) # List sources and headers set(SOURCE_FILES - src/core/wrappers/wrap.h - src/core/wrappers/wrap.c - src/core/wrappers/wrappers.c src/core/abort.c src/core/abort.h src/core/report.c @@ -203,10 +200,6 @@ if (PCRE_FOUND) set(HAVE_PCRE 1) endif () -if (CMAKE_CXX_COMPILER_WORKS) - set(SOURCE_FILES ${SOURCE_FILES} src/core/wrappers/wrap.cc) -endif () - if (CMAKE_ObjectiveC_COMPILER_WORKS) set(CMAKE_ObjectiveC_FLAGS "${CMAKE_ObjectiveC_FLAGS} ${CMAKE_C_FLAGS} -fobjc-exceptions") if ("${CMAKE_ObjectiveC_COMPILER_ID}" STREQUAL "Clang" AND APPLE) @@ -217,7 +210,6 @@ if (CMAKE_ObjectiveC_COMPILER_WORKS) if (NOT WIN32) set(CMAKE_ObjectiveC_FLAGS "${CMAKE_ObjectiveC_FLAGS} -fPIC") endif () - set(SOURCE_FILES ${SOURCE_FILES} src/core/wrappers/wrap.m) endif () set(INTERFACE_FILES diff --git a/include/criterion/criterion.h b/include/criterion/criterion.h index e1ce158..a1b183b 100644 --- a/include/criterion/criterion.h +++ b/include/criterion/criterion.h @@ -113,6 +113,9 @@ CR_API int criterion_handle_args(int argc, char *argv[], bool handle_unknown_arg CR_API void criterion_register_test(struct criterion_test_set *tests, struct criterion_test *test); +extern const struct criterion_test *const criterion_current_test; +extern const struct criterion_suite *const criterion_current_suite; + CR_END_C_API #endif /* !CRITERION_H_ */ diff --git a/include/criterion/internal/test.h b/include/criterion/internal/test.h index 0772f1f..9a2f526 100644 --- a/include/criterion/internal/test.h +++ b/include/criterion/internal/test.h @@ -27,6 +27,14 @@ # include "designated-initializer-compat.h" # include "common.h" +# ifdef __OBJC__ +#import +# endif + +# ifdef __cplusplus +# include +# endif + # define CR_IDENTIFIER_(Category, Name, Suffix) \ Category ## _ ## Name ## _ ## Suffix @@ -55,8 +63,88 @@ # define CR_SUITE_IDENTIFIER_(Name, Suffix) \ suite_ ## Name ## _ ## Suffix +CR_BEGIN_C_API + +CR_API void criterion_internal_test_setup(void); +CR_API void criterion_internal_test_main(void (*fn)(void)); +CR_API void criterion_internal_test_teardown(void); + +CR_END_C_API + +static const char *const cr_msg_test_init_std_exception = "Caught an unexpected exception during the test initialization: %s."; +static const char *const cr_msg_test_init_other_exception = "Caught some unexpected exception during the test initialization."; +static const char *const cr_msg_test_main_std_exception = "Caught an unexpected exception during the test execution: %s."; +static const char *const cr_msg_test_main_other_exception = "Caught some unexpected exception during the test execution."; +static const char *const cr_msg_test_fini_std_exception = "Caught an unexpected exception during the test finalization: %s."; +static const char *const cr_msg_test_fini_other_exception = "Caught some unexpected exception during the test finalization."; + +# ifdef __cplusplus +# define CR_TEST_TRAMPOLINE_(Category, Name) \ + static inline void CR_IDENTIFIER_(Category, Name, jmp)(void) { \ + try { \ + criterion_internal_test_setup(); \ + } catch (const std::exception &e) { \ + criterion_test_die(cr_msg_test_init_std_exception, e.what()); \ + } catch (...) { \ + criterion_test_die(cr_msg_test_init_other_exception); \ + } \ + try { \ + criterion_internal_test_main(CR_IDENTIFIER_(Category, Name, impl)); \ + } catch (const std::exception &e) { \ + criterion_test_die(cr_msg_test_main_std_exception, e.what()); \ + } catch (...) { \ + criterion_test_die(cr_msg_test_main_other_exception); \ + } \ + try { \ + criterion_internal_test_teardown(); \ + } catch (const std::exception &e) { \ + criterion_test_die(cr_msg_test_fini_std_exception, e.what()); \ + } catch (...) { \ + criterion_test_die(cr_msg_test_fini_other_exception); \ + } \ + } +# else +# if defined(__OBJC__) && defined(__EXCEPTIONS) +# define CR_TEST_TRAMPOLINE_(Category, Name) \ + static inline void CR_IDENTIFIER_(Category, Name, jmp)(void) { \ + @try { \ + criterion_internal_test_setup(); \ + } @catch (NSException *e) { \ + NSString *reason = [e reason]; \ + criterion_test_die(cr_msg_test_init_std_exception, [reason UTF8String]); \ + } @catch (...) { \ + criterion_test_die(cr_msg_test_init_other_exception); \ + } \ + @try { \ + criterion_internal_test_main(CR_IDENTIFIER_(Category, Name, impl)); \ + } @catch (NSException *e) { \ + NSString *reason = [e reason]; \ + criterion_test_die(cr_msg_test_main_std_exception, [reason UTF8String]); \ + } @catch (...) { \ + criterion_test_die(cr_msg_test_main_other_exception); \ + } \ + @try { \ + criterion_internal_test_teardown(); \ + } @catch (NSException *e) { \ + NSString *reason = [e reason]; \ + criterion_test_die(cr_msg_test_fini_std_exception, [reason UTF8String]); \ + } @catch (...) { \ + criterion_test_die(cr_msg_test_fini_other_exception); \ + } \ + } +# else +# define CR_TEST_TRAMPOLINE_(Category, Name) \ + static inline void CR_IDENTIFIER_(Category, Name, jmp)(void) { \ + criterion_internal_test_setup(); \ + criterion_internal_test_main(CR_IDENTIFIER_(Category, Name, impl)); \ + criterion_internal_test_teardown(); \ + } +# endif +# endif + # define CR_TEST_BASE(Category, Name, ...) \ CR_TEST_PROTOTYPE_(Category, Name); \ + CR_TEST_TRAMPOLINE_(Category, Name) \ struct criterion_test_extra_data CR_IDENTIFIER_(Category, Name, extra) = \ CR_EXPAND(CRITERION_MAKE_STRUCT(criterion_test_extra_data, \ .lang_ = CR_LANG, \ @@ -70,7 +158,7 @@ struct criterion_test CR_IDENTIFIER_(Category, Name, meta) = { \ #Name, \ #Category, \ - CR_IDENTIFIER_(Category, Name, impl), \ + CR_IDENTIFIER_(Category, Name, jmp), \ &CR_IDENTIFIER_(Category, Name, extra) \ }; \ CR_SECTION_("cr_tst") \ diff --git a/src/core/runner.c b/src/core/runner.c index 79dedb3..2caf844 100644 --- a/src/core/runner.c +++ b/src/core/runner.c @@ -27,7 +27,7 @@ #include #include #include -#include "criterion/criterion.h" +#include "criterion/internal/test.h" #include "criterion/options.h" #include "criterion/internal/ordered-set.h" #include "criterion/logging.h" @@ -35,7 +35,6 @@ #include "compat/time.h" #include "compat/posix.h" #include "compat/processor.h" -#include "wrappers/wrap.h" #include "string/i18n.h" #include "io/event.h" #include "io/output.h" @@ -84,8 +83,8 @@ CR_IMPL_SECTION_LIMITS(struct criterion_test*, cr_tst); CR_IMPL_SECTION_LIMITS(struct criterion_suite*, cr_sts); // This is here to make the test suite & test sections non-empty -TestSuite(); -Test(,) {} +CR_SUITE_BASE(,.sentinel_ = 0); +CR_TEST_BASE(,,.sentinel_ = 0) {} static INLINE void nothing(void) {} @@ -159,6 +158,9 @@ struct criterion_test_set *criterion_init(void) { return set; } +const struct criterion_test *criterion_current_test; +const struct criterion_suite *criterion_current_suite; + void run_test_child(struct criterion_test *test, struct criterion_suite *suite) { @@ -166,19 +168,16 @@ void run_test_child(struct criterion_test *test, VALGRIND_ENABLE_ERROR_REPORTING; #endif + criterion_current_test = test; + criterion_current_suite = suite; + if (suite->data && suite->data->timeout != 0 && test->data->timeout == 0) setup_timeout((uint64_t) (suite->data->timeout * 1e9)); else if (test->data->timeout != 0) setup_timeout((uint64_t) (test->data->timeout * 1e9)); - if (g_wrappers[test->data->lang_]) { - g_wrappers[test->data->lang_](test, suite); - } else { - criterion_test_die( - "The test is compiled in the %s programming language, but the \n" - "criterion runner have been compiled without its language support.", - cr_language_names[test->data->lang_]); - } + if (test->test) + test->test(); } #define push_event(...) \ diff --git a/src/core/wrappers/wrap.c b/src/core/test.c similarity index 71% rename from src/core/wrappers/wrap.c rename to src/core/test.c index 38009a2..02a4578 100644 --- a/src/core/wrappers/wrap.c +++ b/src/core/test.c @@ -27,28 +27,35 @@ #include "core/report.h" #include "compat/time.h" #include "io/event.h" -#include "wrap.h" + +extern const struct criterion_test *criterion_current_test; +extern const struct criterion_suite *criterion_current_suite; static INLINE void nothing(void) {} -void c_wrap(struct criterion_test *test, struct criterion_suite *suite) { +void criterion_internal_test_setup(void) { + const struct criterion_suite *suite = criterion_current_suite; + const struct criterion_test *test = criterion_current_test; criterion_send_event(PRE_INIT, NULL, 0); if (suite->data) (suite->data->init ? suite->data->init : nothing)(); (test->data->init ? test->data->init : nothing)(); +} + +void criterion_internal_test_main(void (*fn)(void)) { + const struct criterion_test *test = criterion_current_test; + criterion_send_event(PRE_TEST, NULL, 0); struct timespec_compat ts; if (!setjmp(g_pre_test)) { timer_start(&ts); - if (test->test) { - if (!test->data->param_) { - test->test(); - } else { - void(*param_test_func)(void *) = (void(*)(void*)) test->test; - param_test_func(g_worker_context.param->ptr); - } + if (!test->data->param_) { + fn(); + } else { + void(*param_test_func)(void *) = (void(*)(void*)) fn; + param_test_func(g_worker_context.param->ptr); } } @@ -57,9 +64,15 @@ void c_wrap(struct criterion_test *test, struct criterion_suite *suite) { elapsed_time = -1; criterion_send_event(POST_TEST, &elapsed_time, sizeof(double)); +} + +void criterion_internal_test_teardown(void) { + const struct criterion_suite *suite = criterion_current_suite; + const struct criterion_test *test = criterion_current_test; + (test->data->fini ? test->data->fini : nothing)(); if (suite->data) (suite->data->fini ? suite->data->fini : nothing)(); criterion_send_event(POST_FINI, NULL, 0); - } + diff --git a/src/core/wrappers/wrap.cc b/src/core/wrappers/wrap.cc deleted file mode 100644 index a5bc3eb..0000000 --- a/src/core/wrappers/wrap.cc +++ /dev/null @@ -1,92 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright © 2015 Franklin "Snaipe" Mathieu - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ -#include -#include "criterion/event.h" - -#include "core/report.h" - -extern "C" { - -#include "core/abort.h" -#include "core/worker.h" -#include "compat/time.h" -#include "wrap.h" -#include "common.h" - -static INLINE void nothing(void) {} - -void cxx_wrap(struct criterion_test *test, struct criterion_suite *suite); - -} - -void cxx_wrap(struct criterion_test *test, struct criterion_suite *suite) { - - criterion_send_event(PRE_INIT, NULL, 0); - try { - if (suite->data) - (suite->data->init ? suite->data->init : nothing)(); - (test->data->init ? test->data->init : nothing)(); - } catch (const std::exception &e) { - criterion_test_die("Caught an unexpected exception during the test initialization: %s.", e.what()); - } catch (...) { - criterion_test_die("Caught some unexpected exception during the test initialization."); - } - criterion_send_event(PRE_TEST, NULL, 0); - - struct timespec_compat ts; - if (!setjmp(g_pre_test)) { - timer_start(&ts); - if (test->test) { - try { - if (!test->data->param_) { - test->test(); - } else { - void(*param_test_func)(void *) = (void(*)(void*)) test->test; - param_test_func(g_worker_context.param->ptr); - } - } catch (const std::exception &e) { - criterion_test_die("Caught an unexpected exception during the test execution: %s.", e.what()); - } catch (...) { - criterion_test_die("Caught some unexpected exception during the test execution."); - } - } - } - - double elapsed_time; - if (!timer_end(&elapsed_time, &ts)) - elapsed_time = -1; - - criterion_send_event(POST_TEST, &elapsed_time, sizeof(double)); - try { - (test->data->fini ? test->data->fini : nothing)(); - if (suite->data) - (suite->data->fini ? suite->data->fini : nothing)(); - } catch (const std::exception &e) { - criterion_test_die("Caught an unexpected exception during the test finalization: %s.", e.what()); - } catch (...) { - criterion_test_die("Caught some unexpected exception during the test finalization."); - } - criterion_send_event(POST_FINI, NULL, 0); - -} diff --git a/src/core/wrappers/wrap.h b/src/core/wrappers/wrap.h deleted file mode 100644 index 5f20da9..0000000 --- a/src/core/wrappers/wrap.h +++ /dev/null @@ -1,44 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright © 2015 Franklin "Snaipe" Mathieu - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ -#ifndef WRAP_H_ -# define WRAP_H_ - -# include "criterion/types.h" -# include "config.h" - -typedef void (f_wrapper)(struct criterion_test *, struct criterion_suite *); - -void c_wrap(struct criterion_test *test, struct criterion_suite *suite); - -# ifdef CXX_BRIDGE -void cxx_wrap(struct criterion_test *test, struct criterion_suite *suite); -# endif - -# ifdef OBJC_BRIDGE -void objc_wrap(struct criterion_test *test, struct criterion_suite *suite); -# endif - -extern f_wrapper *g_wrappers[]; - -#endif /* !WRAP_H_ */ diff --git a/src/core/wrappers/wrap.m b/src/core/wrappers/wrap.m deleted file mode 100644 index db3f75b..0000000 --- a/src/core/wrappers/wrap.m +++ /dev/null @@ -1,96 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright © 2015 Franklin "Snaipe" Mathieu - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ -#import - -#include "criterion/assert.h" -#include "criterion/event.h" - -#include "core/abort.h" -#include "core/report.h" -#include "core/worker.h" -#include "compat/time.h" -#include "wrap.h" -#include "common.h" - -static INLINE void nothing(void) {} - -void objc_wrap(struct criterion_test *test, struct criterion_suite *suite) { - - criterion_send_event(PRE_INIT, NULL, 0); - @try { - if (suite->data) - (suite->data->init ? suite->data->init : nothing)(); - (test->data->init ? test->data->init : nothing)(); - } - @catch (NSException *e) { - NSString *reason = [e reason]; - criterion_test_die("Caught an unexpected exception during the test initialization: %s.", [reason UTF8String]); - } - @catch (...) { - criterion_test_die("Caught some unexpected exception during the test initialization."); - } - criterion_send_event(PRE_TEST, NULL, 0); - - struct timespec_compat ts; - if (!setjmp(g_pre_test)) { - timer_start(&ts); - if (test->test) { - @try { - if (!test->data->param_) { - test->test(); - } else { - void(*param_test_func)(void *) = (void(*)(void*)) test->test; - param_test_func(g_worker_context.param->ptr); - } - } - @catch (NSException *e) { - NSString *reason = [e reason]; - criterion_test_die("Caught an unexpected exception during the test execution: %s.", [reason UTF8String]); - } - @catch (...) { - criterion_test_die("Caught some unexpected exception during the test execution."); - } - } - } - - double elapsed_time; - if (!timer_end(&elapsed_time, &ts)) - elapsed_time = -1; - - criterion_send_event(POST_TEST, &elapsed_time, sizeof(double)); - @try { - (test->data->fini ? test->data->fini : nothing)(); - if (suite->data) - (suite->data->fini ? suite->data->fini : nothing)(); - } - @catch (NSException *e) { - NSString *reason = [e reason]; - criterion_test_die("Caught an unexpected exception during the test finalization: %s.", [reason UTF8String]); - } - @catch (...) { - criterion_test_die("Caught some unexpected exception during the test finalization."); - } - criterion_send_event(POST_FINI, NULL, 0); - -} diff --git a/src/core/wrappers/wrappers.c b/src/core/wrappers/wrappers.c deleted file mode 100644 index a4094e1..0000000 --- a/src/core/wrappers/wrappers.c +++ /dev/null @@ -1,45 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright © 2015 Franklin "Snaipe" Mathieu - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ -#include "wrap.h" - -f_wrapper *g_wrappers[CR_LANG_SIZE_] = { - [CR_LANG_C] = c_wrap, - -#ifdef CXX_BRIDGE - [CR_LANG_CXX] = cxx_wrap, -#endif - -#ifdef OBJC_BRIDGE - [CR_LANG_OBJC] = objc_wrap, - [CR_LANG_OBJCXX] = objc_wrap, -#endif -}; - -const char *const cr_language_names[CR_LANG_SIZE_] = { - [CR_LANG_C] = "C", - [CR_LANG_CXX] = "C++", - [CR_LANG_OBJC] = "Objective-C", - [CR_LANG_OBJCXX] = "Objective-C++", -}; -