Fix bad if path
[m6w6/libmemcached] / libtest / lite.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 #ifdef __cplusplus
40 # include <cstdarg>
41 # include <cstddef>
42 # include <cstdio>
43 # include <cstdlib>
44 # include <cstring>
45 #else
46 # include <stdarg.h>
47 # include <stdbool.h>
48 # include <stddef.h>
49 # include <stdio.h>
50 # include <stdlib.h>
51 # include <string.h>
52 #endif
53
54 #if defined(WIN32)
55 # include <malloc.h>
56 #else
57 # include <alloca.h>
58 #endif
59
60 #ifndef __PRETTY_FUNCTION__
61 # define __PRETTY_FUNCTION__ __func__
62 #endif
63
64 #ifndef EXIT_SKIP
65 # define EXIT_SKIP 77
66 #endif
67
68 #ifndef YATL_FULL
69 # define YATL_FULL 0
70 #endif
71
72 #ifndef FAIL
73 # define FAIL(__message_format, ...)
74 #endif
75
76 #ifndef SKIP
77 # define SKIP(__message_format, ...)
78 #endif
79
80 #include <libtest/valgrind.h>
81
82 static inline size_t yatl_strlen(const char *s)
83 {
84 if (s)
85 {
86 return strlen(s);
87 }
88
89 return (size_t)(0);
90 }
91
92 static inline int yatl_strcmp(const char *s1, const char *s2, size_t *s1_length, size_t *s2_length)
93 {
94 *s1_length= yatl_strlen(s1);
95 *s2_length= yatl_strlen(s2);
96
97 if (*s1_length == 0 && *s1_length == *s2_length)
98 {
99 return 0;
100 }
101
102 if (*s1_length == 0 && *s2_length)
103 {
104 return 1;
105 }
106
107 if (*s1_length && *s2_length == 0)
108 {
109 return 1;
110 }
111
112 return strcmp(s1, s2);
113 }
114
115 #define SKIP_IF(__expression) \
116 do \
117 { \
118 if ((__expression)) { \
119 if (YATL_FULL) { \
120 SKIP(#__expression); \
121 } \
122 fprintf(stdout, "\n%s:%d: %s SKIP '!(%s)'\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression); \
123 exit(EXIT_SKIP); \
124 } \
125 } while (0)
126
127 #define SKIP_IF_(__expression, ...) \
128 do \
129 { \
130 if ((__expression)) { \
131 size_t ask= snprintf(0, 0, __VA_ARGS__); \
132 ask++; \
133 char *buffer= (char*)alloca(sizeof(char) * ask); \
134 snprintf(buffer, ask, __VA_ARGS__); \
135 if (YATL_FULL) { \
136 SKIP(#__expression, buffer); \
137 } \
138 fprintf(stdout, "\n%s:%d: %s SKIP '%s' [ %s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression, buffer); \
139 exit(EXIT_SKIP); \
140 } \
141 } while (0)
142
143 #define SKIP_UNLESS(__expression) \
144 do \
145 { \
146 if (! (__expression)) { \
147 if (YATL_FULL) { \
148 SKIP(#__expression); \
149 } \
150 fprintf(stdout, "\n%s:%d: %s SKIP '(%s)'\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression); \
151 exit(EXIT_SKIP); \
152 } \
153 } while (0)
154
155 #define SKIP_UNLESS_(__expression, ...) \
156 do \
157 { \
158 if (! (__expression)) { \
159 size_t ask= snprintf(0, 0, __VA_ARGS__); \
160 ask++; \
161 char *buffer= (char*)alloca(sizeof(char) * ask); \
162 snprintf(buffer, ask, __VA_ARGS__); \
163 if (YATL_FULL) { \
164 SKIP(#__expression, buffer); \
165 } \
166 fprintf(stdout, "\n%s:%d: %s SKIP '%s' [ %s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression, buffer); \
167 exit(EXIT_SKIP); \
168 } \
169 } while (0)
170
171 #define ASSERT_TRUE(__expression) \
172 do \
173 { \
174 if (! (__expression)) { \
175 if (YATL_FULL) { \
176 FAIL("Assertion '%s'", #__expression); \
177 } \
178 fprintf(stderr, "\n%s:%d: %s Assertion '%s'\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression);\
179 exit(EXIT_FAILURE); \
180 } \
181 } while (0)
182
183 #define ASSERT_FALSE(__expression) \
184 do \
185 { \
186 if ((__expression)) { \
187 if (YATL_FULL) { \
188 FAIL("Assertion '!%s'", #__expression); \
189 } \
190 fprintf(stderr, "\n%s:%d: %s Assertion '!%s'\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression);\
191 exit(EXIT_FAILURE); \
192 } \
193 } while (0)
194
195 #define ASSERT_NULL_(__expression, ...) \
196 do \
197 { \
198 if ((__expression) != NULL) { \
199 size_t ask= snprintf(0, 0, __VA_ARGS__); \
200 ask++; \
201 char *buffer= (char*)alloca(sizeof(char) * ask); \
202 snprintf(buffer, ask, __VA_ARGS__); \
203 if (YATL_FULL) { \
204 FAIL("Assertion '%s' != NULL [ %s ]", #__expression, buffer);\
205 } \
206 fprintf(stderr, "\n%s:%d: %s Assertion '%s' != NULL [ %s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression, buffer);\
207 exit(EXIT_FAILURE); \
208 } \
209 } while (0)
210
211 #define ASSERT_NOT_NULL(__expression) \
212 do \
213 { \
214 if ((__expression) == NULL) { \
215 if (YATL_FULL) { \
216 FAIL("Assertion '%s' == NULL", #__expression,);\
217 } \
218 fprintf(stderr, "\n%s:%d: %s Assertion '%s' == NULL\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression,);\
219 exit(EXIT_FAILURE); \
220 } \
221 } while (0)
222
223 #define ASSERT_NOT_NULL_(__expression, ...) \
224 do \
225 { \
226 if ((__expression) == NULL) { \
227 size_t ask= snprintf(0, 0, __VA_ARGS__); \
228 ask++; \
229 char *buffer= (char*)alloca(sizeof(char) * ask); \
230 snprintf(buffer, ask, __VA_ARGS__); \
231 if (YATL_FULL) { \
232 FAIL("Assertion '%s' == NULL [ %s ]", #__expression, buffer);\
233 } \
234 fprintf(stderr, "\n%s:%d: %s Assertion '%s' == NULL [ %s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression, buffer);\
235 exit(EXIT_FAILURE); \
236 } \
237 } while (0)
238
239 #define ASSERT_TRUE_(__expression, ...) \
240 do \
241 { \
242 if (! (__expression)) { \
243 size_t ask= snprintf(0, 0, __VA_ARGS__); \
244 ask++; \
245 char *buffer= (char*)alloca(sizeof(char) * ask); \
246 snprintf(buffer, ask, __VA_ARGS__); \
247 if (YATL_FULL) { \
248 FAIL("Assertion '%s' [ %s ]", #__expression, buffer); \
249 } \
250 fprintf(stderr, "\n%s:%d: %s Assertion '%s' [ %s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression, buffer); \
251 exit(EXIT_FAILURE); \
252 } \
253 } while (0)
254
255 #define ASSERT_EQ(__expected, __actual) \
256 do \
257 { \
258 if ((__expected) != (__actual)) { \
259 if (YATL_FULL) { \
260 FAIL("Assertion '%s' != '%s'", #__expected, #__actual); \
261 } \
262 fprintf(stderr, "\n%s:%d: %s Assertion '%s' != '%s'\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expected, #__actual); \
263 exit(EXIT_FAILURE); \
264 } \
265 } while (0)
266
267 #define ASSERT_EQ_(__expected, __actual, ...) \
268 do \
269 { \
270 if ((__expected) != (__actual)) { \
271 size_t ask= snprintf(0, 0, __VA_ARGS__); \
272 ask++; \
273 char *buffer= (char*)alloca(sizeof(char) * ask); \
274 snprintf(buffer, ask, __VA_ARGS__); \
275 if (YATL_FULL) { \
276 FAIL("Assertion '%s' != '%s' [ %s ]", #__expected, #__actual, buffer); \
277 } \
278 fprintf(stderr, "\n%s:%d: %s Assertion '%s' != '%s' [ %s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expected, #__actual, buffer); \
279 exit(EXIT_FAILURE); \
280 } \
281 } while (0)
282
283 #define ASSERT_STREQ(__expected_str, __actual_str) \
284 do \
285 { \
286 size_t __expected_length; \
287 size_t __actual_length; \
288 int ret= yatl_strcmp(__expected_str, __actual_str, &__expected_length, &__actual_length); \
289 if (ret) { \
290 if (YATL_FULL) { \
291 FAIL("Assertion '%.*s' != '%.*s'\n", \
292 (int)(__expected_length), (__expected_str), \
293 (int)__actual_length, (__actual_str)) ; \
294 } \
295 fprintf(stderr, "\n%s:%d: %s Assertion '%.*s' != '%.*s'\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, \
296 (int)(__expected_length), (__expected_str), \
297 (int)__actual_length, (__actual_str)) ; \
298 exit(EXIT_FAILURE); \
299 } \
300 } while (0)
301
302 #define ASSERT_STREQ_(__expected_str, __actual_str, ...) \
303 do \
304 { \
305 size_t __expected_length; \
306 size_t __actual_length; \
307 int ret= yatl_strcmp(__expected_str, __actual_str, &__expected_length, &__actual_length); \
308 if (ret) { \
309 size_t ask= snprintf(0, 0, __VA_ARGS__); \
310 ask++; \
311 char *buffer= (char*)alloca(sizeof(char) * ask); \
312 ask= snprintf(buffer, ask, __VA_ARGS__); \
313 if (YATL_FULL) { \
314 FAIL("Assertion '%.*s' != '%.*s' [ %.*s ]", \
315 (int)(__expected_length), (__expected_str), \
316 (int)(__actual_length), (__actual_str), \
317 (int)(ask), buffer); \
318 } \
319 fprintf(stderr, "\n%s:%d: %s Assertion '%.*s' != '%.*s' [ %.*s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, \
320 (int)(__expected_length), (__expected_str), \
321 (int)(__actual_length), (__actual_str), \
322 (int)(ask), buffer); \
323 exit(EXIT_FAILURE); \
324 } \
325 } while (0)
326
327 #define ASSERT_STRNE(__expected_str, __actual_str) \
328 do \
329 { \
330 size_t __expected_length; \
331 size_t __actual_length; \
332 int ret= yatl_strcmp(__expected_str, __actual_str, &__expected_length, &__actual_length); \
333 if (ret == 0) { \
334 if (YATL_FULL) { \
335 FAIL("Assertion '%.*s' == '%.*s'", \
336 (int)(__expected_length), (__expected_str), \
337 (int)__actual_length, (__actual_str)) ; \
338 } \
339 fprintf(stderr, "\n%s:%d: %s Assertion '%.*s' == '%.*s'\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, \
340 (int)(__expected_length), (__expected_str), \
341 (int)__actual_length, (__actual_str)) ; \
342 exit(EXIT_FAILURE); \
343 } \
344 } while (0)
345
346 #define ASSERT_STRNE_(__expected_str, __actual_str, ...) \
347 do \
348 { \
349 size_t __expected_length; \
350 size_t __actual_length; \
351 int ret= yatl_strcmp(__expected_str, __actual_str, &__expected_length, &__actual_length); \
352 if (ret == 0) { \
353 size_t ask= snprintf(0, 0, __VA_ARGS__); \
354 ask++; \
355 char *buffer= (char*)alloca(sizeof(char) * ask); \
356 ask= snprintf(buffer, ask, __VA_ARGS__); \
357 if (YATL_FULL) { \
358 FAIL("Assertion '%.*s' == '%.*s' [ %.*s ]", \
359 (int)(__expected_length), (__expected_str), \
360 (int)(__actual_length), (__actual_str), \
361 (int)(ask), buffer); \
362 } \
363 fprintf(stderr, "\n%s:%d: %s Assertion '%.*s' == '%.*s' [ %.*s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, \
364 (int)(__expected_length), (__expected_str), \
365 (int)(__actual_length), (__actual_str), \
366 (int)(ask), buffer); \
367 exit(EXIT_FAILURE); \
368 } \
369 } while (0)
370
371 #define ASSERT_NEQ(__expected, __actual) \
372 do \
373 { \
374 if ((__expected) == (__actual)) { \
375 if (YATL_FULL) { \
376 FAIL("Assertion '%s' == '%s'", #__expected, #__actual); \
377 } \
378 fprintf(stderr, "\n%s:%d: %s Assertion '%s' == '%s'\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expected, #__actual); \
379 exit(EXIT_FAILURE); \
380 } \
381 } while (0)
382
383 #define ASSERT_NEQ_(__expected, __actual, ...) \
384 do \
385 { \
386 if ((__expected) == (__actual)) { \
387 size_t ask= snprintf(0, 0, __VA_ARGS__); \
388 ask++; \
389 char *buffer= (char*)alloca(sizeof(char) * ask); \
390 snprintf(buffer, ask, __VA_ARGS__); \
391 if (YATL_FULL) { \
392 FAIL("Assertion '%s' == '%s' [ %s ]", #__expected, #__actual, buffer); \
393 } \
394 fprintf(stderr, "\n%s:%d: %s Assertion '%s' == '%s' [ %s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expected, #__actual, buffer); \
395 exit(EXIT_FAILURE); \
396 } \
397 } while (0)
398
399 #define ASSERT_FALSE_(__expression, ...) \
400 do \
401 { \
402 if ((__expression)) { \
403 size_t ask= snprintf(0, 0, __VA_ARGS__); \
404 ask++; \
405 char *buffer= (char*)alloca(sizeof(char) * ask); \
406 snprintf(buffer, ask, __VA_ARGS__); \
407 if (YATL_FULL) { \
408 FAIL("Assertion '!%s' [ %s ]", #__expression, buffer); \
409 } \
410 fprintf(stderr, "\n%s:%d: %s Assertion '!%s' [ %s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression, buffer); \
411 exit(EXIT_FAILURE); \
412 } \
413 } while (0)