123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266 |
- extern "C" {
- TEST foo_should_foo(void) {
- PASS();
- }
- static void setup_cb(void *data) {
- printf("setup callback for each test case\n");
- }
- static void teardown_cb(void *data) {
- printf("teardown callback for each test case\n");
- }
- SUITE(suite) {
-
- SET_SETUP(setup_cb, voidp_to_callback_data);
- SET_TEARDOWN(teardown_cb, voidp_to_callback_data);
- RUN_TEST(foo_should_foo);
- }
- GREATEST_MAIN_DEFS();
- int run_tests(void) {
- GREATEST_INIT();
-
- RUN_SUITE(suite);
-
- RUN_TEST(foo_should_foo);
- GREATEST_PRINT_REPORT();
- return greatest_all_passed();
- }
- int main(int argc, char **argv) {
- GREATEST_MAIN_BEGIN();
- RUN_SUITE(suite);
- GREATEST_MAIN_END();
- }
- typedef struct greatest_suite_info {
- unsigned int tests_run;
- unsigned int passed;
- unsigned int failed;
- unsigned int skipped;
-
- clock_t pre_suite;
- clock_t post_suite;
- clock_t pre_test;
- clock_t post_test;
- } greatest_suite_info;
- typedef void greatest_suite_cb(void);
- typedef void greatest_setup_cb(void *udata);
- typedef void greatest_teardown_cb(void *udata);
- typedef int greatest_equal_cb(const void *expd, const void *got, void *udata);
- typedef int greatest_printf_cb(const void *t, void *udata);
- typedef struct greatest_type_info {
- greatest_equal_cb *equal;
- greatest_printf_cb *print;
- } greatest_type_info;
- typedef struct greatest_memory_cmp_env {
- const unsigned char *exp;
- const unsigned char *got;
- size_t size;
- } greatest_memory_cmp_env;
- extern greatest_type_info greatest_type_info_string;
- extern greatest_type_info greatest_type_info_memory;
- typedef enum {
- GREATEST_FLAG_FIRST_FAIL = 0x01,
- GREATEST_FLAG_LIST_ONLY = 0x02,
- GREATEST_FLAG_ABORT_ON_FAIL = 0x04
- } greatest_flag_t;
- struct greatest_prng {
- unsigned char random_order;
- unsigned char initialized;
- unsigned char pad_0[6];
- unsigned long state;
- unsigned long count;
- unsigned long count_ceil;
- unsigned long count_run;
- unsigned long a;
- unsigned long c;
- unsigned long m;
- };
- typedef struct greatest_run_info {
- unsigned char flags;
- unsigned char verbosity;
- unsigned char running_test;
- unsigned char exact_name_match;
- unsigned int tests_run;
-
- greatest_suite_info suite;
-
- unsigned int passed;
- unsigned int failed;
- unsigned int skipped;
- unsigned int assertions;
-
- unsigned int fail_line;
- unsigned int pad_1;
- const char *fail_file;
- const char *msg;
-
- greatest_setup_cb *setup;
- void *setup_udata;
- greatest_teardown_cb *teardown;
- void *teardown_udata;
-
- unsigned int col;
- unsigned int width;
-
- const char *suite_filter;
- const char *test_filter;
- const char *test_exclude;
- const char *name_suffix;
- char name_buf[GREATEST_TESTNAME_BUF_SIZE];
- struct greatest_prng prng[2];
-
- clock_t begin;
- clock_t end;
- int pad_jmp_buf;
- unsigned char pad_2[4];
- jmp_buf jump_dest;
- } greatest_run_info;
- struct greatest_report_t {
-
- unsigned int passed;
- unsigned int failed;
- unsigned int skipped;
- unsigned int assertions;
- };
- extern greatest_run_info greatest_info;
- typedef const char *greatest_enum_str_fun(int value);
- int greatest_test_pre(const char *name);
- void greatest_test_post(int res);
- int greatest_do_assert_equal_t(const void *expd, const void *got,
- greatest_type_info *type_info, void *udata);
- void greatest_prng_init_first_pass(int id);
- int greatest_prng_init_second_pass(int id, unsigned long seed);
- void greatest_prng_step(int id);
- void GREATEST_SET_SETUP_CB(greatest_setup_cb *cb, void *udata);
- void GREATEST_SET_TEARDOWN_CB(greatest_teardown_cb *cb, void *udata);
- void GREATEST_INIT(void);
- void GREATEST_PRINT_REPORT(void);
- int greatest_all_passed(void);
- void greatest_set_suite_filter(const char *filter);
- void greatest_set_test_filter(const char *filter);
- void greatest_set_test_exclude(const char *filter);
- void greatest_set_exact_name_match(void);
- void greatest_stop_at_first_fail(void);
- void greatest_abort_on_fail(void);
- void greatest_list_only(void);
- void greatest_get_report(struct greatest_report_t *report);
- unsigned int greatest_get_verbosity(void);
- void greatest_set_verbosity(unsigned int verbosity);
- void greatest_set_flag(greatest_flag_t flag);
- void greatest_set_test_suffix(const char *suffix);
- (defined(_MSC_VER) && _MSC_VER >= 1800)
- typedef enum greatest_test_res {
- GREATEST_TEST_RES_PASS = 0,
- GREATEST_TEST_RES_FAIL = -1,
- GREATEST_TEST_RES_SKIP = 1
- } greatest_test_res;
- do { \
- if (greatest_test_pre(
- enum greatest_test_res res = GREATEST_SAVE_CONTEXT(); \
- if (res == GREATEST_TEST_RES_PASS) { \
- res = TEST(); \
- } \
- greatest_test_post(res); \
- } \
- } while (0)
- do { \
- if (greatest_test_pre(
- enum greatest_test_res res = GREATEST_SAVE_CONTEXT(); \
- if (res == GREATEST_TEST_RES_PASS) { \
- res = TEST(ENV); \
- } \
- greatest_test_post(res); \
- } \
- } while (0)
- do { \
- if (greatest_test_pre(
- enum greatest_test_res res = GREATEST_SAVE_CONTEXT(); \
- if (res == GREATEST_TEST_RES_PASS) { \
- res = TEST(__VA_ARGS__); \
- } \
- greatest_test_post(res); \
- } \
- } while (0)
- (greatest_info.flags & GREATEST_FLAG_LIST_ONLY)
- (greatest_info.flags & GREATEST_FLAG_FIRST_FAIL)
- (greatest_info.flags & GREATEST_FLAG_ABORT_ON_FAIL)
- (GREATEST_FIRST_FAIL() && \
- (greatest_info.suite.failed > 0 || greatest_info.failed > 0))
- GREATEST_ASSERTm(
- GREATEST_ASSERT_OR_LONGJMPm(
- GREATEST_ASSERT_FALSEm(
- GREATEST_ASSERT_EQm(
- GREATEST_ASSERT_NEQm(
- GREATEST_ASSERT_GTm(
- GREATEST_ASSERT_GTEm(
- GREATEST_ASSERT_LTm(
- GREATEST_ASSERT_LTEm(
- GREATEST_ASSERT_EQ_FMTm(
- GREATEST_ASSERT_IN_RANGEm(
- GREATEST_ASSERT_EQUAL_Tm(
- GREATEST_ASSERT_STR_EQm(
- GREATEST_ASSERT_STRN_EQm(
- GREATEST_ASSERT_MEM_EQm(
- GREATEST_ASSERT_ENUM_EQm(
- do { \
- greatest_info.assertions++; \
- if (!(COND)) { GREATEST_FAILm(MSG); } \
- } while (0)
- do { \
- greatest_info.assertions++; \
- if (!(COND)) { GREATEST_FAIL_WITH_LONGJMPm(MSG); } \
- } while (0)
- do { \
- greatest_info.assertions++; \
- if ((COND)) { GREATEST_FAILm(MSG); } \
- } while (0)
- do { \
- greatest_info.assertions++; \
- if (!((EXP) REL (GOT))) { GREATEST_FAILm(MSG); } \
- } while (0)
- do { \
- greatest_info.assertions++; \
- if ((EXP) != (GOT)) { \
- GREATEST_FPRINTF(GREATEST_STDOUT, "\nExpected: "); \
- GREATEST_FPRINTF(GREATEST_STDOUT, FMT, EXP); \
- GREATEST_FPRINTF(GREATEST_STDOUT, "\n Got: "); \
- GREATEST_FPRINTF(GREATEST_STDOUT, FMT, GOT); \
- GREATEST_FPRINTF(GREATEST_STDOUT, "\n"); \
- GREATEST_FAILm(MSG); \
- } \
- } while (0)
- do { \
- int greatest_EXP = (int)(EXP); \
- int greatest_GOT = (int)(GOT); \
- greatest_enum_str_fun *greatest_ENUM_STR = ENUM_STR; \
- if (greatest_EXP != greatest_GOT) { \
- GREATEST_FPRINTF(GREATEST_STDOUT, "\nExpected: %s", \
- greatest_ENUM_STR(greatest_EXP)); \
- GREATEST_FPRINTF(GREATEST_STDOUT, "\n Got: %s\n", \
- greatest_ENUM_STR(greatest_GOT)); \
- GREATEST_FAILm(MSG); \
- } \
- } while (0) \
- do { \
- GREATEST_FLOAT greatest_EXP = (EXP); \
- GREATEST_FLOAT greatest_GOT = (GOT); \
- GREATEST_FLOAT greatest_TOL = (TOL); \
- greatest_info.assertions++; \
- if ((greatest_EXP > greatest_GOT && \
- greatest_EXP - greatest_GOT > greatest_TOL) || \
- (greatest_EXP < greatest_GOT && \
- greatest_GOT - greatest_EXP > greatest_TOL)) { \
- GREATEST_FPRINTF(GREATEST_STDOUT, \
- "\nExpected: " GREATEST_FLOAT_FMT \
- " +/- " GREATEST_FLOAT_FMT \
- "\n Got: " GREATEST_FLOAT_FMT \
- "\n", \
- greatest_EXP, greatest_TOL, greatest_GOT); \
- GREATEST_FAILm(MSG); \
- } \
- } while (0)
- do { \
- GREATEST_ASSERT_EQUAL_Tm(MSG, EXP, GOT, \
- &greatest_type_info_string, NULL); \
- } while (0) \
- do { \
- size_t size = SIZE; \
- GREATEST_ASSERT_EQUAL_Tm(MSG, EXP, GOT, \
- &greatest_type_info_string, &size); \
- } while (0) \
- do { \
- greatest_memory_cmp_env env; \
- env.exp = (const unsigned char *)EXP; \
- env.got = (const unsigned char *)GOT; \
- env.size = SIZE; \
- GREATEST_ASSERT_EQUAL_Tm(MSG, env.exp, env.got, \
- &greatest_type_info_memory, &env); \
- } while (0) \
- do { \
- greatest_type_info *type_info = (TYPE_INFO); \
- greatest_info.assertions++; \
- if (!greatest_do_assert_equal_t(EXP, GOT, \
- type_info, UDATA)) { \
- if (type_info == NULL || type_info->equal == NULL) { \
- GREATEST_FAILm("type_info->equal callback missing!"); \
- } else { \
- GREATEST_FAILm(MSG); \
- } \
- } \
- } while (0) \
- do { \
- greatest_info.msg = MSG; \
- return GREATEST_TEST_RES_PASS; \
- } while (0)
- do { \
- greatest_info.fail_file = __FILE__; \
- greatest_info.fail_line = __LINE__; \
- greatest_info.msg = MSG; \
- if (GREATEST_ABORT_ON_FAIL()) { abort(); } \
- return GREATEST_TEST_RES_FAIL; \
- } while (0)
- do { \
- greatest_info.fail_file = __FILE__; \
- greatest_info.fail_line = __LINE__; \
- greatest_info.msg = MSG; \
- longjmp(greatest_info.jump_dest, GREATEST_TEST_RES_FAIL); \
- } while (0)
- do { \
- greatest_info.msg = MSG; \
- return GREATEST_TEST_RES_SKIP; \
- } while (0)
- do { \
- enum greatest_test_res greatest_RES = RES; \
- if (greatest_RES != GREATEST_TEST_RES_PASS) { \
- return greatest_RES; \
- } \
- } while (0) \
- NAME = clock(); \
- if (NAME == (clock_t) -1) { \
- GREATEST_FPRINTF(GREATEST_STDOUT, \
- "clock error: %s\n",
- exit(EXIT_FAILURE); \
- }
- GREATEST_FPRINTF(GREATEST_STDOUT, " (%lu ticks, %.3f sec)", \
- (long unsigned int) (C2) - (long unsigned int)(C1), \
- (double)((C2) - (C1)) / (1.0 * (double)CLOCKS_PER_SEC))
-
- \
- ((enum greatest_test_res)(setjmp(greatest_info.jump_dest)))
- \
- GREATEST_TEST_RES_PASS
- do { \
- struct greatest_prng *prng = &greatest_info.prng[ID]; \
- greatest_prng_init_first_pass(ID); \
- do { \
- prng->count = 0; \
- if (prng->initialized) { greatest_prng_step(ID); } \
- BODY; \
- if (!prng->initialized) { \
- if (!greatest_prng_init_second_pass(ID, SD)) { break; } \
- } else if (prng->count_run == prng->count_ceil) { \
- break; \
- } \
- } while (!GREATEST_FAILURE_ABORT()); \
- prng->count_run = prng->random_order = prng->initialized = 0; \
- } while(0)
- \
- \
- static int greatest_name_match(const char *name, const char *filter, \
- int res_if_none) { \
- size_t offset = 0; \
- size_t filter_len = filter ? strlen(filter) : 0; \
- if (filter_len == 0) { return res_if_none; } \
- if (greatest_info.exact_name_match && strlen(name) != filter_len) { \
- return 0; \
- } \
- while (name[offset] != '\0') { \
- if (name[offset] == filter[0]) { \
- if (0 == strncmp(&name[offset], filter, filter_len)) { \
- return 1; \
- } \
- } \
- offset++; \
- } \
- \
- return 0; \
- } \
- \
- static void greatest_buffer_test_name(const char *name) { \
- struct greatest_run_info *g = &greatest_info; \
- size_t len = strlen(name), size = sizeof(g->name_buf); \
- memset(g->name_buf, 0x00, size); \
- (void)strncat(g->name_buf, name, size - 1); \
- if (g->name_suffix && (len + 1 < size)) { \
- g->name_buf[len] = '_'; \
- strncat(&g->name_buf[len+1], g->name_suffix, size-(len+2)); \
- } \
- } \
- \
- \
- int greatest_test_pre(const char *name) { \
- struct greatest_run_info *g = &greatest_info; \
- int match; \
- greatest_buffer_test_name(name); \
- match = greatest_name_match(g->name_buf, g->test_filter, 1) && \
- !greatest_name_match(g->name_buf, g->test_exclude, 0); \
- if (GREATEST_LIST_ONLY()) { \
- if (match) { \
- GREATEST_FPRINTF(GREATEST_STDOUT, " %s\n", g->name_buf); \
- } \
- goto clear; \
- } \
- if (match && (!GREATEST_FIRST_FAIL() || g->suite.failed == 0)) { \
- struct greatest_prng *p = &g->prng[1]; \
- if (p->random_order) { \
- p->count++; \
- if (!p->initialized || ((p->count - 1) != p->state)) { \
- goto clear; \
- } \
- } \
- if (g->running_test) { \
- fprintf(stderr, "Error: Test run inside another test.\n"); \
- return 0; \
- } \
- GREATEST_SET_TIME(g->suite.pre_test); \
- if (g->setup) { g->setup(g->setup_udata); } \
- p->count_run++; \
- g->running_test = 1; \
- return 1; \
- } else { \
- goto clear; \
- } \
- clear: \
- g->name_suffix = NULL; \
- return 0; \
- } \
- \
- static void greatest_do_pass(void) { \
- struct greatest_run_info *g = &greatest_info; \
- if (GREATEST_IS_VERBOSE()) { \
- GREATEST_FPRINTF(GREATEST_STDOUT, "PASS %s: %s", \
- g->name_buf, g->msg ? g->msg : ""); \
- } else { \
- GREATEST_FPRINTF(GREATEST_STDOUT, "."); \
- } \
- g->suite.passed++; \
- } \
- \
- static void greatest_do_fail(void) { \
- struct greatest_run_info *g = &greatest_info; \
- if (GREATEST_IS_VERBOSE()) { \
- GREATEST_FPRINTF(GREATEST_STDOUT, \
- "FAIL %s: %s (%s:%u)", g->name_buf, \
- g->msg ? g->msg : "", g->fail_file, g->fail_line); \
- } else { \
- GREATEST_FPRINTF(GREATEST_STDOUT, "F"); \
- g->col++; \
- if (g->col != 0) { \
- GREATEST_FPRINTF(GREATEST_STDOUT, "\n"); \
- g->col = 0; \
- } \
- GREATEST_FPRINTF(GREATEST_STDOUT, "FAIL %s: %s (%s:%u)\n", \
- g->name_buf, g->msg ? g->msg : "", \
- g->fail_file, g->fail_line); \
- } \
- g->suite.failed++; \
- } \
- \
- static void greatest_do_skip(void) { \
- struct greatest_run_info *g = &greatest_info; \
- if (GREATEST_IS_VERBOSE()) { \
- GREATEST_FPRINTF(GREATEST_STDOUT, "SKIP %s: %s", \
- g->name_buf, g->msg ? g->msg : ""); \
- } else { \
- GREATEST_FPRINTF(GREATEST_STDOUT, "s"); \
- } \
- g->suite.skipped++; \
- } \
- \
- void greatest_test_post(int res) { \
- GREATEST_SET_TIME(greatest_info.suite.post_test); \
- if (greatest_info.teardown) { \
- void *udata = greatest_info.teardown_udata; \
- greatest_info.teardown(udata); \
- } \
- \
- greatest_info.running_test = 0; \
- if (res <= GREATEST_TEST_RES_FAIL) { \
- greatest_do_fail(); \
- } else if (res >= GREATEST_TEST_RES_SKIP) { \
- greatest_do_skip(); \
- } else if (res == GREATEST_TEST_RES_PASS) { \
- greatest_do_pass(); \
- } \
- greatest_info.name_suffix = NULL; \
- greatest_info.suite.tests_run++; \
- greatest_info.col++; \
- if (GREATEST_IS_VERBOSE()) { \
- GREATEST_CLOCK_DIFF(greatest_info.suite.pre_test, \
- greatest_info.suite.post_test); \
- GREATEST_FPRINTF(GREATEST_STDOUT, "\n"); \
- } else if (greatest_info.col % greatest_info.width == 0) { \
- GREATEST_FPRINTF(GREATEST_STDOUT, "\n"); \
- greatest_info.col = 0; \
- } \
- fflush(GREATEST_STDOUT); \
- } \
- \
- static void report_suite(void) { \
- if (greatest_info.suite.tests_run > 0) { \
- GREATEST_FPRINTF(GREATEST_STDOUT, \
- "\n%u test%s - %u passed, %u failed, %u skipped", \
- greatest_info.suite.tests_run, \
- greatest_info.suite.tests_run == 1 ? "" : "s", \
- greatest_info.suite.passed, \
- greatest_info.suite.failed, \
- greatest_info.suite.skipped); \
- GREATEST_CLOCK_DIFF(greatest_info.suite.pre_suite, \
- greatest_info.suite.post_suite); \
- GREATEST_FPRINTF(GREATEST_STDOUT, "\n"); \
- } \
- } \
- \
- static void update_counts_and_reset_suite(void) { \
- greatest_info.setup = NULL; \
- greatest_info.setup_udata = NULL; \
- greatest_info.teardown = NULL; \
- greatest_info.teardown_udata = NULL; \
- greatest_info.passed += greatest_info.suite.passed; \
- greatest_info.failed += greatest_info.suite.failed; \
- greatest_info.skipped += greatest_info.suite.skipped; \
- greatest_info.tests_run += greatest_info.suite.tests_run; \
- memset(&greatest_info.suite, 0, sizeof(greatest_info.suite)); \
- greatest_info.col = 0; \
- } \
- \
- static int greatest_suite_pre(const char *suite_name) { \
- struct greatest_prng *p = &greatest_info.prng[0]; \
- if (!greatest_name_match(suite_name, greatest_info.suite_filter, 1) \
- || (GREATEST_FAILURE_ABORT())) { return 0; } \
- if (p->random_order) { \
- p->count++; \
- if (!p->initialized || ((p->count - 1) != p->state)) { \
- return 0; \
- } \
- } \
- p->count_run++; \
- update_counts_and_reset_suite(); \
- GREATEST_FPRINTF(GREATEST_STDOUT, "\n* Suite %s:\n", suite_name); \
- GREATEST_SET_TIME(greatest_info.suite.pre_suite); \
- return 1; \
- } \
- \
- static void greatest_suite_post(void) { \
- GREATEST_SET_TIME(greatest_info.suite.post_suite); \
- report_suite(); \
- } \
- \
- static void greatest_run_suite(greatest_suite_cb *suite_cb, \
- const char *suite_name) { \
- if (greatest_suite_pre(suite_name)) { \
- suite_cb(); \
- greatest_suite_post(); \
- } \
- } \
- \
- int greatest_do_assert_equal_t(const void *expd, const void *got, \
- greatest_type_info *type_info, void *udata) { \
- int eq = 0; \
- if (type_info == NULL || type_info->equal == NULL) { return 0; } \
- eq = type_info->equal(expd, got, udata); \
- if (!eq) { \
- if (type_info->print != NULL) { \
- GREATEST_FPRINTF(GREATEST_STDOUT, "\nExpected: "); \
- (void)type_info->print(expd, udata); \
- GREATEST_FPRINTF(GREATEST_STDOUT, "\n Got: "); \
- (void)type_info->print(got, udata); \
- GREATEST_FPRINTF(GREATEST_STDOUT, "\n"); \
- } \
- } \
- return eq; \
- } \
- \
- static void greatest_usage(const char *name) { \
- GREATEST_FPRINTF(GREATEST_STDOUT, \
- "Usage: %s [-hlfavex] [-s SUITE] [-t TEST] [-x EXCLUDE]\n" \
- " -h, --help print this Help\n" \
- " -l List suites and tests, then exit (dry run)\n" \
- " -f Stop runner after first failure\n" \
- " -a Abort on first failure (implies -f)\n" \
- " -v Verbose output\n" \
- " -s SUITE only run suites containing substring SUITE\n" \
- " -t TEST only run tests containing substring TEST\n" \
- " -e only run exact name match for -s or -t\n" \
- " -x EXCLUDE exclude tests containing substring EXCLUDE\n", \
- name); \
- } \
- \
- static void greatest_parse_options(int argc, char **argv) { \
- int i = 0; \
- for (i = 1; i < argc; i++) { \
- if (argv[i][0] == '-') { \
- char f = argv[i][1]; \
- if ((f == 's' || f == 't' || f == 'x') && argc <= i + 1) { \
- greatest_usage(argv[0]); exit(EXIT_FAILURE); \
- } \
- switch (f) { \
- case 's': \
- greatest_set_suite_filter(argv[i + 1]); i++; break; \
- case 't': \
- greatest_set_test_filter(argv[i + 1]); i++; break; \
- case 'x': \
- greatest_set_test_exclude(argv[i + 1]); i++; break; \
- case 'e': \
- greatest_set_exact_name_match(); break; \
- case 'f': \
- greatest_stop_at_first_fail(); break; \
- case 'a': \
- greatest_abort_on_fail(); break; \
- case 'l': \
- greatest_list_only(); break; \
- case 'v': \
- greatest_info.verbosity++; break; \
- case 'h': \
- greatest_usage(argv[0]); exit(EXIT_SUCCESS); \
- default: \
- case '-': \
- if (0 == strncmp("--help", argv[i], 6)) { \
- greatest_usage(argv[0]); exit(EXIT_SUCCESS); \
- } else if (0 == strcmp("--", argv[i])) { \
- return; \
- } \
- GREATEST_FPRINTF(GREATEST_STDOUT, \
- "Unknown argument '%s'\n", argv[i]); \
- greatest_usage(argv[0]); \
- exit(EXIT_FAILURE); \
- } \
- } \
- } \
- } \
- \
- int greatest_all_passed(void) { return (greatest_info.failed == 0); } \
- \
- void greatest_set_test_filter(const char *filter) { \
- greatest_info.test_filter = filter; \
- } \
- \
- void greatest_set_test_exclude(const char *filter) { \
- greatest_info.test_exclude = filter; \
- } \
- \
- void greatest_set_suite_filter(const char *filter) { \
- greatest_info.suite_filter = filter; \
- } \
- \
- void greatest_set_exact_name_match(void) { \
- greatest_info.exact_name_match = 1; \
- } \
- \
- void greatest_stop_at_first_fail(void) { \
- greatest_set_flag(GREATEST_FLAG_FIRST_FAIL); \
- } \
- \
- void greatest_abort_on_fail(void) { \
- greatest_set_flag(GREATEST_FLAG_ABORT_ON_FAIL); \
- } \
- \
- void greatest_list_only(void) { \
- greatest_set_flag(GREATEST_FLAG_LIST_ONLY); \
- } \
- \
- void greatest_get_report(struct greatest_report_t *report) { \
- if (report) { \
- report->passed = greatest_info.passed; \
- report->failed = greatest_info.failed; \
- report->skipped = greatest_info.skipped; \
- report->assertions = greatest_info.assertions; \
- } \
- } \
- \
- unsigned int greatest_get_verbosity(void) { \
- return greatest_info.verbosity; \
- } \
- \
- void greatest_set_verbosity(unsigned int verbosity) { \
- greatest_info.verbosity = (unsigned char)verbosity; \
- } \
- \
- void greatest_set_flag(greatest_flag_t flag) { \
- greatest_info.flags = (unsigned char)(greatest_info.flags | flag); \
- } \
- \
- void greatest_set_test_suffix(const char *suffix) { \
- greatest_info.name_suffix = suffix; \
- } \
- \
- void GREATEST_SET_SETUP_CB(greatest_setup_cb *cb, void *udata) { \
- greatest_info.setup = cb; \
- greatest_info.setup_udata = udata; \
- } \
- \
- void GREATEST_SET_TEARDOWN_CB(greatest_teardown_cb *cb, void *udata) { \
- greatest_info.teardown = cb; \
- greatest_info.teardown_udata = udata; \
- } \
- \
- static int greatest_string_equal_cb(const void *expd, const void *got, \
- void *udata) { \
- size_t *size = (size_t *)udata; \
- return (size != NULL \
- ? (0 == strncmp((const char *)expd, (const char *)got, *size)) \
- : (0 == strcmp((const char *)expd, (const char *)got))); \
- } \
- \
- static int greatest_string_printf_cb(const void *t, void *udata) { \
- (void)udata; \
- return GREATEST_FPRINTF(GREATEST_STDOUT, "%s", (const char *)t); \
- } \
- \
- greatest_type_info greatest_type_info_string = { \
- greatest_string_equal_cb, greatest_string_printf_cb, \
- }; \
- \
- static int greatest_memory_equal_cb(const void *expd, const void *got, \
- void *udata) { \
- greatest_memory_cmp_env *env = (greatest_memory_cmp_env *)udata; \
- return (0 == memcmp(expd, got, env->size)); \
- } \
- \
- \
- static int greatest_memory_printf_cb(const void *t, void *udata) { \
- greatest_memory_cmp_env *env = (greatest_memory_cmp_env *)udata; \
- const unsigned char *buf = (const unsigned char *)t; \
- unsigned char diff_mark = ' '; \
- FILE *out = GREATEST_STDOUT; \
- size_t i, line_i, line_len = 0; \
- int len = 0; \
- for (i = 0; i < env->size; i+= line_len) { \
- diff_mark = ' '; \
- line_len = env->size - i; \
- if (line_len > 16) { line_len = 16; } \
- for (line_i = i; line_i < i + line_len; line_i++) { \
- if (env->exp[line_i] != env->got[line_i]) diff_mark = 'X'; \
- } \
- len += GREATEST_FPRINTF(out, "\n%04x %c ", \
- (unsigned int)i, diff_mark); \
- for (line_i = i; line_i < i + line_len; line_i++) { \
- int m = env->exp[line_i] == env->got[line_i]; \
- len += GREATEST_FPRINTF(out, "%02x%c", \
- buf[line_i], m ? ' ' : '<'); \
- } \
- for (line_i = 0; line_i < 16 - line_len; line_i++) { \
- len += GREATEST_FPRINTF(out, " "); \
- } \
- GREATEST_FPRINTF(out, " "); \
- for (line_i = i; line_i < i + line_len; line_i++) { \
- unsigned char c = buf[line_i]; \
- len += GREATEST_FPRINTF(out, "%c", isprint(c) ? c : '.'); \
- } \
- } \
- len += GREATEST_FPRINTF(out, "\n"); \
- return len; \
- } \
- \
- void greatest_prng_init_first_pass(int id) { \
- greatest_info.prng[id].random_order = 1; \
- greatest_info.prng[id].count_run = 0; \
- } \
- \
- int greatest_prng_init_second_pass(int id, unsigned long seed) { \
- struct greatest_prng *p = &greatest_info.prng[id]; \
- if (p->count == 0) { return 0; } \
- p->count_ceil = p->count; \
- for (p->m = 1; p->m < p->count; p->m <<= 1) {} \
- p->state = seed & 0x1fffffff; \
- p->a = 4LU * p->state; \
- p->a = (p->a ? p->a : 4) | 1; \
- p->c = 2147483647; \
- p->initialized = 1; \
- fprintf(stderr, "init_second_pass: a %lu, c %lu, state %lu\n", \
- p->a, p->c, p->state); \
- return 1; \
- } \
- \
- \
- void greatest_prng_step(int id) { \
- struct greatest_prng *p = &greatest_info.prng[id]; \
- do { \
- p->state = ((p->a * p->state) + p->c) & (p->m - 1); \
- } while (p->state >= p->count_ceil); \
- } \
- \
- void GREATEST_INIT(void) { \
- \
- (void)greatest_run_suite; \
- (void)greatest_parse_options; \
- (void)greatest_prng_step; \
- (void)greatest_prng_init_first_pass; \
- (void)greatest_prng_init_second_pass; \
- (void)greatest_set_test_suffix; \
- \
- memset(&greatest_info, 0, sizeof(greatest_info)); \
- greatest_info.width = GREATEST_DEFAULT_WIDTH; \
- GREATEST_SET_TIME(greatest_info.begin); \
- } \
- \
- \
- void GREATEST_PRINT_REPORT(void) { \
- if (!GREATEST_LIST_ONLY()) { \
- update_counts_and_reset_suite(); \
- GREATEST_SET_TIME(greatest_info.end); \
- GREATEST_FPRINTF(GREATEST_STDOUT, \
- "\nTotal: %u test%s", \
- greatest_info.tests_run, \
- greatest_info.tests_run == 1 ? "" : "s"); \
- GREATEST_CLOCK_DIFF(greatest_info.begin, \
- greatest_info.end); \
- GREATEST_FPRINTF(GREATEST_STDOUT, ", %u assertion%s\n", \
- greatest_info.assertions, \
- greatest_info.assertions == 1 ? "" : "s"); \
- GREATEST_FPRINTF(GREATEST_STDOUT, \
- "Pass: %u, fail: %u, skip: %u.\n", \
- greatest_info.passed, \
- greatest_info.failed, greatest_info.skipped); \
- } \
- } \
- \
- greatest_type_info greatest_type_info_memory = { \
- greatest_memory_equal_cb, greatest_memory_printf_cb, \
- }; \
- \
- greatest_run_info greatest_info
- do { \
- GREATEST_INIT(); \
- greatest_parse_options(argc, argv); \
- } while (0)
- do { \
- GREATEST_PRINT_REPORT(); \
- return (greatest_all_passed() ? EXIT_SUCCESS : EXIT_FAILURE); \
- } while (0)
- }
|