xref: /freebsd/contrib/expat/tests/minicheck.h (revision c66ec88fed842fbaad62c30d510644ceb7bd2d71)
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                         / _ \\  /| '_ \ / _` | __|
11                        |  __//  \| |_) | (_| | |_
12                         \___/_/\_\ .__/ \__,_|\__|
13                                  |_| XML parser
14 
15    Copyright (c) 1997-2000 Thai Open Source Software Center Ltd
16    Copyright (c) 2000-2017 Expat development team
17    Licensed under the MIT license:
18 
19    Permission is  hereby granted,  free of charge,  to any  person obtaining
20    a  copy  of  this  software   and  associated  documentation  files  (the
21    "Software"),  to  deal in  the  Software  without restriction,  including
22    without  limitation the  rights  to use,  copy,  modify, merge,  publish,
23    distribute, sublicense, and/or sell copies of the Software, and to permit
24    persons  to whom  the Software  is  furnished to  do so,  subject to  the
25    following conditions:
26 
27    The above copyright  notice and this permission notice  shall be included
28    in all copies or substantial portions of the Software.
29 
30    THE  SOFTWARE  IS  PROVIDED  "AS  IS",  WITHOUT  WARRANTY  OF  ANY  KIND,
31    EXPRESS  OR IMPLIED,  INCLUDING  BUT  NOT LIMITED  TO  THE WARRANTIES  OF
32    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
33    NO EVENT SHALL THE AUTHORS OR  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
34    DAMAGES OR  OTHER LIABILITY, WHETHER  IN AN  ACTION OF CONTRACT,  TORT OR
35    OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
36    USE OR OTHER DEALINGS IN THE SOFTWARE.
37 */
38 
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42 
43 #define CK_NOFORK 0
44 #define CK_FORK 1
45 
46 #define CK_SILENT 0
47 #define CK_NORMAL 1
48 #define CK_VERBOSE 2
49 
50 /* Workaround for Microsoft's compiler and Tru64 Unix systems where the
51    C compiler has a working __func__, but the C++ compiler only has a
52    working __FUNCTION__.  This could be fixed in configure.in, but it's
53    not worth it right now. */
54 #if defined(_MSC_VER) || (defined(__osf__) && defined(__cplusplus))
55 #  define __func__ __FUNCTION__
56 #endif
57 
58 #define START_TEST(testname)                                                   \
59   static void testname(void) {                                                 \
60     _check_set_test_info(__func__, __FILE__, __LINE__);                        \
61     {
62 #define END_TEST                                                               \
63   }                                                                            \
64   }
65 
66 #define fail(msg) _fail_unless(0, __FILE__, __LINE__, msg)
67 
68 typedef void (*tcase_setup_function)(void);
69 typedef void (*tcase_teardown_function)(void);
70 typedef void (*tcase_test_function)(void);
71 
72 typedef struct SRunner SRunner;
73 typedef struct Suite Suite;
74 typedef struct TCase TCase;
75 
76 struct SRunner {
77   Suite *suite;
78   int nchecks;
79   int nfailures;
80 };
81 
82 struct Suite {
83   const char *name;
84   TCase *tests;
85 };
86 
87 struct TCase {
88   const char *name;
89   tcase_setup_function setup;
90   tcase_teardown_function teardown;
91   tcase_test_function *tests;
92   int ntests;
93   int allocated;
94   TCase *next_tcase;
95 };
96 
97 /* Internal helper. */
98 void _check_set_test_info(char const *function, char const *filename,
99                           int lineno);
100 
101 /*
102  * Prototypes for the actual implementation.
103  */
104 
105 void _fail_unless(int condition, const char *file, int line, const char *msg);
106 Suite *suite_create(const char *name);
107 TCase *tcase_create(const char *name);
108 void suite_add_tcase(Suite *suite, TCase *tc);
109 void tcase_add_checked_fixture(TCase *, tcase_setup_function,
110                                tcase_teardown_function);
111 void tcase_add_test(TCase *tc, tcase_test_function test);
112 SRunner *srunner_create(Suite *suite);
113 void srunner_run_all(SRunner *runner, int verbosity);
114 int srunner_ntests_failed(SRunner *runner);
115 void srunner_free(SRunner *runner);
116 
117 #ifdef __cplusplus
118 }
119 #endif
120