xref: /openbsd-src/lib/libexpat/tests/minicheck.h (revision bd8f1dc3b0e01803a74947836eef57849c13acb0)
133ab7b2bSbluhm /* Miniature re-implementation of the "check" library.
22e724bc9Sbluhm 
32e724bc9Sbluhm    This is intended to support just enough of check to run the Expat
42e724bc9Sbluhm    tests.  This interface is based entirely on the portion of the
52e724bc9Sbluhm    check library being used.
62e724bc9Sbluhm 
72e724bc9Sbluhm    This is *source* compatible, but not necessary *link* compatible.
82e724bc9Sbluhm                             __  __            _
92e724bc9Sbluhm                          ___\ \/ /_ __   __ _| |_
102e724bc9Sbluhm                         / _ \\  /| '_ \ / _` | __|
112e724bc9Sbluhm                        |  __//  \| |_) | (_| | |_
122e724bc9Sbluhm                         \___/_/\_\ .__/ \__,_|\__|
132e724bc9Sbluhm                                  |_| XML parser
142e724bc9Sbluhm 
1508819b41Sbluhm    Copyright (c) 2004-2006 Fred L. Drake, Jr. <fdrake@users.sourceforge.net>
1608819b41Sbluhm    Copyright (c) 2006-2012 Karl Waclawek <karl@waclawek.net>
17*bd8f1dc3Sbluhm    Copyright (c) 2016-2024 Sebastian Pipping <sebastian@pipping.org>
18*bd8f1dc3Sbluhm    Copyright (c) 2022      Rhodri James <rhodri@wildebeest.org.uk>
19*bd8f1dc3Sbluhm    Copyright (c) 2023-2024 Sony Corporation / Snild Dolkow <snild@sony.com>
202e724bc9Sbluhm    Licensed under the MIT license:
212e724bc9Sbluhm 
222e724bc9Sbluhm    Permission is  hereby granted,  free of charge,  to any  person obtaining
232e724bc9Sbluhm    a  copy  of  this  software   and  associated  documentation  files  (the
242e724bc9Sbluhm    "Software"),  to  deal in  the  Software  without restriction,  including
252e724bc9Sbluhm    without  limitation the  rights  to use,  copy,  modify, merge,  publish,
262e724bc9Sbluhm    distribute, sublicense, and/or sell copies of the Software, and to permit
272e724bc9Sbluhm    persons  to whom  the Software  is  furnished to  do so,  subject to  the
282e724bc9Sbluhm    following conditions:
292e724bc9Sbluhm 
302e724bc9Sbluhm    The above copyright  notice and this permission notice  shall be included
312e724bc9Sbluhm    in all copies or substantial portions of the Software.
322e724bc9Sbluhm 
332e724bc9Sbluhm    THE  SOFTWARE  IS  PROVIDED  "AS  IS",  WITHOUT  WARRANTY  OF  ANY  KIND,
342e724bc9Sbluhm    EXPRESS  OR IMPLIED,  INCLUDING  BUT  NOT LIMITED  TO  THE WARRANTIES  OF
352e724bc9Sbluhm    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
362e724bc9Sbluhm    NO EVENT SHALL THE AUTHORS OR  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
372e724bc9Sbluhm    DAMAGES OR  OTHER LIABILITY, WHETHER  IN AN  ACTION OF CONTRACT,  TORT OR
382e724bc9Sbluhm    OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
392e724bc9Sbluhm    USE OR OTHER DEALINGS IN THE SOFTWARE.
4033ab7b2bSbluhm */
4133ab7b2bSbluhm 
4233ab7b2bSbluhm #ifdef __cplusplus
4333ab7b2bSbluhm extern "C" {
4433ab7b2bSbluhm #endif
4533ab7b2bSbluhm 
46*bd8f1dc3Sbluhm #ifndef XML_MINICHECK_H
47*bd8f1dc3Sbluhm #  define XML_MINICHECK_H
48*bd8f1dc3Sbluhm 
4933ab7b2bSbluhm #  define CK_NOFORK 0
5033ab7b2bSbluhm #  define CK_FORK 1
5133ab7b2bSbluhm 
5233ab7b2bSbluhm #  define CK_SILENT 0
5333ab7b2bSbluhm #  define CK_NORMAL 1
5433ab7b2bSbluhm #  define CK_VERBOSE 2
5533ab7b2bSbluhm 
5633ab7b2bSbluhm /* Workaround for Microsoft's compiler and Tru64 Unix systems where the
5733ab7b2bSbluhm    C compiler has a working __func__, but the C++ compiler only has a
5833ab7b2bSbluhm    working __FUNCTION__.  This could be fixed in configure.in, but it's
5933ab7b2bSbluhm    not worth it right now. */
6033ab7b2bSbluhm #  if defined(_MSC_VER) || (defined(__osf__) && defined(__cplusplus))
6133ab7b2bSbluhm #    define __func__ __FUNCTION__
6233ab7b2bSbluhm #  endif
6333ab7b2bSbluhm 
64*bd8f1dc3Sbluhm /* PRINTF_LIKE has two effects:
65*bd8f1dc3Sbluhm     1. Make clang's -Wformat-nonliteral stop warning about non-literal format
66*bd8f1dc3Sbluhm        strings in annotated functions' code.
67*bd8f1dc3Sbluhm     2. Make both clang and gcc's -Wformat-nonliteral warn about *callers* of
68*bd8f1dc3Sbluhm        the annotated function that use a non-literal format string.
69*bd8f1dc3Sbluhm */
70*bd8f1dc3Sbluhm #  if defined(__GNUC__)
71*bd8f1dc3Sbluhm #    define PRINTF_LIKE(fmtpos, argspos)                                       \
72*bd8f1dc3Sbluhm       __attribute__((format(printf, fmtpos, argspos)))
73*bd8f1dc3Sbluhm #  else
74*bd8f1dc3Sbluhm #    define PRINTF_LIKE(fmtpos, argspos)
75*bd8f1dc3Sbluhm #  endif
76*bd8f1dc3Sbluhm 
7728ce3119Sbluhm #  define START_TEST(testname)                                                 \
7828ce3119Sbluhm     static void testname(void) {                                               \
7933ab7b2bSbluhm       _check_set_test_info(__func__, __FILE__, __LINE__);                      \
8033ab7b2bSbluhm       {
8128ce3119Sbluhm #  define END_TEST                                                             \
8228ce3119Sbluhm     }                                                                          \
8328ce3119Sbluhm     }
8433ab7b2bSbluhm 
85*bd8f1dc3Sbluhm void PRINTF_LIKE(1, 2) set_subtest(char const *fmt, ...);
86*bd8f1dc3Sbluhm 
87*bd8f1dc3Sbluhm #  define fail(msg) _fail(__FILE__, __LINE__, msg)
88*bd8f1dc3Sbluhm #  define assert_true(cond)                                                    \
89*bd8f1dc3Sbluhm     do {                                                                       \
90*bd8f1dc3Sbluhm       if (! (cond)) {                                                          \
91*bd8f1dc3Sbluhm         _fail(__FILE__, __LINE__, "check failed: " #cond);                     \
92*bd8f1dc3Sbluhm       }                                                                        \
93*bd8f1dc3Sbluhm     } while (0)
9433ab7b2bSbluhm 
9533ab7b2bSbluhm typedef void (*tcase_setup_function)(void);
9633ab7b2bSbluhm typedef void (*tcase_teardown_function)(void);
9733ab7b2bSbluhm typedef void (*tcase_test_function)(void);
9833ab7b2bSbluhm 
9933ab7b2bSbluhm typedef struct SRunner SRunner;
10033ab7b2bSbluhm typedef struct Suite Suite;
10133ab7b2bSbluhm typedef struct TCase TCase;
10233ab7b2bSbluhm 
10333ab7b2bSbluhm struct SRunner {
10433ab7b2bSbluhm   Suite *suite;
10533ab7b2bSbluhm   int nchecks;
10633ab7b2bSbluhm   int nfailures;
10733ab7b2bSbluhm };
10833ab7b2bSbluhm 
10933ab7b2bSbluhm struct Suite {
11033ab7b2bSbluhm   const char *name;
11133ab7b2bSbluhm   TCase *tests;
11233ab7b2bSbluhm };
11333ab7b2bSbluhm 
11433ab7b2bSbluhm struct TCase {
11533ab7b2bSbluhm   const char *name;
11633ab7b2bSbluhm   tcase_setup_function setup;
11733ab7b2bSbluhm   tcase_teardown_function teardown;
11833ab7b2bSbluhm   tcase_test_function *tests;
11933ab7b2bSbluhm   int ntests;
12033ab7b2bSbluhm   int allocated;
12133ab7b2bSbluhm   TCase *next_tcase;
12233ab7b2bSbluhm };
12333ab7b2bSbluhm 
12433ab7b2bSbluhm /* Internal helper. */
12528ce3119Sbluhm void _check_set_test_info(char const *function, char const *filename,
12628ce3119Sbluhm                           int lineno);
12733ab7b2bSbluhm 
12833ab7b2bSbluhm /*
12933ab7b2bSbluhm  * Prototypes for the actual implementation.
13033ab7b2bSbluhm  */
13133ab7b2bSbluhm 
132*bd8f1dc3Sbluhm #  if defined(__GNUC__)
133*bd8f1dc3Sbluhm __attribute__((noreturn))
134*bd8f1dc3Sbluhm #  endif
135*bd8f1dc3Sbluhm void
136*bd8f1dc3Sbluhm _fail(const char *file, int line, const char *msg);
13733ab7b2bSbluhm Suite *suite_create(const char *name);
13833ab7b2bSbluhm TCase *tcase_create(const char *name);
13933ab7b2bSbluhm void suite_add_tcase(Suite *suite, TCase *tc);
140*bd8f1dc3Sbluhm void tcase_add_checked_fixture(TCase *tc, tcase_setup_function setup,
141*bd8f1dc3Sbluhm                                tcase_teardown_function teardown);
14233ab7b2bSbluhm void tcase_add_test(TCase *tc, tcase_test_function test);
14333ab7b2bSbluhm SRunner *srunner_create(Suite *suite);
144*bd8f1dc3Sbluhm void srunner_run_all(SRunner *runner, const char *context, int verbosity);
145*bd8f1dc3Sbluhm void srunner_summarize(SRunner *runner, int verbosity);
14633ab7b2bSbluhm int srunner_ntests_failed(SRunner *runner);
14733ab7b2bSbluhm void srunner_free(SRunner *runner);
14833ab7b2bSbluhm 
149*bd8f1dc3Sbluhm #endif /* XML_MINICHECK_H */
150*bd8f1dc3Sbluhm 
15133ab7b2bSbluhm #ifdef __cplusplus
15233ab7b2bSbluhm }
15333ab7b2bSbluhm #endif
154