1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3 * Data Differential YATL (i.e. libtest) library
5 * Copyright (C) 2012 Data Differential, http://datadifferential.com/
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
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
19 * * The names of its contributors may not be used to endorse or
20 * promote products derived from this software without specific prior
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.
60 #ifndef __PRETTY_FUNCTION__
61 # define __PRETTY_FUNCTION__ __func__
73 # define FAIL(__message_format, ...)
77 # define SKIP(__message_format, ...)
80 #include <libtest/valgrind.h>
82 static inline size_t yatl_strlen(const char *s
)
92 static inline int yatl_strcmp(const char *s1
, const char *s2
, size_t *s1_length
, size_t *s2_length
)
94 *s1_length
= yatl_strlen(s1
);
95 *s2_length
= yatl_strlen(s2
);
97 if (*s1_length
== 0 && *s1_length
== *s2_length
)
102 if (*s1_length
== 0 && *s2_length
)
107 if (*s1_length
&& *s2_length
== 0)
112 return strcmp(s1
, s2
);
115 #define SKIP_IF(__expression) \
118 if ((__expression)) { \
120 SKIP(#__expression); \
122 fprintf(stdout, "\n%s:%d: %s SKIP '!(%s)'\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression); \
127 #define SKIP_IF_(__expression, ...) \
130 if ((__expression)) { \
131 size_t ask= snprintf(0, 0, __VA_ARGS__); \
133 char *buffer= (char*)alloca(sizeof(char) * ask); \
134 snprintf(buffer, ask, __VA_ARGS__); \
136 SKIP(#__expression, buffer); \
138 fprintf(stdout, "\n%s:%d: %s SKIP '%s' [ %s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression, buffer); \
143 #define SKIP_UNLESS(__expression) \
146 if (! (__expression)) { \
148 SKIP(#__expression); \
150 fprintf(stdout, "\n%s:%d: %s SKIP '(%s)'\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression); \
155 #define SKIP_UNLESS_(__expression, ...) \
158 if (! (__expression)) { \
159 size_t ask= snprintf(0, 0, __VA_ARGS__); \
161 char *buffer= (char*)alloca(sizeof(char) * ask); \
162 snprintf(buffer, ask, __VA_ARGS__); \
164 SKIP(#__expression, buffer); \
166 fprintf(stdout, "\n%s:%d: %s SKIP '%s' [ %s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression, buffer); \
171 #define ASSERT_TRUE(__expression) \
174 if (! (__expression)) { \
176 FAIL("Assertion '%s'", #__expression); \
178 fprintf(stderr, "\n%s:%d: %s Assertion '%s'\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression);\
179 exit(EXIT_FAILURE); \
183 #define ASSERT_FALSE(__expression) \
186 if ((__expression)) { \
188 FAIL("Assertion '!%s'", #__expression); \
190 fprintf(stderr, "\n%s:%d: %s Assertion '!%s'\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression);\
191 exit(EXIT_FAILURE); \
195 #define ASSERT_NULL_(__expression, ...) \
198 if ((__expression) != NULL) { \
199 size_t ask= snprintf(0, 0, __VA_ARGS__); \
201 char *buffer= (char*)alloca(sizeof(char) * ask); \
202 snprintf(buffer, ask, __VA_ARGS__); \
204 FAIL("Assertion '%s' != NULL [ %s ]", #__expression, buffer);\
206 fprintf(stderr, "\n%s:%d: %s Assertion '%s' != NULL [ %s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression, buffer);\
207 exit(EXIT_FAILURE); \
211 #define ASSERT_NOT_NULL(__expression) \
214 if ((__expression) == NULL) { \
216 FAIL("Assertion '%s' == NULL", #__expression,);\
218 fprintf(stderr, "\n%s:%d: %s Assertion '%s' == NULL\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression,);\
219 exit(EXIT_FAILURE); \
223 #define ASSERT_NOT_NULL_(__expression, ...) \
226 if ((__expression) == NULL) { \
227 size_t ask= snprintf(0, 0, __VA_ARGS__); \
229 char *buffer= (char*)alloca(sizeof(char) * ask); \
230 snprintf(buffer, ask, __VA_ARGS__); \
232 FAIL("Assertion '%s' == NULL [ %s ]", #__expression, buffer);\
234 fprintf(stderr, "\n%s:%d: %s Assertion '%s' == NULL [ %s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression, buffer);\
235 exit(EXIT_FAILURE); \
239 #define ASSERT_TRUE_(__expression, ...) \
242 if (! (__expression)) { \
243 size_t ask= snprintf(0, 0, __VA_ARGS__); \
245 char *buffer= (char*)alloca(sizeof(char) * ask); \
246 snprintf(buffer, ask, __VA_ARGS__); \
248 FAIL("Assertion '%s' [ %s ]", #__expression, buffer); \
250 fprintf(stderr, "\n%s:%d: %s Assertion '%s' [ %s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression, buffer); \
251 exit(EXIT_FAILURE); \
255 #define ASSERT_EQ(__expected, __actual) \
258 if ((__expected) != (__actual)) { \
260 FAIL("Assertion '%s' != '%s'", #__expected, #__actual); \
262 fprintf(stderr, "\n%s:%d: %s Assertion '%s' != '%s'\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expected, #__actual); \
263 exit(EXIT_FAILURE); \
267 #define ASSERT_EQ_(__expected, __actual, ...) \
270 if ((__expected) != (__actual)) { \
271 size_t ask= snprintf(0, 0, __VA_ARGS__); \
273 char *buffer= (char*)alloca(sizeof(char) * ask); \
274 snprintf(buffer, ask, __VA_ARGS__); \
276 FAIL("Assertion '%s' != '%s' [ %s ]", #__expected, #__actual, buffer); \
278 fprintf(stderr, "\n%s:%d: %s Assertion '%s' != '%s' [ %s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expected, #__actual, buffer); \
279 exit(EXIT_FAILURE); \
283 #define ASSERT_STREQ(__expected_str, __actual_str) \
286 size_t __expected_length; \
287 size_t __actual_length; \
288 int ret= yatl_strcmp(__expected_str, __actual_str, &__expected_length, &__actual_length); \
291 FAIL("Assertion '%.*s' != '%.*s'\n", \
292 (int)(__expected_length), (__expected_str), \
293 (int)__actual_length, (__actual_str)) ; \
295 fprintf(stderr, "\n%s:%d: %s Assertion '%.*s' != '%.*s'\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, \
296 (int)(__expected_length), (__expected_str), \
297 (int)__actual_length, (__actual_str)) ; \
298 exit(EXIT_FAILURE); \
302 #define ASSERT_STREQ_(__expected_str, __actual_str, ...) \
305 size_t __expected_length; \
306 size_t __actual_length; \
307 int ret= yatl_strcmp(__expected_str, __actual_str, &__expected_length, &__actual_length); \
309 size_t ask= snprintf(0, 0, __VA_ARGS__); \
311 char *buffer= (char*)alloca(sizeof(char) * ask); \
312 ask= snprintf(buffer, ask, __VA_ARGS__); \
314 FAIL("Assertion '%.*s' != '%.*s' [ %.*s ]", \
315 (int)(__expected_length), (__expected_str), \
316 (int)(__actual_length), (__actual_str), \
317 (int)(ask), buffer); \
319 fprintf(stderr, "\n%s:%d: %s Assertion '%.*s' != '%.*s' [ %.*s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, \
320 (int)(__expected_length), (__expected_str), \
321 (int)(__actual_length), (__actual_str), \
322 (int)(ask), buffer); \
323 exit(EXIT_FAILURE); \
327 #define ASSERT_STRNE(__expected_str, __actual_str) \
330 size_t __expected_length; \
331 size_t __actual_length; \
332 int ret= yatl_strcmp(__expected_str, __actual_str, &__expected_length, &__actual_length); \
335 FAIL("Assertion '%.*s' == '%.*s'", \
336 (int)(__expected_length), (__expected_str), \
337 (int)__actual_length, (__actual_str)) ; \
339 fprintf(stderr, "\n%s:%d: %s Assertion '%.*s' == '%.*s'\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, \
340 (int)(__expected_length), (__expected_str), \
341 (int)__actual_length, (__actual_str)) ; \
342 exit(EXIT_FAILURE); \
346 #define ASSERT_STRNE_(__expected_str, __actual_str, ...) \
349 size_t __expected_length; \
350 size_t __actual_length; \
351 int ret= yatl_strcmp(__expected_str, __actual_str, &__expected_length, &__actual_length); \
353 size_t ask= snprintf(0, 0, __VA_ARGS__); \
355 char *buffer= (char*)alloca(sizeof(char) * ask); \
356 ask= snprintf(buffer, ask, __VA_ARGS__); \
358 FAIL("Assertion '%.*s' == '%.*s' [ %.*s ]", \
359 (int)(__expected_length), (__expected_str), \
360 (int)(__actual_length), (__actual_str), \
361 (int)(ask), buffer); \
363 fprintf(stderr, "\n%s:%d: %s Assertion '%.*s' == '%.*s' [ %.*s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, \
364 (int)(__expected_length), (__expected_str), \
365 (int)(__actual_length), (__actual_str), \
366 (int)(ask), buffer); \
367 exit(EXIT_FAILURE); \
371 #define ASSERT_NEQ(__expected, __actual) \
374 if ((__expected) == (__actual)) { \
376 FAIL("Assertion '%s' == '%s'", #__expected, #__actual); \
378 fprintf(stderr, "\n%s:%d: %s Assertion '%s' == '%s'\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expected, #__actual); \
379 exit(EXIT_FAILURE); \
383 #define ASSERT_NEQ_(__expected, __actual, ...) \
386 if ((__expected) == (__actual)) { \
387 size_t ask= snprintf(0, 0, __VA_ARGS__); \
389 char *buffer= (char*)alloca(sizeof(char) * ask); \
390 snprintf(buffer, ask, __VA_ARGS__); \
392 FAIL("Assertion '%s' == '%s' [ %s ]", #__expected, #__actual, buffer); \
394 fprintf(stderr, "\n%s:%d: %s Assertion '%s' == '%s' [ %s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expected, #__actual, buffer); \
395 exit(EXIT_FAILURE); \
399 #define ASSERT_FALSE_(__expression, ...) \
402 if ((__expression)) { \
403 size_t ask= snprintf(0, 0, __VA_ARGS__); \
405 char *buffer= (char*)alloca(sizeof(char) * ask); \
406 snprintf(buffer, ask, __VA_ARGS__); \
408 FAIL("Assertion '!%s' [ %s ]", #__expression, buffer); \
410 fprintf(stderr, "\n%s:%d: %s Assertion '!%s' [ %s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression, buffer); \
411 exit(EXIT_FAILURE); \