CTS-SAT-1-OBC-Firmware
Loading...
Searching...
No Matches
greatest.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2011-2015 Scott Vokes <vokes.s@gmail.com>
3 *
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17#ifndef GREATEST_H
18#define GREATEST_H
19
20/* 1.0.0 */
21#define GREATEST_VERSION_MAJOR 1
22#define GREATEST_VERSION_MINOR 0
23#define GREATEST_VERSION_PATCH 0
24
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
30/*********************************************************************
31 * Minimal test runner template
32 *********************************************************************/
33#if 0
34
35#include "greatest.h"
36
37TEST foo_should_foo() {
38 PASS();
39}
40
41static void setup_cb(void *data) {
42 printf("setup callback for each test case\n");
43}
44
45static void teardown_cb(void *data) {
46 printf("teardown callback for each test case\n");
47}
48
49SUITE(suite) {
50 /* Optional setup/teardown callbacks which will be run before/after
51 * every test case in the suite.
52 * Cleared when the suite finishes. */
53 SET_SETUP(setup_cb, voidp_to_callback_data);
54 SET_TEARDOWN(teardown_cb, voidp_to_callback_data);
55
56 RUN_TEST(foo_should_foo);
57}
58
59/* Add definitions that need to be in the test runner's main file. */
61
62/* Set up, run suite(s) of tests, report pass/fail/skip stats. */
63int run_tests(void) {
64 GREATEST_INIT(); /* init. greatest internals */
65 /* List of suites to run. */
66 RUN_SUITE(suite);
67 GREATEST_REPORT(); /* display results */
68 return greatest_all_passed();
69}
70
71/* main(), for a standalone command-line test runner.
72 * This replaces run_tests above, and adds command line option
73 * handling and exiting with a pass/fail status. */
74int main(int argc, char **argv) {
75 GREATEST_MAIN_BEGIN(); /* init & parse command-line args */
76 RUN_SUITE(suite);
77 GREATEST_MAIN_END(); /* display results */
78}
79
80#endif
81/*********************************************************************/
82
83
84#include <stdlib.h>
85#include <stdio.h>
86#include <string.h>
87
88/***********
89 * Options *
90 ***********/
91
92/* Default column width for non-verbose output. */
93#ifndef GREATEST_DEFAULT_WIDTH
94#define GREATEST_DEFAULT_WIDTH 72
95#endif
96
97/* FILE *, for test logging. */
98#ifndef GREATEST_STDOUT
99#define GREATEST_STDOUT stdout
100#endif
101
102/* Remove GREATEST_ prefix from most commonly used symbols? */
103#ifndef GREATEST_USE_ABBREVS
104#define GREATEST_USE_ABBREVS 1
105#endif
106
107/* Set to 0 to disable all use of setjmp/longjmp. */
108#ifndef GREATEST_USE_LONGJMP
109#define GREATEST_USE_LONGJMP 1
110#endif
111
112#if GREATEST_USE_LONGJMP
113#include <setjmp.h>
114#endif
115
116/* Set to 0 to disable all use of time.h / clock(). */
117#ifndef GREATEST_USE_TIME
118#define GREATEST_USE_TIME 1
119#endif
120
121#if GREATEST_USE_TIME
122#include <time.h>
123#endif
124
125/* Floating point type, for ASSERT_IN_RANGE. */
126#ifndef GREATEST_FLOAT
127#define GREATEST_FLOAT double
128#define GREATEST_FLOAT_FMT "%g"
129#endif
130
131/*********
132 * Types *
133 *********/
134
135/* Info for the current running suite. */
136typedef struct greatest_suite_info {
137 unsigned int tests_run;
138 unsigned int passed;
139 unsigned int failed;
140 unsigned int skipped;
141
142#if GREATEST_USE_TIME
143 /* timers, pre/post running suite and individual tests */
144 clock_t pre_suite;
145 clock_t post_suite;
146 clock_t pre_test;
147 clock_t post_test;
148#endif
150
151/* Type for a suite function. */
152typedef void (greatest_suite_cb)(void);
153
154/* Types for setup/teardown callbacks. If non-NULL, these will be run
155 * and passed the pointer to their additional data. */
156typedef void (greatest_setup_cb)(void *udata);
157typedef void (greatest_teardown_cb)(void *udata);
158
159/* Type for an equality comparison between two pointers of the same type.
160 * Should return non-0 if equal, otherwise 0.
161 * UDATA is a closure value, passed through from ASSERT_EQUAL_T[m]. */
162typedef int greatest_equal_cb(const void *exp, const void *got, void *udata);
163
164/* Type for a callback that prints a value pointed to by T.
165 * Return value has the same meaning as printf's.
166 * UDATA is a closure value, passed through from ASSERT_EQUAL_T[m]. */
167typedef int greatest_printf_cb(const void *t, void *udata);
168
169/* Callbacks for an arbitrary type; needed for type-specific
170 * comparisons via GREATEST_ASSERT_EQUAL_T[m].*/
175
176/* Callbacks for string type. */
178
184
185/* Struct containing all test runner state. */
186typedef struct greatest_run_info {
187 unsigned int flags;
188 unsigned int tests_run; /* total test count */
189
190 /* overall pass/fail/skip counts */
191 unsigned int passed;
192 unsigned int failed;
193 unsigned int skipped;
194 unsigned int assertions;
195
196 /* currently running test suite */
198
199 /* info to print about the most recent failure */
200 const char *fail_file;
201 unsigned int fail_line;
202 const char *msg;
203
204 /* current setup/teardown hooks and userdata */
209
210 /* formatting info for ".....s...F"-style output */
211 unsigned int col;
212 unsigned int width;
213
214 /* only run a specific suite or test */
217
218#if GREATEST_USE_TIME
219 /* overall timers */
220 clock_t begin;
221 clock_t end;
222#endif
223
224#if GREATEST_USE_LONGJMP
225 jmp_buf jump_dest;
226#endif
228
229/* Global var for the current testing context.
230 * Initialized by GREATEST_MAIN_DEFS(). */
232
233
234/**********************
235 * Exported functions *
236 **********************/
237
238/* These are used internally by greatest. */
239void greatest_do_pass(const char *name);
240void greatest_do_fail(const char *name);
241void greatest_do_skip(const char *name);
242int greatest_pre_test(const char *name);
243void greatest_post_test(const char *name, int res);
244void greatest_usage(const char *name);
245int greatest_do_assert_equal_t(const void *exp, const void *got,
246 greatest_type_info *type_info, void *udata);
247
248/* These are part of the public greatest API. */
252
253
254/********************
255* Language Support *
256********************/
257
258/* If __VA_ARGS__ (C99) is supported, allow parametric testing
259* without needing to manually manage the argument struct. */
260#if __STDC_VERSION__ >= 19901L || _MSC_VER >= 1800
261#define GREATEST_VA_ARGS
262#endif
263
264
265/**********
266 * Macros *
267 **********/
268
269/* Define a suite. */
270#define GREATEST_SUITE(NAME) void NAME(void); void NAME(void)
271
272/* Start defining a test function.
273 * The arguments are not included, to allow parametric testing. */
274#define GREATEST_TEST static greatest_test_res
275
276/* PASS/FAIL/SKIP result from a test. Used internally. */
282
283/* Run a suite. */
284#define GREATEST_RUN_SUITE(S_NAME) greatest_run_suite(S_NAME, #S_NAME)
285
286/* Run a test in the current suite. */
287#define GREATEST_RUN_TEST(TEST) \
288 do { \
289 if (greatest_pre_test(#TEST) == 1) { \
290 greatest_test_res res = GREATEST_SAVE_CONTEXT(); \
291 if (res == GREATEST_TEST_RES_PASS) { \
292 res = TEST(); \
293 } \
294 greatest_post_test(#TEST, res); \
295 } else if (GREATEST_LIST_ONLY()) { \
296 fprintf(GREATEST_STDOUT, " %s\n", #TEST); \
297 } \
298 } while (0)
299
300/* Run a test in the current suite with one void * argument,
301 * which can be a pointer to a struct with multiple arguments. */
302#define GREATEST_RUN_TEST1(TEST, ENV) \
303 do { \
304 if (greatest_pre_test(#TEST) == 1) { \
305 int res = TEST(ENV); \
306 greatest_post_test(#TEST, res); \
307 } else if (GREATEST_LIST_ONLY()) { \
308 fprintf(GREATEST_STDOUT, " %s\n", #TEST); \
309 } \
310 } while (0)
311
312#ifdef GREATEST_VA_ARGS
313#define GREATEST_RUN_TESTp(TEST, ...) \
314 do { \
315 if (greatest_pre_test(#TEST) == 1) { \
316 int res = TEST(__VA_ARGS__); \
317 greatest_post_test(#TEST, res); \
318 } else if (GREATEST_LIST_ONLY()) { \
319 fprintf(GREATEST_STDOUT, " %s\n", #TEST); \
320 } \
321 } while (0)
322#endif
323
324
325/* Check if the test runner is in verbose mode. */
326#define GREATEST_IS_VERBOSE() (greatest_info.flags & GREATEST_FLAG_VERBOSE)
327#define GREATEST_LIST_ONLY() (greatest_info.flags & GREATEST_FLAG_LIST_ONLY)
328#define GREATEST_FIRST_FAIL() (greatest_info.flags & GREATEST_FLAG_FIRST_FAIL)
329#define GREATEST_FAILURE_ABORT() (greatest_info.suite.failed > 0 && GREATEST_FIRST_FAIL())
330
331/* Message-less forms of tests defined below. */
332#define GREATEST_PASS() GREATEST_PASSm(NULL)
333#define GREATEST_FAIL() GREATEST_FAILm(NULL)
334#define GREATEST_SKIP() GREATEST_SKIPm(NULL)
335#define GREATEST_ASSERT(COND) \
336 GREATEST_ASSERTm(#COND, COND)
337#define GREATEST_ASSERT_OR_LONGJMP(COND) \
338 GREATEST_ASSERT_OR_LONGJMPm(#COND, COND)
339#define GREATEST_ASSERT_FALSE(COND) \
340 GREATEST_ASSERT_FALSEm(#COND, COND)
341#define GREATEST_ASSERT_EQ(EXP, GOT) \
342 GREATEST_ASSERT_EQm(#EXP " != " #GOT, EXP, GOT)
343#define GREATEST_ASSERT_EQ_FMT(EXP, GOT, FMT) \
344 GREATEST_ASSERT_EQ_FMTm(#EXP " != " #GOT, EXP, GOT, FMT)
345#define GREATEST_ASSERT_IN_RANGE(EXP, GOT, TOL) \
346 GREATEST_ASSERT_IN_RANGEm(#EXP " != " #GOT " +/- " #TOL, EXP, GOT, TOL)
347#define GREATEST_ASSERT_EQUAL_T(EXP, GOT, TYPE_INFO, UDATA) \
348 GREATEST_ASSERT_EQUAL_Tm(#EXP " != " #GOT, EXP, GOT, TYPE_INFO, UDATA)
349#define GREATEST_ASSERT_STR_EQ(EXP, GOT) \
350 GREATEST_ASSERT_STR_EQm(#EXP " != " #GOT, EXP, GOT)
351
352/* The following forms take an additional message argument first,
353 * to be displayed by the test runner. */
354
355/* Fail if a condition is not true, with message. */
356#define GREATEST_ASSERTm(MSG, COND) \
357 do { \
358 greatest_info.assertions++; \
359 if (!(COND)) { GREATEST_FAILm(MSG); } \
360 } while (0)
361
362/* Fail if a condition is not true, longjmping out of test. */
363#define GREATEST_ASSERT_OR_LONGJMPm(MSG, COND) \
364 do { \
365 greatest_info.assertions++; \
366 if (!(COND)) { GREATEST_FAIL_WITH_LONGJMPm(MSG); } \
367 } while (0)
368
369/* Fail if a condition is not false, with message. */
370#define GREATEST_ASSERT_FALSEm(MSG, COND) \
371 do { \
372 greatest_info.assertions++; \
373 if ((COND)) { GREATEST_FAILm(MSG); } \
374 } while (0)
375
376/* Fail if EXP != GOT (equality comparison by ==). */
377#define GREATEST_ASSERT_EQm(MSG, EXP, GOT) \
378 do { \
379 greatest_info.assertions++; \
380 if ((EXP) != (GOT)) { GREATEST_FAILm(MSG); } \
381 } while (0)
382
383/* Fail if EXP != GOT (equality comparison by ==). */
384#define GREATEST_ASSERT_EQ_FMTm(MSG, EXP, GOT, FMT) \
385 do { \
386 greatest_info.assertions++; \
387 const char *fmt = ( FMT ); \
388 if ((EXP) != (GOT)) { \
389 fprintf(GREATEST_STDOUT, "\nExpected: "); \
390 fprintf(GREATEST_STDOUT, fmt, EXP); \
391 fprintf(GREATEST_STDOUT, "\nGot: "); \
392 fprintf(GREATEST_STDOUT, fmt, GOT); \
393 fprintf(GREATEST_STDOUT, "\n"); \
394 GREATEST_FAILm(MSG); \
395 } \
396 } while (0)
397
398/* Fail if GOT not in range of EXP +|- TOL. */
399#define GREATEST_ASSERT_IN_RANGEm(MSG, EXP, GOT, TOL) \
400 do { \
401 greatest_info.assertions++; \
402 GREATEST_FLOAT exp = (EXP); \
403 GREATEST_FLOAT got = (GOT); \
404 GREATEST_FLOAT tol = (TOL); \
405 if ((exp > got && exp - got > tol) || \
406 (exp < got && got - exp > tol)) { \
407 fprintf(GREATEST_STDOUT, \
408 "\nExpected: " GREATEST_FLOAT_FMT \
409 " +/- " GREATEST_FLOAT_FMT "\n" \
410 "Got: " GREATEST_FLOAT_FMT "\n", \
411 exp, tol, got); \
412 GREATEST_FAILm(MSG); \
413 } \
414 } while (0)
415
416/* Fail if EXP is not equal to GOT, according to strcmp. */
417#define GREATEST_ASSERT_STR_EQm(MSG, EXP, GOT) \
418 do { \
419 GREATEST_ASSERT_EQUAL_Tm(MSG, EXP, GOT, \
420 &greatest_type_info_string, NULL); \
421 } while (0) \
422
423/* Fail if EXP is not equal to GOT, according to a comparison
424 * callback in TYPE_INFO. If they are not equal, optionally use a
425 * print callback in TYPE_INFO to print them. */
426#define GREATEST_ASSERT_EQUAL_Tm(MSG, EXP, GOT, TYPE_INFO, UDATA) \
427 do { \
428 greatest_type_info *type_info = (TYPE_INFO); \
429 greatest_info.assertions++; \
430 if (!greatest_do_assert_equal_t(EXP, GOT, \
431 type_info, UDATA)) { \
432 if (type_info == NULL || type_info->equal == NULL) { \
433 GREATEST_FAILm("type_info->equal callback missing!"); \
434 } else { \
435 GREATEST_FAILm(MSG); \
436 } \
437 } \
438 } while (0) \
439
440/* Pass. */
441#define GREATEST_PASSm(MSG) \
442 do { \
443 greatest_info.msg = MSG; \
444 return GREATEST_TEST_RES_PASS; \
445 } while (0)
446
447/* Fail. */
448#define GREATEST_FAILm(MSG) \
449 do { \
450 greatest_info.fail_file = __FILE__; \
451 greatest_info.fail_line = __LINE__; \
452 greatest_info.msg = MSG; \
453 return GREATEST_TEST_RES_FAIL; \
454 } while (0)
455
456/* Optional GREATEST_FAILm variant that longjmps. */
457#if GREATEST_USE_LONGJMP
458#define GREATEST_FAIL_WITH_LONGJMP() GREATEST_FAIL_WITH_LONGJMPm(NULL)
459#define GREATEST_FAIL_WITH_LONGJMPm(MSG) \
460 do { \
461 greatest_info.fail_file = __FILE__; \
462 greatest_info.fail_line = __LINE__; \
463 greatest_info.msg = MSG; \
464 longjmp(greatest_info.jump_dest, GREATEST_TEST_RES_FAIL); \
465 } while (0)
466#endif
467
468/* Skip the current test. */
469#define GREATEST_SKIPm(MSG) \
470 do { \
471 greatest_info.msg = MSG; \
472 return GREATEST_TEST_RES_SKIP; \
473 } while (0)
474
475/* Check the result of a subfunction using ASSERT, etc. */
476#define GREATEST_CHECK_CALL(RES) \
477 do { \
478 int _check_call_res = RES; \
479 if (_check_call_res != GREATEST_TEST_RES_PASS) { \
480 return _check_call_res; \
481 } \
482 } while (0) \
483
484#if GREATEST_USE_TIME
485#define GREATEST_SET_TIME(NAME) \
486 NAME = clock(); \
487 if (NAME == (clock_t) -1) { \
488 fprintf(GREATEST_STDOUT, \
489 "clock error: %s\n", #NAME); \
490 exit(EXIT_FAILURE); \
491 }
492
493#define GREATEST_CLOCK_DIFF(C1, C2) \
494 fprintf(GREATEST_STDOUT, " (%lu ticks, %.3f sec)", \
495 (long unsigned int) (C2) - (long unsigned int)(C1), \
496 (double)((C2) - (C1)) / (1.0 * (double)CLOCKS_PER_SEC))
497#else
498#define GREATEST_SET_TIME(UNUSED)
499#define GREATEST_CLOCK_DIFF(UNUSED1, UNUSED2)
500#endif
501
502#if GREATEST_USE_LONGJMP
503#define GREATEST_SAVE_CONTEXT() \
504 /* setjmp returns 0 (GREATEST_TEST_RES_PASS) on first call */ \
505 /* so the test runs, then RES_FAIL from FAIL_WITH_LONGJMP. */ \
506 ((greatest_test_res)(setjmp(greatest_info.jump_dest)))
507#else
508#define GREATEST_SAVE_CONTEXT() \
509 /*a no-op, since setjmp/longjmp aren't being used */ \
510 GREATEST_TEST_RES_PASS
511#endif
512
513/* Include several function definitions in the main test file. */
514#define GREATEST_MAIN_DEFS() \
515 \
516/* Is FILTER a subset of NAME? */ \
517static int greatest_name_match(const char *name, \
518 const char *filter) { \
519 size_t offset = 0; \
520 size_t filter_len = strlen(filter); \
521 while (name[offset] != '\0') { \
522 if (name[offset] == filter[0]) { \
523 if (0 == strncmp(&name[offset], filter, filter_len)) { \
524 return 1; \
525 } \
526 } \
527 offset++; \
528 } \
529 \
530 return 0; \
531} \
532 \
533int greatest_pre_test(const char *name) { \
534 if (!GREATEST_LIST_ONLY() \
535 && (!GREATEST_FIRST_FAIL() || greatest_info.suite.failed == 0) \
536 && (greatest_info.test_filter == NULL || \
537 greatest_name_match(name, greatest_info.test_filter))) { \
538 GREATEST_SET_TIME(greatest_info.suite.pre_test); \
539 if (greatest_info.setup) { \
540 greatest_info.setup(greatest_info.setup_udata); \
541 } \
542 return 1; /* test should be run */ \
543 } else { \
544 return 0; /* skipped */ \
545 } \
546} \
547 \
548void greatest_post_test(const char *name, int res) { \
549 GREATEST_SET_TIME(greatest_info.suite.post_test); \
550 if (greatest_info.teardown) { \
551 void *udata = greatest_info.teardown_udata; \
552 greatest_info.teardown(udata); \
553 } \
554 \
555 if (res <= GREATEST_TEST_RES_FAIL) { \
556 greatest_do_fail(name); \
557 } else if (res >= GREATEST_TEST_RES_SKIP) { \
558 greatest_do_skip(name); \
559 } else if (res == GREATEST_TEST_RES_PASS) { \
560 greatest_do_pass(name); \
561 } \
562 greatest_info.suite.tests_run++; \
563 greatest_info.col++; \
564 if (GREATEST_IS_VERBOSE()) { \
565 GREATEST_CLOCK_DIFF(greatest_info.suite.pre_test, \
566 greatest_info.suite.post_test); \
567 fprintf(GREATEST_STDOUT, "\n"); \
568 } else if (greatest_info.col % greatest_info.width == 0) { \
569 fprintf(GREATEST_STDOUT, "\n"); \
570 greatest_info.col = 0; \
571 } \
572 if (GREATEST_STDOUT == stdout) fflush(stdout); \
573} \
574 \
575static void greatest_run_suite(greatest_suite_cb *suite_cb, \
576 const char *suite_name) { \
577 if (greatest_info.suite_filter && \
578 !greatest_name_match(suite_name, greatest_info.suite_filter)) { \
579 return; \
580 } \
581 if (GREATEST_FIRST_FAIL() && greatest_info.failed > 0) { return; } \
582 memset(&greatest_info.suite, 0, sizeof(greatest_info.suite)); \
583 greatest_info.col = 0; \
584 fprintf(GREATEST_STDOUT, "\n* Suite %s:\n", suite_name); \
585 GREATEST_SET_TIME(greatest_info.suite.pre_suite); \
586 suite_cb(); \
587 GREATEST_SET_TIME(greatest_info.suite.post_suite); \
588 if (greatest_info.suite.tests_run > 0) { \
589 fprintf(GREATEST_STDOUT, \
590 "\n%u tests - %u pass, %u fail, %u skipped", \
591 greatest_info.suite.tests_run, \
592 greatest_info.suite.passed, \
593 greatest_info.suite.failed, \
594 greatest_info.suite.skipped); \
595 GREATEST_CLOCK_DIFF(greatest_info.suite.pre_suite, \
596 greatest_info.suite.post_suite); \
597 fprintf(GREATEST_STDOUT, "\n"); \
598 } \
599 greatest_info.setup = NULL; \
600 greatest_info.setup_udata = NULL; \
601 greatest_info.teardown = NULL; \
602 greatest_info.teardown_udata = NULL; \
603 greatest_info.passed += greatest_info.suite.passed; \
604 greatest_info.failed += greatest_info.suite.failed; \
605 greatest_info.skipped += greatest_info.suite.skipped; \
606 greatest_info.tests_run += greatest_info.suite.tests_run; \
607} \
608 \
609void greatest_do_pass(const char *name) { \
610 if (GREATEST_IS_VERBOSE()) { \
611 fprintf(GREATEST_STDOUT, "PASS %s: %s", \
612 name, greatest_info.msg ? greatest_info.msg : ""); \
613 } else { \
614 fprintf(GREATEST_STDOUT, "."); \
615 } \
616 greatest_info.suite.passed++; \
617} \
618 \
619void greatest_do_fail(const char *name) { \
620 if (GREATEST_IS_VERBOSE()) { \
621 fprintf(GREATEST_STDOUT, \
622 "FAIL %s: %s (%s:%u)", \
623 name, greatest_info.msg ? greatest_info.msg : "", \
624 greatest_info.fail_file, greatest_info.fail_line); \
625 } else { \
626 fprintf(GREATEST_STDOUT, "F"); \
627 greatest_info.col++; \
628 /* add linebreak if in line of '.'s */ \
629 if (greatest_info.col != 0) { \
630 fprintf(GREATEST_STDOUT, "\n"); \
631 greatest_info.col = 0; \
632 } \
633 fprintf(GREATEST_STDOUT, "FAIL %s: %s (%s:%u)\n", \
634 name, \
635 greatest_info.msg ? greatest_info.msg : "", \
636 greatest_info.fail_file, greatest_info.fail_line); \
637 } \
638 greatest_info.suite.failed++; \
639} \
640 \
641void greatest_do_skip(const char *name) { \
642 if (GREATEST_IS_VERBOSE()) { \
643 fprintf(GREATEST_STDOUT, "SKIP %s: %s", \
644 name, \
645 greatest_info.msg ? \
646 greatest_info.msg : "" ); \
647 } else { \
648 fprintf(GREATEST_STDOUT, "s"); \
649 } \
650 greatest_info.suite.skipped++; \
651} \
652 \
653int greatest_do_assert_equal_t(const void *exp, const void *got, \
654 greatest_type_info *type_info, void *udata) { \
655 int eq = 0; \
656 if (type_info == NULL || type_info->equal == NULL) { \
657 return 0; \
658 } \
659 eq = type_info->equal(exp, got, udata); \
660 if (!eq) { \
661 if (type_info->print != NULL) { \
662 fprintf(GREATEST_STDOUT, "\nExpected: "); \
663 (void)type_info->print(exp, udata); \
664 fprintf(GREATEST_STDOUT, "\nGot: "); \
665 (void)type_info->print(got, udata); \
666 fprintf(GREATEST_STDOUT, "\n"); \
667 } else { \
668 fprintf(GREATEST_STDOUT, \
669 "GREATEST_ASSERT_EQUAL_T failure at %s:%dn", \
670 greatest_info.fail_file, \
671 greatest_info.fail_line); \
672 } \
673 } \
674 return eq; \
675} \
676 \
677void greatest_usage(const char *name) { \
678 fprintf(GREATEST_STDOUT, \
679 "Usage: %s [-hlfv] [-s SUITE] [-t TEST]\n" \
680 " -h print this Help\n" \
681 " -l List suites and their tests, then exit\n" \
682 " -f Stop runner after first failure\n" \
683 " -v Verbose output\n" \
684 " -s SUITE only run suite named SUITE\n" \
685 " -t TEST only run test named TEST\n", \
686 name); \
687} \
688 \
689int greatest_all_passed() { return (greatest_info.failed == 0); } \
690 \
691void GREATEST_SET_SETUP_CB(greatest_setup_cb *cb, void *udata) { \
692 greatest_info.setup = cb; \
693 greatest_info.setup_udata = udata; \
694} \
695 \
696void GREATEST_SET_TEARDOWN_CB(greatest_teardown_cb *cb, \
697 void *udata) { \
698 greatest_info.teardown = cb; \
699 greatest_info.teardown_udata = udata; \
700} \
701 \
702static int greatest_string_equal_cb(const void *exp, const void *got, \
703 void *udata) { \
704 (void)udata; \
705 return (0 == strcmp((const char *)exp, (const char *)got)); \
706} \
707 \
708static int greatest_string_printf_cb(const void *t, void *udata) { \
709 (void)udata; \
710 return fprintf(GREATEST_STDOUT, "%s", (const char *)t); \
711} \
712 \
713greatest_type_info greatest_type_info_string = { \
714 greatest_string_equal_cb, \
715 greatest_string_printf_cb, \
716}; \
717 \
718greatest_run_info greatest_info
719
720/* Init internals. */
721#define GREATEST_INIT() \
722 do { \
723 memset(&greatest_info, 0, sizeof(greatest_info)); \
724 greatest_info.width = GREATEST_DEFAULT_WIDTH; \
725 GREATEST_SET_TIME(greatest_info.begin); \
726 } while (0) \
727
728/* Handle command-line arguments, etc. */
729#define GREATEST_MAIN_BEGIN() \
730 do { \
731 int i = 0; \
732 GREATEST_INIT(); \
733 for (i = 1; i < argc; i++) { \
734 if (0 == strcmp("-t", argv[i])) { \
735 if (argc <= i + 1) { \
736 greatest_usage(argv[0]); \
737 exit(EXIT_FAILURE); \
738 } \
739 greatest_info.test_filter = argv[i+1]; \
740 i++; \
741 } else if (0 == strcmp("-s", argv[i])) { \
742 if (argc <= i + 1) { \
743 greatest_usage(argv[0]); \
744 exit(EXIT_FAILURE); \
745 } \
746 greatest_info.suite_filter = argv[i+1]; \
747 i++; \
748 } else if (0 == strcmp("-f", argv[i])) { \
749 greatest_info.flags |= GREATEST_FLAG_FIRST_FAIL; \
750 } else if (0 == strcmp("-v", argv[i])) { \
751 greatest_info.flags |= GREATEST_FLAG_VERBOSE; \
752 } else if (0 == strcmp("-l", argv[i])) { \
753 greatest_info.flags |= GREATEST_FLAG_LIST_ONLY; \
754 } else if (0 == strcmp("-h", argv[i])) { \
755 greatest_usage(argv[0]); \
756 exit(EXIT_SUCCESS); \
757 } else { \
758 fprintf(GREATEST_STDOUT, \
759 "Unknown argument '%s'\n", argv[i]); \
760 greatest_usage(argv[0]); \
761 exit(EXIT_FAILURE); \
762 } \
763 } \
764 } while (0)
765
766/* Report passes, failures, skipped tests, the number of
767 * assertions, and the overall run time. */
768#define GREATEST_REPORT() \
769 do { \
770 if (!GREATEST_LIST_ONLY()) { \
771 GREATEST_SET_TIME(greatest_info.end); \
772 fprintf(GREATEST_STDOUT, \
773 "\nTotal: %u tests", greatest_info.tests_run); \
774 GREATEST_CLOCK_DIFF(greatest_info.begin, \
775 greatest_info.end); \
776 fprintf(GREATEST_STDOUT, ", %u assertions\n", \
777 greatest_info.assertions); \
778 fprintf(GREATEST_STDOUT, \
779 "Pass: %u, fail: %u, skip: %u.\n", \
780 greatest_info.passed, \
781 greatest_info.failed, greatest_info.skipped); \
782 } \
783 } while (0)
784
785/* Report results, exit with exit status based on results. */
786#define GREATEST_MAIN_END() \
787 do { \
788 GREATEST_REPORT(); \
789 return (greatest_all_passed() ? EXIT_SUCCESS : EXIT_FAILURE); \
790 } while (0)
791
792/* Make abbreviations without the GREATEST_ prefix for the
793 * most commonly used symbols. */
794#if GREATEST_USE_ABBREVS
795#define TEST GREATEST_TEST
796#define SUITE GREATEST_SUITE
797#define RUN_TEST GREATEST_RUN_TEST
798#define RUN_TEST1 GREATEST_RUN_TEST1
799#define RUN_SUITE GREATEST_RUN_SUITE
800#define ASSERT GREATEST_ASSERT
801#define ASSERTm GREATEST_ASSERTm
802#define ASSERT_FALSE GREATEST_ASSERT_FALSE
803#define ASSERT_EQ GREATEST_ASSERT_EQ
804#define ASSERT_EQ_FMT GREATEST_ASSERT_EQ_FMT
805#define ASSERT_IN_RANGE GREATEST_ASSERT_IN_RANGE
806#define ASSERT_EQUAL_T GREATEST_ASSERT_EQUAL_T
807#define ASSERT_STR_EQ GREATEST_ASSERT_STR_EQ
808#define ASSERT_FALSEm GREATEST_ASSERT_FALSEm
809#define ASSERT_EQm GREATEST_ASSERT_EQm
810#define ASSERT_EQ_FMTm GREATEST_ASSERT_EQ_FMTm
811#define ASSERT_IN_RANGEm GREATEST_ASSERT_IN_RANGEm
812#define ASSERT_EQUAL_Tm GREATEST_ASSERT_EQUAL_Tm
813#define ASSERT_STR_EQm GREATEST_ASSERT_STR_EQm
814#define PASS GREATEST_PASS
815#define FAIL GREATEST_FAIL
816#define SKIP GREATEST_SKIP
817#define PASSm GREATEST_PASSm
818#define FAILm GREATEST_FAILm
819#define SKIPm GREATEST_SKIPm
820#define SET_SETUP GREATEST_SET_SETUP_CB
821#define SET_TEARDOWN GREATEST_SET_TEARDOWN_CB
822#define CHECK_CALL GREATEST_CHECK_CALL
823
824#ifdef GREATEST_VA_ARGS
825#define RUN_TESTp GREATEST_RUN_TESTp
826#endif
827
828#if GREATEST_USE_LONGJMP
829#define ASSERT_OR_LONGJMP GREATEST_ASSERT_OR_LONGJMP
830#define ASSERT_OR_LONGJMPm GREATEST_ASSERT_OR_LONGJMPm
831#define FAIL_WITH_LONGJMP GREATEST_FAIL_WITH_LONGJMP
832#define FAIL_WITH_LONGJMPm GREATEST_FAIL_WITH_LONGJMPm
833#endif
834
835#endif /* USE_ABBREVS */
836
837#endif
greatest_type_info greatest_type_info_string
void GREATEST_SET_SETUP_CB(greatest_setup_cb *cb, void *udata)
void GREATEST_SET_TEARDOWN_CB(greatest_teardown_cb *cb, void *udata)
#define GREATEST_MAIN_END()
Definition greatest.h:786
#define RUN_SUITE
Definition greatest.h:799
#define RUN_TEST
Definition greatest.h:797
#define GREATEST_REPORT()
Definition greatest.h:768
void greatest_do_skip(const char *name)
greatest_test_res
Definition greatest.h:277
@ GREATEST_TEST_RES_SKIP
Definition greatest.h:280
@ GREATEST_TEST_RES_FAIL
Definition greatest.h:279
@ GREATEST_TEST_RES_PASS
Definition greatest.h:278
#define SET_TEARDOWN
Definition greatest.h:821
int greatest_equal_cb(const void *exp, const void *got, void *udata)
Definition greatest.h:162
#define GREATEST_INIT()
Definition greatest.h:721
int greatest_do_assert_equal_t(const void *exp, const void *got, greatest_type_info *type_info, void *udata)
int greatest_all_passed(void)
void greatest_suite_cb(void)
Definition greatest.h:152
GREATEST_FLAG
Definition greatest.h:179
@ GREATEST_FLAG_FIRST_FAIL
Definition greatest.h:181
@ GREATEST_FLAG_VERBOSE
Definition greatest.h:180
@ GREATEST_FLAG_LIST_ONLY
Definition greatest.h:182
void greatest_do_fail(const char *name)
#define GREATEST_MAIN_BEGIN()
Definition greatest.h:729
void greatest_setup_cb(void *udata)
Definition greatest.h:156
int greatest_printf_cb(const void *t, void *udata)
Definition greatest.h:167
void greatest_teardown_cb(void *udata)
Definition greatest.h:157
#define TEST
Definition greatest.h:795
#define PASS
Definition greatest.h:814
void greatest_usage(const char *name)
#define SUITE
Definition greatest.h:796
int greatest_pre_test(const char *name)
void greatest_do_pass(const char *name)
#define SET_SETUP
Definition greatest.h:820
greatest_run_info greatest_info
#define GREATEST_MAIN_DEFS()
Definition greatest.h:514
void greatest_post_test(const char *name, int res)
int main(void)
The application entry point.
Definition main.c:271
Definition greatest.h:186
jmp_buf jump_dest
Definition greatest.h:225
char * suite_filter
Definition greatest.h:215
unsigned int width
Definition greatest.h:212
char * test_filter
Definition greatest.h:216
greatest_setup_cb * setup
Definition greatest.h:205
clock_t end
Definition greatest.h:221
unsigned int passed
Definition greatest.h:191
greatest_suite_info suite
Definition greatest.h:197
const char * fail_file
Definition greatest.h:200
void * setup_udata
Definition greatest.h:206
unsigned int failed
Definition greatest.h:192
void * teardown_udata
Definition greatest.h:208
clock_t begin
Definition greatest.h:220
unsigned int assertions
Definition greatest.h:194
const char * msg
Definition greatest.h:202
unsigned int col
Definition greatest.h:211
unsigned int skipped
Definition greatest.h:193
unsigned int tests_run
Definition greatest.h:188
greatest_teardown_cb * teardown
Definition greatest.h:207
unsigned int flags
Definition greatest.h:187
unsigned int fail_line
Definition greatest.h:201
Definition greatest.h:136
clock_t post_suite
Definition greatest.h:145
clock_t pre_test
Definition greatest.h:146
unsigned int tests_run
Definition greatest.h:137
unsigned int failed
Definition greatest.h:139
unsigned int passed
Definition greatest.h:138
clock_t post_test
Definition greatest.h:147
clock_t pre_suite
Definition greatest.h:144
unsigned int skipped
Definition greatest.h:140
Definition greatest.h:171
greatest_printf_cb * print
Definition greatest.h:173
greatest_equal_cb * equal
Definition greatest.h:172