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