Github User Fetcher 1.0.0
C Application with Server and GUI
Loading...
Searching...
No Matches
test.h
Go to the documentation of this file.
1/*
2 * The MIT License (MIT)
3 *
4 * Copyright © 2015-2016 Franklin "Snaipe" Mathieu <http://snai.pe/>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24#ifndef CRITERION_INTERNAL_TEST_H_
25#define CRITERION_INTERNAL_TEST_H_
26
28#include "common.h"
29#include "assert.h"
30
31#ifdef __OBJC__
32# import <Foundation/Foundation.h>
33#endif
34
35#ifdef __cplusplus
36# include <exception>
37#endif
38
39#define CR_IDENTIFIER_(Category, Name, Suffix) \
40 Category ## _ ## Name ## _ ## Suffix
41
42#ifdef __cplusplus
43# ifdef __OBJC__
44# define CR_LANG CR_LANG_OBJCXX
45# else
46# define CR_LANG CR_LANG_CXX
47# endif
48#else
49# ifdef __OBJC__
50# define CR_LANG CR_LANG_OBJC
51# else
52# define CR_LANG CR_LANG_C
53# endif
54#endif
55
56#ifdef __cplusplus
57# define CR_TEST_PROTOTYPE_(Category, Name) \
58 extern "C" void CR_IDENTIFIER_(Category, Name, impl)(void)
59#else
60# define CR_TEST_PROTOTYPE_(Category, Name) \
61 void CR_IDENTIFIER_(Category, Name, impl)(void)
62#endif
63
64#define CR_SUITE_IDENTIFIER_(Name, Suffix) \
65 suite_ ## Name ## _ ## Suffix
66
68
70CR_API void criterion_internal_test_main(void (*fn)(void));
72
74
75static const char *const cr_msg_test_init_std_exception = "Caught an unexpected exception during the test initialization: %s.";
76static const char *const cr_msg_test_init_other_exception = "Caught some unexpected exception during the test initialization.";
77static const char *const cr_msg_test_main_std_exception = "Caught an unexpected exception during the test execution: %s.";
78static const char *const cr_msg_test_main_other_exception = "Caught some unexpected exception during the test execution.";
79static const char *const cr_msg_test_fini_std_exception = "Caught an unexpected exception during the test finalization: %s.";
80static const char *const cr_msg_test_fini_other_exception = "Caught some unexpected exception during the test finalization.";
81
82#ifdef __cplusplus
83# define CR_TEST_TRAMPOLINE_(Category, Name) \
84 static inline void CR_IDENTIFIER_(Category, Name, jmp)(void) { \
85 try { \
86 criterion_internal_test_setup(); \
87 } catch (const std::exception &e) { \
88 criterion_test_die(cr_msg_test_init_std_exception, e.what()); \
89 } catch (...) { \
90 criterion_test_die(cr_msg_test_init_other_exception); \
91 } \
92 try { \
93 criterion_internal_test_main((void (*)(void))CR_IDENTIFIER_(Category, Name, impl)); \
94 } catch (const std::exception &e) { \
95 criterion_test_die(cr_msg_test_main_std_exception, e.what()); \
96 } catch (...) { \
97 criterion_test_die(cr_msg_test_main_other_exception); \
98 } \
99 try { \
100 criterion_internal_test_teardown(); \
101 } catch (const std::exception &e) { \
102 criterion_test_die(cr_msg_test_fini_std_exception, e.what()); \
103 } catch (...) { \
104 criterion_test_die(cr_msg_test_fini_other_exception); \
105 } \
106 }
107#else
108# if defined (__OBJC__) && defined (__EXCEPTIONS)
109# define CR_TEST_TRAMPOLINE_(Category, Name) \
110 static inline void CR_IDENTIFIER_(Category, Name, jmp)(void) { \
111 @try { \
112 criterion_internal_test_setup(); \
113 } @catch (NSException *e) { \
114 NSString *reason = [e reason]; \
115 criterion_test_die(cr_msg_test_init_std_exception, [reason UTF8String]); \
116 } @catch (...) { \
117 criterion_test_die(cr_msg_test_init_other_exception); \
118 } \
119 @try { \
120 criterion_internal_test_main((void (*)(void))CR_IDENTIFIER_(Category, Name, impl)); \
121 } @catch (NSException *e) { \
122 NSString *reason = [e reason]; \
123 criterion_test_die(cr_msg_test_main_std_exception, [reason UTF8String]); \
124 } @catch (...) { \
125 criterion_test_die(cr_msg_test_main_other_exception); \
126 } \
127 @try { \
128 criterion_internal_test_teardown(); \
129 } @catch (NSException *e) { \
130 NSString *reason = [e reason]; \
131 criterion_test_die(cr_msg_test_fini_std_exception, [reason UTF8String]); \
132 } @catch (...) { \
133 criterion_test_die(cr_msg_test_fini_other_exception); \
134 } \
135 }
136# else
137# define CR_TEST_TRAMPOLINE_(Category, Name) \
138 static inline void CR_IDENTIFIER_(Category, Name, jmp)(void) { \
139 criterion_internal_test_setup(); \
140 criterion_internal_test_main((void (*)(void))CR_IDENTIFIER_(Category, Name, impl)); \
141 criterion_internal_test_teardown(); \
142 }
143# endif
144#endif
145
146#if defined (_MSC_VER)
147# define CR_COMPILER_ CR_COMP_MSVC
148#elif defined (__clang__)
149# define CR_COMPILER_ CR_COMP_CLANG
150#elif defined (__GNUC__)
151# define CR_COMPILER_ CR_COMP_GCC
152#else
153# define CR_COMPILER_ CR_COMP_UNKNOWN
154#endif
155
156#define CR_TEST_BASE(Category, Name, ...) \
157 CR_TEST_PROTOTYPE_(Category, Name); \
158 CR_TEST_TRAMPOLINE_(Category, Name) \
159 struct criterion_test_extra_data CR_IDENTIFIER_(Category, Name, extra) = \
160 CR_EXPAND(CRITERION_MAKE_STRUCT(criterion_test_extra_data, \
161 .compiler_ = CR_COMPILER_, \
162 .lang_ = CR_LANG, \
163 .kind_ = CR_TEST_NORMAL, \
164 .param_ = (struct criterion_test_params (*)(void))NULL, \
165 .identifier_ = #Category "/" #Name, \
166 .file_ = __FILE__, \
167 .line_ = __LINE__, \
168 __VA_ARGS__ \
169 )); \
170 struct criterion_test CR_IDENTIFIER_(Category, Name, meta) = { \
171 #Name, \
172 #Category, \
173 CR_IDENTIFIER_(Category, Name, jmp), \
174 &CR_IDENTIFIER_(Category, Name, extra) \
175 }; \
176 CR_ATTRIBUTE(used) \
177 CR_SECTION_("cr_tst") \
178 struct criterion_test *CR_IDENTIFIER_(Category, Name, ptr) \
179 = &CR_IDENTIFIER_(Category, Name, meta) CR_SECTION_SUFFIX_; \
180 CR_TEST_PROTOTYPE_(Category, Name)
181
182#define CR_SUITE_BASE(Name, ...) \
183 struct criterion_test_extra_data CR_SUITE_IDENTIFIER_(Name, extra) = \
184 CR_EXPAND(CRITERION_MAKE_STRUCT(criterion_test_extra_data, \
185 .file_ = __FILE__, \
186 .line_ = 0, \
187 __VA_ARGS__ \
188 )); \
189 struct criterion_suite CR_SUITE_IDENTIFIER_(Name, meta) = { \
190 #Name, \
191 &CR_SUITE_IDENTIFIER_(Name, extra), \
192 }; \
193 CR_ATTRIBUTE(used) \
194 CR_SECTION_("cr_sts") \
195 struct criterion_suite *CR_SUITE_IDENTIFIER_(Name, ptr) \
196 = &CR_SUITE_IDENTIFIER_(Name, meta) CR_SECTION_SUFFIX_
197
198#undef Test
199#define Test(...) CR_EXPAND(CR_TEST_BASE(__VA_ARGS__, .sentinel_ = 0))
200#undef TestSuite
201#define TestSuite(...) CR_EXPAND(CR_SUITE_BASE(__VA_ARGS__, .sentinel_ = 0))
202
203#endif /* !CRITERION_INTERNAL_TEST_H_ */
#define CR_BEGIN_C_API
Definition common.h:54
#define CR_API
Definition common.h:128
#define CR_END_C_API
Definition common.h:55
static const char *const cr_msg_test_main_other_exception
Definition test.h:78
static const char *const cr_msg_test_fini_std_exception
Definition test.h:79
static CR_END_C_API const char *const cr_msg_test_init_std_exception
Definition test.h:75
CR_API void criterion_internal_test_teardown(void)
CR_API void criterion_internal_test_main(void(*fn)(void))
CR_BEGIN_C_API CR_API void criterion_internal_test_setup(void)
static const char *const cr_msg_test_fini_other_exception
Definition test.h:80
static const char *const cr_msg_test_main_std_exception
Definition test.h:77
static const char *const cr_msg_test_init_other_exception
Definition test.h:76