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#define GBL_TEST_VERIFY(expr) GBL_CTX_VERIFY_EXPRESSION(expr)
22#define GBL_TEST_COMPARE_FMT_(value) GBL_META_GENERIC_MACRO_GENERATE(GBL_TEST_COMPARE_FMT_TABLE_, value)()
23#define GBL_TEST_COMPARE_CMP_(actual, expected) GBL_META_GENERIC_MACRO_GENERATE(GBL_TEST_COMPARE_CMP_TABLE_, actual)(actual, expected)
24
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#define GBL_TEST_COMPARE_FMT_TABLE_ (
39 GBL_TEST_COMPARE_FMT_PTR_,
40 (
41 (char, GBL_TEST_COMPARE_FMT_CHAR_),
42 (int16_t, GBL_TEST_COMPARE_FMT_INT_),
43 (int32_t, GBL_TEST_COMPARE_FMT_INT_),
44 (int64_t, GBL_TEST_COMPARE_FMT_LINT_),
45 (uint8_t, GBL_TEST_COMPARE_FMT_UINT_),
46 (uint16_t, GBL_TEST_COMPARE_FMT_UINT_),
47 (uint32_t, GBL_TEST_COMPARE_FMT_UINT_),
48 (uint64_t, GBL_TEST_COMPARE_FMT_LUINT_),
49 (const char*, GBL_TEST_COMPARE_FMT_STR_),
50 (char*, GBL_TEST_COMPARE_FMT_STR_),
51 (float, GBL_TEST_COMPARE_FMT_FLT_),
52 (double, GBL_TEST_COMPARE_FMT_FLT_),
53 (const void*, GBL_TEST_COMPARE_FMT_PTR_)
54 )
55 )
56
57
58GBL_INLINE GblBool GBL_TEST_COMPARE_CMP_UINTPTR_(uintptr_t actual, uintptr_t expected) { return actual == expected; }
59GBL_INLINE GblBool GBL_TEST_COMPARE_CMP_PTR_ (const void* pActual, const void* pExpected) { return pActual == pExpected; }
60GBL_INLINE GblBool GBL_TEST_COMPARE_CMP_UINT32_ (uint32_t actual, uint32_t expected) { return actual == expected; }
61GBL_INLINE GblBool GBL_TEST_COMPARE_CMP_UINT64_ (uint64_t actual, uint64_t expected) { return actual == expected; }
62GBL_INLINE GblBool GBL_TEST_COMPARE_CMP_INT32_ (int32_t actual, int32_t expected) { return actual == expected; }
63GBL_INLINE GblBool GBL_TEST_COMPARE_CMP_INT64_ (int64_t actual, int64_t expected) { return actual == expected; }
64GBL_INLINE GblBool GBL_TEST_COMPARE_CMP_DBL_ (double actual, double expected) { return actual == expected; }
65GBL_INLINE GblBool GBL_TEST_COMPARE_CMP_STR_ (const char* pActual, const char* pExpected) { return pActual == pExpected ||
66 (pActual && pExpected &&
67 strcmp(pActual, pExpected) == 0); }
69
70#if defined(GBL_DREAMCAST)
71# define GBL_TEST_CMP_PLATFORM_ENTRIES()
72 (size_t , GBL_TEST_COMPARE_CMP_UINT32_),
73 (int, GBL_TEST_COMPARE_CMP_INT32_),
74#elif defined(GBL_PSP)
75# define GBL_TEST_CMP_PLATFORM_ENTRIES()
76 (size_t , GBL_TEST_COMPARE_CMP_UINT32_),
77 (int, GBL_TEST_COMPARE_CMP_INT32_),
78#elif (defined(__APPLE__) && defined(GBL_64BIT))
79# define GBL_TEST_CMP_PLATFORM_ENTRIES()
80 (size_t , GBL_TEST_COMPARE_CMP_UINT64_),
81 (long, GBL_TEST_COMPARE_CMP_INT64_),
82#elif defined(__EMSCRIPTEN__)
83# define GBL_TEST_CMP_PLATFORM_ENTRIES()
84 (size_t , GBL_TEST_COMPARE_CMP_UINT64_),
85 (int8_t, GBL_TEST_COMPARE_CMP_UINT32_),
86#else
87# define GBL_TEST_CMP_PLATFORM_ENTRIES()
88#endif
89
90#define GBL_TEST_COMPARE_CMP_TABLE_ (
91 GBL_TEST_COMPARE_CMP_PTR_,
92 (
93 (char, GBL_TEST_COMPARE_CMP_INT32_),
94 (int16_t, GBL_TEST_COMPARE_CMP_INT32_),
95 (int32_t, GBL_TEST_COMPARE_CMP_INT32_),
96 (int64_t, GBL_TEST_COMPARE_CMP_INT64_),
97 (uint8_t, GBL_TEST_COMPARE_CMP_UINT32_),
98 (uint16_t, GBL_TEST_COMPARE_CMP_UINT32_),
99 (uint32_t, GBL_TEST_COMPARE_CMP_UINT32_),
100 (uint64_t, GBL_TEST_COMPARE_CMP_UINT64_),
101 GBL_TEST_CMP_PLATFORM_ENTRIES()
102 (const char*, GBL_TEST_COMPARE_CMP_STR_),
103 (char*, GBL_TEST_COMPARE_CMP_STR_),
104 (float, GBL_TEST_COMPARE_CMP_DBL_),
105 (double, GBL_TEST_COMPARE_CMP_DBL_),
106 (const void*, GBL_TEST_COMPARE_CMP_PTR_)
107 )
108 )
109/// \endcond
110
111#define GBL_TEST_COMPARE(actual, expected) GBL_CTX_VERIFY_EXPRESSION(GBL_TEST_COMPARE_CMP_(actual, expected),
112 GBL_TEST_COMPARE_FMT_(actual),
113 actual, expected)
114
115#define GBL_TEST_COMPARE_INT(actual, expected) GBL_CTX_VERIFY_EXPRESSION(actual == expected,
116 "Values differed [expected: %d, actual: %d]",
117 actual, expected)
118
119#define GBL_TEST_COMPARE_UINT(actual, expected) GBL_CTX_VERIFY_EXPRESSION(actual == expected,
120 "Values differed [expected: %u, actual: %u]",
121 actual, expected)
122
123#define GBL_TEST_COMPARE_STRING(actual, expected) GBL_CTX_VERIFY_EXPRESSION(strcmp(actual, expected) == 0,
124 "Values differed [expected: %s, actual: %s]",
125 actual, expected)
126
127#define GBL_TEST_COMPARE_FLOAT(actual, expected) GBL_CTX_VERIFY_EXPRESSION(actual == expected,
128 "Values differed [expected: %f, actual: %f]",
129 actual, expected)
130
131#define GBL_TEST_COMPARE_POINTER(actual, expected) GBL_CTX_VERIFY_EXPRESSION(actual == expected,
132 "Values differed [exptected: %p, actual: %p]",
133 actual, expected)
134
135#define GBL_TEST_CALL GBL_CTX_VERIFY_CALL
136
137#define GBL_TEST_SKIP(reason) GBL_CTX_VERIFY(GBL_FALSE,
138 GBL_RESULT_SKIPPED,
139 reason)
140
141#define GBL_TEST_FAIL(reason) GBL_CTX_VERIFY(GBL_FALSE,
143 reason)
144
145#define GBL_TEST_EXPECT_ERROR()
147 GBL_CTX_VERIFY(!GBL_CONFIG_ASSERT_ERROR_ENABLED,
148 GBL_RESULT_SKIPPED,
149 "Skipping test case due to GBL_CONFIG_ASSERT_ERROR_ENABLED.");
150 GblTestScenario* pScenario = GBL_TEST_SCENARIO(GBL_CTX_CONTEXT());
151 if(pScenario) GblTestScenario_expectError(pScenario);
153
154
155#ifndef GBL_SELF_TYPE_NAME
156# define GBL_SELF_TYPE_NAME pSelf
157#endif
158
159#ifndef GBL_TEST_SUITE_CONTEXT_NAME
160# define GBL_TEST_SUITE_CONTEXT_NAME pCtx
161#endif
162
163#ifndef GBL_SELF_TYPE_PRIVATE_NAME
164# define GBL_SELF_TYPE_PRIVATE_NAME pFixture
165#endif
166
167#ifndef GBL_SELF_TYPE_PRIVATE
168# define GBL_SELF_TYPE_PRIVATE GBL_GLUE(GBL_SELF_TYPE, _)
169#endif
170
171#define GBL_TEST_CASE(name)
172 static GBL_RESULT GBL_GLUE(GBL_GLUE(GBL_GLUE(GBL_SELF_TYPE, _), name), _)
173 (GblTestSuite* GBL_SELF_TYPE_NAME,
174 GblContext* GBL_TEST_SUITE_CONTEXT_NAME) {
178 GBL_PRIVATE(GBL_SELF_TYPE, pSelf);
182
183#define GBL_TEST_CASE_END
184 GBL_CTX_END(); }
185
186#define GBL_TEST_FIXTURE_TYPE
188
189#define GBL_TEST_FIXTURE
191 typedef struct GBL_TEST_FIXTURE_TYPE GblTestFixture;
193
194#ifdef __cplusplus
195# define GBL_TEST_REGISTER(...)
196 extern "C" GBL_TEST_REGISTER_(__VA_ARGS__)
197#else
198# define GBL_TEST_REGISTER(...)
199 GBL_TEST_REGISTER_(__VA_ARGS__)
200#endif
201
202#define GBL_TEST_REGISTER_(...)
203 GBL_EXPORT GblType GBL_GLUE(GBL_GLUE(GBL_SELF_TYPE, _), type)(void) GBL_NOEXCEPT {
204 static GblType type = GBL_INVALID_TYPE;
205 const static GblTestCase cases[] = {
206 GBL_TUPLE_FOREACH(GBL_TEST_CASE_PAIR, GBL_GLUE(GBL_SELF_TYPE,_), (__VA_ARGS__))
207 { NULL, NULL }
208 };
209 const static GblTestSuiteVTable vTable = {
210 .pFnSuiteInit = GBL_GLUE(GBL_GLUE(GBL_GLUE(GBL_SELF_TYPE, _), init), _),
211 .pFnSuiteFinal = GBL_GLUE(GBL_GLUE(GBL_GLUE(GBL_SELF_TYPE, _), final), _),
212 .pCases = cases
213 };
214 if(type == GBL_INVALID_TYPE) {
215 GBL_CTX_BEGIN(NULL);
216 type = GblTestSuite_register(GblQuark_internStatic(GBL_STRINGIFY(GBL_SELF_TYPE)),
217 &vTable,
218 sizeof(GBL_SELF_TYPE),
219 sizeof(GBL_SELF_TYPE_PRIVATE),
220 GBL_TYPE_FLAGS_NONE);
223 }
224 return type; \
225}
226
227#define GBL_TEST_CASE_PAIR(suiteName, caseName)
228 { GBL_STRINGIFY(caseName), GBL_GLUE(GBL_GLUE(suiteName, caseName), _) },
229
230#define GBL_TEST_INIT() GBL_TEST_CASE(init)
231#define GBL_TEST_FINAL() GBL_TEST_CASE(final)
232#define GBL_TEST_NO_INIT
235#define GBL_TEST_NO_FINAL
238#define GBL_TEST_INIT_END GBL_TEST_CASE_END
239#define GBL_TEST_FINAL_END GBL_TEST_CASE_END
240
241#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:539
#define GBL_CTX_END_BLOCK()
Definition gimbal_ctx.h:567
#define GBL_CTX_VERIFY_LAST_RECORD()
Definition gimbal_ctx.h:160
#define GBL_CTX_BEGIN(...)
Definition gimbal_ctx.h:561
#define GBL_CTX_VERIFY_EXPRESSION(...)
Definition gimbal_ctx.h:111
#define GBL_CTX_END()
Definition gimbal_ctx.h:574
#define GBL_CTX_VERIFY(...)
Definition gimbal_ctx.h:97
#define GBL_CTX_CONTEXT()
Definition gimbal_ctx.h:23
#define GBL_DECLS_BEGIN
#define GBL_UNUSED(...)
#define GBL_EXPORT
#define GBL_DECLARE_STRUCT(S)
#define GBL_PRIVATE(cType, self)
#define GBL_STRINGIFY(a)
#define GBL_GLUE(A, B)
#define GBL_TUPLE_FOREACH(MACRO_, DATA_, TUPLE_)
#define GBL_META_GENERIC_MACRO_GENERATE(traits, X)
#define GBL_RESULT_ERROR(value)
#define GBL_TEST_FIXTURE_TYPE
#define GBL_TEST_INIT()
#define GBL_TEST_COMPARE_FMT_(value)
#define GBL_TEST_COMPARE_CMP_(actual, expected)
#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_FINAL()
#define GBL_TEST_SUITE_CONTEXT_NAME
#define GBL_TEST_SCENARIO(self)
#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)