tinytest_macros.h revision 1.1
1/*	$NetBSD: tinytest_macros.h,v 1.1 2013/04/11 16:43:31 christos Exp $	*/
2/* tinytest_macros.h -- Copyright 2009-2012 Nick Mathewson
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 * 3. The name of the author may not be used to endorse or promote products
13 *    derived from this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#ifndef _TINYTEST_MACROS_H
28#define _TINYTEST_MACROS_H
29
30/* Helpers for defining statement-like macros */
31#define TT_STMT_BEGIN do {
32#define TT_STMT_END } while (0)
33
34/* Redefine this if your test functions want to abort with something besides
35 * "goto end;" */
36#ifndef TT_EXIT_TEST_FUNCTION
37#define TT_EXIT_TEST_FUNCTION TT_STMT_BEGIN goto end; TT_STMT_END
38#endif
39
40/* Redefine this if you want to note success/failure in some different way. */
41#ifndef TT_DECLARE
42#define TT_DECLARE(prefix, args)				\
43	TT_STMT_BEGIN						\
44	printf("\n  %s %s:%d: ",prefix,__FILE__,__LINE__);	\
45	printf args ;						\
46	TT_STMT_END
47#endif
48
49/* Announce a failure. Args are parenthesized printf args. */
50#define TT_GRIPE(args) TT_DECLARE("FAIL", args)
51
52/* Announce a non-failure if we're verbose. */
53#define TT_BLATHER(args)						\
54	TT_STMT_BEGIN							\
55	if (_tinytest_get_verbosity()>1) TT_DECLARE("  OK", args);	\
56	TT_STMT_END
57
58#define TT_DIE(args)						\
59	TT_STMT_BEGIN						\
60	_tinytest_set_test_failed();				\
61	TT_GRIPE(args);						\
62	TT_EXIT_TEST_FUNCTION;					\
63	TT_STMT_END
64
65#define TT_FAIL(args)				\
66	TT_STMT_BEGIN						\
67	_tinytest_set_test_failed();				\
68	TT_GRIPE(args);						\
69	TT_STMT_END
70
71/* Fail and abort the current test for the reason in msg */
72#define tt_abort_printf(msg) TT_DIE(msg)
73#define tt_abort_perror(op) TT_DIE(("%s: %s [%d]",(op),strerror(errno), errno))
74#define tt_abort_msg(msg) TT_DIE(("%s", msg))
75#define tt_abort() TT_DIE(("%s", "(Failed.)"))
76
77/* Fail but do not abort the current test for the reason in msg. */
78#define tt_fail_printf(msg) TT_FAIL(msg)
79#define tt_fail_perror(op) TT_FAIL(("%s: %s [%d]",(op),strerror(errno), errno))
80#define tt_fail_msg(msg) TT_FAIL(("%s", msg))
81#define tt_fail() TT_FAIL(("%s", "(Failed.)"))
82
83/* End the current test, and indicate we are skipping it. */
84#define tt_skip()						\
85	TT_STMT_BEGIN						\
86	_tinytest_set_test_skipped();				\
87	TT_EXIT_TEST_FUNCTION;					\
88	TT_STMT_END
89
90#define _tt_want(b, msg, fail)				\
91	TT_STMT_BEGIN					\
92	if (!(b)) {					\
93		_tinytest_set_test_failed();		\
94		TT_GRIPE(("%s",msg));			\
95		fail;					\
96	} else {					\
97		TT_BLATHER(("%s",msg));			\
98	}						\
99	TT_STMT_END
100
101/* Assert b, but do not stop the test if b fails.  Log msg on failure. */
102#define tt_want_msg(b, msg)			\
103	_tt_want(b, msg, );
104
105/* Assert b and stop the test if b fails.  Log msg on failure. */
106#define tt_assert_msg(b, msg)			\
107	_tt_want(b, msg, TT_EXIT_TEST_FUNCTION);
108
109/* Assert b, but do not stop the test if b fails. */
110#define tt_want(b)   tt_want_msg( (b), "want("#b")")
111/* Assert b, and stop the test if b fails. */
112#define tt_assert(b) tt_assert_msg((b), "assert("#b")")
113
114#define tt_assert_test_fmt_type(a,b,str_test,type,test,printf_type,printf_fmt, \
115    setup_block,cleanup_block,die_on_fail)				\
116	TT_STMT_BEGIN							\
117	type _val1 = (type)(a);						\
118	type _val2 = (type)(b);						\
119	int _tt_status = (test);					\
120	if (!_tt_status || _tinytest_get_verbosity()>1)	{		\
121		printf_type _print;					\
122		printf_type _print1;					\
123		printf_type _print2;					\
124		type _value = _val1;					\
125		setup_block;						\
126		_print1 = _print;					\
127		_value = _val2;						\
128		setup_block;						\
129		_print2 = _print;					\
130		TT_DECLARE(_tt_status?"	 OK":"FAIL",			\
131			   ("assert(%s): "printf_fmt" vs "printf_fmt,	\
132			    str_test, _print1, _print2));		\
133		_print = _print1;					\
134		cleanup_block;						\
135		_print = _print2;					\
136		cleanup_block;						\
137		if (!_tt_status) {					\
138			_tinytest_set_test_failed();			\
139			die_on_fail ;					\
140		}							\
141	}								\
142	TT_STMT_END
143
144#define tt_assert_test_type(a,b,str_test,type,test,fmt,die_on_fail)	\
145	tt_assert_test_fmt_type(a,b,str_test,type,test,type,fmt,	\
146	    {_print=_value;},{},die_on_fail)
147
148/* Helper: assert that a op b, when cast to type.  Format the values with
149 * printf format fmt on failure. */
150#define tt_assert_op_type(a,op,b,type,fmt)				\
151	tt_assert_test_type(a,b,#a" "#op" "#b,type,(_val1 op _val2),fmt, \
152	    TT_EXIT_TEST_FUNCTION)
153
154#define tt_int_op(a,op,b)			\
155	tt_assert_test_type(a,b,#a" "#op" "#b,long,(_val1 op _val2), \
156	    "%ld",TT_EXIT_TEST_FUNCTION)
157
158#define tt_uint_op(a,op,b)						\
159	tt_assert_test_type(a,b,#a" "#op" "#b,unsigned long,		\
160	    (_val1 op _val2),"%lu",TT_EXIT_TEST_FUNCTION)
161
162#define tt_ptr_op(a,op,b)						\
163	tt_assert_test_type(a,b,#a" "#op" "#b,void*,			\
164	    (_val1 op _val2),"%p",TT_EXIT_TEST_FUNCTION)
165
166#define tt_str_op(a,op,b)						\
167	tt_assert_test_type(a,b,#a" "#op" "#b,const char *,		\
168	    (strcmp(_val1,_val2) op 0),"<%s>",TT_EXIT_TEST_FUNCTION)
169
170#define tt_want_int_op(a,op,b)						\
171	tt_assert_test_type(a,b,#a" "#op" "#b,long,(_val1 op _val2),"%ld",(void)0)
172
173#define tt_want_uint_op(a,op,b)						\
174	tt_assert_test_type(a,b,#a" "#op" "#b,unsigned long,		\
175	    (_val1 op _val2),"%lu",(void)0)
176
177#define tt_want_ptr_op(a,op,b)						\
178	tt_assert_test_type(a,b,#a" "#op" "#b,void*,			\
179	    (_val1 op _val2),"%p",(void)0)
180
181#define tt_want_str_op(a,op,b)						\
182	tt_assert_test_type(a,b,#a" "#op" "#b,const char *,		\
183	    (strcmp(_val1,_val2) op 0),"<%s>",(void)0)
184
185#endif
186