xref: /minix3/external/bsd/libevent/dist/test/tinytest.h (revision e985b929927b5932e3b68f4b50587d458900107a)
1*e985b929SDavid van Moolenbroek /*	$NetBSD: tinytest.h,v 1.1.1.1 2013/04/11 16:43:32 christos Exp $	*/
2*e985b929SDavid van Moolenbroek /* tinytest.h -- Copyright 2009-2012 Nick Mathewson
3*e985b929SDavid van Moolenbroek  *
4*e985b929SDavid van Moolenbroek  * Redistribution and use in source and binary forms, with or without
5*e985b929SDavid van Moolenbroek  * modification, are permitted provided that the following conditions
6*e985b929SDavid van Moolenbroek  * are met:
7*e985b929SDavid van Moolenbroek  * 1. Redistributions of source code must retain the above copyright
8*e985b929SDavid van Moolenbroek  *    notice, this list of conditions and the following disclaimer.
9*e985b929SDavid van Moolenbroek  * 2. Redistributions in binary form must reproduce the above copyright
10*e985b929SDavid van Moolenbroek  *    notice, this list of conditions and the following disclaimer in the
11*e985b929SDavid van Moolenbroek  *    documentation and/or other materials provided with the distribution.
12*e985b929SDavid van Moolenbroek  * 3. The name of the author may not be used to endorse or promote products
13*e985b929SDavid van Moolenbroek  *    derived from this software without specific prior written permission.
14*e985b929SDavid van Moolenbroek  *
15*e985b929SDavid van Moolenbroek  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16*e985b929SDavid van Moolenbroek  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17*e985b929SDavid van Moolenbroek  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18*e985b929SDavid van Moolenbroek  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19*e985b929SDavid van Moolenbroek  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20*e985b929SDavid van Moolenbroek  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21*e985b929SDavid van Moolenbroek  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22*e985b929SDavid van Moolenbroek  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23*e985b929SDavid van Moolenbroek  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24*e985b929SDavid van Moolenbroek  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25*e985b929SDavid van Moolenbroek  */
26*e985b929SDavid van Moolenbroek 
27*e985b929SDavid van Moolenbroek #ifndef _TINYTEST_H
28*e985b929SDavid van Moolenbroek #define _TINYTEST_H
29*e985b929SDavid van Moolenbroek 
30*e985b929SDavid van Moolenbroek /** Flag for a test that needs to run in a subprocess. */
31*e985b929SDavid van Moolenbroek #define TT_FORK  (1<<0)
32*e985b929SDavid van Moolenbroek /** Runtime flag for a test we've decided to skip. */
33*e985b929SDavid van Moolenbroek #define TT_SKIP  (1<<1)
34*e985b929SDavid van Moolenbroek /** Internal runtime flag for a test we've decided to run. */
35*e985b929SDavid van Moolenbroek #define _TT_ENABLED  (1<<2)
36*e985b929SDavid van Moolenbroek /** If you add your own flags, make them start at this point. */
37*e985b929SDavid van Moolenbroek #define TT_FIRST_USER_FLAG (1<<3)
38*e985b929SDavid van Moolenbroek 
39*e985b929SDavid van Moolenbroek typedef void (*testcase_fn)(void *);
40*e985b929SDavid van Moolenbroek 
41*e985b929SDavid van Moolenbroek struct testcase_t;
42*e985b929SDavid van Moolenbroek 
43*e985b929SDavid van Moolenbroek /** Functions to initialize/teardown a structure for a testcase. */
44*e985b929SDavid van Moolenbroek struct testcase_setup_t {
45*e985b929SDavid van Moolenbroek 	/** Return a new structure for use by a given testcase. */
46*e985b929SDavid van Moolenbroek 	void *(*setup_fn)(const struct testcase_t *);
47*e985b929SDavid van Moolenbroek 	/** Clean/free a structure from setup_fn. Return 1 if ok, 0 on err. */
48*e985b929SDavid van Moolenbroek 	int (*cleanup_fn)(const struct testcase_t *, void *);
49*e985b929SDavid van Moolenbroek };
50*e985b929SDavid van Moolenbroek 
51*e985b929SDavid van Moolenbroek /** A single test-case that you can run. */
52*e985b929SDavid van Moolenbroek struct testcase_t {
53*e985b929SDavid van Moolenbroek 	const char *name; /**< An identifier for this case. */
54*e985b929SDavid van Moolenbroek 	testcase_fn fn; /**< The function to run to implement this case. */
55*e985b929SDavid van Moolenbroek 	unsigned long flags; /**< Bitfield of TT_* flags. */
56*e985b929SDavid van Moolenbroek 	const struct testcase_setup_t *setup; /**< Optional setup/cleanup fns*/
57*e985b929SDavid van Moolenbroek 	void *setup_data; /**< Extra data usable by setup function */
58*e985b929SDavid van Moolenbroek };
59*e985b929SDavid van Moolenbroek #define END_OF_TESTCASES { NULL, NULL, 0, NULL, NULL }
60*e985b929SDavid van Moolenbroek 
61*e985b929SDavid van Moolenbroek /** A group of tests that are selectable together. */
62*e985b929SDavid van Moolenbroek struct testgroup_t {
63*e985b929SDavid van Moolenbroek 	const char *prefix; /**< Prefix to prepend to testnames. */
64*e985b929SDavid van Moolenbroek 	struct testcase_t *cases; /** Array, ending with END_OF_TESTCASES */
65*e985b929SDavid van Moolenbroek };
66*e985b929SDavid van Moolenbroek #define END_OF_GROUPS { NULL, NULL}
67*e985b929SDavid van Moolenbroek 
68*e985b929SDavid van Moolenbroek /** Implementation: called from a test to indicate failure, before logging. */
69*e985b929SDavid van Moolenbroek void _tinytest_set_test_failed(void);
70*e985b929SDavid van Moolenbroek /** Implementation: called from a test to indicate that we're skipping. */
71*e985b929SDavid van Moolenbroek void _tinytest_set_test_skipped(void);
72*e985b929SDavid van Moolenbroek /** Implementation: return 0 for quiet, 1 for normal, 2 for loud. */
73*e985b929SDavid van Moolenbroek int _tinytest_get_verbosity(void);
74*e985b929SDavid van Moolenbroek /** Implementation: Set a flag on tests matching a name; returns number
75*e985b929SDavid van Moolenbroek  * of tests that matched. */
76*e985b929SDavid van Moolenbroek int _tinytest_set_flag(struct testgroup_t *, const char *, unsigned long);
77*e985b929SDavid van Moolenbroek 
78*e985b929SDavid van Moolenbroek /** Set all tests in 'groups' matching the name 'named' to be skipped. */
79*e985b929SDavid van Moolenbroek #define tinytest_skip(groups, named) \
80*e985b929SDavid van Moolenbroek 	_tinytest_set_flag(groups, named, TT_SKIP)
81*e985b929SDavid van Moolenbroek 
82*e985b929SDavid van Moolenbroek /** Run a single testcase in a single group. */
83*e985b929SDavid van Moolenbroek int testcase_run_one(const struct testgroup_t *,const struct testcase_t *);
84*e985b929SDavid van Moolenbroek /** Run a set of testcases from an END_OF_GROUPS-terminated array of groups,
85*e985b929SDavid van Moolenbroek     as selected from the command line. */
86*e985b929SDavid van Moolenbroek int tinytest_main(int argc, const char **argv, struct testgroup_t *groups);
87*e985b929SDavid van Moolenbroek 
88*e985b929SDavid van Moolenbroek #endif
89