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