Harmonize WIN32, and update bootstrap
[awesomized/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 static inline bool valgrind_is_caller(void)
81 {
82 if (getenv("TESTS_ENVIRONMENT") && strstr(getenv("TESTS_ENVIRONMENT"), "valgrind"))
83 {
84 return true;
85 }
86
87 return false;
88 }
89
90 static inline size_t yatl_strlen(const char *s)
91 {
92 if (s)
93 {
94 return strlen(s);
95 }
96
97 return (size_t)(0);
98 }
99
100 static inline int yatl_strcmp(const char *s1, const char *s2, size_t *s1_length, size_t *s2_length)
101 {
102 *s1_length= yatl_strlen(s1);
103 *s2_length= yatl_strlen(s2);
104
105 if (*s1_length == 0 && *s1_length == *s2_length)
106 {
107 return 0;
108 }
109
110 if (*s1_length == 0 && *s2_length)
111 {
112 return 1;
113 }
114
115 if (*s1_length && *s2_length == 0)
116 {
117 return 1;
118 }
119
120 return strcmp(s1, s2);
121 }
122
123 #define SKIP_IF(__expression) \
124 do \
125 { \
126 if ((__expression)) { \
127 if (YATL_FULL) { \
128 SKIP(#__expression); \
129 } \
130 fprintf(stdout, "\n%s:%d: %s SKIP '!(%s)'\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression); \
131 exit(EXIT_SKIP); \
132 } \
133 } while (0)
134
135 #define SKIP_IF_(__expression, ...) \
136 do \
137 { \
138 if ((__expression)) { \
139 size_t ask= snprintf(0, 0, __VA_ARGS__); \
140 ask++; \
141 char *buffer= (char*)alloca(sizeof(char) * ask); \
142 snprintf(buffer, ask, __VA_ARGS__); \
143 if (YATL_FULL) { \
144 SKIP(#__expression, buffer); \
145 } \
146 fprintf(stdout, "\n%s:%d: %s SKIP '%s' [ %s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression, buffer); \
147 exit(EXIT_SKIP); \
148 } \
149 } while (0)
150
151 #define ASSERT_TRUE(__expression) \
152 do \
153 { \
154 if (! (__expression)) { \
155 if (YATL_FULL) { \
156 FAIL("Assertion '%s'", #__expression); \
157 } \
158 fprintf(stderr, "\n%s:%d: %s Assertion '%s'\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression);\
159 exit(EXIT_FAILURE); \
160 } \
161 } while (0)
162
163 #define ASSERT_FALSE(__expression) \
164 do \
165 { \
166 if ((__expression)) { \
167 if (YATL_FULL) { \
168 FAIL("Assertion '!%s'", #__expression); \
169 } \
170 fprintf(stderr, "\n%s:%d: %s Assertion '!%s'\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression);\
171 exit(EXIT_FAILURE); \
172 } \
173 } while (0)
174
175 #define ASSERT_NULL_(__expression, ...) \
176 do \
177 { \
178 if ((__expression) != NULL) { \
179 size_t ask= snprintf(0, 0, __VA_ARGS__); \
180 ask++; \
181 char *buffer= (char*)alloca(sizeof(char) * ask); \
182 snprintf(buffer, ask, __VA_ARGS__); \
183 if (YATL_FULL) { \
184 FAIL("Assertion '%s' != NULL [ %s ]", #__expression, buffer);\
185 } \
186 fprintf(stderr, "\n%s:%d: %s Assertion '%s' != NULL [ %s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression, buffer);\
187 exit(EXIT_FAILURE); \
188 } \
189 } while (0)
190
191 #define ASSERT_NOT_NULL(__expression) \
192 do \
193 { \
194 if ((__expression) == NULL) { \
195 if (YATL_FULL) { \
196 FAIL("Assertion '%s' == NULL", #__expression,);\
197 } \
198 fprintf(stderr, "\n%s:%d: %s Assertion '%s' == NULL\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression,);\
199 exit(EXIT_FAILURE); \
200 } \
201 } while (0)
202
203 #define ASSERT_NOT_NULL_(__expression, ...) \
204 do \
205 { \
206 if ((__expression) == NULL) { \
207 size_t ask= snprintf(0, 0, __VA_ARGS__); \
208 ask++; \
209 char *buffer= (char*)alloca(sizeof(char) * ask); \
210 snprintf(buffer, ask, __VA_ARGS__); \
211 if (YATL_FULL) { \
212 FAIL("Assertion '%s' == NULL [ %s ]", #__expression, buffer);\
213 } \
214 fprintf(stderr, "\n%s:%d: %s Assertion '%s' == NULL [ %s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression, buffer);\
215 exit(EXIT_FAILURE); \
216 } \
217 } while (0)
218
219 #define ASSERT_TRUE_(__expression, ...) \
220 do \
221 { \
222 if (! (__expression)) { \
223 size_t ask= snprintf(0, 0, __VA_ARGS__); \
224 ask++; \
225 char *buffer= (char*)alloca(sizeof(char) * ask); \
226 snprintf(buffer, ask, __VA_ARGS__); \
227 if (YATL_FULL) { \
228 FAIL("Assertion '%s' [ %s ]", #__expression, buffer); \
229 } \
230 fprintf(stderr, "\n%s:%d: %s Assertion '%s' [ %s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression, buffer); \
231 exit(EXIT_FAILURE); \
232 } \
233 } while (0)
234
235 #define ASSERT_EQ(__expected, __actual) \
236 do \
237 { \
238 if ((__expected) != (__actual)) { \
239 if (YATL_FULL) { \
240 FAIL("Assertion '%s' != '%s'", #__expected, #__actual); \
241 } \
242 fprintf(stderr, "\n%s:%d: %s Assertion '%s' != '%s'\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expected, #__actual); \
243 exit(EXIT_FAILURE); \
244 } \
245 } while (0)
246
247 #define ASSERT_EQ_(__expected, __actual, ...) \
248 do \
249 { \
250 if ((__expected) != (__actual)) { \
251 size_t ask= snprintf(0, 0, __VA_ARGS__); \
252 ask++; \
253 char *buffer= (char*)alloca(sizeof(char) * ask); \
254 snprintf(buffer, ask, __VA_ARGS__); \
255 if (YATL_FULL) { \
256 FAIL("Assertion '%s' != '%s' [ %s ]", #__expected, #__actual, buffer); \
257 } \
258 fprintf(stderr, "\n%s:%d: %s Assertion '%s' != '%s' [ %s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expected, #__actual, buffer); \
259 exit(EXIT_FAILURE); \
260 } \
261 } while (0)
262
263 #define ASSERT_STREQ(__expected_str, __actual_str) \
264 do \
265 { \
266 size_t __expected_length; \
267 size_t __actual_length; \
268 int ret= yatl_strcmp(__expected_str, __actual_str, &__expected_length, &__actual_length); \
269 if (ret) { \
270 if (YATL_FULL) { \
271 FAIL("Assertion '%.*s' != '%.*s'\n", \
272 (int)(__expected_length), (__expected_str), \
273 (int)__actual_length, (__actual_str)) ; \
274 } \
275 fprintf(stderr, "\n%s:%d: %s Assertion '%.*s' != '%.*s'\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, \
276 (int)(__expected_length), (__expected_str), \
277 (int)__actual_length, (__actual_str)) ; \
278 exit(EXIT_FAILURE); \
279 } \
280 } while (0)
281
282 #define ASSERT_STREQ_(__expected_str, __actual_str, ...) \
283 do \
284 { \
285 size_t __expected_length; \
286 size_t __actual_length; \
287 int ret= yatl_strcmp(__expected_str, __actual_str, &__expected_length, &__actual_length); \
288 if (ret) { \
289 size_t ask= snprintf(0, 0, __VA_ARGS__); \
290 ask++; \
291 char *buffer= (char*)alloca(sizeof(char) * ask); \
292 ask= snprintf(buffer, ask, __VA_ARGS__); \
293 if (YATL_FULL) { \
294 FAIL("Assertion '%.*s' != '%.*s' [ %.*s ]", \
295 (int)(__expected_length), (__expected_str), \
296 (int)(__actual_length), (__actual_str), \
297 (int)(ask), buffer); \
298 } \
299 fprintf(stderr, "\n%s:%d: %s Assertion '%.*s' != '%.*s' [ %.*s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, \
300 (int)(__expected_length), (__expected_str), \
301 (int)(__actual_length), (__actual_str), \
302 (int)(ask), buffer); \
303 exit(EXIT_FAILURE); \
304 } \
305 } while (0)
306
307 #define ASSERT_STRNE(__expected_str, __actual_str) \
308 do \
309 { \
310 size_t __expected_length; \
311 size_t __actual_length; \
312 int ret= yatl_strcmp(__expected_str, __actual_str, &__expected_length, &__actual_length); \
313 if (ret == 0) { \
314 if (YATL_FULL) { \
315 FAIL("Assertion '%.*s' == '%.*s'", \
316 (int)(__expected_length), (__expected_str), \
317 (int)__actual_length, (__actual_str)) ; \
318 } \
319 fprintf(stderr, "\n%s:%d: %s Assertion '%.*s' == '%.*s'\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, \
320 (int)(__expected_length), (__expected_str), \
321 (int)__actual_length, (__actual_str)) ; \
322 exit(EXIT_FAILURE); \
323 } \
324 } while (0)
325
326 #define ASSERT_STRNE_(__expected_str, __actual_str, ...) \
327 do \
328 { \
329 size_t __expected_length; \
330 size_t __actual_length; \
331 int ret= yatl_strcmp(__expected_str, __actual_str, &__expected_length, &__actual_length); \
332 if (ret == 0) { \
333 size_t ask= snprintf(0, 0, __VA_ARGS__); \
334 ask++; \
335 char *buffer= (char*)alloca(sizeof(char) * ask); \
336 ask= snprintf(buffer, ask, __VA_ARGS__); \
337 if (YATL_FULL) { \
338 FAIL("Assertion '%.*s' == '%.*s' [ %.*s ]", \
339 (int)(__expected_length), (__expected_str), \
340 (int)(__actual_length), (__actual_str), \
341 (int)(ask), buffer); \
342 } \
343 fprintf(stderr, "\n%s:%d: %s Assertion '%.*s' == '%.*s' [ %.*s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, \
344 (int)(__expected_length), (__expected_str), \
345 (int)(__actual_length), (__actual_str), \
346 (int)(ask), buffer); \
347 exit(EXIT_FAILURE); \
348 } \
349 } while (0)
350
351 #define ASSERT_NEQ(__expected, __actual, ...) \
352 do \
353 { \
354 if ((__expected) == (__actual)) { \
355 if (YATL_FULL) { \
356 FAIL("Assertion '%s' == '%s'", #__expected, #__actual); \
357 } \
358 fprintf(stderr, "\n%s:%d: %s Assertion '%s' == '%s'\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expected, #__actual); \
359 exit(EXIT_FAILURE); \
360 } \
361 } while (0)
362
363 #define ASSERT_NEQ_(__expected, __actual, ...) \
364 do \
365 { \
366 if ((__expected) == (__actual)) { \
367 size_t ask= snprintf(0, 0, __VA_ARGS__); \
368 ask++; \
369 char *buffer= (char*)alloca(sizeof(char) * ask); \
370 snprintf(buffer, ask, __VA_ARGS__); \
371 if (YATL_FULL) { \
372 FAIL("Assertion '%s' == '%s' [ %s ]", #__expected, #__actual, buffer); \
373 } \
374 fprintf(stderr, "\n%s:%d: %s Assertion '%s' == '%s' [ %s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expected, #__actual, buffer); \
375 exit(EXIT_FAILURE); \
376 } \
377 } while (0)
378
379 #define ASSERT_FALSE_(__expression, ...) \
380 do \
381 { \
382 if ((__expression)) { \
383 size_t ask= snprintf(0, 0, __VA_ARGS__); \
384 ask++; \
385 char *buffer= (char*)alloca(sizeof(char) * ask); \
386 snprintf(buffer, ask, __VA_ARGS__); \
387 if (YATL_FULL) { \
388 FAIL("Assertion '!%s' [ %s ]", #__expression, buffer); \
389 } \
390 fprintf(stderr, "\n%s:%d: %s Assertion '!%s' [ %s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression, buffer); \
391 exit(EXIT_FAILURE); \
392 } \
393 } while (0)