xref: /freebsd/contrib/expat/tests/minicheck.h (revision c7b67985633c408cae69703ca443cbfd84d326a8)
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-2026 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    SPDX-License-Identifier: MIT
42 */
43 
44 #ifndef XML_MINICHECK_H
45 #define XML_MINICHECK_H
46 
47 #define CK_NOFORK 0
48 #define CK_FORK 1
49 
50 #define CK_SILENT 0
51 #define CK_NORMAL 1
52 #define CK_VERBOSE 2
53 
54 /* PRINTF_LIKE has two effects:
55     1. Make clang's -Wformat-nonliteral stop warning about non-literal format
56        strings in annotated functions' code.
57     2. Make both clang and gcc's -Wformat-nonliteral warn about *callers* of
58        the annotated function that use a non-literal format string.
59 */
60 #if defined(__GNUC__)
61 #  define PRINTF_LIKE(fmtpos, argspos)                                         \
62     __attribute__((format(printf, fmtpos, argspos)))
63 #else
64 #  define PRINTF_LIKE(fmtpos, argspos)
65 #endif
66 
67 #define START_TEST(testname)                                                   \
68   static void testname(void) {                                                 \
69     _check_set_test_info(__func__, __FILE__, __LINE__);                        \
70     {
71 #define END_TEST                                                               \
72   }                                                                            \
73   }
74 
75 void PRINTF_LIKE(1, 2) set_subtest(char const *fmt, ...);
76 
77 #define fail(msg) _fail(__FILE__, __LINE__, msg)
78 #define assert_true(cond)                                                      \
79   do {                                                                         \
80     if (! (cond)) {                                                            \
81       _fail(__FILE__, __LINE__, "check failed: " #cond);                       \
82     }                                                                          \
83   } while (0)
84 
85 typedef void (*tcase_setup_function)(void);
86 typedef void (*tcase_teardown_function)(void);
87 typedef void (*tcase_test_function)(void);
88 
89 typedef struct SRunner SRunner;
90 typedef struct Suite Suite;
91 typedef struct TCase TCase;
92 
93 struct SRunner {
94   Suite *suite;
95   int nchecks;
96   int nfailures;
97 };
98 
99 struct Suite {
100   const char *name;
101   TCase *tests;
102 };
103 
104 struct TCase {
105   const char *name;
106   tcase_setup_function setup;
107   tcase_teardown_function teardown;
108   tcase_test_function *tests;
109   int ntests;
110   int allocated;
111   TCase *next_tcase;
112 };
113 
114 /* Internal helper. */
115 void _check_set_test_info(char const *function, char const *filename,
116                           int lineno);
117 
118 /*
119  * Prototypes for the actual implementation.
120  */
121 
122 #if defined(__has_attribute)
123 #  if __has_attribute(noreturn)
124 __attribute__((noreturn))
125 #  endif
126 #endif
127 void _fail(const char *file, int line, const char *msg);
128 Suite *suite_create(const char *name);
129 TCase *tcase_create(const char *name);
130 void suite_add_tcase(Suite *suite, TCase *tc);
131 void tcase_add_checked_fixture(TCase *tc, tcase_setup_function setup,
132                                tcase_teardown_function teardown);
133 void tcase_add_test(TCase *tc, tcase_test_function test);
134 SRunner *srunner_create(Suite *suite);
135 void srunner_run_all(SRunner *runner, const char *context, int verbosity);
136 void srunner_summarize(SRunner *runner, int verbosity);
137 int srunner_ntests_failed(SRunner *runner);
138 void srunner_free(SRunner *runner);
139 
140 #endif /* XML_MINICHECK_H */
141