• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Miniature re-implementation of the "check" library.
2  *
3  * This is intended to support just enough of check to run the Expat
4  * tests.  This interface is based entirely on the portion of the
5  * check library being used.
6  *
7  * This is *source* compatible, but not necessary *link* compatible.
8  */
9 
10 #ifdef __cplusplus
11 extern "C" {
12 #endif
13 
14 #define CK_NOFORK 0
15 #define CK_FORK   1
16 
17 #define CK_SILENT  0
18 #define CK_NORMAL  1
19 #define CK_VERBOSE 2
20 
21 /* Workaround for Microsoft's compiler and Tru64 Unix systems where the
22    C compiler has a working __func__, but the C++ compiler only has a
23    working __FUNCTION__.  This could be fixed in configure.in, but it's
24    not worth it right now. */
25 #if defined (_MSC_VER) || (defined(__osf__) && defined(__cplusplus))
26 #define __func__ __FUNCTION__
27 #endif
28 
29 /* ISO C90 does not support '__func__' predefined identifier */
30 #if defined(__STDC_VERSION__) && (__STDC_VERSION__ < 199901)
31 # define __func__ "(unknown)"
32 #endif
33 
34 #define START_TEST(testname) static void testname(void) { \
35     _check_set_test_info(__func__, __FILE__, __LINE__);   \
36     {
37 #define END_TEST } }
38 
39 #define fail(msg)  _fail_unless(0, __FILE__, __LINE__, msg)
40 
41 typedef void (*tcase_setup_function)(void);
42 typedef void (*tcase_teardown_function)(void);
43 typedef void (*tcase_test_function)(void);
44 
45 typedef struct SRunner SRunner;
46 typedef struct Suite Suite;
47 typedef struct TCase TCase;
48 
49 struct SRunner {
50     Suite *suite;
51     int nchecks;
52     int nfailures;
53 };
54 
55 struct Suite {
56     const char *name;
57     TCase *tests;
58 };
59 
60 struct TCase {
61     const char *name;
62     tcase_setup_function setup;
63     tcase_teardown_function teardown;
64     tcase_test_function *tests;
65     int ntests;
66     int allocated;
67     TCase *next_tcase;
68 };
69 
70 
71 /* Internal helper. */
72 void _check_set_test_info(char const *function,
73                           char const *filename, int lineno);
74 
75 
76 /*
77  * Prototypes for the actual implementation.
78  */
79 
80 void _fail_unless(int condition, const char *file, int line, const char *msg);
81 Suite *suite_create(const char *name);
82 TCase *tcase_create(const char *name);
83 void suite_add_tcase(Suite *suite, TCase *tc);
84 void tcase_add_checked_fixture(TCase *,
85                                tcase_setup_function,
86                                tcase_teardown_function);
87 void tcase_add_test(TCase *tc, tcase_test_function test);
88 SRunner *srunner_create(Suite *suite);
89 void srunner_run_all(SRunner *runner, int verbosity);
90 int srunner_ntests_failed(SRunner *runner);
91 void srunner_free(SRunner *runner);
92 
93 #ifdef __cplusplus
94 }
95 #endif
96