Debian Codegraph @

storage/maria/libmarias3/yatl/lite.h

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