xref: /freebsd/contrib/expat/tests/minicheck.h (revision fe9278888fd4414abe2d922e469cf608005f4c65)
1220ed979SColeman Kane /* Miniature re-implementation of the "check" library.
20a48773fSEric van Gyzen 
30a48773fSEric van Gyzen    This is intended to support just enough of check to run the Expat
40a48773fSEric van Gyzen    tests.  This interface is based entirely on the portion of the
50a48773fSEric van Gyzen    check library being used.
60a48773fSEric van Gyzen 
70a48773fSEric van Gyzen    This is *source* compatible, but not necessary *link* compatible.
80a48773fSEric van Gyzen                             __  __            _
90a48773fSEric van Gyzen                          ___\ \/ /_ __   __ _| |_
100a48773fSEric van Gyzen                         / _ \\  /| '_ \ / _` | __|
110a48773fSEric van Gyzen                        |  __//  \| |_) | (_| | |_
120a48773fSEric van Gyzen                         \___/_/\_\ .__/ \__,_|\__|
130a48773fSEric van Gyzen                                  |_| XML parser
140a48773fSEric van Gyzen 
15cc68614dSXin LI    Copyright (c) 2004-2006 Fred L. Drake, Jr. <fdrake@users.sourceforge.net>
16cc68614dSXin LI    Copyright (c) 2006-2012 Karl Waclawek <karl@waclawek.net>
17*fe927888SPhilip Paeps    Copyright (c) 2016-2025 Sebastian Pipping <sebastian@pipping.org>
184543ef51SXin LI    Copyright (c) 2022      Rhodri James <rhodri@wildebeest.org.uk>
194543ef51SXin LI    Copyright (c) 2023-2024 Sony Corporation / Snild Dolkow <snild@sony.com>
200a48773fSEric van Gyzen    Licensed under the MIT license:
210a48773fSEric van Gyzen 
220a48773fSEric van Gyzen    Permission is  hereby granted,  free of charge,  to any  person obtaining
230a48773fSEric van Gyzen    a  copy  of  this  software   and  associated  documentation  files  (the
240a48773fSEric van Gyzen    "Software"),  to  deal in  the  Software  without restriction,  including
250a48773fSEric van Gyzen    without  limitation the  rights  to use,  copy,  modify, merge,  publish,
260a48773fSEric van Gyzen    distribute, sublicense, and/or sell copies of the Software, and to permit
270a48773fSEric van Gyzen    persons  to whom  the Software  is  furnished to  do so,  subject to  the
280a48773fSEric van Gyzen    following conditions:
290a48773fSEric van Gyzen 
300a48773fSEric van Gyzen    The above copyright  notice and this permission notice  shall be included
310a48773fSEric van Gyzen    in all copies or substantial portions of the Software.
320a48773fSEric van Gyzen 
330a48773fSEric van Gyzen    THE  SOFTWARE  IS  PROVIDED  "AS  IS",  WITHOUT  WARRANTY  OF  ANY  KIND,
340a48773fSEric van Gyzen    EXPRESS  OR IMPLIED,  INCLUDING  BUT  NOT LIMITED  TO  THE WARRANTIES  OF
350a48773fSEric van Gyzen    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
360a48773fSEric van Gyzen    NO EVENT SHALL THE AUTHORS OR  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
370a48773fSEric van Gyzen    DAMAGES OR  OTHER LIABILITY, WHETHER  IN AN  ACTION OF CONTRACT,  TORT OR
380a48773fSEric van Gyzen    OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
390a48773fSEric van Gyzen    USE OR OTHER DEALINGS IN THE SOFTWARE.
40220ed979SColeman Kane */
41220ed979SColeman Kane 
42220ed979SColeman Kane #ifdef __cplusplus
43220ed979SColeman Kane extern "C" {
44220ed979SColeman Kane #endif
45220ed979SColeman Kane 
464543ef51SXin LI #ifndef XML_MINICHECK_H
474543ef51SXin LI #  define XML_MINICHECK_H
484543ef51SXin LI 
49220ed979SColeman Kane #  define CK_NOFORK 0
50220ed979SColeman Kane #  define CK_FORK 1
51220ed979SColeman Kane 
52220ed979SColeman Kane #  define CK_SILENT 0
53220ed979SColeman Kane #  define CK_NORMAL 1
54220ed979SColeman Kane #  define CK_VERBOSE 2
55220ed979SColeman Kane 
56e3466a89SXin LI /* Workaround for Microsoft's compiler and Tru64 Unix systems where the
57e3466a89SXin LI    C compiler has a working __func__, but the C++ compiler only has a
58e3466a89SXin LI    working __FUNCTION__.  This could be fixed in configure.in, but it's
59e3466a89SXin LI    not worth it right now. */
60e3466a89SXin LI #  if defined(_MSC_VER) || (defined(__osf__) && defined(__cplusplus))
61220ed979SColeman Kane #    define __func__ __FUNCTION__
62220ed979SColeman Kane #  endif
63220ed979SColeman Kane 
644543ef51SXin LI /* PRINTF_LIKE has two effects:
654543ef51SXin LI     1. Make clang's -Wformat-nonliteral stop warning about non-literal format
664543ef51SXin LI        strings in annotated functions' code.
674543ef51SXin LI     2. Make both clang and gcc's -Wformat-nonliteral warn about *callers* of
684543ef51SXin LI        the annotated function that use a non-literal format string.
694543ef51SXin LI */
704543ef51SXin LI #  if defined(__GNUC__)
714543ef51SXin LI #    define PRINTF_LIKE(fmtpos, argspos)                                       \
724543ef51SXin LI       __attribute__((format(printf, fmtpos, argspos)))
734543ef51SXin LI #  else
744543ef51SXin LI #    define PRINTF_LIKE(fmtpos, argspos)
754543ef51SXin LI #  endif
764543ef51SXin LI 
776b2c1e49SXin LI #  define START_TEST(testname)                                                 \
786b2c1e49SXin LI     static void testname(void) {                                               \
79220ed979SColeman Kane       _check_set_test_info(__func__, __FILE__, __LINE__);                      \
80220ed979SColeman Kane       {
816b2c1e49SXin LI #  define END_TEST                                                             \
826b2c1e49SXin LI     }                                                                          \
836b2c1e49SXin LI     }
84220ed979SColeman Kane 
854543ef51SXin LI void PRINTF_LIKE(1, 2) set_subtest(char const *fmt, ...);
864543ef51SXin LI 
874543ef51SXin LI #  define fail(msg) _fail(__FILE__, __LINE__, msg)
884543ef51SXin LI #  define assert_true(cond)                                                    \
894543ef51SXin LI     do {                                                                       \
904543ef51SXin LI       if (! (cond)) {                                                          \
914543ef51SXin LI         _fail(__FILE__, __LINE__, "check failed: " #cond);                     \
924543ef51SXin LI       }                                                                        \
934543ef51SXin LI     } while (0)
94220ed979SColeman Kane 
95220ed979SColeman Kane typedef void (*tcase_setup_function)(void);
96220ed979SColeman Kane typedef void (*tcase_teardown_function)(void);
97220ed979SColeman Kane typedef void (*tcase_test_function)(void);
98220ed979SColeman Kane 
99220ed979SColeman Kane typedef struct SRunner SRunner;
100220ed979SColeman Kane typedef struct Suite Suite;
101220ed979SColeman Kane typedef struct TCase TCase;
102220ed979SColeman Kane 
103220ed979SColeman Kane struct SRunner {
104220ed979SColeman Kane   Suite *suite;
105220ed979SColeman Kane   int nchecks;
106220ed979SColeman Kane   int nfailures;
107220ed979SColeman Kane };
108220ed979SColeman Kane 
109220ed979SColeman Kane struct Suite {
110be8aff81SXin LI   const char *name;
111220ed979SColeman Kane   TCase *tests;
112220ed979SColeman Kane };
113220ed979SColeman Kane 
114220ed979SColeman Kane struct TCase {
115be8aff81SXin LI   const char *name;
116220ed979SColeman Kane   tcase_setup_function setup;
117220ed979SColeman Kane   tcase_teardown_function teardown;
118220ed979SColeman Kane   tcase_test_function *tests;
119220ed979SColeman Kane   int ntests;
120220ed979SColeman Kane   int allocated;
121220ed979SColeman Kane   TCase *next_tcase;
122220ed979SColeman Kane };
123220ed979SColeman Kane 
124220ed979SColeman Kane /* Internal helper. */
1256b2c1e49SXin LI void _check_set_test_info(char const *function, char const *filename,
1266b2c1e49SXin LI                           int lineno);
127220ed979SColeman Kane 
128220ed979SColeman Kane /*
129220ed979SColeman Kane  * Prototypes for the actual implementation.
130220ed979SColeman Kane  */
131220ed979SColeman Kane 
132*fe927888SPhilip Paeps #  if defined(__has_attribute)
133*fe927888SPhilip Paeps #    if __has_attribute(noreturn)
1344543ef51SXin LI __attribute__((noreturn))
1354543ef51SXin LI #    endif
136*fe927888SPhilip Paeps #  endif
1374543ef51SXin LI void
1384543ef51SXin LI _fail(const char *file, int line, const char *msg);
139be8aff81SXin LI Suite *suite_create(const char *name);
140be8aff81SXin LI TCase *tcase_create(const char *name);
141220ed979SColeman Kane void suite_add_tcase(Suite *suite, TCase *tc);
1424543ef51SXin LI void tcase_add_checked_fixture(TCase *tc, tcase_setup_function setup,
1434543ef51SXin LI                                tcase_teardown_function teardown);
144220ed979SColeman Kane void tcase_add_test(TCase *tc, tcase_test_function test);
145220ed979SColeman Kane SRunner *srunner_create(Suite *suite);
1464543ef51SXin LI void srunner_run_all(SRunner *runner, const char *context, int verbosity);
1474543ef51SXin LI void srunner_summarize(SRunner *runner, int verbosity);
148220ed979SColeman Kane int srunner_ntests_failed(SRunner *runner);
149220ed979SColeman Kane void srunner_free(SRunner *runner);
150220ed979SColeman Kane 
1514543ef51SXin LI #endif /* XML_MINICHECK_H */
1524543ef51SXin LI 
153220ed979SColeman Kane #ifdef __cplusplus
154220ed979SColeman Kane }
155220ed979SColeman Kane #endif
156