1 /* Copyright (c) 2001-2003, Roger Dingledine.
2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3 * Copyright (c) 2007-2011, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
11 * \brief Macros used by unit tests.
17 #define PRETTY_FUNCTION __PRETTY_FUNCTION__
19 #define PRETTY_FUNCTION ""
22 #define test_fail_msg(msg) \
25 printf("\nFile %s: line %d (%s): %s", \
33 #define test_fail() test_fail_msg("Assertion failed.")
35 #define test_assert(expr) \
37 if (expr) { printf("."); fflush(stdout); } else { \
39 printf("\nFile %s: line %d (%s): assertion failed: (%s)\n", \
47 #define test_eq_type(tp, fmt, expr1, expr2) \
49 tp _test_v1=(tp)(expr1); \
50 tp _test_v2=(tp)(expr2); \
51 if (_test_v1==_test_v2) { printf("."); fflush(stdout); } else { \
53 printf("\nFile %s: line %d (%s): Assertion failed: (%s==%s)\n" \
59 _test_v1, _test_v2); \
63 #define test_eq(expr1, expr2) \
64 test_eq_type(long, "%ld", expr1, expr2)
66 #define test_eq_ptr(expr1, expr2) \
67 test_eq_type(void*, "%p", expr1, expr2)
69 #define test_neq_type(tp, fmt, expr1, expr2) \
71 tp _test_v1=(tp)(expr1); \
72 tp _test_v2=(tp)(expr2); \
73 if (_test_v1!=_test_v2) { printf("."); fflush(stdout); } else { \
75 printf("\nFile %s: line %d (%s): Assertion failed: (%s!=%s)\n" \
76 " ("fmt" == "fmt")\n", \
81 _test_v1, _test_v2); \
85 #define test_neq(expr1, expr2) \
86 test_neq_type(long, "%ld", expr1, expr2)
88 #define test_neq_ptr(expr1, expr2) \
89 test_neq_type(void *, "%p", expr1, expr2)
91 #define test_streq(expr1, expr2) \
93 const char *_test_v1=(expr1), *_test_v2=(expr2); \
94 if (!strcmp(_test_v1,_test_v2)) { printf("."); fflush(stdout); } else { \
96 printf("\nFile %s: line %d (%s): Assertion failed: (%s==%s)\n"\
97 " (\"%s\" != \"%s\")\n", \
102 _test_v1, _test_v2); \
106 #define test_strneq(expr1, expr2) \
108 const char *_test_v1=(expr1), *_test_v2=(expr2); \
109 if (strcmp(_test_v1,_test_v2)) { printf("."); fflush(stdout); } else { \
111 printf("\nFile %s: line %d (%s): Assertion failed: (%s!=%s)\n"\
112 " (\"%s\" == \"%s\")\n", \
117 _test_v1, _test_v2); \
121 #define test_memeq(expr1, expr2, len) \
123 const void *_test_v1=(expr1), *_test_v2=(expr2); \
125 if (!memcmp(_test_v1,_test_v2,(len))) { \
126 printf("."); fflush(stdout); } else { \
128 mem1 = tor_malloc(len*2+1); \
129 mem2 = tor_malloc(len*2+1); \
130 base16_encode(mem1, len*2+1, _test_v1, len); \
131 base16_encode(mem2, len*2+1, _test_v2, len); \
132 printf("\nFile %s: line %d (%s): Assertion failed: (%s==%s)\n" \
137 #expr1, #expr2, mem1, mem2); \
143 #define test_memeq_hex(expr1, hex) \
145 const char *_test_v1 = (char*)(expr1); \
146 const char *_test_v2 = (hex); \
147 size_t _len_v2 = strlen(_test_v2); \
148 char *_mem2 = tor_malloc(_len_v2/2); \
149 tor_assert((_len_v2 & 1) == 0); \
150 base16_decode(_mem2, _len_v2/2, _test_v2, _len_v2); \
151 if (!memcmp(_mem2, _test_v1, _len_v2/2)) { \
152 printf("."); fflush(stdout); } else { \
153 char *_mem1 = tor_malloc(_len_v2+1); \
154 base16_encode(_mem1, _len_v2+1, _test_v1, _len_v2/2); \
155 printf("\nFile %s: line %d (%s): Assertion failed: (%s==%s)\n" \
160 #expr1, _test_v2, _mem1, _test_v2); \
168 #define test_memneq(expr1, expr2, len) \
170 void *_test_v1=(expr1), *_test_v2=(expr2); \
171 if (memcmp(_test_v1,_test_v2,(len))) { \
172 printf("."); fflush(stdout); \
175 printf("\nFile %s: line %d (%s): Assertion failed: (%s!=%s)\n", \