123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591 |
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- TEST foo_should_foo() {
- 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 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 enum {
- GREATEST_FLAG_VERBOSE = 0x01,
- GREATEST_FLAG_FIRST_FAIL = 0x02,
- GREATEST_FLAG_LIST_ONLY = 0x04
- } GREATEST_FLAG;
-
- typedef struct greatest_run_info {
- unsigned int flags;
- unsigned int tests_run;
-
-
- unsigned int passed;
- unsigned int failed;
- unsigned int skipped;
-
-
- greatest_suite_info suite;
-
-
- const char *fail_file;
- unsigned int fail_line;
- const char *msg;
-
-
- greatest_setup_cb *setup;
- void *setup_udata;
- greatest_teardown_cb *teardown;
- void *teardown_udata;
-
-
- unsigned int col;
- unsigned int width;
-
-
- char *suite_filter;
- char *test_filter;
-
-
- clock_t begin;
- clock_t end;
- } greatest_run_info;
-
-
- extern greatest_run_info greatest_info;
-
-
-
-
- void greatest_do_pass(const char *name);
- void greatest_do_fail(const char *name);
- void greatest_do_skip(const char *name);
- int greatest_pre_test(const char *name);
- void greatest_post_test(const char *name, int res);
- void greatest_usage(const char *name);
- void GREATEST_SET_SETUP_CB(greatest_setup_cb *cb, void *udata);
- void GREATEST_SET_TEARDOWN_CB(greatest_teardown_cb *cb, void *udata);
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- do { \
- if (greatest_pre_test(
- int res = TEST(); \
- greatest_post_test(
- } else if (GREATEST_LIST_ONLY()) { \
- fprintf(GREATEST_STDOUT, " %s\n",
- } \
- } while (0)
-
-
-
- do { \
- if (greatest_pre_test(
- int res = TEST(ENV); \
- greatest_post_test(
- } else if (GREATEST_LIST_ONLY()) { \
- fprintf(GREATEST_STDOUT, " %s\n",
- } \
- } while (0)
-
-
-
-
- do { \
- if (greatest_pre_test(
- int res = TEST(__VA_ARGS__); \
- greatest_post_test(
- } else if (GREATEST_LIST_ONLY()) { \
- fprintf(GREATEST_STDOUT, " %s\n",
- } \
- } while (0)
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- do { \
- greatest_info.msg = MSG; \
- greatest_info.fail_file = __FILE__; \
- greatest_info.fail_line = __LINE__; \
- if (!(COND)) return -1; \
- greatest_info.msg = NULL; \
- } while (0)
-
-
- do { \
- greatest_info.msg = MSG; \
- greatest_info.fail_file = __FILE__; \
- greatest_info.fail_line = __LINE__; \
- if ((COND)) return -1; \
- greatest_info.msg = NULL; \
- } while (0)
-
-
- do { \
- greatest_info.msg = MSG; \
- greatest_info.fail_file = __FILE__; \
- greatest_info.fail_line = __LINE__; \
- if ((EXP) != (GOT)) return -1; \
- greatest_info.msg = NULL; \
- } while (0)
-
-
- do { \
- const char *exp_s = (EXP); \
- const char *got_s = (GOT); \
- greatest_info.msg = MSG; \
- greatest_info.fail_file = __FILE__; \
- greatest_info.fail_line = __LINE__; \
- if (0 != strcmp(exp_s, got_s)) { \
- fprintf(GREATEST_STDOUT, \
- "Expected:\n####\n%s\n####\n", exp_s); \
- fprintf(GREATEST_STDOUT, \
- "Got:\n####\n%s\n####\n", got_s); \
- return -1; \
- } \
- greatest_info.msg = NULL; \
- } while (0)
-
-
- do { \
- greatest_info.msg = MSG; \
- return 0; \
- } while (0)
-
-
- do { \
- greatest_info.fail_file = __FILE__; \
- greatest_info.fail_line = __LINE__; \
- greatest_info.msg = MSG; \
- return -1; \
- } while (0)
-
-
- do { \
- greatest_info.msg = MSG; \
- return 1; \
- } while (0)
-
-
- NAME = clock(); \
- if (NAME == (clock_t) -1) { \
- fprintf(GREATEST_STDOUT, \
- "clock error: %s\n",
- exit(EXIT_FAILURE); \
- }
-
-
- fprintf(GREATEST_STDOUT, " (%lu ticks, %.3f sec)", \
- (long unsigned int) (C2) - (C1), \
- (double)((C2) - (C1)) / (1.0 * (double)CLOCKS_PER_SEC)) \
-
-
-
- \
- \
- static int greatest_name_match(const char *name, \
- const char *filter) { \
- size_t offset = 0; \
- size_t filter_len = strlen(filter); \
- while (name[offset] != '\0') { \
- if (name[offset] == filter[0]) { \
- if (0 == strncmp(&name[offset], filter, filter_len)) { \
- return 1; \
- } \
- } \
- offset++; \
- } \
- \
- return 0; \
- } \
- \
- int greatest_pre_test(const char *name) { \
- if (!GREATEST_LIST_ONLY() \
- && (!GREATEST_FIRST_FAIL() || greatest_info.suite.failed == 0) \
- && (greatest_info.test_filter == NULL || \
- greatest_name_match(name, greatest_info.test_filter))) { \
- GREATEST_SET_TIME(greatest_info.suite.pre_test); \
- if (greatest_info.setup) { \
- greatest_info.setup(greatest_info.setup_udata); \
- } \
- return 1; \
- } else { \
- return 0; \
- } \
- } \
- \
- void greatest_post_test(const char *name, int res) { \
- GREATEST_SET_TIME(greatest_info.suite.post_test); \
- if (greatest_info.teardown) { \
- void *udata = greatest_info.teardown_udata; \
- greatest_info.teardown(udata); \
- } \
- \
- if (res < 0) { \
- greatest_do_fail(name); \
- } else if (res > 0) { \
- greatest_do_skip(name); \
- } else if (res == 0) { \
- greatest_do_pass(name); \
- } \
- 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); \
- fprintf(GREATEST_STDOUT, "\n"); \
- } else if (greatest_info.col % greatest_info.width == 0) { \
- fprintf(GREATEST_STDOUT, "\n"); \
- greatest_info.col = 0; \
- } \
- if (GREATEST_STDOUT == stdout) fflush(stdout); \
- } \
- \
- static void greatest_run_suite(greatest_suite_cb *suite_cb, \
- const char *suite_name) { \
- if (greatest_info.suite_filter && \
- !greatest_name_match(suite_name, greatest_info.suite_filter)) \
- return; \
- if (GREATEST_FIRST_FAIL() && greatest_info.failed > 0) return; \
- greatest_info.suite.tests_run = 0; \
- greatest_info.suite.failed = 0; \
- greatest_info.suite.passed = 0; \
- greatest_info.suite.skipped = 0; \
- greatest_info.suite.pre_suite = 0; \
- greatest_info.suite.post_suite = 0; \
- greatest_info.suite.pre_test = 0; \
- greatest_info.suite.post_test = 0; \
- greatest_info.col = 0; \
- fprintf(GREATEST_STDOUT, "\n* Suite %s:\n", suite_name); \
- GREATEST_SET_TIME(greatest_info.suite.pre_suite); \
- suite_cb(); \
- GREATEST_SET_TIME(greatest_info.suite.post_suite); \
- if (greatest_info.suite.tests_run > 0) { \
- fprintf(GREATEST_STDOUT, \
- "\n%u tests - %u pass, %u fail, %u skipped", \
- greatest_info.suite.tests_run, \
- 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); \
- fprintf(GREATEST_STDOUT, "\n"); \
- } \
- 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; \
- } \
- \
- void greatest_do_pass(const char *name) { \
- if (GREATEST_IS_VERBOSE()) { \
- fprintf(GREATEST_STDOUT, "PASS %s: %s", \
- name, greatest_info.msg ? greatest_info.msg : ""); \
- } else { \
- fprintf(GREATEST_STDOUT, "."); \
- } \
- greatest_info.suite.passed++; \
- } \
- \
- void greatest_do_fail(const char *name) { \
- if (GREATEST_IS_VERBOSE()) { \
- fprintf(GREATEST_STDOUT, \
- "FAIL %s: %s (%s:%u)", \
- name, greatest_info.msg ? greatest_info.msg : "", \
- greatest_info.fail_file, greatest_info.fail_line); \
- } else { \
- fprintf(GREATEST_STDOUT, "F"); \
- \
- if (greatest_info.col % greatest_info.width != 0) \
- fprintf(GREATEST_STDOUT, "\n"); \
- greatest_info.col = 0; \
- fprintf(GREATEST_STDOUT, "FAIL %s: %s (%s:%u)\n", \
- name, \
- greatest_info.msg ? greatest_info.msg : "", \
- greatest_info.fail_file, greatest_info.fail_line); \
- } \
- greatest_info.suite.failed++; \
- } \
- \
- void greatest_do_skip(const char *name) { \
- if (GREATEST_IS_VERBOSE()) { \
- fprintf(GREATEST_STDOUT, "SKIP %s: %s", \
- name, \
- greatest_info.msg ? \
- greatest_info.msg : "" ); \
- } else { \
- fprintf(GREATEST_STDOUT, "s"); \
- } \
- greatest_info.suite.skipped++; \
- } \
- \
- void greatest_usage(const char *name) { \
- fprintf(GREATEST_STDOUT, \
- "Usage: %s [-hlfv] [-s SUITE] [-t TEST]\n" \
- " -h print this Help\n" \
- " -l List suites and their tests, then exit\n" \
- " -f Stop runner after first failure\n" \
- " -v Verbose output\n" \
- " -s SUITE only run suite named SUITE\n" \
- " -t TEST only run test named TEST\n", \
- name); \
- } \
- \
- 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; \
- } \
- \
- greatest_run_info greatest_info
-
-
-
- do { \
- int i = 0; \
- memset(&greatest_info, 0, sizeof(greatest_info)); \
- if (greatest_info.width == 0) { \
- greatest_info.width = GREATEST_DEFAULT_WIDTH; \
- } \
- for (i = 1; i < argc; i++) { \
- if (0 == strcmp("-t", argv[i])) { \
- if (argc <= i + 1) { \
- greatest_usage(argv[0]); \
- exit(EXIT_FAILURE); \
- } \
- greatest_info.test_filter = argv[i+1]; \
- i++; \
- } else if (0 == strcmp("-s", argv[i])) { \
- if (argc <= i + 1) { \
- greatest_usage(argv[0]); \
- exit(EXIT_FAILURE); \
- } \
- greatest_info.suite_filter = argv[i+1]; \
- i++; \
- } else if (0 == strcmp("-f", argv[i])) { \
- greatest_info.flags |= GREATEST_FLAG_FIRST_FAIL; \
- } else if (0 == strcmp("-v", argv[i])) { \
- greatest_info.flags |= GREATEST_FLAG_VERBOSE; \
- } else if (0 == strcmp("-l", argv[i])) { \
- greatest_info.flags |= GREATEST_FLAG_LIST_ONLY; \
- } else if (0 == strcmp("-h", argv[i])) { \
- greatest_usage(argv[0]); \
- exit(EXIT_SUCCESS); \
- } else { \
- fprintf(GREATEST_STDOUT, \
- "Unknown argument '%s'\n", argv[i]); \
- greatest_usage(argv[0]); \
- exit(EXIT_FAILURE); \
- } \
- } \
- } while (0); \
- GREATEST_SET_TIME(greatest_info.begin)
-
-
- do { \
- if (!GREATEST_LIST_ONLY()) { \
- GREATEST_SET_TIME(greatest_info.end); \
- fprintf(GREATEST_STDOUT, \
- "\nTotal: %u tests", greatest_info.tests_run); \
- GREATEST_CLOCK_DIFF(greatest_info.begin, \
- greatest_info.end); \
- fprintf(GREATEST_STDOUT, "\n"); \
- fprintf(GREATEST_STDOUT, \
- "Pass: %u, fail: %u, skip: %u.\n", \
- greatest_info.passed, \
- greatest_info.failed, greatest_info.skipped); \
- } \
- return (greatest_info.failed > 0 \
- ? EXIT_FAILURE : EXIT_SUCCESS); \
- } while (0)
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
|