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