26 #ifndef TINYTEST_MACROS_H_INCLUDED_
27 #define TINYTEST_MACROS_H_INCLUDED_
30 #define TT_STMT_BEGIN do {
31 #define TT_STMT_END } while (0)
35 #ifndef TT_EXIT_TEST_FUNCTION
36 #define TT_EXIT_TEST_FUNCTION TT_STMT_BEGIN goto end; TT_STMT_END
41 #define TT_DECLARE(prefix, args) \
43 printf("\n %s %s:%d: ",prefix,__FILE__,__LINE__); \
49 #define TT_GRIPE(args) TT_DECLARE("FAIL", args)
52 #define TT_BLATHER(args) \
54 if (tinytest_get_verbosity_()>1) TT_DECLARE(" OK", args); \
57 #define TT_DIE(args) \
59 tinytest_set_test_failed_(); \
61 TT_EXIT_TEST_FUNCTION; \
64 #define TT_FAIL(args) \
66 tinytest_set_test_failed_(); \
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.)"))
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.)"))
85 tinytest_set_test_skipped_(); \
86 TT_EXIT_TEST_FUNCTION; \
89 #define tt_want_(b, msg, fail) \
92 tinytest_set_test_failed_(); \
93 TT_GRIPE(("%s",msg)); \
96 TT_BLATHER(("%s",msg)); \
101 #define tt_want_msg(b, msg) \
105 #define tt_assert_msg(b, msg) \
106 tt_want_(b, msg, TT_EXIT_TEST_FUNCTION)
109 #define tt_want(b) tt_want_msg( (b), "want("#b")")
111 #define tt_assert(b) tt_assert_msg((b), "assert("#b")")
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) \
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_; \
129 TT_DECLARE(tt_status_?" OK":"FAIL", \
130 ("assert(%s): "printf_fmt" vs "printf_fmt, \
131 str_test, print1_, print2_)); \
137 tinytest_set_test_failed_(); \
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)
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)
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)
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)
161 #define tt_uint_op(a,op,b) \
162 tt_assert_test_type(a,b,#a" "#op" "#b,unsigned long, \
163 (val1_ op val2_),"%lu",TT_EXIT_TEST_FUNCTION)
165 #define tt_ptr_op(a,op,b) \
166 tt_assert_test_type(a,b,#a" "#op" "#b,const void*, \
167 (val1_ op val2_),"%p",TT_EXIT_TEST_FUNCTION)
169 #define tt_str_op(a,op,b) \
170 tt_assert_test_type_opt(a,b,#a" "#op" "#b,const char *, \
171 (val1_ && val2_ && strcmp(val1_,val2_) op 0),"<%s>", \
172 TT_EXIT_TEST_FUNCTION)
174 #define tt_mem_op(expr1, op, expr2, len) \
175 tt_assert_test_fmt_type(expr1,expr2,#expr1" "#op" "#expr2, \
177 (val1_ && val2_ && memcmp(val1_, val2_, len) op 0), \
179 { print_ = tinytest_format_hex_(value_, (len)); }, \
180 { if (print_) free(print_); }, \
181 TT_EXIT_TEST_FUNCTION \
184 #define tt_want_int_op(a,op,b) \
185 tt_assert_test_type(a,b,#a" "#op" "#b,long,(val1_ op val2_),"%ld",(void)0)
187 #define tt_want_uint_op(a,op,b) \
188 tt_assert_test_type(a,b,#a" "#op" "#b,unsigned long, \
189 (val1_ op val2_),"%lu",(void)0)
191 #define tt_want_ptr_op(a,op,b) \
192 tt_assert_test_type(a,b,#a" "#op" "#b,const void*, \
193 (val1_ op val2_),"%p",(void)0)
195 #define tt_want_str_op(a,op,b) \
196 tt_assert_test_type(a,b,#a" "#op" "#b,const char *, \
197 (strcmp(val1_,val2_) op 0),"<%s>",(void)0)