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