Update for protocol (and a single fix).
[m6w6/libmemcached] / libtest / test.h
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * libtest
4 *
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 3 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22
23 #pragma once
24
25 #ifndef __INTEL_COMPILER
26 #pragma GCC diagnostic ignored "-Wold-style-cast"
27 #endif
28
29 /**
30 A structure describing the test case.
31 */
32 struct test_st {
33 const char *name;
34 bool requires_flush;
35 test_callback_fn *test_fn;
36 };
37
38 #define test_assert_errno(A) \
39 do \
40 { \
41 if ((A)) { \
42 fprintf(stderr, "\n%s:%d: Assertion failed for %s: ", __FILE__, __LINE__, __func__);\
43 perror(#A); \
44 fprintf(stderr, "\n"); \
45 libtest::create_core(); \
46 assert((A)); \
47 } \
48 } while (0)
49
50 #define test_assert(A, B) \
51 do \
52 { \
53 if ((A)) { \
54 fprintf(stderr, "\n%s:%d: Assertion failed %s, with message %s, in %s", __FILE__, __LINE__, (B), #A, __func__ );\
55 fprintf(stderr, "\n"); \
56 libtest::create_core(); \
57 assert((A)); \
58 } \
59 } while (0)
60
61 #define test_truth(A) \
62 do \
63 { \
64 if (! (A)) { \
65 fprintf(stderr, "\n%s:%d: Assertion \"%s\" failed, in %s\n", __FILE__, __LINE__, #A, __func__);\
66 libtest::create_core(); \
67 return TEST_FAILURE; \
68 } \
69 } while (0)
70
71 #define test_true(A) \
72 do \
73 { \
74 if (! (A)) { \
75 fprintf(stderr, "\n%s:%d: Assertion \"%s\" failed, in %s\n", __FILE__, __LINE__, #A, __func__);\
76 libtest::create_core(); \
77 return TEST_FAILURE; \
78 } \
79 } while (0)
80
81 #define test_true_got(__expected, __hint) \
82 do \
83 { \
84 if (not libtest::_compare_truth_hint(__FILE__, __LINE__, __func__, ((__expected)), #__expected, ((__hint)))) \
85 { \
86 libtest::create_core(); \
87 return TEST_FAILURE; \
88 } \
89 } while (0)
90 #define test_true_hint test_true_got
91
92 #define test_skip(__expected, __actual) \
93 do \
94 { \
95 if (libtest::_compare(__FILE__, __LINE__, __func__, ((__expected)), ((__actual)), false) == false) \
96 { \
97 return TEST_SKIPPED; \
98 } \
99 } while (0)
100
101 #define test_skip_hint(__expected, __actual, __hint) \
102 do \
103 { \
104 if (libtest::_compare_hint(__FILE__, __LINE__, __func__, (__expected), (__actual), (__hint)) == false) \
105 { \
106 return TEST_SKIPPED; \
107 } \
108 } while (0)
109
110 #define test_skip_valgrind() \
111 do \
112 { \
113 if (libtest::_in_valgrind(__FILE__, __LINE__, __func__)) \
114 { \
115 return TEST_SKIPPED; \
116 } \
117 } while (0)
118
119 #define test_fail(A) \
120 do \
121 { \
122 if (1) { \
123 fprintf(stderr, "\n%s:%d: Failed with %s, in %s\n", __FILE__, __LINE__, #A, __func__);\
124 libtest::create_core(); \
125 return TEST_FAILURE; \
126 } \
127 } while (0)
128
129
130 #define test_false(A) \
131 do \
132 { \
133 if ((A)) { \
134 fprintf(stderr, "\n%s:%d: Assertion failed %s, in %s\n", __FILE__, __LINE__, #A, __func__);\
135 libtest::create_core(); \
136 return TEST_FAILURE; \
137 } \
138 } while (0)
139
140 #define test_false_with(A,B) \
141 do \
142 { \
143 if ((A)) { \
144 fprintf(stderr, "\n%s:%d: Assertion failed %s with %s\n", __FILE__, __LINE__, #A, (B));\
145 libtest::create_core(); \
146 return TEST_FAILURE; \
147 } \
148 } while (0)
149
150 #define test_ne_compare(__expected, __actual) \
151 do \
152 { \
153 if (libtest::_ne_compare_hint(__FILE__, __LINE__, __func__, ((__expected)), ((__actual)), true) == false) \
154 { \
155 libtest::create_core(); \
156 return TEST_FAILURE; \
157 } \
158 } while (0)
159
160 #define test_compare(__expected, __actual) \
161 do \
162 { \
163 if (libtest::_compare(__FILE__, __LINE__, __func__, ((__expected)), ((__actual)), true) == false) \
164 { \
165 libtest::create_core(); \
166 return TEST_FAILURE; \
167 } \
168 } while (0)
169
170 #define test_zero(__actual) \
171 do \
172 { \
173 if (not libtest::_compare_zero(__FILE__, __LINE__, __func__, ((__actual)))) \
174 { \
175 libtest::create_core(); \
176 return TEST_FAILURE; \
177 } \
178 } while (0)
179
180 #define test_null test_zero
181
182 #define test_compare_got(__expected, __actual, __hint) \
183 do \
184 { \
185 if (not libtest::_compare_hint(__FILE__, __LINE__, __func__, (__expected), (__actual), (__hint))) \
186 { \
187 libtest::create_core(); \
188 return TEST_FAILURE; \
189 } \
190 } while (0)
191
192 #define test_compare_hint test_compare_got
193
194 #define test_compare_warn(__expected, __actual) \
195 do \
196 { \
197 void(libtest::_compare(__FILE__, __LINE__, __func__, (__expected), (__actual)), true); \
198 } while (0)
199
200 #define test_compare_warn_hint(__expected, __actual, __hint) \
201 do \
202 { \
203 libtest::_compare_hint(__FILE__, __LINE__, __func__, (__expected), (__actual), (__hint)); \
204 } while (0)
205
206 #define test_warn(__truth) \
207 do \
208 { \
209 void(libtest::_truth(__FILE__, __LINE__, __func__, (__truth))); \
210 } while (0)
211
212 #define test_warn_hint(__truth, __hint) \
213 do \
214 { \
215 void(libtest::_compare_truth_hint(__FILE__, __LINE__, __func__, (__truth), #__truth, (__hint))); \
216 } while (0)
217
218
219 #define test_strcmp(A,B) \
220 do \
221 { \
222 if ((A) == NULL or (B) == NULL or strcmp((A), (B))) \
223 { \
224 if ((B) == NULL) fprintf(stderr, "\n%s:%d: Expected %s, got <null>\n", __FILE__, __LINE__, (A)); \
225 else fprintf(stderr, "\n%s:%d: Expected %s, got \"%s\"\n", __FILE__, __LINE__, (A), (B)); \
226 libtest::create_core(); \
227 return TEST_FAILURE; \
228 } \
229 } while (0)
230
231 #define test_memcmp(A,B,C) \
232 do \
233 { \
234 if ((A) == NULL or (B) == NULL or memcmp((A), (B), (C))) \
235 { \
236 fprintf(stderr, "\n%s:%d: %.*s -> %.*s\n", __FILE__, __LINE__, (int)(C), (char *)(A), (int)(C), (char *)(B)); \
237 libtest::create_core(); \
238 return TEST_FAILURE; \
239 } \
240 } while (0)
241
242 #define test_memcmp_hint(A,B,C,__hint) \
243 do \
244 { \
245 if ((A) == NULL or (B) == NULL or memcmp((A), (B), (C))) \
246 { \
247 fprintf(stderr, "\n%s:%d: (hint:%s) %.*s -> %.*s\n", __FILE__, __LINE__, __hint, (int)(C), (char *)(A), (int)(C), (char *)(B)); \
248 libtest::create_core(); \
249 return TEST_FAILURE; \
250 } \
251 } while (0)
252
253 #define test_return_if(__test_return_t) \
254 do \
255 { \
256 if ((__test_return_t) != TEST_SUCCESS) \
257 { \
258 return __test_return_t; \
259 } \
260 } while (0)
261