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