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