342be97d820bc85abcd244ba4a65cffeaee33987
[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 #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 ASSERT_TRUE(__expression) \
144 do \
145 { \
146 if (! (__expression)) { \
147 if (YATL_FULL) { \
148 FAIL("Assertion '%s'", #__expression); \
149 } \
150 fprintf(stderr, "\n%s:%d: %s Assertion '%s'\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression);\
151 exit(EXIT_FAILURE); \
152 } \
153 } while (0)
154
155 #define ASSERT_FALSE(__expression) \
156 do \
157 { \
158 if ((__expression)) { \
159 if (YATL_FULL) { \
160 FAIL("Assertion '!%s'", #__expression); \
161 } \
162 fprintf(stderr, "\n%s:%d: %s Assertion '!%s'\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression);\
163 exit(EXIT_FAILURE); \
164 } \
165 } while (0)
166
167 #define ASSERT_NULL_(__expression, ...) \
168 do \
169 { \
170 if ((__expression) != NULL) { \
171 size_t ask= snprintf(0, 0, __VA_ARGS__); \
172 ask++; \
173 char *buffer= (char*)alloca(sizeof(char) * ask); \
174 snprintf(buffer, ask, __VA_ARGS__); \
175 if (YATL_FULL) { \
176 FAIL("Assertion '%s' != NULL [ %s ]", #__expression, buffer);\
177 } \
178 fprintf(stderr, "\n%s:%d: %s Assertion '%s' != NULL [ %s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression, buffer);\
179 exit(EXIT_FAILURE); \
180 } \
181 } while (0)
182
183 #define ASSERT_NOT_NULL(__expression) \
184 do \
185 { \
186 if ((__expression) == NULL) { \
187 if (YATL_FULL) { \
188 FAIL("Assertion '%s' == NULL", #__expression,);\
189 } \
190 fprintf(stderr, "\n%s:%d: %s Assertion '%s' == NULL\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression,);\
191 exit(EXIT_FAILURE); \
192 } \
193 } while (0)
194
195 #define ASSERT_NOT_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_TRUE_(__expression, ...) \
212 do \
213 { \
214 if (! (__expression)) { \
215 size_t ask= snprintf(0, 0, __VA_ARGS__); \
216 ask++; \
217 char *buffer= (char*)alloca(sizeof(char) * ask); \
218 snprintf(buffer, ask, __VA_ARGS__); \
219 if (YATL_FULL) { \
220 FAIL("Assertion '%s' [ %s ]", #__expression, buffer); \
221 } \
222 fprintf(stderr, "\n%s:%d: %s Assertion '%s' [ %s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression, buffer); \
223 exit(EXIT_FAILURE); \
224 } \
225 } while (0)
226
227 #define ASSERT_EQ(__expected, __actual) \
228 do \
229 { \
230 if ((__expected) != (__actual)) { \
231 if (YATL_FULL) { \
232 FAIL("Assertion '%s' != '%s'", #__expected, #__actual); \
233 } \
234 fprintf(stderr, "\n%s:%d: %s Assertion '%s' != '%s'\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expected, #__actual); \
235 exit(EXIT_FAILURE); \
236 } \
237 } while (0)
238
239 #define ASSERT_EQ_(__expected, __actual, ...) \
240 do \
241 { \
242 if ((__expected) != (__actual)) { \
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' [ %s ]", #__expected, #__actual, buffer); \
249 } \
250 fprintf(stderr, "\n%s:%d: %s Assertion '%s' != '%s' [ %s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expected, #__actual, buffer); \
251 exit(EXIT_FAILURE); \
252 } \
253 } while (0)
254
255 #define ASSERT_STREQ(__expected_str, __actual_str) \
256 do \
257 { \
258 size_t __expected_length; \
259 size_t __actual_length; \
260 int ret= yatl_strcmp(__expected_str, __actual_str, &__expected_length, &__actual_length); \
261 if (ret) { \
262 if (YATL_FULL) { \
263 FAIL("Assertion '%.*s' != '%.*s'\n", \
264 (int)(__expected_length), (__expected_str), \
265 (int)__actual_length, (__actual_str)) ; \
266 } \
267 fprintf(stderr, "\n%s:%d: %s Assertion '%.*s' != '%.*s'\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, \
268 (int)(__expected_length), (__expected_str), \
269 (int)__actual_length, (__actual_str)) ; \
270 exit(EXIT_FAILURE); \
271 } \
272 } while (0)
273
274 #define ASSERT_STREQ_(__expected_str, __actual_str, ...) \
275 do \
276 { \
277 size_t __expected_length; \
278 size_t __actual_length; \
279 int ret= yatl_strcmp(__expected_str, __actual_str, &__expected_length, &__actual_length); \
280 if (ret) { \
281 size_t ask= snprintf(0, 0, __VA_ARGS__); \
282 ask++; \
283 char *buffer= (char*)alloca(sizeof(char) * ask); \
284 ask= snprintf(buffer, ask, __VA_ARGS__); \
285 if (YATL_FULL) { \
286 FAIL("Assertion '%.*s' != '%.*s' [ %.*s ]", \
287 (int)(__expected_length), (__expected_str), \
288 (int)(__actual_length), (__actual_str), \
289 (int)(ask), buffer); \
290 } \
291 fprintf(stderr, "\n%s:%d: %s Assertion '%.*s' != '%.*s' [ %.*s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, \
292 (int)(__expected_length), (__expected_str), \
293 (int)(__actual_length), (__actual_str), \
294 (int)(ask), buffer); \
295 exit(EXIT_FAILURE); \
296 } \
297 } while (0)
298
299 #define ASSERT_STRNE(__expected_str, __actual_str) \
300 do \
301 { \
302 size_t __expected_length; \
303 size_t __actual_length; \
304 int ret= yatl_strcmp(__expected_str, __actual_str, &__expected_length, &__actual_length); \
305 if (ret == 0) { \
306 if (YATL_FULL) { \
307 FAIL("Assertion '%.*s' == '%.*s'", \
308 (int)(__expected_length), (__expected_str), \
309 (int)__actual_length, (__actual_str)) ; \
310 } \
311 fprintf(stderr, "\n%s:%d: %s Assertion '%.*s' == '%.*s'\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, \
312 (int)(__expected_length), (__expected_str), \
313 (int)__actual_length, (__actual_str)) ; \
314 exit(EXIT_FAILURE); \
315 } \
316 } while (0)
317
318 #define ASSERT_STRNE_(__expected_str, __actual_str, ...) \
319 do \
320 { \
321 size_t __expected_length; \
322 size_t __actual_length; \
323 int ret= yatl_strcmp(__expected_str, __actual_str, &__expected_length, &__actual_length); \
324 if (ret == 0) { \
325 size_t ask= snprintf(0, 0, __VA_ARGS__); \
326 ask++; \
327 char *buffer= (char*)alloca(sizeof(char) * ask); \
328 ask= snprintf(buffer, ask, __VA_ARGS__); \
329 if (YATL_FULL) { \
330 FAIL("Assertion '%.*s' == '%.*s' [ %.*s ]", \
331 (int)(__expected_length), (__expected_str), \
332 (int)(__actual_length), (__actual_str), \
333 (int)(ask), buffer); \
334 } \
335 fprintf(stderr, "\n%s:%d: %s Assertion '%.*s' == '%.*s' [ %.*s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, \
336 (int)(__expected_length), (__expected_str), \
337 (int)(__actual_length), (__actual_str), \
338 (int)(ask), buffer); \
339 exit(EXIT_FAILURE); \
340 } \
341 } while (0)
342
343 #define ASSERT_NEQ(__expected, __actual) \
344 do \
345 { \
346 if ((__expected) == (__actual)) { \
347 if (YATL_FULL) { \
348 FAIL("Assertion '%s' == '%s'", #__expected, #__actual); \
349 } \
350 fprintf(stderr, "\n%s:%d: %s Assertion '%s' == '%s'\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expected, #__actual); \
351 exit(EXIT_FAILURE); \
352 } \
353 } while (0)
354
355 #define ASSERT_NEQ_(__expected, __actual, ...) \
356 do \
357 { \
358 if ((__expected) == (__actual)) { \
359 size_t ask= snprintf(0, 0, __VA_ARGS__); \
360 ask++; \
361 char *buffer= (char*)alloca(sizeof(char) * ask); \
362 snprintf(buffer, ask, __VA_ARGS__); \
363 if (YATL_FULL) { \
364 FAIL("Assertion '%s' == '%s' [ %s ]", #__expected, #__actual, buffer); \
365 } \
366 fprintf(stderr, "\n%s:%d: %s Assertion '%s' == '%s' [ %s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expected, #__actual, buffer); \
367 exit(EXIT_FAILURE); \
368 } \
369 } while (0)
370
371 #define ASSERT_FALSE_(__expression, ...) \
372 do \
373 { \
374 if ((__expression)) { \
375 size_t ask= snprintf(0, 0, __VA_ARGS__); \
376 ask++; \
377 char *buffer= (char*)alloca(sizeof(char) * ask); \
378 snprintf(buffer, ask, __VA_ARGS__); \
379 if (YATL_FULL) { \
380 FAIL("Assertion '!%s' [ %s ]", #__expression, buffer); \
381 } \
382 fprintf(stderr, "\n%s:%d: %s Assertion '!%s' [ %s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression, buffer); \
383 exit(EXIT_FAILURE); \
384 } \
385 } while (0)