xref: /netbsd-src/external/bsd/libevent/dist/test/tinytest_macros.h (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*	$NetBSD: tinytest_macros.h,v 1.4 2017/01/31 23:17:40 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_INCLUDED_
28 #define TINYTEST_MACROS_H_INCLUDED_
29 
30 /* Helpers for defining statement-like macros */
31 #define TT_STMT_BEGIN do {
32 #define TT_STMT_END } while (/*CONSTCOND*/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_failprint_f(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_ = (a);						\
118 	type val2_ = (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 #define tt_assert_test_type_opt(a,b,str_test,type,test,fmt,die_on_fail)	\
149 	tt_assert_test_fmt_type(a,b,str_test,type,test,type,fmt,	\
150             {print_=value_?value_:"<NULL>";},{},die_on_fail)
151 
152 /* Helper: assert that a op b, when cast to type.  Format the values with
153  * printf format fmt on failure. */
154 #define tt_assert_op_type(a,op,b,type,fmt)				\
155 	tt_assert_test_type(a,b,#a" "#op" "#b,type,(val1_ op val2_),fmt, \
156 	    TT_EXIT_TEST_FUNCTION)
157 
158 #define tt_int_op(a,op,b)			\
159 	tt_assert_test_type(a,b,#a" "#op" "#b,long,(val1_ op val2_), \
160 	    "%ld",TT_EXIT_TEST_FUNCTION)
161 
162 #define tt_uint_op(a,op,b)						\
163 	tt_assert_test_type(a,b,#a" "#op" "#b,unsigned long,		\
164 	    (val1_ op val2_),"%lu",TT_EXIT_TEST_FUNCTION)
165 
166 #define tt_ptr_op(a,op,b)						\
167 	tt_assert_test_type(a,b,#a" "#op" "#b,const void*,              \
168 	    (val1_ op val2_),"%p",TT_EXIT_TEST_FUNCTION)
169 
170 /** XXX: have some issues with printing this non-NUL terminated strings */
171 #define tt_nstr_op(n,a,op,b)						\
172 	tt_assert_test_type_opt(a,b,#a" "#op" "#b,const char *,		\
173 	    (val1_ && val2_ && strncmp(val1_,val2_,(n)) op 0),"<%s>",	\
174 	    TT_EXIT_TEST_FUNCTION)
175 
176 #define tt_str_op(a,op,b)						\
177 	tt_assert_test_type_opt(a,b,#a" "#op" "#b,const char *,		\
178 	    (val1_ && val2_ && strcmp(val1_,val2_) op 0),"<%s>",	\
179 	    TT_EXIT_TEST_FUNCTION)
180 
181 #define tt_mem_op(expr1, op, expr2, len)                                \
182   tt_assert_test_fmt_type(expr1,expr2,#expr1" "#op" "#expr2,            \
183 			  const void *,                                 \
184 			  (val1_ && val2_ && memcmp(val1_, val2_, len) op 0), \
185 			  char *, "%s",					\
186 			  { print_ = tinytest_format_hex_(value_, (len)); }, \
187 			  { if (print_) free(print_); },		\
188 			  TT_EXIT_TEST_FUNCTION				\
189                           );
190 
191 #define tt_want_int_op(a,op,b)						\
192 	tt_assert_test_type(a,b,#a" "#op" "#b,long,(val1_ op val2_),"%ld",(void)0)
193 
194 #define tt_want_uint_op(a,op,b)						\
195 	tt_assert_test_type(a,b,#a" "#op" "#b,unsigned long,		\
196 	    (val1_ op val2_),"%lu",(void)0)
197 
198 #define tt_want_ptr_op(a,op,b)						\
199   tt_assert_test_type(a,b,#a" "#op" "#b,const void*,			\
200 	    (val1_ op val2_),"%p",(void)0)
201 
202 #define tt_want_str_op(a,op,b)						\
203 	tt_assert_test_type(a,b,#a" "#op" "#b,const char *,		\
204 	    (strcmp(val1_,val2_) op 0),"<%s>",(void)0)
205 
206 #endif
207