xref: /freebsd/contrib/expat/tests/minicheck.h (revision 9a5aaa97cbae024f90bb626f78c3dbde28653c58)
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-2024 Sebastian Pipping <sebastian@pipping.org>
18    Copyright (c) 2022      Rhodri James <rhodri@wildebeest.org.uk>
19    Copyright (c) 2023-2024 Sony Corporation / Snild Dolkow <snild@sony.com>
20    Licensed under the MIT license:
21 
22    Permission is  hereby granted,  free of charge,  to any  person obtaining
23    a  copy  of  this  software   and  associated  documentation  files  (the
24    "Software"),  to  deal in  the  Software  without restriction,  including
25    without  limitation the  rights  to use,  copy,  modify, merge,  publish,
26    distribute, sublicense, and/or sell copies of the Software, and to permit
27    persons  to whom  the Software  is  furnished to  do so,  subject to  the
28    following conditions:
29 
30    The above copyright  notice and this permission notice  shall be included
31    in all copies or substantial portions of the Software.
32 
33    THE  SOFTWARE  IS  PROVIDED  "AS  IS",  WITHOUT  WARRANTY  OF  ANY  KIND,
34    EXPRESS  OR IMPLIED,  INCLUDING  BUT  NOT LIMITED  TO  THE WARRANTIES  OF
35    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
36    NO EVENT SHALL THE AUTHORS OR  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
37    DAMAGES OR  OTHER LIABILITY, WHETHER  IN AN  ACTION OF CONTRACT,  TORT OR
38    OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
39    USE OR OTHER DEALINGS IN THE SOFTWARE.
40 */
41 
42 #ifdef __cplusplus
43 extern "C" {
44 #endif
45 
46 #ifndef XML_MINICHECK_H
47 #  define XML_MINICHECK_H
48 
49 #  define CK_NOFORK 0
50 #  define CK_FORK 1
51 
52 #  define CK_SILENT 0
53 #  define CK_NORMAL 1
54 #  define CK_VERBOSE 2
55 
56 /* Workaround for Microsoft's compiler and Tru64 Unix systems where the
57    C compiler has a working __func__, but the C++ compiler only has a
58    working __FUNCTION__.  This could be fixed in configure.in, but it's
59    not worth it right now. */
60 #  if defined(_MSC_VER) || (defined(__osf__) && defined(__cplusplus))
61 #    define __func__ __FUNCTION__
62 #  endif
63 
64 /* PRINTF_LIKE has two effects:
65     1. Make clang's -Wformat-nonliteral stop warning about non-literal format
66        strings in annotated functions' code.
67     2. Make both clang and gcc's -Wformat-nonliteral warn about *callers* of
68        the annotated function that use a non-literal format string.
69 */
70 #  if defined(__GNUC__)
71 #    define PRINTF_LIKE(fmtpos, argspos)                                       \
72       __attribute__((format(printf, fmtpos, argspos)))
73 #  else
74 #    define PRINTF_LIKE(fmtpos, argspos)
75 #  endif
76 
77 #  define START_TEST(testname)                                                 \
78     static void testname(void) {                                               \
79       _check_set_test_info(__func__, __FILE__, __LINE__);                      \
80       {
81 #  define END_TEST                                                             \
82     }                                                                          \
83     }
84 
85 void PRINTF_LIKE(1, 2) set_subtest(char const *fmt, ...);
86 
87 #  define fail(msg) _fail(__FILE__, __LINE__, msg)
88 #  define assert_true(cond)                                                    \
89     do {                                                                       \
90       if (! (cond)) {                                                          \
91         _fail(__FILE__, __LINE__, "check failed: " #cond);                     \
92       }                                                                        \
93     } while (0)
94 
95 typedef void (*tcase_setup_function)(void);
96 typedef void (*tcase_teardown_function)(void);
97 typedef void (*tcase_test_function)(void);
98 
99 typedef struct SRunner SRunner;
100 typedef struct Suite Suite;
101 typedef struct TCase TCase;
102 
103 struct SRunner {
104   Suite *suite;
105   int nchecks;
106   int nfailures;
107 };
108 
109 struct Suite {
110   const char *name;
111   TCase *tests;
112 };
113 
114 struct TCase {
115   const char *name;
116   tcase_setup_function setup;
117   tcase_teardown_function teardown;
118   tcase_test_function *tests;
119   int ntests;
120   int allocated;
121   TCase *next_tcase;
122 };
123 
124 /* Internal helper. */
125 void _check_set_test_info(char const *function, char const *filename,
126                           int lineno);
127 
128 /*
129  * Prototypes for the actual implementation.
130  */
131 
132 #  if defined(__GNUC__)
133 __attribute__((noreturn))
134 #  endif
135 void
136 _fail(const char *file, int line, const char *msg);
137 Suite *suite_create(const char *name);
138 TCase *tcase_create(const char *name);
139 void suite_add_tcase(Suite *suite, TCase *tc);
140 void tcase_add_checked_fixture(TCase *tc, tcase_setup_function setup,
141                                tcase_teardown_function teardown);
142 void tcase_add_test(TCase *tc, tcase_test_function test);
143 SRunner *srunner_create(Suite *suite);
144 void srunner_run_all(SRunner *runner, const char *context, int verbosity);
145 void srunner_summarize(SRunner *runner, int verbosity);
146 int srunner_ntests_failed(SRunner *runner);
147 void srunner_free(SRunner *runner);
148 
149 #endif /* XML_MINICHECK_H */
150 
151 #ifdef __cplusplus
152 }
153 #endif
154