Back to index

tor  0.2.3.18-rc
tinytest_macros.h
Go to the documentation of this file.
00001 /* tinytest_macros.h -- Copyright 2009-2012 Nick Mathewson
00002  *
00003  * Redistribution and use in source and binary forms, with or without
00004  * modification, are permitted provided that the following conditions
00005  * are met:
00006  * 1. Redistributions of source code must retain the above copyright
00007  *    notice, this list of conditions and the following disclaimer.
00008  * 2. Redistributions in binary form must reproduce the above copyright
00009  *    notice, this list of conditions and the following disclaimer in the
00010  *    documentation and/or other materials provided with the distribution.
00011  * 3. The name of the author may not be used to endorse or promote products
00012  *    derived from this software without specific prior written permission.
00013  *
00014  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00015  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00016  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00017  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00018  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00019  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00020  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00021  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00022  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00023  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00024  */
00025 
00026 #ifndef TINYTEST_MACROS_H_INCLUDED_
00027 #define TINYTEST_MACROS_H_INCLUDED_
00028 
00029 /* Helpers for defining statement-like macros */
00030 #define TT_STMT_BEGIN do {
00031 #define TT_STMT_END } while (0)
00032 
00033 /* Redefine this if your test functions want to abort with something besides
00034  * "goto end;" */
00035 #ifndef TT_EXIT_TEST_FUNCTION
00036 #define TT_EXIT_TEST_FUNCTION TT_STMT_BEGIN goto end; TT_STMT_END
00037 #endif
00038 
00039 /* Redefine this if you want to note success/failure in some different way. */
00040 #ifndef TT_DECLARE
00041 #define TT_DECLARE(prefix, args)                        \
00042        TT_STMT_BEGIN                                    \
00043        printf("\n  %s %s:%d: ",prefix,__FILE__,__LINE__);      \
00044        printf args ;                                    \
00045        TT_STMT_END
00046 #endif
00047 
00048 /* Announce a failure. Args are parenthesized printf args. */
00049 #define TT_GRIPE(args) TT_DECLARE("FAIL", args)
00050 
00051 /* Announce a non-failure if we're verbose. */
00052 #define TT_BLATHER(args)                                       \
00053        TT_STMT_BEGIN                                           \
00054        if (tinytest_get_verbosity_()>1) TT_DECLARE("  OK", args);     \
00055        TT_STMT_END
00056 
00057 #define TT_DIE(args)                                    \
00058        TT_STMT_BEGIN                                    \
00059        tinytest_set_test_failed_();                            \
00060        TT_GRIPE(args);                                         \
00061        TT_EXIT_TEST_FUNCTION;                                  \
00062        TT_STMT_END
00063 
00064 #define TT_FAIL(args)                            \
00065        TT_STMT_BEGIN                                    \
00066        tinytest_set_test_failed_();                            \
00067        TT_GRIPE(args);                                         \
00068        TT_STMT_END
00069 
00070 /* Fail and abort the current test for the reason in msg */
00071 #define tt_abort_printf(msg) TT_DIE(msg)
00072 #define tt_abort_perror(op) TT_DIE(("%s: %s [%d]",(op),strerror(errno), errno))
00073 #define tt_abort_msg(msg) TT_DIE(("%s", msg))
00074 #define tt_abort() TT_DIE(("%s", "(Failed.)"))
00075 
00076 /* Fail but do not abort the current test for the reason in msg. */
00077 #define tt_failprint_f(msg) TT_FAIL(msg)
00078 #define tt_fail_perror(op) TT_FAIL(("%s: %s [%d]",(op),strerror(errno), errno))
00079 #define tt_fail_msg(msg) TT_FAIL(("%s", msg))
00080 #define tt_fail() TT_FAIL(("%s", "(Failed.)"))
00081 
00082 /* End the current test, and indicate we are skipping it. */
00083 #define tt_skip()                                       \
00084        TT_STMT_BEGIN                                    \
00085        tinytest_set_test_skipped_();                           \
00086        TT_EXIT_TEST_FUNCTION;                                  \
00087        TT_STMT_END
00088 
00089 #define tt_want_(b, msg, fail)                          \
00090        TT_STMT_BEGIN                             \
00091        if (!(b)) {                               \
00092               tinytest_set_test_failed_();              \
00093               TT_GRIPE(("%s",msg));                     \
00094               fail;                              \
00095        } else {                                  \
00096               TT_BLATHER(("%s",msg));                   \
00097        }                                         \
00098        TT_STMT_END
00099 
00100 /* Assert b, but do not stop the test if b fails.  Log msg on failure. */
00101 #define tt_want_msg(b, msg)               \
00102        tt_want_(b, msg, );
00103 
00104 /* Assert b and stop the test if b fails.  Log msg on failure. */
00105 #define tt_assert_msg(b, msg)                    \
00106        tt_want_(b, msg, TT_EXIT_TEST_FUNCTION);
00107 
00108 /* Assert b, but do not stop the test if b fails. */
00109 #define tt_want(b)   tt_want_msg( (b), "want("#b")")
00110 /* Assert b, and stop the test if b fails. */
00111 #define tt_assert(b) tt_assert_msg((b), "assert("#b")")
00112 
00113 #define tt_assert_test_fmt_type(a,b,str_test,type,test,printf_type,printf_fmt, \
00114     setup_block,cleanup_block,die_on_fail)                            \
00115        TT_STMT_BEGIN                                           \
00116        type val1_ = (type)(a);                                        \
00117        type val2_ = (type)(b);                                        \
00118        int tt_status_ = (test);                                \
00119        if (!tt_status_ || tinytest_get_verbosity_()>1)  {             \
00120               printf_type print_;                              \
00121               printf_type print1_;                             \
00122               printf_type print2_;                             \
00123               type value_ = val1_;                             \
00124               setup_block;                                     \
00125               print1_ = print_;                                \
00126               value_ = val2_;                                         \
00127               setup_block;                                     \
00128               print2_ = print_;                                \
00129               TT_DECLARE(tt_status_?"      OK":"FAIL",                \
00130                         ("assert(%s): "printf_fmt" vs "printf_fmt,    \
00131                          str_test, print1_, print2_));         \
00132               print_ = print1_;                                \
00133               cleanup_block;                                          \
00134               print_ = print2_;                                \
00135               cleanup_block;                                          \
00136               if (!tt_status_) {                               \
00137                      tinytest_set_test_failed_();                     \
00138                      die_on_fail ;                             \
00139               }                                                \
00140        }                                                       \
00141        TT_STMT_END
00142 
00143 #define tt_assert_test_type(a,b,str_test,type,test,fmt,die_on_fail)   \
00144        tt_assert_test_fmt_type(a,b,str_test,type,test,type,fmt,       \
00145            {print_=value_;},{},die_on_fail)
00146 
00147 /* Helper: assert that a op b, when cast to type.  Format the values with
00148  * printf format fmt on failure. */
00149 #define tt_assert_op_type(a,op,b,type,fmt)                            \
00150        tt_assert_test_type(a,b,#a" "#op" "#b,type,(val1_ op val2_),fmt, \
00151            TT_EXIT_TEST_FUNCTION)
00152 
00153 #define tt_int_op(a,op,b)                 \
00154        tt_assert_test_type(a,b,#a" "#op" "#b,long,(val1_ op val2_), \
00155            "%ld",TT_EXIT_TEST_FUNCTION)
00156 
00157 #define tt_uint_op(a,op,b)                                     \
00158        tt_assert_test_type(a,b,#a" "#op" "#b,unsigned long,           \
00159            (val1_ op val2_),"%lu",TT_EXIT_TEST_FUNCTION)
00160 
00161 #define tt_ptr_op(a,op,b)                                      \
00162        tt_assert_test_type(a,b,#a" "#op" "#b,void*,                   \
00163            (val1_ op val2_),"%p",TT_EXIT_TEST_FUNCTION)
00164 
00165 #define tt_str_op(a,op,b)                                      \
00166        tt_assert_test_type(a,b,#a" "#op" "#b,const char *,            \
00167            (strcmp(val1_,val2_) op 0),"<%s>",TT_EXIT_TEST_FUNCTION)
00168 
00169 #define tt_want_int_op(a,op,b)                                        \
00170        tt_assert_test_type(a,b,#a" "#op" "#b,long,(val1_ op val2_),"%ld",(void)0)
00171 
00172 #define tt_want_uint_op(a,op,b)                                       \
00173        tt_assert_test_type(a,b,#a" "#op" "#b,unsigned long,           \
00174            (val1_ op val2_),"%lu",(void)0)
00175 
00176 #define tt_want_ptr_op(a,op,b)                                        \
00177        tt_assert_test_type(a,b,#a" "#op" "#b,void*,                   \
00178            (val1_ op val2_),"%p",(void)0)
00179 
00180 #define tt_want_str_op(a,op,b)                                        \
00181        tt_assert_test_type(a,b,#a" "#op" "#b,const char *,            \
00182            (strcmp(val1_,val2_) op 0),"<%s>",(void)0)
00183 
00184 #endif