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