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