Use -Wno-system-headers on openbsd to resolve 2nd case of bug1848
[tor/rransom.git] / src / common / test.h
blob1e3db58333664bb4bfac7101eeb06e59c490977b
1 /* Copyright (c) 2001-2003, Roger Dingledine.
2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3 * Copyright (c) 2007-2010, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
6 #ifndef _TOR_TEST_H
7 #define _TOR_TEST_H
9 /**
10 * \file test.h
11 * \brief Macros used by unit tests.
14 #include "compat.h"
16 #ifdef __GNUC__
17 #define PRETTY_FUNCTION __PRETTY_FUNCTION__
18 #else
19 #define PRETTY_FUNCTION ""
20 #endif
22 #define test_fail_msg(msg) \
23 STMT_BEGIN \
24 have_failed = 1; \
25 printf("\nFile %s: line %d (%s): %s", \
26 _SHORT_FILE_, \
27 __LINE__, \
28 PRETTY_FUNCTION, \
29 msg); \
30 goto done; \
31 STMT_END
33 #define test_fail() test_fail_msg("Assertion failed.")
35 #define test_assert(expr) \
36 STMT_BEGIN \
37 if (expr) { printf("."); fflush(stdout); } else { \
38 have_failed = 1; \
39 printf("\nFile %s: line %d (%s): assertion failed: (%s)\n", \
40 _SHORT_FILE_, \
41 __LINE__, \
42 PRETTY_FUNCTION, \
43 #expr); \
44 goto done; \
45 } STMT_END
47 #define test_eq_type(tp, fmt, expr1, expr2) \
48 STMT_BEGIN \
49 tp _test_v1=(tp)(expr1); \
50 tp _test_v2=(tp)(expr2); \
51 if (_test_v1==_test_v2) { printf("."); fflush(stdout); } else { \
52 have_failed = 1; \
53 printf("\nFile %s: line %d (%s): Assertion failed: (%s==%s)\n" \
54 " "fmt "!="fmt"\n", \
55 _SHORT_FILE_, \
56 __LINE__, \
57 PRETTY_FUNCTION, \
58 #expr1, #expr2, \
59 _test_v1, _test_v2); \
60 goto done; \
61 } STMT_END
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) \
70 STMT_BEGIN \
71 tp _test_v1=(tp)(expr1); \
72 tp _test_v2=(tp)(expr2); \
73 if (_test_v1!=_test_v2) { printf("."); fflush(stdout); } else { \
74 have_failed = 1; \
75 printf("\nFile %s: line %d (%s): Assertion failed: (%s!=%s)\n" \
76 " ("fmt" == "fmt")\n", \
77 _SHORT_FILE_, \
78 __LINE__, \
79 PRETTY_FUNCTION, \
80 #expr1, #expr2, \
81 _test_v1, _test_v2); \
82 goto done; \
83 } STMT_END
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) \
92 STMT_BEGIN \
93 const char *_test_v1=(expr1), *_test_v2=(expr2); \
94 if (!strcmp(_test_v1,_test_v2)) { printf("."); fflush(stdout); } else { \
95 have_failed = 1; \
96 printf("\nFile %s: line %d (%s): Assertion failed: (%s==%s)\n"\
97 " (\"%s\" != \"%s\")\n", \
98 _SHORT_FILE_, \
99 __LINE__, \
100 PRETTY_FUNCTION, \
101 #expr1, #expr2, \
102 _test_v1, _test_v2); \
103 goto done; \
104 } STMT_END
106 #define test_strneq(expr1, expr2) \
107 STMT_BEGIN \
108 const char *_test_v1=(expr1), *_test_v2=(expr2); \
109 if (strcmp(_test_v1,_test_v2)) { printf("."); fflush(stdout); } else { \
110 have_failed = 1; \
111 printf("\nFile %s: line %d (%s): Assertion failed: (%s!=%s)\n"\
112 " (\"%s\" == \"%s\")\n", \
113 _SHORT_FILE_, \
114 __LINE__, \
115 PRETTY_FUNCTION, \
116 #expr1, #expr2, \
117 _test_v1, _test_v2); \
118 goto done; \
119 } STMT_END
121 #define test_memeq(expr1, expr2, len) \
122 STMT_BEGIN \
123 const void *_test_v1=(expr1), *_test_v2=(expr2); \
124 char *mem1, *mem2; \
125 if (!memcmp(_test_v1,_test_v2,(len))) { \
126 printf("."); fflush(stdout); } else { \
127 have_failed = 1; \
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" \
133 " %s != %s\n", \
134 _SHORT_FILE_, \
135 __LINE__, \
136 PRETTY_FUNCTION, \
137 #expr1, #expr2, mem1, mem2); \
138 tor_free(mem1); \
139 tor_free(mem2); \
140 goto done; \
141 } STMT_END
143 #define test_memeq_hex(expr1, hex) \
144 STMT_BEGIN \
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" \
156 " %s != %s\n", \
157 _SHORT_FILE_, \
158 __LINE__, \
159 PRETTY_FUNCTION, \
160 #expr1, _test_v2, _mem1, _test_v2); \
161 tor_free(_mem1); \
162 tor_free(_mem2); \
163 goto done; \
165 tor_free(_mem2); \
166 STMT_END
168 #define test_memneq(expr1, expr2, len) \
169 STMT_BEGIN \
170 void *_test_v1=(expr1), *_test_v2=(expr2); \
171 if (memcmp(_test_v1,_test_v2,(len))) { \
172 printf("."); fflush(stdout); \
173 } else { \
174 have_failed = 1; \
175 printf("\nFile %s: line %d (%s): Assertion failed: (%s!=%s)\n", \
176 _SHORT_FILE_, \
177 __LINE__, \
178 PRETTY_FUNCTION, \
179 #expr1, #expr2); \
180 goto done; \
181 } STMT_END
183 #endif