1290001Sglebius/* tinytest_macros.h -- Copyright 2009-2012 Nick Mathewson
2290001Sglebius *
3290001Sglebius * Redistribution and use in source and binary forms, with or without
4290001Sglebius * modification, are permitted provided that the following conditions
5290001Sglebius * are met:
6290001Sglebius * 1. Redistributions of source code must retain the above copyright
7290001Sglebius *    notice, this list of conditions and the following disclaimer.
8290001Sglebius * 2. Redistributions in binary form must reproduce the above copyright
9290001Sglebius *    notice, this list of conditions and the following disclaimer in the
10290001Sglebius *    documentation and/or other materials provided with the distribution.
11290001Sglebius * 3. The name of the author may not be used to endorse or promote products
12290001Sglebius *    derived from this software without specific prior written permission.
13290001Sglebius *
14290001Sglebius * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15290001Sglebius * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16290001Sglebius * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17290001Sglebius * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18290001Sglebius * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19290001Sglebius * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20290001Sglebius * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21290001Sglebius * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22290001Sglebius * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23290001Sglebius * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24290001Sglebius */
25290001Sglebius
26290001Sglebius#ifndef TINYTEST_MACROS_H_INCLUDED_
27290001Sglebius#define TINYTEST_MACROS_H_INCLUDED_
28290001Sglebius
29290001Sglebius/* Helpers for defining statement-like macros */
30290001Sglebius#define TT_STMT_BEGIN do {
31290001Sglebius#define TT_STMT_END } while (0)
32290001Sglebius
33290001Sglebius/* Redefine this if your test functions want to abort with something besides
34290001Sglebius * "goto end;" */
35290001Sglebius#ifndef TT_EXIT_TEST_FUNCTION
36290001Sglebius#define TT_EXIT_TEST_FUNCTION TT_STMT_BEGIN goto end; TT_STMT_END
37290001Sglebius#endif
38290001Sglebius
39290001Sglebius/* Redefine this if you want to note success/failure in some different way. */
40290001Sglebius#ifndef TT_DECLARE
41290001Sglebius#define TT_DECLARE(prefix, args)				\
42290001Sglebius	TT_STMT_BEGIN						\
43290001Sglebius	printf("\n  %s %s:%d: ",prefix,__FILE__,__LINE__);	\
44290001Sglebius	printf args ;						\
45290001Sglebius	TT_STMT_END
46290001Sglebius#endif
47290001Sglebius
48290001Sglebius/* Announce a failure. Args are parenthesized printf args. */
49290001Sglebius#define TT_GRIPE(args) TT_DECLARE("FAIL", args)
50290001Sglebius
51290001Sglebius/* Announce a non-failure if we're verbose. */
52290001Sglebius#define TT_BLATHER(args)						\
53290001Sglebius	TT_STMT_BEGIN							\
54290001Sglebius	if (tinytest_get_verbosity_()>1) TT_DECLARE("  OK", args);	\
55290001Sglebius	TT_STMT_END
56290001Sglebius
57290001Sglebius#define TT_DIE(args)						\
58290001Sglebius	TT_STMT_BEGIN						\
59290001Sglebius	tinytest_set_test_failed_();				\
60290001Sglebius	TT_GRIPE(args);						\
61290001Sglebius	TT_EXIT_TEST_FUNCTION;					\
62290001Sglebius	TT_STMT_END
63290001Sglebius
64290001Sglebius#define TT_FAIL(args)				\
65290001Sglebius	TT_STMT_BEGIN						\
66290001Sglebius	tinytest_set_test_failed_();				\
67290001Sglebius	TT_GRIPE(args);						\
68290001Sglebius	TT_STMT_END
69290001Sglebius
70290001Sglebius/* Fail and abort the current test for the reason in msg */
71290001Sglebius#define tt_abort_printf(msg) TT_DIE(msg)
72290001Sglebius#define tt_abort_perror(op) TT_DIE(("%s: %s [%d]",(op),strerror(errno), errno))
73290001Sglebius#define tt_abort_msg(msg) TT_DIE(("%s", msg))
74290001Sglebius#define tt_abort() TT_DIE(("%s", "(Failed.)"))
75290001Sglebius
76290001Sglebius/* Fail but do not abort the current test for the reason in msg. */
77290001Sglebius#define tt_failprint_f(msg) TT_FAIL(msg)
78290001Sglebius#define tt_fail_perror(op) TT_FAIL(("%s: %s [%d]",(op),strerror(errno), errno))
79290001Sglebius#define tt_fail_msg(msg) TT_FAIL(("%s", msg))
80290001Sglebius#define tt_fail() TT_FAIL(("%s", "(Failed.)"))
81290001Sglebius
82290001Sglebius/* End the current test, and indicate we are skipping it. */
83290001Sglebius#define tt_skip()						\
84290001Sglebius	TT_STMT_BEGIN						\
85290001Sglebius	tinytest_set_test_skipped_();				\
86290001Sglebius	TT_EXIT_TEST_FUNCTION;					\
87290001Sglebius	TT_STMT_END
88290001Sglebius
89290001Sglebius#define tt_want_(b, msg, fail)				\
90290001Sglebius	TT_STMT_BEGIN					\
91290001Sglebius	if (!(b)) {					\
92290001Sglebius		tinytest_set_test_failed_();		\
93290001Sglebius		TT_GRIPE(("%s",msg));			\
94290001Sglebius		fail;					\
95290001Sglebius	} else {					\
96290001Sglebius		TT_BLATHER(("%s",msg));			\
97290001Sglebius	}						\
98290001Sglebius	TT_STMT_END
99290001Sglebius
100290001Sglebius/* Assert b, but do not stop the test if b fails.  Log msg on failure. */
101290001Sglebius#define tt_want_msg(b, msg)			\
102290001Sglebius	tt_want_(b, msg, );
103290001Sglebius
104290001Sglebius/* Assert b and stop the test if b fails.  Log msg on failure. */
105290001Sglebius#define tt_assert_msg(b, msg)			\
106290001Sglebius	tt_want_(b, msg, TT_EXIT_TEST_FUNCTION);
107290001Sglebius
108290001Sglebius/* Assert b, but do not stop the test if b fails. */
109290001Sglebius#define tt_want(b)   tt_want_msg( (b), "want("#b")")
110290001Sglebius/* Assert b, and stop the test if b fails. */
111290001Sglebius#define tt_assert(b) tt_assert_msg((b), "assert("#b")")
112290001Sglebius
113290001Sglebius#define tt_assert_test_fmt_type(a,b,str_test,type,test,printf_type,printf_fmt, \
114290001Sglebius    setup_block,cleanup_block,die_on_fail)				\
115290001Sglebius	TT_STMT_BEGIN							\
116290001Sglebius	type val1_ = (a);						\
117290001Sglebius	type val2_ = (b);						\
118290001Sglebius	int tt_status_ = (test);					\
119290001Sglebius	if (!tt_status_ || tinytest_get_verbosity_()>1)	{		\
120290001Sglebius		printf_type print_;					\
121290001Sglebius		printf_type print1_;					\
122290001Sglebius		printf_type print2_;					\
123290001Sglebius		type value_ = val1_;					\
124290001Sglebius		setup_block;						\
125290001Sglebius		print1_ = print_;					\
126290001Sglebius		value_ = val2_;						\
127290001Sglebius		setup_block;						\
128290001Sglebius		print2_ = print_;					\
129290001Sglebius		TT_DECLARE(tt_status_?"	 OK":"FAIL",			\
130290001Sglebius			   ("assert(%s): "printf_fmt" vs "printf_fmt,	\
131290001Sglebius			    str_test, print1_, print2_));		\
132290001Sglebius		print_ = print1_;					\
133290001Sglebius		cleanup_block;						\
134290001Sglebius		print_ = print2_;					\
135290001Sglebius		cleanup_block;						\
136290001Sglebius		if (!tt_status_) {					\
137290001Sglebius			tinytest_set_test_failed_();			\
138290001Sglebius			die_on_fail ;					\
139290001Sglebius		}							\
140290001Sglebius	}								\
141290001Sglebius	TT_STMT_END
142290001Sglebius
143290001Sglebius#define tt_assert_test_type(a,b,str_test,type,test,fmt,die_on_fail)	\
144290001Sglebius	tt_assert_test_fmt_type(a,b,str_test,type,test,type,fmt,	\
145290001Sglebius	    {print_=value_;},{},die_on_fail)
146290001Sglebius
147290001Sglebius#define tt_assert_test_type_opt(a,b,str_test,type,test,fmt,die_on_fail)	\
148290001Sglebius	tt_assert_test_fmt_type(a,b,str_test,type,test,type,fmt,	\
149290001Sglebius            {print_=value_?value_:"<NULL>";},{},die_on_fail)
150290001Sglebius
151290001Sglebius/* Helper: assert that a op b, when cast to type.  Format the values with
152290001Sglebius * printf format fmt on failure. */
153290001Sglebius#define tt_assert_op_type(a,op,b,type,fmt)				\
154290001Sglebius	tt_assert_test_type(a,b,#a" "#op" "#b,type,(val1_ op val2_),fmt, \
155290001Sglebius	    TT_EXIT_TEST_FUNCTION)
156290001Sglebius
157290001Sglebius#define tt_int_op(a,op,b)			\
158290001Sglebius	tt_assert_test_type(a,b,#a" "#op" "#b,long,(val1_ op val2_), \
159290001Sglebius	    "%ld",TT_EXIT_TEST_FUNCTION)
160290001Sglebius
161290001Sglebius#define tt_uint_op(a,op,b)						\
162290001Sglebius	tt_assert_test_type(a,b,#a" "#op" "#b,unsigned long,		\
163290001Sglebius	    (val1_ op val2_),"%lu",TT_EXIT_TEST_FUNCTION)
164290001Sglebius
165290001Sglebius#define tt_ptr_op(a,op,b)						\
166290001Sglebius	tt_assert_test_type(a,b,#a" "#op" "#b,const void*,              \
167290001Sglebius	    (val1_ op val2_),"%p",TT_EXIT_TEST_FUNCTION)
168290001Sglebius
169290001Sglebius#define tt_str_op(a,op,b)						\
170290001Sglebius	tt_assert_test_type_opt(a,b,#a" "#op" "#b,const char *,		\
171290001Sglebius	    (val1_ && val2_ && strcmp(val1_,val2_) op 0),"<%s>",	\
172290001Sglebius	    TT_EXIT_TEST_FUNCTION)
173290001Sglebius
174290001Sglebius#define tt_mem_op(expr1, op, expr2, len)                                \
175290001Sglebius  tt_assert_test_fmt_type(expr1,expr2,#expr1" "#op" "#expr2,            \
176290001Sglebius			  const void *,                                 \
177290001Sglebius			  (val1_ && val2_ && memcmp(val1_, val2_, len) op 0), \
178290001Sglebius			  char *, "%s",					\
179290001Sglebius			  { print_ = tinytest_format_hex_(value_, (len)); }, \
180290001Sglebius			  { if (print_) free(print_); },		\
181290001Sglebius			  TT_EXIT_TEST_FUNCTION				\
182290001Sglebius                          );
183290001Sglebius
184290001Sglebius#define tt_want_int_op(a,op,b)						\
185290001Sglebius	tt_assert_test_type(a,b,#a" "#op" "#b,long,(val1_ op val2_),"%ld",(void)0)
186290001Sglebius
187290001Sglebius#define tt_want_uint_op(a,op,b)						\
188290001Sglebius	tt_assert_test_type(a,b,#a" "#op" "#b,unsigned long,		\
189290001Sglebius	    (val1_ op val2_),"%lu",(void)0)
190290001Sglebius
191290001Sglebius#define tt_want_ptr_op(a,op,b)						\
192290001Sglebius  tt_assert_test_type(a,b,#a" "#op" "#b,const void*,			\
193290001Sglebius	    (val1_ op val2_),"%p",(void)0)
194290001Sglebius
195290001Sglebius#define tt_want_str_op(a,op,b)						\
196290001Sglebius	tt_assert_test_type(a,b,#a" "#op" "#b,const char *,		\
197290001Sglebius	    (strcmp(val1_,val2_) op 0),"<%s>",(void)0)
198290001Sglebius
199290001Sglebius#endif
200