Mercurial > sci
comparison extern/libgreatest/greatest.h @ 5:566bc028cdcb
tests: initial tests coming
- Add tests for database access.
- While here transform common code into libsci.a
author | David Demelier <markand@malikania.fr> |
---|---|
date | Wed, 16 Jun 2021 13:25:42 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
4:9c4fea43803c | 5:566bc028cdcb |
---|---|
1 /* | |
2 * Copyright (c) 2011-2019 Scott Vokes <vokes.s@gmail.com> | |
3 * | |
4 * Permission to use, copy, modify, and/or distribute this software for any | |
5 * purpose with or without fee is hereby granted, provided that the above | |
6 * copyright notice and this permission notice appear in all copies. | |
7 * | |
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
15 */ | |
16 | |
17 #ifndef GREATEST_H | |
18 #define GREATEST_H | |
19 | |
20 #if defined(__cplusplus) && !defined(GREATEST_NO_EXTERN_CPLUSPLUS) | |
21 extern "C" { | |
22 #endif | |
23 | |
24 /* 1.4.2 */ | |
25 #define GREATEST_VERSION_MAJOR 1 | |
26 #define GREATEST_VERSION_MINOR 4 | |
27 #define GREATEST_VERSION_PATCH 2 | |
28 | |
29 /* A unit testing system for C, contained in 1 file. | |
30 * It doesn't use dynamic allocation or depend on anything | |
31 * beyond ANSI C89. | |
32 * | |
33 * An up-to-date version can be found at: | |
34 * https://github.com/silentbicycle/greatest/ | |
35 */ | |
36 | |
37 | |
38 /********************************************************************* | |
39 * Minimal test runner template | |
40 *********************************************************************/ | |
41 #if 0 | |
42 | |
43 #include "greatest.h" | |
44 | |
45 TEST foo_should_foo(void) { | |
46 PASS(); | |
47 } | |
48 | |
49 static void setup_cb(void *data) { | |
50 printf("setup callback for each test case\n"); | |
51 } | |
52 | |
53 static void teardown_cb(void *data) { | |
54 printf("teardown callback for each test case\n"); | |
55 } | |
56 | |
57 SUITE(suite) { | |
58 /* Optional setup/teardown callbacks which will be run before/after | |
59 * every test case. If using a test suite, they will be cleared when | |
60 * the suite finishes. */ | |
61 SET_SETUP(setup_cb, voidp_to_callback_data); | |
62 SET_TEARDOWN(teardown_cb, voidp_to_callback_data); | |
63 | |
64 RUN_TEST(foo_should_foo); | |
65 } | |
66 | |
67 /* Add definitions that need to be in the test runner's main file. */ | |
68 GREATEST_MAIN_DEFS(); | |
69 | |
70 /* Set up, run suite(s) of tests, report pass/fail/skip stats. */ | |
71 int run_tests(void) { | |
72 GREATEST_INIT(); /* init. greatest internals */ | |
73 /* List of suites to run (if any). */ | |
74 RUN_SUITE(suite); | |
75 | |
76 /* Tests can also be run directly, without using test suites. */ | |
77 RUN_TEST(foo_should_foo); | |
78 | |
79 GREATEST_PRINT_REPORT(); /* display results */ | |
80 return greatest_all_passed(); | |
81 } | |
82 | |
83 /* main(), for a standalone command-line test runner. | |
84 * This replaces run_tests above, and adds command line option | |
85 * handling and exiting with a pass/fail status. */ | |
86 int main(int argc, char **argv) { | |
87 GREATEST_MAIN_BEGIN(); /* init & parse command-line args */ | |
88 RUN_SUITE(suite); | |
89 GREATEST_MAIN_END(); /* display results */ | |
90 } | |
91 | |
92 #endif | |
93 /*********************************************************************/ | |
94 | |
95 | |
96 #include <stdlib.h> | |
97 #include <stdio.h> | |
98 #include <string.h> | |
99 #include <ctype.h> | |
100 | |
101 /*********** | |
102 * Options * | |
103 ***********/ | |
104 | |
105 /* Default column width for non-verbose output. */ | |
106 #ifndef GREATEST_DEFAULT_WIDTH | |
107 #define GREATEST_DEFAULT_WIDTH 72 | |
108 #endif | |
109 | |
110 /* FILE *, for test logging. */ | |
111 #ifndef GREATEST_STDOUT | |
112 #define GREATEST_STDOUT stdout | |
113 #endif | |
114 | |
115 /* Remove GREATEST_ prefix from most commonly used symbols? */ | |
116 #ifndef GREATEST_USE_ABBREVS | |
117 #define GREATEST_USE_ABBREVS 1 | |
118 #endif | |
119 | |
120 /* Set to 0 to disable all use of setjmp/longjmp. */ | |
121 #ifndef GREATEST_USE_LONGJMP | |
122 #define GREATEST_USE_LONGJMP 1 | |
123 #endif | |
124 | |
125 /* Make it possible to replace fprintf with another | |
126 * function with the same interface. */ | |
127 #ifndef GREATEST_FPRINTF | |
128 #define GREATEST_FPRINTF fprintf | |
129 #endif | |
130 | |
131 #if GREATEST_USE_LONGJMP | |
132 #include <setjmp.h> | |
133 #endif | |
134 | |
135 /* Set to 0 to disable all use of time.h / clock(). */ | |
136 #ifndef GREATEST_USE_TIME | |
137 #define GREATEST_USE_TIME 1 | |
138 #endif | |
139 | |
140 #if GREATEST_USE_TIME | |
141 #include <time.h> | |
142 #endif | |
143 | |
144 /* Floating point type, for ASSERT_IN_RANGE. */ | |
145 #ifndef GREATEST_FLOAT | |
146 #define GREATEST_FLOAT double | |
147 #define GREATEST_FLOAT_FMT "%g" | |
148 #endif | |
149 | |
150 /* Size of buffer for test name + optional '_' separator and suffix */ | |
151 #ifndef GREATEST_TESTNAME_BUF_SIZE | |
152 #define GREATEST_TESTNAME_BUF_SIZE 128 | |
153 #endif | |
154 | |
155 | |
156 /********* | |
157 * Types * | |
158 *********/ | |
159 | |
160 /* Info for the current running suite. */ | |
161 typedef struct greatest_suite_info { | |
162 unsigned int tests_run; | |
163 unsigned int passed; | |
164 unsigned int failed; | |
165 unsigned int skipped; | |
166 | |
167 #if GREATEST_USE_TIME | |
168 /* timers, pre/post running suite and individual tests */ | |
169 clock_t pre_suite; | |
170 clock_t post_suite; | |
171 clock_t pre_test; | |
172 clock_t post_test; | |
173 #endif | |
174 } greatest_suite_info; | |
175 | |
176 /* Type for a suite function. */ | |
177 typedef void greatest_suite_cb(void); | |
178 | |
179 /* Types for setup/teardown callbacks. If non-NULL, these will be run | |
180 * and passed the pointer to their additional data. */ | |
181 typedef void greatest_setup_cb(void *udata); | |
182 typedef void greatest_teardown_cb(void *udata); | |
183 | |
184 /* Type for an equality comparison between two pointers of the same type. | |
185 * Should return non-0 if equal, otherwise 0. | |
186 * UDATA is a closure value, passed through from ASSERT_EQUAL_T[m]. */ | |
187 typedef int greatest_equal_cb(const void *expd, const void *got, void *udata); | |
188 | |
189 /* Type for a callback that prints a value pointed to by T. | |
190 * Return value has the same meaning as printf's. | |
191 * UDATA is a closure value, passed through from ASSERT_EQUAL_T[m]. */ | |
192 typedef int greatest_printf_cb(const void *t, void *udata); | |
193 | |
194 /* Callbacks for an arbitrary type; needed for type-specific | |
195 * comparisons via GREATEST_ASSERT_EQUAL_T[m].*/ | |
196 typedef struct greatest_type_info { | |
197 greatest_equal_cb *equal; | |
198 greatest_printf_cb *print; | |
199 } greatest_type_info; | |
200 | |
201 typedef struct greatest_memory_cmp_env { | |
202 const unsigned char *exp; | |
203 const unsigned char *got; | |
204 size_t size; | |
205 } greatest_memory_cmp_env; | |
206 | |
207 /* Callbacks for string and raw memory types. */ | |
208 extern greatest_type_info greatest_type_info_string; | |
209 extern greatest_type_info greatest_type_info_memory; | |
210 | |
211 typedef enum { | |
212 GREATEST_FLAG_FIRST_FAIL = 0x01, | |
213 GREATEST_FLAG_LIST_ONLY = 0x02, | |
214 GREATEST_FLAG_ABORT_ON_FAIL = 0x04 | |
215 } greatest_flag_t; | |
216 | |
217 /* Internal state for a PRNG, used to shuffle test order. */ | |
218 struct greatest_prng { | |
219 unsigned char random_order; /* use random ordering? */ | |
220 unsigned char initialized; /* is random ordering initialized? */ | |
221 unsigned char pad_0[6]; | |
222 unsigned long state; /* PRNG state */ | |
223 unsigned long count; /* how many tests, this pass */ | |
224 unsigned long count_ceil; /* total number of tests */ | |
225 unsigned long count_run; /* total tests run */ | |
226 unsigned long a; /* LCG multiplier */ | |
227 unsigned long c; /* LCG increment */ | |
228 unsigned long m; /* LCG modulus, based on count_ceil */ | |
229 }; | |
230 | |
231 /* Struct containing all test runner state. */ | |
232 typedef struct greatest_run_info { | |
233 unsigned char flags; | |
234 unsigned char verbosity; | |
235 unsigned char pad_0[2]; | |
236 | |
237 unsigned int tests_run; /* total test count */ | |
238 | |
239 /* currently running test suite */ | |
240 greatest_suite_info suite; | |
241 | |
242 /* overall pass/fail/skip counts */ | |
243 unsigned int passed; | |
244 unsigned int failed; | |
245 unsigned int skipped; | |
246 unsigned int assertions; | |
247 | |
248 /* info to print about the most recent failure */ | |
249 unsigned int fail_line; | |
250 unsigned int pad_1; | |
251 const char *fail_file; | |
252 const char *msg; | |
253 | |
254 /* current setup/teardown hooks and userdata */ | |
255 greatest_setup_cb *setup; | |
256 void *setup_udata; | |
257 greatest_teardown_cb *teardown; | |
258 void *teardown_udata; | |
259 | |
260 /* formatting info for ".....s...F"-style output */ | |
261 unsigned int col; | |
262 unsigned int width; | |
263 | |
264 /* only run a specific suite or test */ | |
265 const char *suite_filter; | |
266 const char *test_filter; | |
267 const char *test_exclude; | |
268 const char *name_suffix; /* print suffix with test name */ | |
269 char name_buf[GREATEST_TESTNAME_BUF_SIZE]; | |
270 | |
271 struct greatest_prng prng[2]; /* 0: suites, 1: tests */ | |
272 | |
273 #if GREATEST_USE_TIME | |
274 /* overall timers */ | |
275 clock_t begin; | |
276 clock_t end; | |
277 #endif | |
278 | |
279 #if GREATEST_USE_LONGJMP | |
280 int pad_jmp_buf; | |
281 unsigned char pad_2[4]; | |
282 jmp_buf jump_dest; | |
283 #endif | |
284 } greatest_run_info; | |
285 | |
286 struct greatest_report_t { | |
287 /* overall pass/fail/skip counts */ | |
288 unsigned int passed; | |
289 unsigned int failed; | |
290 unsigned int skipped; | |
291 unsigned int assertions; | |
292 }; | |
293 | |
294 /* Global var for the current testing context. | |
295 * Initialized by GREATEST_MAIN_DEFS(). */ | |
296 extern greatest_run_info greatest_info; | |
297 | |
298 /* Type for ASSERT_ENUM_EQ's ENUM_STR argument. */ | |
299 typedef const char *greatest_enum_str_fun(int value); | |
300 | |
301 | |
302 /********************** | |
303 * Exported functions * | |
304 **********************/ | |
305 | |
306 /* These are used internally by greatest macros. */ | |
307 int greatest_test_pre(const char *name); | |
308 void greatest_test_post(int res); | |
309 int greatest_do_assert_equal_t(const void *expd, const void *got, | |
310 greatest_type_info *type_info, void *udata); | |
311 void greatest_prng_init_first_pass(int id); | |
312 int greatest_prng_init_second_pass(int id, unsigned long seed); | |
313 void greatest_prng_step(int id); | |
314 | |
315 /* These are part of the public greatest API. */ | |
316 void GREATEST_SET_SETUP_CB(greatest_setup_cb *cb, void *udata); | |
317 void GREATEST_SET_TEARDOWN_CB(greatest_teardown_cb *cb, void *udata); | |
318 void GREATEST_INIT(void); | |
319 void GREATEST_PRINT_REPORT(void); | |
320 int greatest_all_passed(void); | |
321 void greatest_set_suite_filter(const char *filter); | |
322 void greatest_set_test_filter(const char *filter); | |
323 void greatest_set_test_exclude(const char *filter); | |
324 void greatest_stop_at_first_fail(void); | |
325 void greatest_abort_on_fail(void); | |
326 void greatest_list_only(void); | |
327 void greatest_get_report(struct greatest_report_t *report); | |
328 unsigned int greatest_get_verbosity(void); | |
329 void greatest_set_verbosity(unsigned int verbosity); | |
330 void greatest_set_flag(greatest_flag_t flag); | |
331 void greatest_set_test_suffix(const char *suffix); | |
332 | |
333 | |
334 /******************** | |
335 * Language Support * | |
336 ********************/ | |
337 | |
338 /* If __VA_ARGS__ (C99) is supported, allow parametric testing | |
339 * without needing to manually manage the argument struct. */ | |
340 #if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 19901L) || \ | |
341 (defined(_MSC_VER) && _MSC_VER >= 1800) | |
342 #define GREATEST_VA_ARGS | |
343 #endif | |
344 | |
345 | |
346 /********** | |
347 * Macros * | |
348 **********/ | |
349 | |
350 /* Define a suite. (The duplication is intentional -- it eliminates | |
351 * a warning from -Wmissing-declarations.) */ | |
352 #define GREATEST_SUITE(NAME) void NAME(void); void NAME(void) | |
353 | |
354 /* Declare a suite, provided by another compilation unit. */ | |
355 #define GREATEST_SUITE_EXTERN(NAME) void NAME(void) | |
356 | |
357 /* Start defining a test function. | |
358 * The arguments are not included, to allow parametric testing. */ | |
359 #define GREATEST_TEST static enum greatest_test_res | |
360 | |
361 /* PASS/FAIL/SKIP result from a test. Used internally. */ | |
362 typedef enum greatest_test_res { | |
363 GREATEST_TEST_RES_PASS = 0, | |
364 GREATEST_TEST_RES_FAIL = -1, | |
365 GREATEST_TEST_RES_SKIP = 1 | |
366 } greatest_test_res; | |
367 | |
368 /* Run a suite. */ | |
369 #define GREATEST_RUN_SUITE(S_NAME) greatest_run_suite(S_NAME, #S_NAME) | |
370 | |
371 /* Run a test in the current suite. */ | |
372 #define GREATEST_RUN_TEST(TEST) \ | |
373 do { \ | |
374 if (greatest_test_pre(#TEST) == 1) { \ | |
375 enum greatest_test_res res = GREATEST_SAVE_CONTEXT(); \ | |
376 if (res == GREATEST_TEST_RES_PASS) { \ | |
377 res = TEST(); \ | |
378 } \ | |
379 greatest_test_post(res); \ | |
380 } \ | |
381 } while (0) | |
382 | |
383 /* Ignore a test, don't warn about it being unused. */ | |
384 #define GREATEST_IGNORE_TEST(TEST) (void)TEST | |
385 | |
386 /* Run a test in the current suite with one void * argument, | |
387 * which can be a pointer to a struct with multiple arguments. */ | |
388 #define GREATEST_RUN_TEST1(TEST, ENV) \ | |
389 do { \ | |
390 if (greatest_test_pre(#TEST) == 1) { \ | |
391 enum greatest_test_res res = GREATEST_SAVE_CONTEXT(); \ | |
392 if (res == GREATEST_TEST_RES_PASS) { \ | |
393 res = TEST(ENV); \ | |
394 } \ | |
395 greatest_test_post(res); \ | |
396 } \ | |
397 } while (0) | |
398 | |
399 #ifdef GREATEST_VA_ARGS | |
400 #define GREATEST_RUN_TESTp(TEST, ...) \ | |
401 do { \ | |
402 if (greatest_test_pre(#TEST) == 1) { \ | |
403 enum greatest_test_res res = GREATEST_SAVE_CONTEXT(); \ | |
404 if (res == GREATEST_TEST_RES_PASS) { \ | |
405 res = TEST(__VA_ARGS__); \ | |
406 } \ | |
407 greatest_test_post(res); \ | |
408 } \ | |
409 } while (0) | |
410 #endif | |
411 | |
412 | |
413 /* Check if the test runner is in verbose mode. */ | |
414 #define GREATEST_IS_VERBOSE() ((greatest_info.verbosity) > 0) | |
415 #define GREATEST_LIST_ONLY() \ | |
416 (greatest_info.flags & GREATEST_FLAG_LIST_ONLY) | |
417 #define GREATEST_FIRST_FAIL() \ | |
418 (greatest_info.flags & GREATEST_FLAG_FIRST_FAIL) | |
419 #define GREATEST_ABORT_ON_FAIL() \ | |
420 (greatest_info.flags & GREATEST_FLAG_ABORT_ON_FAIL) | |
421 #define GREATEST_FAILURE_ABORT() \ | |
422 (GREATEST_FIRST_FAIL() && \ | |
423 (greatest_info.suite.failed > 0 || greatest_info.failed > 0)) | |
424 | |
425 /* Message-less forms of tests defined below. */ | |
426 #define GREATEST_PASS() GREATEST_PASSm(NULL) | |
427 #define GREATEST_FAIL() GREATEST_FAILm(NULL) | |
428 #define GREATEST_SKIP() GREATEST_SKIPm(NULL) | |
429 #define GREATEST_ASSERT(COND) \ | |
430 GREATEST_ASSERTm(#COND, COND) | |
431 #define GREATEST_ASSERT_OR_LONGJMP(COND) \ | |
432 GREATEST_ASSERT_OR_LONGJMPm(#COND, COND) | |
433 #define GREATEST_ASSERT_FALSE(COND) \ | |
434 GREATEST_ASSERT_FALSEm(#COND, COND) | |
435 #define GREATEST_ASSERT_EQ(EXP, GOT) \ | |
436 GREATEST_ASSERT_EQm(#EXP " != " #GOT, EXP, GOT) | |
437 #define GREATEST_ASSERT_EQ_FMT(EXP, GOT, FMT) \ | |
438 GREATEST_ASSERT_EQ_FMTm(#EXP " != " #GOT, EXP, GOT, FMT) | |
439 #define GREATEST_ASSERT_IN_RANGE(EXP, GOT, TOL) \ | |
440 GREATEST_ASSERT_IN_RANGEm(#EXP " != " #GOT " +/- " #TOL, EXP, GOT, TOL) | |
441 #define GREATEST_ASSERT_EQUAL_T(EXP, GOT, TYPE_INFO, UDATA) \ | |
442 GREATEST_ASSERT_EQUAL_Tm(#EXP " != " #GOT, EXP, GOT, TYPE_INFO, UDATA) | |
443 #define GREATEST_ASSERT_STR_EQ(EXP, GOT) \ | |
444 GREATEST_ASSERT_STR_EQm(#EXP " != " #GOT, EXP, GOT) | |
445 #define GREATEST_ASSERT_STRN_EQ(EXP, GOT, SIZE) \ | |
446 GREATEST_ASSERT_STRN_EQm(#EXP " != " #GOT, EXP, GOT, SIZE) | |
447 #define GREATEST_ASSERT_MEM_EQ(EXP, GOT, SIZE) \ | |
448 GREATEST_ASSERT_MEM_EQm(#EXP " != " #GOT, EXP, GOT, SIZE) | |
449 #define GREATEST_ASSERT_ENUM_EQ(EXP, GOT, ENUM_STR) \ | |
450 GREATEST_ASSERT_ENUM_EQm(#EXP " != " #GOT, EXP, GOT, ENUM_STR) | |
451 | |
452 /* The following forms take an additional message argument first, | |
453 * to be displayed by the test runner. */ | |
454 | |
455 /* Fail if a condition is not true, with message. */ | |
456 #define GREATEST_ASSERTm(MSG, COND) \ | |
457 do { \ | |
458 greatest_info.assertions++; \ | |
459 if (!(COND)) { GREATEST_FAILm(MSG); } \ | |
460 } while (0) | |
461 | |
462 /* Fail if a condition is not true, longjmping out of test. */ | |
463 #define GREATEST_ASSERT_OR_LONGJMPm(MSG, COND) \ | |
464 do { \ | |
465 greatest_info.assertions++; \ | |
466 if (!(COND)) { GREATEST_FAIL_WITH_LONGJMPm(MSG); } \ | |
467 } while (0) | |
468 | |
469 /* Fail if a condition is not false, with message. */ | |
470 #define GREATEST_ASSERT_FALSEm(MSG, COND) \ | |
471 do { \ | |
472 greatest_info.assertions++; \ | |
473 if ((COND)) { GREATEST_FAILm(MSG); } \ | |
474 } while (0) | |
475 | |
476 /* Fail if EXP != GOT (equality comparison by ==). */ | |
477 #define GREATEST_ASSERT_EQm(MSG, EXP, GOT) \ | |
478 do { \ | |
479 greatest_info.assertions++; \ | |
480 if ((EXP) != (GOT)) { GREATEST_FAILm(MSG); } \ | |
481 } while (0) | |
482 | |
483 /* Fail if EXP != GOT (equality comparison by ==). | |
484 * Warning: FMT, EXP, and GOT will be evaluated more | |
485 * than once on failure. */ | |
486 #define GREATEST_ASSERT_EQ_FMTm(MSG, EXP, GOT, FMT) \ | |
487 do { \ | |
488 greatest_info.assertions++; \ | |
489 if ((EXP) != (GOT)) { \ | |
490 GREATEST_FPRINTF(GREATEST_STDOUT, "\nExpected: "); \ | |
491 GREATEST_FPRINTF(GREATEST_STDOUT, FMT, EXP); \ | |
492 GREATEST_FPRINTF(GREATEST_STDOUT, "\n Got: "); \ | |
493 GREATEST_FPRINTF(GREATEST_STDOUT, FMT, GOT); \ | |
494 GREATEST_FPRINTF(GREATEST_STDOUT, "\n"); \ | |
495 GREATEST_FAILm(MSG); \ | |
496 } \ | |
497 } while (0) | |
498 | |
499 /* Fail if EXP is not equal to GOT, printing enum IDs. */ | |
500 #define GREATEST_ASSERT_ENUM_EQm(MSG, EXP, GOT, ENUM_STR) \ | |
501 do { \ | |
502 int greatest_EXP = (int)(EXP); \ | |
503 int greatest_GOT = (int)(GOT); \ | |
504 greatest_enum_str_fun *greatest_ENUM_STR = ENUM_STR; \ | |
505 if (greatest_EXP != greatest_GOT) { \ | |
506 GREATEST_FPRINTF(GREATEST_STDOUT, "\nExpected: %s", \ | |
507 greatest_ENUM_STR(greatest_EXP)); \ | |
508 GREATEST_FPRINTF(GREATEST_STDOUT, "\n Got: %s\n", \ | |
509 greatest_ENUM_STR(greatest_GOT)); \ | |
510 GREATEST_FAILm(MSG); \ | |
511 } \ | |
512 } while (0) \ | |
513 | |
514 /* Fail if GOT not in range of EXP +|- TOL. */ | |
515 #define GREATEST_ASSERT_IN_RANGEm(MSG, EXP, GOT, TOL) \ | |
516 do { \ | |
517 GREATEST_FLOAT greatest_EXP = (EXP); \ | |
518 GREATEST_FLOAT greatest_GOT = (GOT); \ | |
519 GREATEST_FLOAT greatest_TOL = (TOL); \ | |
520 greatest_info.assertions++; \ | |
521 if ((greatest_EXP > greatest_GOT && \ | |
522 greatest_EXP - greatest_GOT > greatest_TOL) || \ | |
523 (greatest_EXP < greatest_GOT && \ | |
524 greatest_GOT - greatest_EXP > greatest_TOL)) { \ | |
525 GREATEST_FPRINTF(GREATEST_STDOUT, \ | |
526 "\nExpected: " GREATEST_FLOAT_FMT \ | |
527 " +/- " GREATEST_FLOAT_FMT \ | |
528 "\n Got: " GREATEST_FLOAT_FMT \ | |
529 "\n", \ | |
530 greatest_EXP, greatest_TOL, greatest_GOT); \ | |
531 GREATEST_FAILm(MSG); \ | |
532 } \ | |
533 } while (0) | |
534 | |
535 /* Fail if EXP is not equal to GOT, according to strcmp. */ | |
536 #define GREATEST_ASSERT_STR_EQm(MSG, EXP, GOT) \ | |
537 do { \ | |
538 GREATEST_ASSERT_EQUAL_Tm(MSG, EXP, GOT, \ | |
539 &greatest_type_info_string, NULL); \ | |
540 } while (0) \ | |
541 | |
542 /* Fail if EXP is not equal to GOT, according to strncmp. */ | |
543 #define GREATEST_ASSERT_STRN_EQm(MSG, EXP, GOT, SIZE) \ | |
544 do { \ | |
545 size_t size = SIZE; \ | |
546 GREATEST_ASSERT_EQUAL_Tm(MSG, EXP, GOT, \ | |
547 &greatest_type_info_string, &size); \ | |
548 } while (0) \ | |
549 | |
550 /* Fail if EXP is not equal to GOT, according to memcmp. */ | |
551 #define GREATEST_ASSERT_MEM_EQm(MSG, EXP, GOT, SIZE) \ | |
552 do { \ | |
553 greatest_memory_cmp_env env; \ | |
554 env.exp = (const unsigned char *)EXP; \ | |
555 env.got = (const unsigned char *)GOT; \ | |
556 env.size = SIZE; \ | |
557 GREATEST_ASSERT_EQUAL_Tm(MSG, env.exp, env.got, \ | |
558 &greatest_type_info_memory, &env); \ | |
559 } while (0) \ | |
560 | |
561 /* Fail if EXP is not equal to GOT, according to a comparison | |
562 * callback in TYPE_INFO. If they are not equal, optionally use a | |
563 * print callback in TYPE_INFO to print them. */ | |
564 #define GREATEST_ASSERT_EQUAL_Tm(MSG, EXP, GOT, TYPE_INFO, UDATA) \ | |
565 do { \ | |
566 greatest_type_info *type_info = (TYPE_INFO); \ | |
567 greatest_info.assertions++; \ | |
568 if (!greatest_do_assert_equal_t(EXP, GOT, \ | |
569 type_info, UDATA)) { \ | |
570 if (type_info == NULL || type_info->equal == NULL) { \ | |
571 GREATEST_FAILm("type_info->equal callback missing!"); \ | |
572 } else { \ | |
573 GREATEST_FAILm(MSG); \ | |
574 } \ | |
575 } \ | |
576 } while (0) \ | |
577 | |
578 /* Pass. */ | |
579 #define GREATEST_PASSm(MSG) \ | |
580 do { \ | |
581 greatest_info.msg = MSG; \ | |
582 return GREATEST_TEST_RES_PASS; \ | |
583 } while (0) | |
584 | |
585 /* Fail. */ | |
586 #define GREATEST_FAILm(MSG) \ | |
587 do { \ | |
588 greatest_info.fail_file = __FILE__; \ | |
589 greatest_info.fail_line = __LINE__; \ | |
590 greatest_info.msg = MSG; \ | |
591 if (GREATEST_ABORT_ON_FAIL()) { abort(); } \ | |
592 return GREATEST_TEST_RES_FAIL; \ | |
593 } while (0) | |
594 | |
595 /* Optional GREATEST_FAILm variant that longjmps. */ | |
596 #if GREATEST_USE_LONGJMP | |
597 #define GREATEST_FAIL_WITH_LONGJMP() GREATEST_FAIL_WITH_LONGJMPm(NULL) | |
598 #define GREATEST_FAIL_WITH_LONGJMPm(MSG) \ | |
599 do { \ | |
600 greatest_info.fail_file = __FILE__; \ | |
601 greatest_info.fail_line = __LINE__; \ | |
602 greatest_info.msg = MSG; \ | |
603 longjmp(greatest_info.jump_dest, GREATEST_TEST_RES_FAIL); \ | |
604 } while (0) | |
605 #endif | |
606 | |
607 /* Skip the current test. */ | |
608 #define GREATEST_SKIPm(MSG) \ | |
609 do { \ | |
610 greatest_info.msg = MSG; \ | |
611 return GREATEST_TEST_RES_SKIP; \ | |
612 } while (0) | |
613 | |
614 /* Check the result of a subfunction using ASSERT, etc. */ | |
615 #define GREATEST_CHECK_CALL(RES) \ | |
616 do { \ | |
617 enum greatest_test_res greatest_RES = RES; \ | |
618 if (greatest_RES != GREATEST_TEST_RES_PASS) { \ | |
619 return greatest_RES; \ | |
620 } \ | |
621 } while (0) \ | |
622 | |
623 #if GREATEST_USE_TIME | |
624 #define GREATEST_SET_TIME(NAME) \ | |
625 NAME = clock(); \ | |
626 if (NAME == (clock_t) -1) { \ | |
627 GREATEST_FPRINTF(GREATEST_STDOUT, \ | |
628 "clock error: %s\n", #NAME); \ | |
629 exit(EXIT_FAILURE); \ | |
630 } | |
631 | |
632 #define GREATEST_CLOCK_DIFF(C1, C2) \ | |
633 GREATEST_FPRINTF(GREATEST_STDOUT, " (%lu ticks, %.3f sec)", \ | |
634 (long unsigned int) (C2) - (long unsigned int)(C1), \ | |
635 (double)((C2) - (C1)) / (1.0 * (double)CLOCKS_PER_SEC)) | |
636 #else | |
637 #define GREATEST_SET_TIME(UNUSED) | |
638 #define GREATEST_CLOCK_DIFF(UNUSED1, UNUSED2) | |
639 #endif | |
640 | |
641 #if GREATEST_USE_LONGJMP | |
642 #define GREATEST_SAVE_CONTEXT() \ | |
643 /* setjmp returns 0 (GREATEST_TEST_RES_PASS) on first call * \ | |
644 * so the test runs, then RES_FAIL from FAIL_WITH_LONGJMP. */ \ | |
645 ((enum greatest_test_res)(setjmp(greatest_info.jump_dest))) | |
646 #else | |
647 #define GREATEST_SAVE_CONTEXT() \ | |
648 /*a no-op, since setjmp/longjmp aren't being used */ \ | |
649 GREATEST_TEST_RES_PASS | |
650 #endif | |
651 | |
652 /* Run every suite / test function run within BODY in pseudo-random | |
653 * order, seeded by SEED. (The top 3 bits of the seed are ignored.) | |
654 * | |
655 * This should be called like: | |
656 * GREATEST_SHUFFLE_TESTS(seed, { | |
657 * GREATEST_RUN_TEST(some_test); | |
658 * GREATEST_RUN_TEST(some_other_test); | |
659 * GREATEST_RUN_TEST(yet_another_test); | |
660 * }); | |
661 * | |
662 * Note that the body of the second argument will be evaluated | |
663 * multiple times. */ | |
664 #define GREATEST_SHUFFLE_SUITES(SD, BODY) GREATEST_SHUFFLE(0, SD, BODY) | |
665 #define GREATEST_SHUFFLE_TESTS(SD, BODY) GREATEST_SHUFFLE(1, SD, BODY) | |
666 #define GREATEST_SHUFFLE(ID, SD, BODY) \ | |
667 do { \ | |
668 struct greatest_prng *prng = &greatest_info.prng[ID]; \ | |
669 greatest_prng_init_first_pass(ID); \ | |
670 do { \ | |
671 prng->count = 0; \ | |
672 if (prng->initialized) { greatest_prng_step(ID); } \ | |
673 BODY; \ | |
674 if (!prng->initialized) { \ | |
675 if (!greatest_prng_init_second_pass(ID, SD)) { break; } \ | |
676 } else if (prng->count_run == prng->count_ceil) { \ | |
677 break; \ | |
678 } \ | |
679 } while (!GREATEST_FAILURE_ABORT()); \ | |
680 prng->count_run = prng->random_order = prng->initialized = 0; \ | |
681 } while(0) | |
682 | |
683 /* Include several function definitions in the main test file. */ | |
684 #define GREATEST_MAIN_DEFS() \ | |
685 \ | |
686 /* Is FILTER a subset of NAME? */ \ | |
687 static int greatest_name_match(const char *name, const char *filter, \ | |
688 int res_if_none) { \ | |
689 size_t offset = 0; \ | |
690 size_t filter_len = filter ? strlen(filter) : 0; \ | |
691 if (filter_len == 0) { return res_if_none; } /* no filter */ \ | |
692 while (name[offset] != '\0') { \ | |
693 if (name[offset] == filter[0]) { \ | |
694 if (0 == strncmp(&name[offset], filter, filter_len)) { \ | |
695 return 1; \ | |
696 } \ | |
697 } \ | |
698 offset++; \ | |
699 } \ | |
700 \ | |
701 return 0; \ | |
702 } \ | |
703 \ | |
704 static void greatest_buffer_test_name(const char *name) { \ | |
705 struct greatest_run_info *g = &greatest_info; \ | |
706 size_t len = strlen(name), size = sizeof(g->name_buf); \ | |
707 memset(g->name_buf, 0x00, size); \ | |
708 (void)strncat(g->name_buf, name, size - 1); \ | |
709 if (g->name_suffix && (len + 1 < size)) { \ | |
710 g->name_buf[len] = '_'; \ | |
711 strncat(&g->name_buf[len+1], g->name_suffix, size-(len+2)); \ | |
712 } \ | |
713 } \ | |
714 \ | |
715 /* Before running a test, check the name filtering and \ | |
716 * test shuffling state, if applicable, and then call setup hooks. */ \ | |
717 int greatest_test_pre(const char *name) { \ | |
718 struct greatest_run_info *g = &greatest_info; \ | |
719 int match; \ | |
720 greatest_buffer_test_name(name); \ | |
721 match = greatest_name_match(g->name_buf, g->test_filter, 1) && \ | |
722 !greatest_name_match(g->name_buf, g->test_exclude, 0); \ | |
723 if (GREATEST_LIST_ONLY()) { /* just listing test names */ \ | |
724 if (match) { \ | |
725 GREATEST_FPRINTF(GREATEST_STDOUT, " %s\n", g->name_buf); \ | |
726 } \ | |
727 goto clear; \ | |
728 } \ | |
729 if (match && (!GREATEST_FIRST_FAIL() || g->suite.failed == 0)) { \ | |
730 struct greatest_prng *p = &g->prng[1]; \ | |
731 if (p->random_order) { \ | |
732 p->count++; \ | |
733 if (!p->initialized || ((p->count - 1) != p->state)) { \ | |
734 goto clear; /* don't run this test yet */ \ | |
735 } \ | |
736 } \ | |
737 GREATEST_SET_TIME(g->suite.pre_test); \ | |
738 if (g->setup) { g->setup(g->setup_udata); } \ | |
739 p->count_run++; \ | |
740 return 1; /* test should be run */ \ | |
741 } else { \ | |
742 goto clear; /* skipped */ \ | |
743 } \ | |
744 clear: \ | |
745 g->name_suffix = NULL; \ | |
746 return 0; \ | |
747 } \ | |
748 \ | |
749 static void greatest_do_pass(void) { \ | |
750 struct greatest_run_info *g = &greatest_info; \ | |
751 if (GREATEST_IS_VERBOSE()) { \ | |
752 GREATEST_FPRINTF(GREATEST_STDOUT, "PASS %s: %s", \ | |
753 g->name_buf, g->msg ? g->msg : ""); \ | |
754 } else { \ | |
755 GREATEST_FPRINTF(GREATEST_STDOUT, "."); \ | |
756 } \ | |
757 g->suite.passed++; \ | |
758 } \ | |
759 \ | |
760 static void greatest_do_fail(void) { \ | |
761 struct greatest_run_info *g = &greatest_info; \ | |
762 if (GREATEST_IS_VERBOSE()) { \ | |
763 GREATEST_FPRINTF(GREATEST_STDOUT, \ | |
764 "FAIL %s: %s (%s:%u)", g->name_buf, \ | |
765 g->msg ? g->msg : "", g->fail_file, g->fail_line); \ | |
766 } else { \ | |
767 GREATEST_FPRINTF(GREATEST_STDOUT, "F"); \ | |
768 g->col++; /* add linebreak if in line of '.'s */ \ | |
769 if (g->col != 0) { \ | |
770 GREATEST_FPRINTF(GREATEST_STDOUT, "\n"); \ | |
771 g->col = 0; \ | |
772 } \ | |
773 GREATEST_FPRINTF(GREATEST_STDOUT, "FAIL %s: %s (%s:%u)\n", \ | |
774 g->name_buf, g->msg ? g->msg : "", \ | |
775 g->fail_file, g->fail_line); \ | |
776 } \ | |
777 g->suite.failed++; \ | |
778 } \ | |
779 \ | |
780 static void greatest_do_skip(void) { \ | |
781 struct greatest_run_info *g = &greatest_info; \ | |
782 if (GREATEST_IS_VERBOSE()) { \ | |
783 GREATEST_FPRINTF(GREATEST_STDOUT, "SKIP %s: %s", \ | |
784 g->name_buf, g->msg ? g->msg : ""); \ | |
785 } else { \ | |
786 GREATEST_FPRINTF(GREATEST_STDOUT, "s"); \ | |
787 } \ | |
788 g->suite.skipped++; \ | |
789 } \ | |
790 \ | |
791 void greatest_test_post(int res) { \ | |
792 GREATEST_SET_TIME(greatest_info.suite.post_test); \ | |
793 if (greatest_info.teardown) { \ | |
794 void *udata = greatest_info.teardown_udata; \ | |
795 greatest_info.teardown(udata); \ | |
796 } \ | |
797 \ | |
798 if (res <= GREATEST_TEST_RES_FAIL) { \ | |
799 greatest_do_fail(); \ | |
800 } else if (res >= GREATEST_TEST_RES_SKIP) { \ | |
801 greatest_do_skip(); \ | |
802 } else if (res == GREATEST_TEST_RES_PASS) { \ | |
803 greatest_do_pass(); \ | |
804 } \ | |
805 greatest_info.name_suffix = NULL; \ | |
806 greatest_info.suite.tests_run++; \ | |
807 greatest_info.col++; \ | |
808 if (GREATEST_IS_VERBOSE()) { \ | |
809 GREATEST_CLOCK_DIFF(greatest_info.suite.pre_test, \ | |
810 greatest_info.suite.post_test); \ | |
811 GREATEST_FPRINTF(GREATEST_STDOUT, "\n"); \ | |
812 } else if (greatest_info.col % greatest_info.width == 0) { \ | |
813 GREATEST_FPRINTF(GREATEST_STDOUT, "\n"); \ | |
814 greatest_info.col = 0; \ | |
815 } \ | |
816 fflush(GREATEST_STDOUT); \ | |
817 } \ | |
818 \ | |
819 static void report_suite(void) { \ | |
820 if (greatest_info.suite.tests_run > 0) { \ | |
821 GREATEST_FPRINTF(GREATEST_STDOUT, \ | |
822 "\n%u test%s - %u passed, %u failed, %u skipped", \ | |
823 greatest_info.suite.tests_run, \ | |
824 greatest_info.suite.tests_run == 1 ? "" : "s", \ | |
825 greatest_info.suite.passed, \ | |
826 greatest_info.suite.failed, \ | |
827 greatest_info.suite.skipped); \ | |
828 GREATEST_CLOCK_DIFF(greatest_info.suite.pre_suite, \ | |
829 greatest_info.suite.post_suite); \ | |
830 GREATEST_FPRINTF(GREATEST_STDOUT, "\n"); \ | |
831 } \ | |
832 } \ | |
833 \ | |
834 static void update_counts_and_reset_suite(void) { \ | |
835 greatest_info.setup = NULL; \ | |
836 greatest_info.setup_udata = NULL; \ | |
837 greatest_info.teardown = NULL; \ | |
838 greatest_info.teardown_udata = NULL; \ | |
839 greatest_info.passed += greatest_info.suite.passed; \ | |
840 greatest_info.failed += greatest_info.suite.failed; \ | |
841 greatest_info.skipped += greatest_info.suite.skipped; \ | |
842 greatest_info.tests_run += greatest_info.suite.tests_run; \ | |
843 memset(&greatest_info.suite, 0, sizeof(greatest_info.suite)); \ | |
844 greatest_info.col = 0; \ | |
845 } \ | |
846 \ | |
847 static int greatest_suite_pre(const char *suite_name) { \ | |
848 struct greatest_prng *p = &greatest_info.prng[0]; \ | |
849 if (!greatest_name_match(suite_name, greatest_info.suite_filter, 1) \ | |
850 || (GREATEST_FAILURE_ABORT())) { return 0; } \ | |
851 if (p->random_order) { \ | |
852 p->count++; \ | |
853 if (!p->initialized || ((p->count - 1) != p->state)) { \ | |
854 return 0; /* don't run this suite yet */ \ | |
855 } \ | |
856 } \ | |
857 p->count_run++; \ | |
858 update_counts_and_reset_suite(); \ | |
859 GREATEST_FPRINTF(GREATEST_STDOUT, "\n* Suite %s:\n", suite_name); \ | |
860 GREATEST_SET_TIME(greatest_info.suite.pre_suite); \ | |
861 return 1; \ | |
862 } \ | |
863 \ | |
864 static void greatest_suite_post(void) { \ | |
865 GREATEST_SET_TIME(greatest_info.suite.post_suite); \ | |
866 report_suite(); \ | |
867 } \ | |
868 \ | |
869 static void greatest_run_suite(greatest_suite_cb *suite_cb, \ | |
870 const char *suite_name) { \ | |
871 if (greatest_suite_pre(suite_name)) { \ | |
872 suite_cb(); \ | |
873 greatest_suite_post(); \ | |
874 } \ | |
875 } \ | |
876 \ | |
877 int greatest_do_assert_equal_t(const void *expd, const void *got, \ | |
878 greatest_type_info *type_info, void *udata) { \ | |
879 int eq = 0; \ | |
880 if (type_info == NULL || type_info->equal == NULL) { \ | |
881 return 0; \ | |
882 } \ | |
883 eq = type_info->equal(expd, got, udata); \ | |
884 if (!eq) { \ | |
885 if (type_info->print != NULL) { \ | |
886 GREATEST_FPRINTF(GREATEST_STDOUT, "\nExpected: "); \ | |
887 (void)type_info->print(expd, udata); \ | |
888 GREATEST_FPRINTF(GREATEST_STDOUT, "\n Got: "); \ | |
889 (void)type_info->print(got, udata); \ | |
890 GREATEST_FPRINTF(GREATEST_STDOUT, "\n"); \ | |
891 } \ | |
892 } \ | |
893 return eq; \ | |
894 } \ | |
895 \ | |
896 static void greatest_usage(const char *name) { \ | |
897 GREATEST_FPRINTF(GREATEST_STDOUT, \ | |
898 "Usage: %s [-hlfav] [-s SUITE] [-t TEST] [-x EXCLUDE]\n" \ | |
899 " -h, --help print this Help\n" \ | |
900 " -l List suites and tests, then exit (dry run)\n" \ | |
901 " -f Stop runner after first failure\n" \ | |
902 " -a Abort on first failure (implies -f)\n" \ | |
903 " -v Verbose output\n" \ | |
904 " -s SUITE only run suites containing substring SUITE\n" \ | |
905 " -t TEST only run tests containing substring TEST\n" \ | |
906 " -x EXCLUDE exclude tests containing substring EXCLUDE\n", \ | |
907 name); \ | |
908 } \ | |
909 \ | |
910 static void greatest_parse_options(int argc, char **argv) { \ | |
911 int i = 0; \ | |
912 for (i = 1; i < argc; i++) { \ | |
913 if (argv[i][0] == '-') { \ | |
914 char f = argv[i][1]; \ | |
915 if ((f == 's' || f == 't' || f == 'x') && argc <= i + 1) { \ | |
916 greatest_usage(argv[0]); exit(EXIT_FAILURE); \ | |
917 } \ | |
918 switch (f) { \ | |
919 case 's': /* suite name filter */ \ | |
920 greatest_set_suite_filter(argv[i + 1]); i++; break; \ | |
921 case 't': /* test name filter */ \ | |
922 greatest_set_test_filter(argv[i + 1]); i++; break; \ | |
923 case 'x': /* test name exclusion */ \ | |
924 greatest_set_test_exclude(argv[i + 1]); i++; break; \ | |
925 case 'f': /* first fail flag */ \ | |
926 greatest_stop_at_first_fail(); break; \ | |
927 case 'a': /* abort() on fail flag */ \ | |
928 greatest_abort_on_fail(); break; \ | |
929 case 'l': /* list only (dry run) */ \ | |
930 greatest_list_only(); break; \ | |
931 case 'v': /* first fail flag */ \ | |
932 greatest_info.verbosity++; break; \ | |
933 case 'h': /* help */ \ | |
934 greatest_usage(argv[0]); exit(EXIT_SUCCESS); \ | |
935 case '-': \ | |
936 if (0 == strncmp("--help", argv[i], 6)) { \ | |
937 greatest_usage(argv[0]); exit(EXIT_SUCCESS); \ | |
938 } else if (0 == strncmp("--", argv[i], 2)) { \ | |
939 return; /* ignore following arguments */ \ | |
940 } /* fall through */ \ | |
941 default: \ | |
942 GREATEST_FPRINTF(GREATEST_STDOUT, \ | |
943 "Unknown argument '%s'\n", argv[i]); \ | |
944 greatest_usage(argv[0]); \ | |
945 exit(EXIT_FAILURE); \ | |
946 } \ | |
947 } \ | |
948 } \ | |
949 } \ | |
950 \ | |
951 int greatest_all_passed(void) { return (greatest_info.failed == 0); } \ | |
952 \ | |
953 void greatest_set_test_filter(const char *filter) { \ | |
954 greatest_info.test_filter = filter; \ | |
955 } \ | |
956 \ | |
957 void greatest_set_test_exclude(const char *filter) { \ | |
958 greatest_info.test_exclude = filter; \ | |
959 } \ | |
960 \ | |
961 void greatest_set_suite_filter(const char *filter) { \ | |
962 greatest_info.suite_filter = filter; \ | |
963 } \ | |
964 \ | |
965 void greatest_stop_at_first_fail(void) { \ | |
966 greatest_set_flag(GREATEST_FLAG_FIRST_FAIL); \ | |
967 } \ | |
968 \ | |
969 void greatest_abort_on_fail(void) { \ | |
970 greatest_set_flag(GREATEST_FLAG_ABORT_ON_FAIL); \ | |
971 } \ | |
972 \ | |
973 void greatest_list_only(void) { \ | |
974 greatest_set_flag(GREATEST_FLAG_LIST_ONLY); \ | |
975 } \ | |
976 \ | |
977 void greatest_get_report(struct greatest_report_t *report) { \ | |
978 if (report) { \ | |
979 report->passed = greatest_info.passed; \ | |
980 report->failed = greatest_info.failed; \ | |
981 report->skipped = greatest_info.skipped; \ | |
982 report->assertions = greatest_info.assertions; \ | |
983 } \ | |
984 } \ | |
985 \ | |
986 unsigned int greatest_get_verbosity(void) { \ | |
987 return greatest_info.verbosity; \ | |
988 } \ | |
989 \ | |
990 void greatest_set_verbosity(unsigned int verbosity) { \ | |
991 greatest_info.verbosity = (unsigned char)verbosity; \ | |
992 } \ | |
993 \ | |
994 void greatest_set_flag(greatest_flag_t flag) { \ | |
995 greatest_info.flags = (unsigned char)(greatest_info.flags | flag); \ | |
996 } \ | |
997 \ | |
998 void greatest_set_test_suffix(const char *suffix) { \ | |
999 greatest_info.name_suffix = suffix; \ | |
1000 } \ | |
1001 \ | |
1002 void GREATEST_SET_SETUP_CB(greatest_setup_cb *cb, void *udata) { \ | |
1003 greatest_info.setup = cb; \ | |
1004 greatest_info.setup_udata = udata; \ | |
1005 } \ | |
1006 \ | |
1007 void GREATEST_SET_TEARDOWN_CB(greatest_teardown_cb *cb, \ | |
1008 void *udata) { \ | |
1009 greatest_info.teardown = cb; \ | |
1010 greatest_info.teardown_udata = udata; \ | |
1011 } \ | |
1012 \ | |
1013 static int greatest_string_equal_cb(const void *expd, const void *got, \ | |
1014 void *udata) { \ | |
1015 size_t *size = (size_t *)udata; \ | |
1016 return (size != NULL \ | |
1017 ? (0 == strncmp((const char *)expd, (const char *)got, *size)) \ | |
1018 : (0 == strcmp((const char *)expd, (const char *)got))); \ | |
1019 } \ | |
1020 \ | |
1021 static int greatest_string_printf_cb(const void *t, void *udata) { \ | |
1022 (void)udata; /* note: does not check \0 termination. */ \ | |
1023 return GREATEST_FPRINTF(GREATEST_STDOUT, "%s", (const char *)t); \ | |
1024 } \ | |
1025 \ | |
1026 greatest_type_info greatest_type_info_string = { \ | |
1027 greatest_string_equal_cb, \ | |
1028 greatest_string_printf_cb, \ | |
1029 }; \ | |
1030 \ | |
1031 static int greatest_memory_equal_cb(const void *expd, const void *got, \ | |
1032 void *udata) { \ | |
1033 greatest_memory_cmp_env *env = (greatest_memory_cmp_env *)udata; \ | |
1034 return (0 == memcmp(expd, got, env->size)); \ | |
1035 } \ | |
1036 \ | |
1037 /* Hexdump raw memory, with differences highlighted */ \ | |
1038 static int greatest_memory_printf_cb(const void *t, void *udata) { \ | |
1039 greatest_memory_cmp_env *env = (greatest_memory_cmp_env *)udata; \ | |
1040 const unsigned char *buf = (const unsigned char *)t; \ | |
1041 unsigned char diff_mark = ' '; \ | |
1042 FILE *out = GREATEST_STDOUT; \ | |
1043 size_t i, line_i, line_len = 0; \ | |
1044 int len = 0; /* format hexdump with differences highlighted */ \ | |
1045 for (i = 0; i < env->size; i+= line_len) { \ | |
1046 diff_mark = ' '; \ | |
1047 line_len = env->size - i; \ | |
1048 if (line_len > 16) { line_len = 16; } \ | |
1049 for (line_i = i; line_i < i + line_len; line_i++) { \ | |
1050 if (env->exp[line_i] != env->got[line_i]) diff_mark = 'X'; \ | |
1051 } \ | |
1052 len += GREATEST_FPRINTF(out, "\n%04x %c ", \ | |
1053 (unsigned int)i, diff_mark); \ | |
1054 for (line_i = i; line_i < i + line_len; line_i++) { \ | |
1055 int m = env->exp[line_i] == env->got[line_i]; /* match? */ \ | |
1056 len += GREATEST_FPRINTF(out, "%02x%c", \ | |
1057 buf[line_i], m ? ' ' : '<'); \ | |
1058 } \ | |
1059 for (line_i = 0; line_i < 16 - line_len; line_i++) { \ | |
1060 len += GREATEST_FPRINTF(out, " "); \ | |
1061 } \ | |
1062 GREATEST_FPRINTF(out, " "); \ | |
1063 for (line_i = i; line_i < i + line_len; line_i++) { \ | |
1064 unsigned char c = buf[line_i]; \ | |
1065 len += GREATEST_FPRINTF(out, "%c", isprint(c) ? c : '.'); \ | |
1066 } \ | |
1067 } \ | |
1068 len += GREATEST_FPRINTF(out, "\n"); \ | |
1069 return len; \ | |
1070 } \ | |
1071 \ | |
1072 void greatest_prng_init_first_pass(int id) { \ | |
1073 greatest_info.prng[id].random_order = 1; \ | |
1074 greatest_info.prng[id].count_run = 0; \ | |
1075 } \ | |
1076 \ | |
1077 int greatest_prng_init_second_pass(int id, unsigned long seed) { \ | |
1078 struct greatest_prng *p = &greatest_info.prng[id]; \ | |
1079 if (p->count == 0) { return 0; } \ | |
1080 p->count_ceil = p->count; \ | |
1081 for (p->m = 1; p->m < p->count; p->m <<= 1) {} \ | |
1082 p->state = seed & 0x1fffffff; /* only use lower 29 bits */ \ | |
1083 p->a = 4LU * p->state; /* to avoid overflow when */ \ | |
1084 p->a = (p->a ? p->a : 4) | 1; /* multiplied by 4 */ \ | |
1085 p->c = 2147483647; /* and so p->c ((2 ** 31) - 1) is */ \ | |
1086 p->initialized = 1; /* always relatively prime to p->a. */ \ | |
1087 fprintf(stderr, "init_second_pass: a %lu, c %lu, state %lu\n", \ | |
1088 p->a, p->c, p->state); \ | |
1089 return 1; \ | |
1090 } \ | |
1091 \ | |
1092 /* Step the pseudorandom number generator until its state reaches \ | |
1093 * another test ID between 0 and the test count. \ | |
1094 * This use a linear congruential pseudorandom number generator, \ | |
1095 * with the power-of-two ceiling of the test count as the modulus, the \ | |
1096 * masked seed as the multiplier, and a prime as the increment. For \ | |
1097 * each generated value < the test count, run the corresponding test. \ | |
1098 * This will visit all IDs 0 <= X < mod once before repeating, \ | |
1099 * with a starting position chosen based on the initial seed. \ | |
1100 * For details, see: Knuth, The Art of Computer Programming \ | |
1101 * Volume. 2, section 3.2.1. */ \ | |
1102 void greatest_prng_step(int id) { \ | |
1103 struct greatest_prng *p = &greatest_info.prng[id]; \ | |
1104 do { \ | |
1105 p->state = ((p->a * p->state) + p->c) & (p->m - 1); \ | |
1106 } while (p->state >= p->count_ceil); \ | |
1107 } \ | |
1108 \ | |
1109 void GREATEST_INIT(void) { \ | |
1110 /* Suppress unused function warning if features aren't used */ \ | |
1111 (void)greatest_run_suite; \ | |
1112 (void)greatest_parse_options; \ | |
1113 (void)greatest_prng_step; \ | |
1114 (void)greatest_prng_init_first_pass; \ | |
1115 (void)greatest_prng_init_second_pass; \ | |
1116 (void)greatest_set_test_suffix; \ | |
1117 \ | |
1118 memset(&greatest_info, 0, sizeof(greatest_info)); \ | |
1119 greatest_info.width = GREATEST_DEFAULT_WIDTH; \ | |
1120 GREATEST_SET_TIME(greatest_info.begin); \ | |
1121 } \ | |
1122 \ | |
1123 /* Report passes, failures, skipped tests, the number of \ | |
1124 * assertions, and the overall run time. */ \ | |
1125 void GREATEST_PRINT_REPORT(void) { \ | |
1126 if (!GREATEST_LIST_ONLY()) { \ | |
1127 update_counts_and_reset_suite(); \ | |
1128 GREATEST_SET_TIME(greatest_info.end); \ | |
1129 GREATEST_FPRINTF(GREATEST_STDOUT, \ | |
1130 "\nTotal: %u test%s", \ | |
1131 greatest_info.tests_run, \ | |
1132 greatest_info.tests_run == 1 ? "" : "s"); \ | |
1133 GREATEST_CLOCK_DIFF(greatest_info.begin, \ | |
1134 greatest_info.end); \ | |
1135 GREATEST_FPRINTF(GREATEST_STDOUT, ", %u assertion%s\n", \ | |
1136 greatest_info.assertions, \ | |
1137 greatest_info.assertions == 1 ? "" : "s"); \ | |
1138 GREATEST_FPRINTF(GREATEST_STDOUT, \ | |
1139 "Pass: %u, fail: %u, skip: %u.\n", \ | |
1140 greatest_info.passed, \ | |
1141 greatest_info.failed, greatest_info.skipped); \ | |
1142 } \ | |
1143 } \ | |
1144 \ | |
1145 greatest_type_info greatest_type_info_memory = { \ | |
1146 greatest_memory_equal_cb, \ | |
1147 greatest_memory_printf_cb, \ | |
1148 }; \ | |
1149 \ | |
1150 greatest_run_info greatest_info | |
1151 | |
1152 /* Handle command-line arguments, etc. */ | |
1153 #define GREATEST_MAIN_BEGIN() \ | |
1154 do { \ | |
1155 GREATEST_INIT(); \ | |
1156 greatest_parse_options(argc, argv); \ | |
1157 } while (0) | |
1158 | |
1159 /* Report results, exit with exit status based on results. */ | |
1160 #define GREATEST_MAIN_END() \ | |
1161 do { \ | |
1162 GREATEST_PRINT_REPORT(); \ | |
1163 return (greatest_all_passed() ? EXIT_SUCCESS : EXIT_FAILURE); \ | |
1164 } while (0) | |
1165 | |
1166 /* Make abbreviations without the GREATEST_ prefix for the | |
1167 * most commonly used symbols. */ | |
1168 #if GREATEST_USE_ABBREVS | |
1169 #define TEST GREATEST_TEST | |
1170 #define SUITE GREATEST_SUITE | |
1171 #define SUITE_EXTERN GREATEST_SUITE_EXTERN | |
1172 #define RUN_TEST GREATEST_RUN_TEST | |
1173 #define RUN_TEST1 GREATEST_RUN_TEST1 | |
1174 #define RUN_SUITE GREATEST_RUN_SUITE | |
1175 #define IGNORE_TEST GREATEST_IGNORE_TEST | |
1176 #define ASSERT GREATEST_ASSERT | |
1177 #define ASSERTm GREATEST_ASSERTm | |
1178 #define ASSERT_FALSE GREATEST_ASSERT_FALSE | |
1179 #define ASSERT_EQ GREATEST_ASSERT_EQ | |
1180 #define ASSERT_EQ_FMT GREATEST_ASSERT_EQ_FMT | |
1181 #define ASSERT_IN_RANGE GREATEST_ASSERT_IN_RANGE | |
1182 #define ASSERT_EQUAL_T GREATEST_ASSERT_EQUAL_T | |
1183 #define ASSERT_STR_EQ GREATEST_ASSERT_STR_EQ | |
1184 #define ASSERT_STRN_EQ GREATEST_ASSERT_STRN_EQ | |
1185 #define ASSERT_MEM_EQ GREATEST_ASSERT_MEM_EQ | |
1186 #define ASSERT_ENUM_EQ GREATEST_ASSERT_ENUM_EQ | |
1187 #define ASSERT_FALSEm GREATEST_ASSERT_FALSEm | |
1188 #define ASSERT_EQm GREATEST_ASSERT_EQm | |
1189 #define ASSERT_EQ_FMTm GREATEST_ASSERT_EQ_FMTm | |
1190 #define ASSERT_IN_RANGEm GREATEST_ASSERT_IN_RANGEm | |
1191 #define ASSERT_EQUAL_Tm GREATEST_ASSERT_EQUAL_Tm | |
1192 #define ASSERT_STR_EQm GREATEST_ASSERT_STR_EQm | |
1193 #define ASSERT_STRN_EQm GREATEST_ASSERT_STRN_EQm | |
1194 #define ASSERT_MEM_EQm GREATEST_ASSERT_MEM_EQm | |
1195 #define ASSERT_ENUM_EQm GREATEST_ASSERT_ENUM_EQm | |
1196 #define PASS GREATEST_PASS | |
1197 #define FAIL GREATEST_FAIL | |
1198 #define SKIP GREATEST_SKIP | |
1199 #define PASSm GREATEST_PASSm | |
1200 #define FAILm GREATEST_FAILm | |
1201 #define SKIPm GREATEST_SKIPm | |
1202 #define SET_SETUP GREATEST_SET_SETUP_CB | |
1203 #define SET_TEARDOWN GREATEST_SET_TEARDOWN_CB | |
1204 #define CHECK_CALL GREATEST_CHECK_CALL | |
1205 #define SHUFFLE_TESTS GREATEST_SHUFFLE_TESTS | |
1206 #define SHUFFLE_SUITES GREATEST_SHUFFLE_SUITES | |
1207 | |
1208 #ifdef GREATEST_VA_ARGS | |
1209 #define RUN_TESTp GREATEST_RUN_TESTp | |
1210 #endif | |
1211 | |
1212 #if GREATEST_USE_LONGJMP | |
1213 #define ASSERT_OR_LONGJMP GREATEST_ASSERT_OR_LONGJMP | |
1214 #define ASSERT_OR_LONGJMPm GREATEST_ASSERT_OR_LONGJMPm | |
1215 #define FAIL_WITH_LONGJMP GREATEST_FAIL_WITH_LONGJMP | |
1216 #define FAIL_WITH_LONGJMPm GREATEST_FAIL_WITH_LONGJMPm | |
1217 #endif | |
1218 | |
1219 #endif /* USE_ABBREVS */ | |
1220 | |
1221 #if defined(__cplusplus) && !defined(GREATEST_NO_EXTERN_CPLUSPLUS) | |
1222 } | |
1223 #endif | |
1224 | |
1225 #endif |