greatest.h 67 KB

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