libGimbal 0.1.0
C17-Based Extended Standard Library and Cross-Language Runtime Framework
Loading...
Searching...
No Matches
gimbal_test_macros.h
Go to the documentation of this file.
1/*! \file
2 * \brief Unit testing macro utilities
3 * \ingroup testing
4 *
5 * \todo
6 * - need to make an actual exported set of C
7 * functions as the top-level entry-point into
8 * GBL_TEST_COMPARE(), so that language bindings
9 * without macros can use the test framework.
10 *
11 * \author Falco Girgis
12 */
13#ifndef GIMBAL_TEST_MACROS_H
14#define GIMBAL_TEST_MACROS_H
15
16#include "../preprocessor/gimbal_macro_utils.h"
18#include <stdint.h>
19#include <inttypes.h>
20
21#ifdef __cplusplus
22# include <concepts>
23#endif
24
25#define GBL_TEST_VERIFY(expr) GBL_CTX_VERIFY_EXPRESSION(expr)
26
27/// \cond
28GBL_INLINE const char* GBL_TEST_COMPARE_FMT_DFLT_ (void) { return "Values differed"; }
29GBL_INLINE const char* GBL_TEST_COMPARE_FMT_CHAR_ (void) { return "Values differed [actual: %c, expected: %c]"; }
30GBL_INLINE const char* GBL_TEST_COMPARE_FMT_INT_ (void) { return "Values differed [actual: %d, expected: %d]"; }
31GBL_INLINE const char* GBL_TEST_COMPARE_FMT_UINT_ (void) { return "Values differed [actual: %u, expected: %u]"; }
32GBL_INLINE const char* GBL_TEST_COMPARE_FMT_LINT_ (void) { return "Values differed [actual: %" PRId64 ", expected: %" PRId64 "]"; }
33GBL_INLINE const char* GBL_TEST_COMPARE_FMT_LUINT_(void) { return "Values differed [actual: %" PRIu64 ", expected: %" PRIu64 "]"; }
34GBL_INLINE const char* GBL_TEST_COMPARE_FMT_STR_ (void) { return "Values differed [actual: %s, expected: %s]"; }
35GBL_INLINE const char* GBL_TEST_COMPARE_FMT_FLT_ (void) { return "Values differed [actual: %f, expected: %f]"; }
36GBL_INLINE const char* GBL_TEST_COMPARE_FMT_PTR_ (void) { return "Values differed [actual: %p, expected: %p]"; }
37
38#ifndef __cplusplus
39# define GBL_TEST_COMPARE_FMT_TABLE_ (
40 GBL_TEST_COMPARE_FMT_PTR_,
41 (
42 (char, GBL_TEST_COMPARE_FMT_CHAR_),
43 (int16_t, GBL_TEST_COMPARE_FMT_INT_),
44 (int32_t, GBL_TEST_COMPARE_FMT_INT_),
45 (int64_t, GBL_TEST_COMPARE_FMT_LINT_),
46 (uint8_t, GBL_TEST_COMPARE_FMT_UINT_),
47 (uint16_t, GBL_TEST_COMPARE_FMT_UINT_),
48 (uint32_t, GBL_TEST_COMPARE_FMT_UINT_),
49 (uint64_t, GBL_TEST_COMPARE_FMT_LUINT_),
50 (const char*, GBL_TEST_COMPARE_FMT_STR_),
51 (char*, GBL_TEST_COMPARE_FMT_STR_),
52 (float, GBL_TEST_COMPARE_FMT_FLT_),
53 (double, GBL_TEST_COMPARE_FMT_FLT_),
54 (const void*, GBL_TEST_COMPARE_FMT_PTR_)
55 )
56 )
57
58# define GBL_TEST_COMPARE_FMT_(value) GBL_META_GENERIC_MACRO_GENERATE(GBL_TEST_COMPARE_FMT_TABLE_, value)()
59#else
60
61inline constexpr const char* GBL_TEST_COMPARE_FMT_(char) { return "Values differed [actual: %c, expected: %c]"; }
62inline constexpr const char* GBL_TEST_COMPARE_FMT_(short) { return "Values differed [actual: %d, expected: %d]"; }
63inline constexpr const char* GBL_TEST_COMPARE_FMT_(unsigned short) { return "Values differed [actual: %u, expected: %u]"; }
64inline constexpr const char* GBL_TEST_COMPARE_FMT_(int) { return "Values differed [actual: %d, expected: %d]"; }
65inline constexpr const char* GBL_TEST_COMPARE_FMT_(unsigned) { return "Values differed [actual: %u, expected: %u]"; }
66inline constexpr const char* GBL_TEST_COMPARE_FMT_(long) { return "Values differed [actual: %ld, expected: %ld]"; }
67inline constexpr const char* GBL_TEST_COMPARE_FMT_(unsigned long) { return "Values differed [actual: %lu, expected: %lu]"; }
68inline constexpr const char* GBL_TEST_COMPARE_FMT_(long long) { return "Values differed [actual: %lld, expected: %lld]"; }
69inline constexpr const char* GBL_TEST_COMPARE_FMT_(unsigned long long) { return "Values differed [actual: %llu, expected: %llu]"; }
70inline constexpr const char* GBL_TEST_COMPARE_FMT_(const char*) { return "Values differed [actual: %s, expected: %s]"; }
71inline constexpr const char* GBL_TEST_COMPARE_FMT_(float) { return "Values differed [actual: %f, expected: %f]"; }
72inline constexpr const char* GBL_TEST_COMPARE_FMT_(double) { return "Values differed [actual: %f, expected: %f]"; }
73inline constexpr const char* GBL_TEST_COMPARE_FMT_(auto*) { return "Values differed [actual: %p, expected: %p]"; }
74inline constexpr const char* GBL_TEST_COMPARE_FMT_(auto) { return "Values differed"; }
75
76#endif
77
78
79GBL_INLINE GblBool GBL_TEST_COMPARE_CMP_UINTPTR_(uintptr_t actual, uintptr_t expected) { return actual == expected; }
80GBL_INLINE GblBool GBL_TEST_COMPARE_CMP_PTR_ (const void* pActual, const void* pExpected) { return pActual == pExpected; }
81GBL_INLINE GblBool GBL_TEST_COMPARE_CMP_UINT32_ (uint32_t actual, uint32_t expected) { return actual == expected; }
82GBL_INLINE GblBool GBL_TEST_COMPARE_CMP_UINT64_ (uint64_t actual, uint64_t expected) { return actual == expected; }
83GBL_INLINE GblBool GBL_TEST_COMPARE_CMP_INT32_ (int32_t actual, int32_t expected) { return actual == expected; }
84GBL_INLINE GblBool GBL_TEST_COMPARE_CMP_INT64_ (int64_t actual, int64_t expected) { return actual == expected; }
85GBL_INLINE GblBool GBL_TEST_COMPARE_CMP_DBL_ (double actual, double expected) { return actual == expected; }
86GBL_INLINE GblBool GBL_TEST_COMPARE_CMP_STR_ (const char* pActual, const char* pExpected) { return pActual == pExpected ||
87 (pActual && pExpected &&
88 strcmp(pActual, pExpected) == 0); }
89
90#ifndef __cplusplus
91#if defined(GBL_DREAMCAST) || defined(GBL_GAMECUBE)
92# define GBL_TEST_CMP_PLATFORM_ENTRIES()
93 (size_t , GBL_TEST_COMPARE_CMP_UINT32_),
94 (int, GBL_TEST_COMPARE_CMP_INT32_),
95#elif defined(GBL_PSP)
96# define GBL_TEST_CMP_PLATFORM_ENTRIES()
97 (size_t , GBL_TEST_COMPARE_CMP_UINT32_),
98 (int, GBL_TEST_COMPARE_CMP_INT32_),
99#elif (defined(__APPLE__) && defined(GBL_64BIT))
100# define GBL_TEST_CMP_PLATFORM_ENTRIES()
101 (size_t , GBL_TEST_COMPARE_CMP_UINT64_),
102 (long, GBL_TEST_COMPARE_CMP_INT64_),
103#elif defined(__EMSCRIPTEN__)
104# define GBL_TEST_CMP_PLATFORM_ENTRIES()
105 (size_t , GBL_TEST_COMPARE_CMP_UINT64_),
106 (int8_t, GBL_TEST_COMPARE_CMP_UINT32_),
107# else
108# define GBL_TEST_CMP_PLATFORM_ENTRIES()
109# endif
110
111# define GBL_TEST_COMPARE_CMP_TABLE_ (
112 GBL_TEST_COMPARE_CMP_PTR_,
113 (
114 (char, GBL_TEST_COMPARE_CMP_INT32_),
115 (int16_t, GBL_TEST_COMPARE_CMP_INT32_),
116 (int32_t, GBL_TEST_COMPARE_CMP_INT32_),
117 (int64_t, GBL_TEST_COMPARE_CMP_INT64_),
118 (uint8_t, GBL_TEST_COMPARE_CMP_UINT32_),
119 (uint16_t, GBL_TEST_COMPARE_CMP_UINT32_),
120 (uint32_t, GBL_TEST_COMPARE_CMP_UINT32_),
121 (uint64_t, GBL_TEST_COMPARE_CMP_UINT64_),
122 GBL_TEST_CMP_PLATFORM_ENTRIES()
123 (const char*, GBL_TEST_COMPARE_CMP_STR_),
124 (char*, GBL_TEST_COMPARE_CMP_STR_),
125 (float, GBL_TEST_COMPARE_CMP_DBL_),
126 (double, GBL_TEST_COMPARE_CMP_DBL_),
127 (const void*, GBL_TEST_COMPARE_CMP_PTR_)
128 )
129 )
130
131# define GBL_TEST_COMPARE_CMP_(actual, expected) GBL_META_GENERIC_MACRO_GENERATE(GBL_TEST_COMPARE_CMP_TABLE_, actual)(actual, expected)
132
133#else
134inline constexpr GblBool GBL_TEST_COMPARE_CMP_(auto actual, auto expected) noexcept {
135 if constexpr(std::convertible_to<decltype(actual), decltype(expected)>)
136 return static_cast<decltype(expected)>(actual) == expected;
137 else if constexpr(std::convertible_to<decltype(expected), decltype(actual)>)
138 return actual == static_cast<decltype(actual)>(expected);
139 else static_assert("Incompatible types!");
140}
141
142inline GblBool GBL_TEST_COMPARE_CMP_(const char* pActual, const char *pExpected) noexcept {
143 return GBL_TEST_COMPARE_CMP_STR_(pActual, pExpected);
144}
145
146#endif
147/// \endcond
148
149#define GBL_TEST_COMPARE(actual, expected) GBL_CTX_VERIFY_EXPRESSION(GBL_TEST_COMPARE_CMP_(actual, expected),
150 GBL_TEST_COMPARE_FMT_(actual),
151 actual, expected)
152
153#define GBL_TEST_COMPARE_INT(actual, expected) GBL_CTX_VERIFY_EXPRESSION(actual == expected,
154 "Values differed [expected: %d, actual: %d]",
155 actual, expected)
156
157#define GBL_TEST_COMPARE_UINT(actual, expected) GBL_CTX_VERIFY_EXPRESSION(actual == expected,
158 "Values differed [expected: %u, actual: %u]",
159 actual, expected)
160
161#define GBL_TEST_COMPARE_STRING(actual, expected) GBL_CTX_VERIFY_EXPRESSION(strcmp(actual, expected) == 0,
162 "Values differed [expected: %s, actual: %s]",
163 actual, expected)
164
165#define GBL_TEST_COMPARE_FLOAT(actual, expected) GBL_CTX_VERIFY_EXPRESSION(actual == expected,
166 "Values differed [expected: %f, actual: %f]",
167 actual, expected)
168
169#define GBL_TEST_COMPARE_POINTER(actual, expected) GBL_CTX_VERIFY_EXPRESSION(actual == expected,
170 "Values differed [exptected: %p, actual: %p]",
171 actual, expected)
172
173typedef enum GBL_TEST_ERROR_COMPARISON {
174 GBL_TEST_ERROR_ABSOLUTE,
175 GBL_TEST_ERROR_RELATIVE,
176 GBL_TEST_ERROR_FUZZY
177} GBL_TEST_ERROR_COMPARISON;
178
179#define GBL_TEST_ERROR(actual, precise, epsilon, cmp)
181 double relative = gblRelativeError((actual), (precise));
182 double absolute = gblAbsoluteError((actual), (precise));
183 if(((cmp) == GBL_TEST_ERROR_ABSOLUTE && absolute > (epsilon)) ||
184 ((cmp) == GBL_TEST_ERROR_RELATIVE && relative > (epsilon)) ||
185 (absolute > (epsilon) && relative > (epsilon)))
186 GBL_TEST_FAIL("(actual == %.9f) != (expected == %.9f)\n"
187 "\t ERROR: Rel == %.9f, Abs == %.9f, Max == %.9f",
188 (actual), (precise), relative, absolute, (epsilon));
190
191
192#define GBL_TEST_CALL GBL_CTX_VERIFY_CALL
193
194#define GBL_TEST_SKIP(reason) GBL_CTX_VERIFY(GBL_FALSE,
195 GBL_RESULT_SKIPPED,
196 reason)
197
198#define GBL_TEST_FAIL(...) GBL_CTX_VERIFY(GBL_FALSE,
200 __VA_ARGS__)
201
202#define GBL_TEST_EXPECT_ERROR()
204 GBL_CTX_VERIFY(!GBL_CONFIG_ASSERT_ERROR_ENABLED,
205 GBL_RESULT_SKIPPED,
206 "Skipping test case due to GBL_CONFIG_ASSERT_ERROR_ENABLED.");
207 GblTestScenario* pScenario = GBL_TEST_SCENARIO(GBL_CTX_CONTEXT());
208 if(pScenario) GblTestScenario_expectError(pScenario);
210
211
212#ifndef GBL_SELF_TYPE_NAME
213# define GBL_SELF_TYPE_NAME pSelf
214#endif
215
216#ifndef GBL_TEST_SUITE_CONTEXT_NAME
217# define GBL_TEST_SUITE_CONTEXT_NAME pCtx
218#endif
219
220#ifndef GBL_SELF_TYPE_PRIVATE_NAME
221# define GBL_SELF_TYPE_PRIVATE_NAME pFixture
222#endif
223
224#ifndef GBL_SELF_TYPE_PRIVATE
225# define GBL_SELF_TYPE_PRIVATE GBL_GLUE(GBL_SELF_TYPE, _)
226#endif
227
228#define GBL_TEST_CASE(name)
229 static GBL_RESULT GBL_GLUE(GBL_GLUE(GBL_GLUE(GBL_SELF_TYPE, _), name), _)
230 (GblTestSuite* GBL_SELF_TYPE_NAME,
231 GblContext* GBL_TEST_SUITE_CONTEXT_NAME) {
235 GBL_PRIVATE(GBL_SELF_TYPE, pSelf);
239
240#define GBL_TEST_CASE_END
241 GBL_CTX_END(); }
242
243#define GBL_TEST_FIXTURE_TYPE
245
246#define GBL_TEST_FIXTURE
248 typedef struct GBL_TEST_FIXTURE_TYPE GblTestFixture;
250
251#ifdef __cplusplus
252# define GBL_TEST_REGISTER(...)
253 extern "C" GBL_TEST_REGISTER_(__VA_ARGS__)
254#else
255# define GBL_TEST_REGISTER(...)
256 GBL_TEST_REGISTER_(__VA_ARGS__)
257#endif
258
259#define GBL_TEST_REGISTER_(...)
260 GBL_EXPORT GblType GBL_GLUE(GBL_GLUE(GBL_SELF_TYPE, _), type)(void) GBL_NOEXCEPT {
261 static GblType type = GBL_INVALID_TYPE;
262 const static GblTestCase cases[] = {
263 GBL_TUPLE_FOREACH(GBL_TEST_CASE_PAIR, GBL_GLUE(GBL_SELF_TYPE,_), (__VA_ARGS__))
264 { NULL, NULL }
265 };
266 const static GblTestSuiteVTable vTable = {
267 .pFnSuiteInit = GBL_GLUE(GBL_GLUE(GBL_GLUE(GBL_SELF_TYPE, _), init), _),
268 .pFnSuiteFinal = GBL_GLUE(GBL_GLUE(GBL_GLUE(GBL_SELF_TYPE, _), final), _),
269 .pCases = cases
270 };
271 if(type == GBL_INVALID_TYPE) {
272 GBL_CTX_BEGIN(NULL);
273 type = GblTestSuite_register(GblQuark_internStatic(GBL_STRINGIFY(GBL_SELF_TYPE)),
274 &vTable,
275 sizeof(GBL_SELF_TYPE),
276 sizeof(GBL_SELF_TYPE_PRIVATE),
277 GBL_TYPE_FLAGS_NONE);
280 }
281 return type; \
282}
283
284#define GBL_TEST_CASE_PAIR(suiteName, caseName)
285 { GBL_STRINGIFY(caseName), GBL_GLUE(GBL_GLUE(suiteName, caseName), _) },
286
287#define GBL_TEST_INIT() GBL_TEST_CASE(init)
288#define GBL_TEST_FINAL() GBL_TEST_CASE(final)
289#define GBL_TEST_INIT_NONE
292#define GBL_TEST_FINAL_NONE
295#define GBL_TEST_FIXTURE_NONE
296 GBL_TEST_FIXTURE { uintptr_t compat; };
297#define GBL_TEST_INIT_END GBL_TEST_CASE_END
298#define GBL_TEST_FINAL_END GBL_TEST_CASE_END
299
300#endif // GIMBAL_TEST_MACROS_H
#define GBL_STMT_START
#define GBL_NOEXCEPT
#define GBL_INLINE
#define GBL_STMT_END
#define GBL_CTX_VERIFY_CALL(...)
Definition gimbal_ctx.h:541
#define GBL_CTX_END_BLOCK()
Definition gimbal_ctx.h:569
#define GBL_CTX_VERIFY_LAST_RECORD()
Definition gimbal_ctx.h:162
#define GBL_CTX_BEGIN(...)
Definition gimbal_ctx.h:563
#define GBL_CTX_VERIFY_EXPRESSION(...)
Definition gimbal_ctx.h:113
#define GBL_CTX_END()
Definition gimbal_ctx.h:576
#define GBL_CTX_VERIFY(...)
Definition gimbal_ctx.h:99
#define GBL_CTX_CONTEXT()
Definition gimbal_ctx.h:25
#define GBL_UNUSED(...)
#define GBL_EXPORT
#define GBL_DECLARE_STRUCT(S)
#define GBL_STRINGIFY(a)
#define GBL_GLUE(A, B)
#define GBL_TUPLE_FOREACH(MACRO_, DATA_, TUPLE_)
#define GBL_RESULT_ERROR(value)
#define GBL_TEST_FIXTURE_TYPE
#define GBL_TEST_INIT()
#define GBL_SELF_TYPE_PRIVATE
#define GBL_TEST_CASE(name)
#define GBL_TEST_CASE_PAIR(suiteName, caseName)
#define GBL_TEST_REGISTER_(...)
#define GBL_SELF_TYPE_PRIVATE_NAME
#define GBL_TEST_CASE_END
#define GBL_SELF_TYPE_NAME
#define GBL_TEST_FAIL(...)
#define GBL_TEST_FIXTURE
#define GBL_TEST_FINAL()
#define GBL_TEST_SUITE_CONTEXT_NAME
#define GBL_TEST_SCENARIO(self)
Casts a GblInstance to a GblTestScenario.
#define GBL_INVALID_TYPE
GblType UUID of the invalid type.
Definition gimbal_type.h:31
#define GBL_FALSE
uint8_t GblBool
Basic boolean type, standardized to sizeof(char)