michael@0: /* tinytest_macros.h -- Copyright 2009-2012 Nick Mathewson michael@0: * michael@0: * Redistribution and use in source and binary forms, with or without michael@0: * modification, are permitted provided that the following conditions michael@0: * are met: michael@0: * 1. Redistributions of source code must retain the above copyright michael@0: * notice, this list of conditions and the following disclaimer. michael@0: * 2. Redistributions in binary form must reproduce the above copyright michael@0: * notice, this list of conditions and the following disclaimer in the michael@0: * documentation and/or other materials provided with the distribution. michael@0: * 3. The name of the author may not be used to endorse or promote products michael@0: * derived from this software without specific prior written permission. michael@0: * michael@0: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR michael@0: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES michael@0: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. michael@0: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, michael@0: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT michael@0: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF michael@0: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: */ michael@0: michael@0: #ifndef _TINYTEST_MACROS_H michael@0: #define _TINYTEST_MACROS_H michael@0: michael@0: /* Helpers for defining statement-like macros */ michael@0: #define TT_STMT_BEGIN do { michael@0: #define TT_STMT_END } while (0) michael@0: michael@0: /* Redefine this if your test functions want to abort with something besides michael@0: * "goto end;" */ michael@0: #ifndef TT_EXIT_TEST_FUNCTION michael@0: #define TT_EXIT_TEST_FUNCTION TT_STMT_BEGIN goto end; TT_STMT_END michael@0: #endif michael@0: michael@0: /* Redefine this if you want to note success/failure in some different way. */ michael@0: #ifndef TT_DECLARE michael@0: #define TT_DECLARE(prefix, args) \ michael@0: TT_STMT_BEGIN \ michael@0: printf("\n %s %s:%d: ",prefix,__FILE__,__LINE__); \ michael@0: printf args ; \ michael@0: TT_STMT_END michael@0: #endif michael@0: michael@0: /* Announce a failure. Args are parenthesized printf args. */ michael@0: #define TT_GRIPE(args) TT_DECLARE("FAIL", args) michael@0: michael@0: /* Announce a non-failure if we're verbose. */ michael@0: #define TT_BLATHER(args) \ michael@0: TT_STMT_BEGIN \ michael@0: if (_tinytest_get_verbosity()>1) TT_DECLARE(" OK", args); \ michael@0: TT_STMT_END michael@0: michael@0: #define TT_DIE(args) \ michael@0: TT_STMT_BEGIN \ michael@0: _tinytest_set_test_failed(); \ michael@0: TT_GRIPE(args); \ michael@0: TT_EXIT_TEST_FUNCTION; \ michael@0: TT_STMT_END michael@0: michael@0: #define TT_FAIL(args) \ michael@0: TT_STMT_BEGIN \ michael@0: _tinytest_set_test_failed(); \ michael@0: TT_GRIPE(args); \ michael@0: TT_STMT_END michael@0: michael@0: /* Fail and abort the current test for the reason in msg */ michael@0: #define tt_abort_printf(msg) TT_DIE(msg) michael@0: #define tt_abort_perror(op) TT_DIE(("%s: %s [%d]",(op),strerror(errno), errno)) michael@0: #define tt_abort_msg(msg) TT_DIE(("%s", msg)) michael@0: #define tt_abort() TT_DIE(("%s", "(Failed.)")) michael@0: michael@0: /* Fail but do not abort the current test for the reason in msg. */ michael@0: #define tt_fail_printf(msg) TT_FAIL(msg) michael@0: #define tt_fail_perror(op) TT_FAIL(("%s: %s [%d]",(op),strerror(errno), errno)) michael@0: #define tt_fail_msg(msg) TT_FAIL(("%s", msg)) michael@0: #define tt_fail() TT_FAIL(("%s", "(Failed.)")) michael@0: michael@0: /* End the current test, and indicate we are skipping it. */ michael@0: #define tt_skip() \ michael@0: TT_STMT_BEGIN \ michael@0: _tinytest_set_test_skipped(); \ michael@0: TT_EXIT_TEST_FUNCTION; \ michael@0: TT_STMT_END michael@0: michael@0: #define _tt_want(b, msg, fail) \ michael@0: TT_STMT_BEGIN \ michael@0: if (!(b)) { \ michael@0: _tinytest_set_test_failed(); \ michael@0: TT_GRIPE(("%s",msg)); \ michael@0: fail; \ michael@0: } else { \ michael@0: TT_BLATHER(("%s",msg)); \ michael@0: } \ michael@0: TT_STMT_END michael@0: michael@0: /* Assert b, but do not stop the test if b fails. Log msg on failure. */ michael@0: #define tt_want_msg(b, msg) \ michael@0: _tt_want(b, msg, ); michael@0: michael@0: /* Assert b and stop the test if b fails. Log msg on failure. */ michael@0: #define tt_assert_msg(b, msg) \ michael@0: _tt_want(b, msg, TT_EXIT_TEST_FUNCTION); michael@0: michael@0: /* Assert b, but do not stop the test if b fails. */ michael@0: #define tt_want(b) tt_want_msg( (b), "want("#b")") michael@0: /* Assert b, and stop the test if b fails. */ michael@0: #define tt_assert(b) tt_assert_msg((b), "assert("#b")") michael@0: michael@0: #define tt_assert_test_fmt_type(a,b,str_test,type,test,printf_type,printf_fmt, \ michael@0: setup_block,cleanup_block,die_on_fail) \ michael@0: TT_STMT_BEGIN \ michael@0: type _val1 = (type)(a); \ michael@0: type _val2 = (type)(b); \ michael@0: int _tt_status = (test); \ michael@0: if (!_tt_status || _tinytest_get_verbosity()>1) { \ michael@0: printf_type _print; \ michael@0: printf_type _print1; \ michael@0: printf_type _print2; \ michael@0: type _value = _val1; \ michael@0: setup_block; \ michael@0: _print1 = _print; \ michael@0: _value = _val2; \ michael@0: setup_block; \ michael@0: _print2 = _print; \ michael@0: TT_DECLARE(_tt_status?" OK":"FAIL", \ michael@0: ("assert(%s): "printf_fmt" vs "printf_fmt, \ michael@0: str_test, _print1, _print2)); \ michael@0: _print = _print1; \ michael@0: cleanup_block; \ michael@0: _print = _print2; \ michael@0: cleanup_block; \ michael@0: if (!_tt_status) { \ michael@0: _tinytest_set_test_failed(); \ michael@0: die_on_fail ; \ michael@0: } \ michael@0: } \ michael@0: TT_STMT_END michael@0: michael@0: #define tt_assert_test_type(a,b,str_test,type,test,fmt,die_on_fail) \ michael@0: tt_assert_test_fmt_type(a,b,str_test,type,test,type,fmt, \ michael@0: {_print=_value;},{},die_on_fail) michael@0: michael@0: /* Helper: assert that a op b, when cast to type. Format the values with michael@0: * printf format fmt on failure. */ michael@0: #define tt_assert_op_type(a,op,b,type,fmt) \ michael@0: tt_assert_test_type(a,b,#a" "#op" "#b,type,(_val1 op _val2),fmt, \ michael@0: TT_EXIT_TEST_FUNCTION) michael@0: michael@0: #define tt_int_op(a,op,b) \ michael@0: tt_assert_test_type(a,b,#a" "#op" "#b,long,(_val1 op _val2), \ michael@0: "%ld",TT_EXIT_TEST_FUNCTION) michael@0: michael@0: #define tt_uint_op(a,op,b) \ michael@0: tt_assert_test_type(a,b,#a" "#op" "#b,unsigned long, \ michael@0: (_val1 op _val2),"%lu",TT_EXIT_TEST_FUNCTION) michael@0: michael@0: #define tt_ptr_op(a,op,b) \ michael@0: tt_assert_test_type(a,b,#a" "#op" "#b,void*, \ michael@0: (_val1 op _val2),"%p",TT_EXIT_TEST_FUNCTION) michael@0: michael@0: #define tt_str_op(a,op,b) \ michael@0: tt_assert_test_type(a,b,#a" "#op" "#b,const char *, \ michael@0: (strcmp(_val1,_val2) op 0),"<%s>",TT_EXIT_TEST_FUNCTION) michael@0: michael@0: #define tt_want_int_op(a,op,b) \ michael@0: tt_assert_test_type(a,b,#a" "#op" "#b,long,(_val1 op _val2),"%ld",(void)0) michael@0: michael@0: #define tt_want_uint_op(a,op,b) \ michael@0: tt_assert_test_type(a,b,#a" "#op" "#b,unsigned long, \ michael@0: (_val1 op _val2),"%lu",(void)0) michael@0: michael@0: #define tt_want_ptr_op(a,op,b) \ michael@0: tt_assert_test_type(a,b,#a" "#op" "#b,void*, \ michael@0: (_val1 op _val2),"%p",(void)0) michael@0: michael@0: #define tt_want_str_op(a,op,b) \ michael@0: tt_assert_test_type(a,b,#a" "#op" "#b,const char *, \ michael@0: (strcmp(_val1,_val2) op 0),"<%s>",(void)0) michael@0: michael@0: #endif