First pass on sync with gearman yatl.
[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 <cstddef>
41 # include <cstdlib>
42 # include <cstring>
43 #include <cstdarg>
44 #else
45 # include <stddef.h>
46 # include <stdlib.h>
47 # include <stdbool.h>
48 # include <string.h>
49 # include <stdarg.h>
50 #endif
51
52 #include <alloca.h>
53
54 #ifndef __PRETTY_FUNCTION__
55 # define __PRETTY_FUNCTION__ __func__
56 #endif
57
58 #ifndef EXIT_SKIP
59 # define EXIT_SKIP 77
60 #endif
61
62 #ifndef YATL_FULL
63 # define YATL_FULL 0
64 #endif
65
66 #ifndef FAIL
67 # define FAIL(__message_format, ...)
68 #endif
69
70 static inline bool valgrind_is_caller(void)
71 {
72 if (getenv("TESTS_ENVIRONMENT") && strstr(getenv("TESTS_ENVIRONMENT"), "valgrind"))
73 {
74 return true;
75 }
76
77 return false;
78 }
79
80 static inline size_t yatl_strlen(const char *s)
81 {
82 if (s)
83 {
84 return strlen(s);
85 }
86
87 return (size_t)(0);
88 }
89
90 static inline int yatl_strcmp(const char *s1, const char *s2, size_t *s1_length, size_t *s2_length)
91 {
92 *s1_length= yatl_strlen(s1);
93 *s2_length= yatl_strlen(s2);
94
95 if (*s1_length == 0 && *s1_length == *s2_length)
96 {
97 return 0;
98 }
99
100 if (*s1_length == 0 && *s2_length)
101 {
102 return 1;
103 }
104
105 if (*s1_length && *s2_length == 0)
106 {
107 return 1;
108 }
109
110 return strcmp(s1, s2);
111 }
112
113 #define SKIP_IF(__expression) \
114 do \
115 { \
116 if ((__expression)) { \
117 fprintf(stderr, "\n%s:%d: %s SKIP '!%s'\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression); \
118 exit(EXIT_SKIP); \
119 } \
120 } while (0)
121
122 #define ASSERT_TRUE(__expression) \
123 do \
124 { \
125 if (! (__expression)) { \
126 if (YATL_FULL) { \
127 FAIL("Assertion '%s'", #__expression); \
128 } \
129 fprintf(stderr, "\n%s:%d: %s Assertion '%s'\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression);\
130 exit(EXIT_FAILURE); \
131 } \
132 } while (0)
133
134 #define ASSERT_FALSE(__expression) \
135 do \
136 { \
137 if ((__expression)) { \
138 if (YATL_FULL) { \
139 FAIL("Assertion '!%s'", #__expression); \
140 } \
141 fprintf(stderr, "\n%s:%d: %s Assertion '!%s'\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression);\
142 exit(EXIT_FAILURE); \
143 } \
144 } while (0)
145
146 #define ASSERT_NULL_(__expression, ...) \
147 do \
148 { \
149 if ((__expression) != NULL) { \
150 size_t ask= snprintf(0, 0, __VA_ARGS__); \
151 ask++; \
152 char *buffer= (char*)malloc(sizeof(char) * ask); \
153 snprintf(buffer, ask, __VA_ARGS__); \
154 fprintf(stderr, "\n%s:%d: %s Assertion '%s' != NULL [ %s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression, buffer);\
155 free(buffer); \
156 exit(EXIT_FAILURE); \
157 } \
158 } while (0)
159
160 #define ASSERT_NOT_NULL(__expression) \
161 do \
162 { \
163 if ((__expression) == NULL) { \
164 fprintf(stderr, "\n%s:%d: %s Assertion '%s' == NULL\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression,);\
165 exit(EXIT_FAILURE); \
166 } \
167 } while (0)
168
169 #define ASSERT_NOT_NULL_(__expression, ...) \
170 do \
171 { \
172 if ((__expression) == NULL) { \
173 size_t ask= snprintf(0, 0, __VA_ARGS__); \
174 ask++; \
175 char *buffer= (char*)malloc(sizeof(char) * ask); \
176 snprintf(buffer, ask, __VA_ARGS__); \
177 fprintf(stderr, "\n%s:%d: %s Assertion '%s' == NULL [ %s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression, buffer);\
178 free(buffer); \
179 exit(EXIT_FAILURE); \
180 } \
181 } while (0)
182
183 #define SKIP_IF_(__expression, ...) \
184 do \
185 { \
186 if ((__expression)) { \
187 size_t ask= snprintf(0, 0, __VA_ARGS__); \
188 ask++; \
189 char *buffer= (char*)malloc(sizeof(char) * ask); \
190 snprintf(buffer, ask, __VA_ARGS__); \
191 fprintf(stdout, "\n%s:%d: %s SKIP '%s' [ %s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression, buffer); \
192 free(buffer); \
193 exit(EXIT_SKIP); \
194 } \
195 } while (0)
196
197 #define ASSERT_TRUE_(__expression, ...) \
198 do \
199 { \
200 if (! (__expression)) { \
201 size_t ask= snprintf(0, 0, __VA_ARGS__); \
202 ask++; \
203 char *buffer= (char*)malloc(sizeof(char) * ask); \
204 snprintf(buffer, ask, __VA_ARGS__); \
205 fprintf(stderr, "\n%s:%d: %s Assertion '%s' [ %s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression, buffer); \
206 free(buffer); \
207 exit(EXIT_FAILURE); \
208 } \
209 } while (0)
210
211 #define ASSERT_EQ(__expected, __actual) \
212 do \
213 { \
214 if ((__expected) != (__actual)) { \
215 fprintf(stderr, "\n%s:%d: %s Assertion '%s' != '%s'\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expected, #__actual); \
216 exit(EXIT_FAILURE); \
217 } \
218 } while (0)
219
220 #define ASSERT_EQ_(__expected, __actual, ...) \
221 do \
222 { \
223 if ((__expected) != (__actual)) { \
224 size_t ask= snprintf(0, 0, __VA_ARGS__); \
225 ask++; \
226 char *buffer= (char*)malloc(sizeof(char) * ask); \
227 snprintf(buffer, ask, __VA_ARGS__); \
228 fprintf(stderr, "\n%s:%d: %s Assertion '%s' != '%s' [ %s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expected, #__actual, buffer); \
229 free(buffer); \
230 exit(EXIT_FAILURE); \
231 } \
232 } while (0)
233
234 #define ASSERT_STREQ(__expected_str, __actual_str) \
235 do \
236 { \
237 size_t __expected_length; \
238 size_t __actual_length; \
239 int ret= yatl_strcmp(__expected_str, __actual_str, &__expected_length, &__actual_length); \
240 if (ret) { \
241 fprintf(stderr, "\n%s:%d: %s Assertion '%.*s' != '%.*s'\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, \
242 (int)(__expected_length), (__expected_str), \
243 (int)__actual_length, (__actual_str)) ; \
244 exit(EXIT_FAILURE); \
245 } \
246 } while (0)
247
248 #define ASSERT_STREQ_(__expected_str, __actual_str, ...) \
249 do \
250 { \
251 size_t __expected_length; \
252 size_t __actual_length; \
253 int ret= yatl_strcmp(__expected_str, __actual_str, &__expected_length, &__actual_length); \
254 if (ret) { \
255 size_t ask= snprintf(0, 0, __VA_ARGS__); \
256 ask++; \
257 char *buffer= (char*)malloc(sizeof(char) * ask); \
258 ask= snprintf(buffer, ask, __VA_ARGS__); \
259 fprintf(stderr, "\n%s:%d: %s Assertion '%.*s' != '%.*s' [ %.*s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, \
260 (int)(__expected_length), (__expected_str), \
261 (int)(__actual_length), (__actual_str), \
262 (int)(ask), buffer); \
263 free(buffer); \
264 exit(EXIT_FAILURE); \
265 } \
266 } while (0)
267
268 #define ASSERT_STRNE(__expected_str, __actual_str) \
269 do \
270 { \
271 size_t __expected_length; \
272 size_t __actual_length; \
273 int ret= yatl_strcmp(__expected_str, __actual_str, &__expected_length, &__actual_length); \
274 if (ret == 0) { \
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_STRNE_(__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 == 0) { \
289 size_t ask= snprintf(0, 0, __VA_ARGS__); \
290 ask++; \
291 char *buffer= (char*)malloc(sizeof(char) * ask); \
292 ask= snprintf(buffer, ask, __VA_ARGS__); \
293 fprintf(stderr, "\n%s:%d: %s Assertion '%.*s' == '%.*s' [ %.*s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, \
294 (int)(__expected_length), (__expected_str), \
295 (int)(__actual_length), (__actual_str), \
296 (int)(ask), buffer); \
297 free(buffer); \
298 exit(EXIT_FAILURE); \
299 } \
300 } while (0)
301
302 #define ASSERT_NEQ(__expected, __actual, ...) \
303 do \
304 { \
305 if ((__expected) == (__actual)) { \
306 fprintf(stderr, "\n%s:%d: %s Assertion '%s' == '%s'\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expected, #__actual); \
307 exit(EXIT_FAILURE); \
308 } \
309 } while (0)
310
311 #define ASSERT_NEQ_(__expected, __actual, ...) \
312 do \
313 { \
314 if ((__expected) == (__actual)) { \
315 size_t ask= snprintf(0, 0, __VA_ARGS__); \
316 ask++; \
317 char *buffer= (char*)malloc(sizeof(char) * ask); \
318 snprintf(buffer, ask, __VA_ARGS__); \
319 fprintf(stderr, "\n%s:%d: %s Assertion '%s' == '%s' [ %s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expected, #__actual, buffer); \
320 free(buffer); \
321 exit(EXIT_FAILURE); \
322 } \
323 } while (0)
324
325 #define ASSERT_FALSE_(__expression, ...) \
326 do \
327 { \
328 if ((__expression)) { \
329 size_t ask= snprintf(0, 0, __VA_ARGS__); \
330 ask++; \
331 char *buffer= (char*)alloca(sizeof(char) * ask); \
332 snprintf(buffer, ask, __VA_ARGS__); \
333 if (YATL_FULL) { \
334 throw libtest::__failure(__FILE__, __LINE__, __PRETTY_FUNCTION__, "Assertion '!%s' [ %s ]", #__expression, buffer); \
335 } \
336 fprintf(stderr, "\n%s:%d: %s Assertion '!%s' [ %s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression, buffer); \
337 exit(EXIT_FAILURE); \
338 } \
339 } while (0)