| 1 |
/* Convenience macros to reduce munit boiler plate. */ |
| 2 |
|
| 3 |
#ifndef TEST_RUNNER_H |
| 4 |
#define TEST_RUNNER_H |
| 5 |
|
| 6 |
#include <signal.h> |
| 7 |
|
| 8 |
#include "munit.h" |
| 9 |
|
| 10 |
#include "../../src/tracing.h" |
| 11 |
|
| 12 |
/* Top-level suites array declaration. |
| 13 |
* |
| 14 |
* These top-level suites hold all module-level child suites and must be defined |
| 15 |
* and then set as child suites of a root suite created at runtime by the test |
| 16 |
* runner's main(). This can be done using the RUNNER macro. */ |
| 17 |
extern MunitSuite _main_suites[]; |
| 18 |
extern int _main_suites_n; |
| 19 |
|
| 20 |
/* Maximum number of test cases for each suite */ |
| 21 |
#define SUITE__CAP 128 |
| 22 |
#define TEST__CAP SUITE__CAP |
| 23 |
|
| 24 |
/* Define the top-level suites array and the main() function of the test. */ |
| 25 |
#define RUNNER(NAME) \ |
| 26 |
MunitSuite _main_suites[SUITE__CAP]; \ |
| 27 |
int _main_suites_n = 0; \ |
| 28 |
\ |
| 29 |
int main(int argc, char *argv[MUNIT_ARRAY_PARAM(argc)]) \ |
| 30 |
{ \ |
| 31 |
signal(SIGPIPE, SIG_IGN); \ |
| 32 |
cowsqlTracingMaybeEnable(true); \ |
| 33 |
MunitSuite suite = {(char *)"", NULL, _main_suites, 1, 0}; \ |
| 34 |
return munit_suite_main(&suite, (void *)NAME, argc, argv); \ |
| 35 |
} |
| 36 |
|
| 37 |
/* Declare and register a new test suite #S belonging to the file's test module. |
| 38 |
* |
| 39 |
* A test suite is a pair of static variables: |
| 40 |
* |
| 41 |
* static MunitTest _##S##_suites[SUITE__CAP] |
| 42 |
* static MunitTest _##S##_tests[SUITE__CAP] |
| 43 |
* |
| 44 |
* The tests and suites attributes of the next available MunitSuite slot in the |
| 45 |
* _module_suites array will be set to the suite's tests and suites arrays, and |
| 46 |
* the prefix attribute of the slot will be set to /S. */ |
| 47 |
#define SUITE(S) \ |
| 48 |
SUITE__DECLARE(S) \ |
| 49 |
SUITE__ADD_CHILD(main, #S, S) |
| 50 |
|
| 51 |
/* Declare and register a new test. */ |
| 52 |
#define TEST(S, C, SETUP, TEAR_DOWN, OPTIONS, PARAMS) \ |
| 53 |
static MunitResult test_##S##_##C(const MunitParameter params[], \ |
| 54 |
void *data); \ |
| 55 |
TEST__ADD_TO_SUITE(S, C, SETUP, TEAR_DOWN, OPTIONS, PARAMS) \ |
| 56 |
static MunitResult test_##S##_##C( \ |
| 57 |
MUNIT_UNUSED const MunitParameter params[], \ |
| 58 |
MUNIT_UNUSED void *data) |
| 59 |
|
| 60 |
#define SKIP_IF_NO_FIXTURE \ |
| 61 |
if (f == NULL) { \ |
| 62 |
return MUNIT_SKIP; \ |
| 63 |
} |
| 64 |
|
| 65 |
/* Declare the MunitSuite[] and the MunitTest[] arrays that compose the test |
| 66 |
* suite identified by S. */ |
| 67 |
#define SUITE__DECLARE(S) \ |
| 68 |
static MunitSuite _##S##_suites[SUITE__CAP]; \ |
| 69 |
static MunitTest _##S##_tests[SUITE__CAP]; \ |
| 70 |
static MunitTestSetup _##S##_setup = NULL; \ |
| 71 |
static MunitTestTearDown _##S##_tear_down = NULL; \ |
| 72 |
static int _##S##_suites_n = 0; \ |
| 73 |
static int _##S##_tests_n = 0; \ |
| 74 |
__attribute__((constructor(101))) static void _##S##_init(void) \ |
| 75 |
{ \ |
| 76 |
memset(_##S##_suites, 0, sizeof(_##S##_suites)); \ |
| 77 |
memset(_##S##_tests, 0, sizeof(_##S##_tests)); \ |
| 78 |
(void)_##S##_suites_n; \ |
| 79 |
(void)_##S##_tests_n; \ |
| 80 |
(void)_##S##_setup; \ |
| 81 |
(void)_##S##_tear_down; \ |
| 82 |
} |
| 83 |
|
| 84 |
/* Set the tests and suites attributes of the next available slot of the |
| 85 |
* MunitSuite[] array of S1 to the MunitTest[] and MunitSuite[] arrays of S2, |
| 86 |
* using the given PREXIX. */ |
| 87 |
#define SUITE__ADD_CHILD(S1, PREFIX, S2) \ |
| 88 |
__attribute__((constructor(102))) static void _##S1##_##S2##_init( \ |
| 89 |
void) \ |
| 90 |
{ \ |
| 91 |
int n = _##S1##_suites_n; \ |
| 92 |
_##S1##_suites[n].prefix = PREFIX; \ |
| 93 |
_##S1##_suites[n].tests = _##S2##_tests; \ |
| 94 |
_##S1##_suites[n].suites = _##S2##_suites; \ |
| 95 |
_##S1##_suites[n].iterations = 0; \ |
| 96 |
_##S1##_suites[n].options = 0; \ |
| 97 |
_##S1##_suites_n = n + 1; \ |
| 98 |
} |
| 99 |
|
| 100 |
/* Add a test case to the MunitTest[] array of suite S. */ |
| 101 |
#define TEST__ADD_TO_SUITE(S, C, SETUP, TEAR_DOWN, OPTIONS, PARAMS) \ |
| 102 |
__attribute__((constructor(103))) static void _##S##_tests_##C##_init( \ |
| 103 |
void) \ |
| 104 |
{ \ |
| 105 |
MunitTest *tests = _##S##_tests; \ |
| 106 |
int n = _##S##_tests_n; \ |
| 107 |
TEST__SET_IN_ARRAY(tests, n, "/" #C, test_##S##_##C, SETUP, \ |
| 108 |
TEAR_DOWN, OPTIONS, PARAMS); \ |
| 109 |
_##S##_tests_n = n + 1; \ |
| 110 |
} |
| 111 |
|
| 112 |
/* Set the values of the I'th test case slot in the given test array */ |
| 113 |
#define TEST__SET_IN_ARRAY(TESTS, I, NAME, FUNC, SETUP, TEAR_DOWN, OPTIONS, \ |
| 114 |
PARAMS) \ |
| 115 |
TESTS[I].name = NAME; \ |
| 116 |
TESTS[I].test = FUNC; \ |
| 117 |
TESTS[I].setup = SETUP; \ |
| 118 |
TESTS[I].tear_down = TEAR_DOWN; \ |
| 119 |
TESTS[I].options = OPTIONS; \ |
| 120 |
TESTS[I].parameters = PARAMS |
| 121 |
|
| 122 |
/** |
| 123 |
* Declare and register a new test module #M. |
| 124 |
* |
| 125 |
* A test module is a test suite (i.e. a pair of MunitTest[] and MunitSuite[] |
| 126 |
* arrays), directly or indirectly containing all test cases in a test file. |
| 127 |
* |
| 128 |
* This macro uses hard-coded names to declare the module's tests and suites |
| 129 |
* arrays static, so they can be easly referenced in other static declarations |
| 130 |
* generated by the macros below: |
| 131 |
* |
| 132 |
* static MunitTest _module_tests[TEST__CAP]; |
| 133 |
* static MunitSuite _module_suites[TEST__CAP]; |
| 134 |
* |
| 135 |
* The tests and suites attributes of the next available MunitSuite slot in the |
| 136 |
* top-level suites array will be set to the module's tests and suites arrays, |
| 137 |
* and the prefix attribute of the slot will be set to #M. |
| 138 |
* |
| 139 |
* Each test file should declare one and only one test module. |
| 140 |
*/ |
| 141 |
#define TEST_MODULE(M) \ |
| 142 |
TEST_SUITE__DECLARE(module); \ |
| 143 |
TEST_SUITE__ADD_CHILD(main, #M, module); |
| 144 |
|
| 145 |
/** |
| 146 |
* Declare and register a new test suite #S belonging to the file's test module. |
| 147 |
* |
| 148 |
* A test suite is a pair of static variables: |
| 149 |
* |
| 150 |
* static MunitTest _##S##_suites[TEST__CAP] |
| 151 |
* static MunitTest _##S##_tests[TEST__CAP] |
| 152 |
* |
| 153 |
* The tests and suites attributes of the next available MunitSuite slot in the |
| 154 |
* #_module_suites array will be set to the suite's tests and suites arrays, and |
| 155 |
* the prefix attribute of the slot will be set to /S. |
| 156 |
* |
| 157 |
* All tests in the suite will use the same setup and tear down functions. |
| 158 |
*/ |
| 159 |
#define TEST_SUITE(S) \ |
| 160 |
TEST_SUITE__DECLARE(S); \ |
| 161 |
TEST_SUITE__ADD_CHILD(module, "/" #S, S); |
| 162 |
|
| 163 |
/** |
| 164 |
* Declare a setup function. |
| 165 |
* |
| 166 |
* Possible signatures are: |
| 167 |
* |
| 168 |
* - TEST_SETUP(S): Declare the setup function for suite S inline. |
| 169 |
* - TEST_SETUP(S, F): Set the setup function for suite S to F. |
| 170 |
*/ |
| 171 |
#define TEST_SETUP(...) TEST_SETUP__MACRO_CHOOSER(__VA_ARGS__)(__VA_ARGS__) |
| 172 |
#define TEST_SETUP_(S) \ |
| 173 |
static void *S##_setup(const MunitParameter[], void *); \ |
| 174 |
_##S##_setup = S##_setup; \ |
| 175 |
static void *S##_setup(const MunitParameter params[], void *user_data) |
| 176 |
|
| 177 |
/** |
| 178 |
* Declare a tear down function. |
| 179 |
* |
| 180 |
* Possible signatures are: |
| 181 |
* |
| 182 |
* - TEST_SETUP(S): Declare the tear down function for suite S inline. |
| 183 |
* - TEST_SETUP(S, F): Set the tear down function for suite S to F. |
| 184 |
*/ |
| 185 |
#define TEST_TEAR_DOWN(...) \ |
| 186 |
TEST_TEAR_DOWN__MACRO_CHOOSER(__VA_ARGS__)(__VA_ARGS__) |
| 187 |
|
| 188 |
/** |
| 189 |
* Declare and register a new group of tests #G, belonging to suite #S in the |
| 190 |
* file's test module. |
| 191 |
*/ |
| 192 |
#define TEST_GROUP(C, T) \ |
| 193 |
static MunitTest _##C##_##T##_tests[TEST__CAP]; \ |
| 194 |
static int _##C##_##T##_tests_n = 0; \ |
| 195 |
TEST_SUITE__ADD_GROUP(C, T); |
| 196 |
|
| 197 |
/** |
| 198 |
* Declare and register a new test case. |
| 199 |
* |
| 200 |
* Possible signatures are: |
| 201 |
* |
| 202 |
* - TEST_CASE(C): C gets added to the tests array of the file module. |
| 203 |
* - TEST_CASE(S, C): C gets added to the tests array of suite S. |
| 204 |
* - TEST_CASE(S, G, C): C gets added to the tests array of group G in suite S. |
| 205 |
* |
| 206 |
* The test body declaration must follow the macro. |
| 207 |
*/ |
| 208 |
#define TEST_CASE(...) TEST_CASE__MACRO_CHOOSER(__VA_ARGS__)(__VA_ARGS__) |
| 209 |
|
| 210 |
/* Declare the MunitSuite[] and the MunitTest[] arrays that compose the test |
| 211 |
* suite identified by S. */ |
| 212 |
#define TEST_SUITE__DECLARE(S) \ |
| 213 |
static MunitSuite _##S##_suites[TEST__CAP]; \ |
| 214 |
static MunitTest _##S##_tests[TEST__CAP]; \ |
| 215 |
static MunitTestSetup _##S##_setup = NULL; \ |
| 216 |
static MunitTestTearDown _##S##_tear_down = NULL; \ |
| 217 |
static int _##S##_suites_n = 0; \ |
| 218 |
static int _##S##_tests_n = 0; \ |
| 219 |
__attribute__((constructor)) static void _##S##_init(void) \ |
| 220 |
{ \ |
| 221 |
memset(_##S##_suites, 0, sizeof(_##S##_suites)); \ |
| 222 |
memset(_##S##_tests, 0, sizeof(_##S##_tests)); \ |
| 223 |
(void)_##S##_suites_n; \ |
| 224 |
(void)_##S##_tests_n; \ |
| 225 |
(void)_##S##_setup; \ |
| 226 |
(void)_##S##_tear_down; \ |
| 227 |
} |
| 228 |
|
| 229 |
/* Set the tests and suites attributes of the next available slot of the |
| 230 |
* MunitSuite[] array of S1 to the MunitTest[] and MunitSuite[] arrays of S2, |
| 231 |
* using the given PREXIX. */ |
| 232 |
#define TEST_SUITE__ADD_CHILD(S1, PREFIX, S2) \ |
| 233 |
__attribute__((constructor)) static void _##S1##_##S2##_init(void) \ |
| 234 |
{ \ |
| 235 |
int n = _##S1##_suites_n; \ |
| 236 |
_##S1##_suites[n].prefix = PREFIX; \ |
| 237 |
_##S1##_suites[n].tests = _##S2##_tests; \ |
| 238 |
_##S1##_suites[n].suites = _##S2##_suites; \ |
| 239 |
_##S1##_suites[n].iterations = 0; \ |
| 240 |
_##S1##_suites[n].options = 0; \ |
| 241 |
_##S1##_suites_n = n + 1; \ |
| 242 |
} |
| 243 |
|
| 244 |
/* Set the tests attribute of the next available slot of the MunitSuite[] array |
| 245 |
* of S to the MunitTest[] array of G, using /G as prefix. */ |
| 246 |
#define TEST_SUITE__ADD_GROUP(S, G) \ |
| 247 |
__attribute__((constructor)) static void _##S##_##G##_init(void) \ |
| 248 |
{ \ |
| 249 |
int n = _##S##_suites_n; \ |
| 250 |
_##S##_suites[n].prefix = "/" #G; \ |
| 251 |
_##S##_suites[n].tests = _##S##_##G##_tests; \ |
| 252 |
_##S##_suites[n].suites = NULL; \ |
| 253 |
_##S##_suites[n].iterations = 0; \ |
| 254 |
_##S##_suites[n].options = 0; \ |
| 255 |
_##S##_suites_n = n + 1; \ |
| 256 |
} |
| 257 |
|
| 258 |
/* Choose the appropriate TEST_SETUP__N_ARGS() macro depending on the number of |
| 259 |
* arguments passed to TEST_SETUP(). */ |
| 260 |
#define TEST_SETUP__MACRO_CHOOSER(...) \ |
| 261 |
TEST__GET_3RD_ARG(__VA_ARGS__, TEST_SETUP__2_ARGS, TEST_SETUP__1_ARGS) |
| 262 |
|
| 263 |
#define TEST_SETUP__1_ARGS(S) \ |
| 264 |
static void *S##__setup(const MunitParameter[], void *); \ |
| 265 |
__attribute__((constructor)) static void _##S##_setup_init(void) \ |
| 266 |
{ \ |
| 267 |
_##S##_setup = S##__setup; \ |
| 268 |
} \ |
| 269 |
static void *S##__setup(const MunitParameter params[], void *user_data) |
| 270 |
|
| 271 |
#define TEST_SETUP__2_ARGS(S, F) \ |
| 272 |
__attribute__((constructor)) static void _##S##_setup_init(void) \ |
| 273 |
{ \ |
| 274 |
_##S##_setup = F; \ |
| 275 |
} |
| 276 |
|
| 277 |
/* Choose the appropriate TEST_TEAR_DOWN__N_ARGS() macro depending on the number |
| 278 |
* of arguments passed to TEST_TEAR_DOWN(). */ |
| 279 |
#define TEST_TEAR_DOWN__MACRO_CHOOSER(...) \ |
| 280 |
TEST__GET_3RD_ARG(__VA_ARGS__, TEST_TEAR_DOWN__2_ARGS, \ |
| 281 |
TEST_TEAR_DOWN__1_ARGS) |
| 282 |
|
| 283 |
#define TEST_TEAR_DOWN__1_ARGS(S) \ |
| 284 |
static void S##__tear_down(void *data); \ |
| 285 |
__attribute__((constructor)) static void _##S##__tear_down_init(void) \ |
| 286 |
{ \ |
| 287 |
_##S##_tear_down = S##__tear_down; \ |
| 288 |
} \ |
| 289 |
static void S##__tear_down(void *data) |
| 290 |
|
| 291 |
#define TEST_TEAR_DOWN__2_ARGS(S, F) \ |
| 292 |
__attribute__((constructor)) static void _##S##_tear_down_init(void) \ |
| 293 |
{ \ |
| 294 |
_##S##_tear_down = F; \ |
| 295 |
} |
| 296 |
|
| 297 |
/* Choose the appropriate TEST_CASE__N_ARGS() macro depending on the number of |
| 298 |
* arguments passed to TEST_CASE(). */ |
| 299 |
#define TEST_CASE__MACRO_CHOOSER(...) \ |
| 300 |
TEST__GET_5TH_ARG(__VA_ARGS__, TEST_CASE__4_ARGS, TEST_CASE__3_ARGS, \ |
| 301 |
TEST_CASE__2_ARGS) |
| 302 |
|
| 303 |
/* Add the test case to the module's MunitTest[] array. */ |
| 304 |
#define TEST_CASE__2_ARGS(C, PARAMS) \ |
| 305 |
static MunitResult test_##C(const MunitParameter[], void *); \ |
| 306 |
TEST_CASE__ADD_TO_MODULE(C, PARAMS); \ |
| 307 |
static MunitResult test_##C(const MunitParameter params[], void *data) |
| 308 |
|
| 309 |
/* Add test case C to the MunitTest[] array of suite S. */ |
| 310 |
#define TEST_CASE__3_ARGS(S, C, PARAMS) \ |
| 311 |
static MunitResult test_##S##_##C(const MunitParameter[], void *); \ |
| 312 |
TEST_CASE__ADD_TO_SUITE(S, C, PARAMS); \ |
| 313 |
static MunitResult test_##S##_##C(const MunitParameter params[], \ |
| 314 |
void *data) |
| 315 |
|
| 316 |
/* Add test case C to the MunitTest[] array of group G of suite S. */ |
| 317 |
#define TEST_CASE__4_ARGS(S, G, C, PARAMS) \ |
| 318 |
static MunitResult test_##S##_##G##_##C(const MunitParameter[], \ |
| 319 |
void *); \ |
| 320 |
TEST_CASE__ADD_TO_GROUP(S, G, C, PARAMS); \ |
| 321 |
static MunitResult test_##S##_##G##_##C(const MunitParameter params[], \ |
| 322 |
void *data) |
| 323 |
|
| 324 |
/* Add a test case to the MunitTest[] array of the file module. */ |
| 325 |
#define TEST_CASE__ADD_TO_MODULE(C, PARAMS) \ |
| 326 |
__attribute__((constructor)) static void _module_tests_##C##_init( \ |
| 327 |
void) \ |
| 328 |
{ \ |
| 329 |
MunitTest *tests = _module_tests; \ |
| 330 |
int n = _module_tests_n; \ |
| 331 |
TEST_CASE__SET_IN_ARRAY(tests, n, "/" #C, test_##C, NULL, \ |
| 332 |
NULL, PARAMS); \ |
| 333 |
_module_tests_n = n + 1; \ |
| 334 |
} |
| 335 |
|
| 336 |
/* Add a test case to the MunitTest[] array of suite S. */ |
| 337 |
#define TEST_CASE__ADD_TO_SUITE(S, C, PARAMS) \ |
| 338 |
__attribute__((constructor)) static void _##S##_tests_##C##_init(void) \ |
| 339 |
{ \ |
| 340 |
MunitTest *tests = _##S##_tests; \ |
| 341 |
int n = _##S##_tests_n; \ |
| 342 |
TEST_CASE__SET_IN_ARRAY(tests, n, "/" #C, test_##S##_##C, \ |
| 343 |
_##S##_setup, _##S##_tear_down, \ |
| 344 |
PARAMS); \ |
| 345 |
_##S##_tests_n = n + 1; \ |
| 346 |
} |
| 347 |
|
| 348 |
/* Add a test case to MunitTest[] array of group G in suite S. */ |
| 349 |
#define TEST_CASE__ADD_TO_GROUP(S, G, C, PARAMS) \ |
| 350 |
__attribute__(( \ |
| 351 |
constructor)) static void _##S##_##G##_tests_##C##_init(void) \ |
| 352 |
{ \ |
| 353 |
MunitTest *tests = _##S##_##G##_tests; \ |
| 354 |
int n = _##S##_##G##_tests_n; \ |
| 355 |
TEST_CASE__SET_IN_ARRAY(tests, n, "/" #C, \ |
| 356 |
test_##S##_##G##_##C, _##S##_setup, \ |
| 357 |
_##S##_tear_down, PARAMS); \ |
| 358 |
_##S##_##G##_tests_n = n + 1; \ |
| 359 |
} |
| 360 |
|
| 361 |
/* Set the values of the I'th test case slot in the given test array */ |
| 362 |
#define TEST_CASE__SET_IN_ARRAY(TESTS, I, NAME, FUNC, SETUP, TEAR_DOWN, \ |
| 363 |
PARAMS) \ |
| 364 |
TESTS[I].name = NAME; \ |
| 365 |
TESTS[I].test = FUNC; \ |
| 366 |
TESTS[I].setup = SETUP; \ |
| 367 |
TESTS[I].tear_down = TEAR_DOWN; \ |
| 368 |
TESTS[I].options = 0; \ |
| 369 |
TESTS[I].parameters = PARAMS |
| 370 |
|
| 371 |
#define TEST__GET_3RD_ARG(arg1, arg2, arg3, ...) arg3 |
| 372 |
#define TEST__GET_5TH_ARG(arg1, arg2, arg3, arg4, arg5, ...) arg5 |
| 373 |
|
| 374 |
#endif /* TEST_RUNNER_H */ |