Added first tentative to decouple the wrappers from the library

This commit is contained in:
Snaipe 2015-12-05 01:37:58 +01:00
parent 214bd95a62
commit 30c3afedda
9 changed files with 126 additions and 308 deletions

View file

@ -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

View file

@ -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_ */

View file

@ -27,6 +27,14 @@
# include "designated-initializer-compat.h"
# include "common.h"
# ifdef __OBJC__
#import <Foundation/Foundation.h>
# endif
# ifdef __cplusplus
# include <exception>
# 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") \

View file

@ -27,7 +27,7 @@
#include <errno.h>
#include <csptr/smalloc.h>
#include <valgrind/valgrind.h>
#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(...) \

View file

@ -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);
}

View file

@ -1,92 +0,0 @@
/*
* The MIT License (MIT)
*
* Copyright © 2015 Franklin "Snaipe" Mathieu <http://snai.pe/>
*
* 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 <exception>
#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);
}

View file

@ -1,44 +0,0 @@
/*
* The MIT License (MIT)
*
* Copyright © 2015 Franklin "Snaipe" Mathieu <http://snai.pe/>
*
* 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_ */

View file

@ -1,96 +0,0 @@
/*
* The MIT License (MIT)
*
* Copyright © 2015 Franklin "Snaipe" Mathieu <http://snai.pe/>
*
* 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 <Foundation/Foundation.h>
#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);
}

View file

@ -1,45 +0,0 @@
/*
* The MIT License (MIT)
*
* Copyright © 2015 Franklin "Snaipe" Mathieu <http://snai.pe/>
*
* 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++",
};