Fix server messages (clean up errors in general).
[m6w6/libmemcached] / libtest / test.h
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * Data Differential YATL (i.e. libtest) library
4 *
5 * Copyright (C) 2012 Data Differential, http://datadifferential.com/
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following disclaimer
16 * in the documentation and/or other materials provided with the
17 * distribution.
18 *
19 * * The names of its contributors may not be used to endorse or
20 * promote products derived from this software without specific prior
21 * written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *
35 */
36
37 #pragma once
38
39 #ifndef __INTEL_COMPILER
40 #pragma GCC diagnostic ignored "-Wold-style-cast"
41 #endif
42
43 /**
44 A structure describing the test case.
45 */
46 struct test_st {
47 const char *name;
48 bool requires_flush;
49 test_callback_fn *test_fn;
50 };
51
52 #define test_assert_errno(A) \
53 do \
54 { \
55 if ((A)) { \
56 fprintf(stderr, "\n%s:%d: Assertion failed for %s: ", __FILE__, __LINE__, __func__);\
57 perror(#A); \
58 fprintf(stderr, "\n"); \
59 libtest::create_core(); \
60 assert((A)); \
61 } \
62 } while (0)
63
64 #define test_assert(A, B) \
65 do \
66 { \
67 if ((A)) { \
68 fprintf(stderr, "\n%s:%d: Assertion failed %s, with message %s, in %s", __FILE__, __LINE__, (B), #A, __func__ );\
69 fprintf(stderr, "\n"); \
70 libtest::create_core(); \
71 assert((A)); \
72 } \
73 } while (0)
74
75 #define test_truth(A) \
76 do \
77 { \
78 if (! (A)) { \
79 fprintf(stderr, "\n%s:%d: Assertion \"%s\" failed, in %s\n", __FILE__, __LINE__, #A, __func__);\
80 libtest::create_core(); \
81 return TEST_FAILURE; \
82 } \
83 } while (0)
84
85 #define test_true(A) \
86 do \
87 { \
88 if (! (A)) { \
89 fprintf(stderr, "\n%s:%d: Assertion \"%s\" failed, in %s\n", __FILE__, __LINE__, #A, __func__);\
90 libtest::create_core(); \
91 return TEST_FAILURE; \
92 } \
93 } while (0)
94
95 #define test_true_got(__expected, __hint) \
96 do \
97 { \
98 if (not libtest::_compare_truth_hint(__FILE__, __LINE__, __func__, ((__expected)), #__expected, ((__hint)))) \
99 { \
100 libtest::create_core(); \
101 return TEST_FAILURE; \
102 } \
103 } while (0)
104 #define test_true_hint test_true_got
105
106 #define test_skip(__expected, __actual) \
107 do \
108 { \
109 if (libtest::_compare(__FILE__, __LINE__, __func__, ((__expected)), ((__actual)), false) == false) \
110 { \
111 return TEST_SKIPPED; \
112 } \
113 } while (0)
114
115 #define test_skip_hint(__expected, __actual, __hint) \
116 do \
117 { \
118 if (libtest::_compare_hint(__FILE__, __LINE__, __func__, (__expected), (__actual), (__hint)) == false) \
119 { \
120 return TEST_SKIPPED; \
121 } \
122 } while (0)
123
124 #define test_skip_valgrind() \
125 do \
126 { \
127 if (libtest::_in_valgrind(__FILE__, __LINE__, __func__)) \
128 { \
129 return TEST_SKIPPED; \
130 } \
131 } while (0)
132
133 #define test_fail(A) \
134 do \
135 { \
136 if (1) { \
137 fprintf(stderr, "\n%s:%d: Failed with %s, in %s\n", __FILE__, __LINE__, #A, __func__);\
138 libtest::create_core(); \
139 return TEST_FAILURE; \
140 } \
141 } while (0)
142
143
144 #define test_false(A) \
145 do \
146 { \
147 if ((A)) { \
148 fprintf(stderr, "\n%s:%d: Assertion failed %s, in %s\n", __FILE__, __LINE__, #A, __func__);\
149 libtest::create_core(); \
150 return TEST_FAILURE; \
151 } \
152 } while (0)
153
154 #define test_false_with(A,B) \
155 do \
156 { \
157 if ((A)) { \
158 fprintf(stderr, "\n%s:%d: Assertion failed %s with %s\n", __FILE__, __LINE__, #A, (B));\
159 libtest::create_core(); \
160 return TEST_FAILURE; \
161 } \
162 } while (0)
163
164 #define test_ne_compare(__expected, __actual) \
165 do \
166 { \
167 if (libtest::_ne_compare_hint(__FILE__, __LINE__, __func__, ((__expected)), ((__actual)), true) == false) \
168 { \
169 libtest::create_core(); \
170 return TEST_FAILURE; \
171 } \
172 } while (0)
173
174 #define test_compare(__expected, __actual) \
175 do \
176 { \
177 if (libtest::_compare(__FILE__, __LINE__, __func__, ((__expected)), ((__actual)), true) == false) \
178 { \
179 libtest::create_core(); \
180 return TEST_FAILURE; \
181 } \
182 } while (0)
183
184 #define test_zero(__actual) \
185 do \
186 { \
187 if (not libtest::_compare_zero(__FILE__, __LINE__, __func__, ((__actual)))) \
188 { \
189 libtest::create_core(); \
190 return TEST_FAILURE; \
191 } \
192 } while (0)
193
194 #define test_null test_zero
195
196 #define test_compare_got(__expected, __actual, __hint) \
197 do \
198 { \
199 if (libtest::_compare_hint(__FILE__, __LINE__, __func__, (__expected), (__actual), (__hint)) == false) \
200 { \
201 libtest::create_core(); \
202 return TEST_FAILURE; \
203 } \
204 } while (0)
205
206 #define test_compare_hint test_compare_got
207
208 #define test_compare_warn(__expected, __actual) \
209 do \
210 { \
211 void(libtest::_compare(__FILE__, __LINE__, __func__, (__expected), (__actual)), true); \
212 } while (0)
213
214 #define test_compare_warn_hint(__expected, __actual, __hint) \
215 do \
216 { \
217 libtest::_compare_hint(__FILE__, __LINE__, __func__, (__expected), (__actual), (__hint)); \
218 } while (0)
219
220 #define test_warn(__truth) \
221 do \
222 { \
223 void(libtest::_truth(__FILE__, __LINE__, __func__, (__truth))); \
224 } while (0)
225
226 #define test_warn_hint(__truth, __hint) \
227 do \
228 { \
229 void(libtest::_compare_truth_hint(__FILE__, __LINE__, __func__, (__truth), #__truth, (__hint))); \
230 } while (0)
231
232
233 #define test_strcmp(__expected, __actual) \
234 do \
235 { \
236 void(libtest::_compare_strcmp(__FILE__, __LINE__, __func__, (__expected), (__actual))); \
237 } while (0)
238
239 #define test_memcmp(A,B,C) \
240 do \
241 { \
242 if ((A) == NULL or (B) == NULL or memcmp((A), (B), (C))) \
243 { \
244 fprintf(stderr, "\n%s:%d: %.*s -> %.*s\n", __FILE__, __LINE__, (int)(C), (char *)(A), (int)(C), (char *)(B)); \
245 libtest::create_core(); \
246 return TEST_FAILURE; \
247 } \
248 } while (0)
249
250 #define test_memcmp_hint(A,B,C,__hint) \
251 do \
252 { \
253 if ((A) == NULL or (B) == NULL or memcmp((A), (B), (C))) \
254 { \
255 fprintf(stderr, "\n%s:%d: (hint:%s) %.*s -> %.*s\n", __FILE__, __LINE__, __hint, (int)(C), (char *)(A), (int)(C), (char *)(B)); \
256 libtest::create_core(); \
257 return TEST_FAILURE; \
258 } \
259 } while (0)
260
261 #define test_return_if(__test_return_t) \
262 do \
263 { \
264 if ((__test_return_t) != TEST_SUCCESS) \
265 { \
266 return __test_return_t; \
267 } \
268 } while (0)
269