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 ASSERT_TRUE(__expression) \
146 if (! (__expression)) { \
148 FAIL("Assertion '%s'", #__expression); \
150 fprintf(stderr, "\n%s:%d: %s Assertion '%s'\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression);\
151 exit(EXIT_FAILURE); \
155 #define ASSERT_FALSE(__expression) \
158 if ((__expression)) { \
160 FAIL("Assertion '!%s'", #__expression); \
162 fprintf(stderr, "\n%s:%d: %s Assertion '!%s'\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression);\
163 exit(EXIT_FAILURE); \
167 #define ASSERT_NULL_(__expression, ...) \
170 if ((__expression) != NULL) { \
171 size_t ask= snprintf(0, 0, __VA_ARGS__); \
173 char *buffer= (char*)alloca(sizeof(char) * ask); \
174 snprintf(buffer, ask, __VA_ARGS__); \
176 FAIL("Assertion '%s' != NULL [ %s ]", #__expression, buffer);\
178 fprintf(stderr, "\n%s:%d: %s Assertion '%s' != NULL [ %s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression, buffer);\
179 exit(EXIT_FAILURE); \
183 #define ASSERT_NOT_NULL(__expression) \
186 if ((__expression) == NULL) { \
188 FAIL("Assertion '%s' == NULL", #__expression,);\
190 fprintf(stderr, "\n%s:%d: %s Assertion '%s' == NULL\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression,);\
191 exit(EXIT_FAILURE); \
195 #define ASSERT_NOT_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_TRUE_(__expression, ...) \
214 if (! (__expression)) { \
215 size_t ask= snprintf(0, 0, __VA_ARGS__); \
217 char *buffer= (char*)alloca(sizeof(char) * ask); \
218 snprintf(buffer, ask, __VA_ARGS__); \
220 FAIL("Assertion '%s' [ %s ]", #__expression, buffer); \
222 fprintf(stderr, "\n%s:%d: %s Assertion '%s' [ %s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression, buffer); \
223 exit(EXIT_FAILURE); \
227 #define ASSERT_EQ(__expected, __actual) \
230 if ((__expected) != (__actual)) { \
232 FAIL("Assertion '%s' != '%s'", #__expected, #__actual); \
234 fprintf(stderr, "\n%s:%d: %s Assertion '%s' != '%s'\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expected, #__actual); \
235 exit(EXIT_FAILURE); \
239 #define ASSERT_EQ_(__expected, __actual, ...) \
242 if ((__expected) != (__actual)) { \
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' [ %s ]", #__expected, #__actual, buffer); \
250 fprintf(stderr, "\n%s:%d: %s Assertion '%s' != '%s' [ %s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expected, #__actual, buffer); \
251 exit(EXIT_FAILURE); \
255 #define ASSERT_STREQ(__expected_str, __actual_str) \
258 size_t __expected_length; \
259 size_t __actual_length; \
260 int ret= yatl_strcmp(__expected_str, __actual_str, &__expected_length, &__actual_length); \
263 FAIL("Assertion '%.*s' != '%.*s'\n", \
264 (int)(__expected_length), (__expected_str), \
265 (int)__actual_length, (__actual_str)) ; \
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); \
274 #define ASSERT_STREQ_(__expected_str, __actual_str, ...) \
277 size_t __expected_length; \
278 size_t __actual_length; \
279 int ret= yatl_strcmp(__expected_str, __actual_str, &__expected_length, &__actual_length); \
281 size_t ask= snprintf(0, 0, __VA_ARGS__); \
283 char *buffer= (char*)alloca(sizeof(char) * ask); \
284 ask= snprintf(buffer, ask, __VA_ARGS__); \
286 FAIL("Assertion '%.*s' != '%.*s' [ %.*s ]", \
287 (int)(__expected_length), (__expected_str), \
288 (int)(__actual_length), (__actual_str), \
289 (int)(ask), buffer); \
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); \
299 #define ASSERT_STRNE(__expected_str, __actual_str) \
302 size_t __expected_length; \
303 size_t __actual_length; \
304 int ret= yatl_strcmp(__expected_str, __actual_str, &__expected_length, &__actual_length); \
307 FAIL("Assertion '%.*s' == '%.*s'", \
308 (int)(__expected_length), (__expected_str), \
309 (int)__actual_length, (__actual_str)) ; \
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); \
318 #define ASSERT_STRNE_(__expected_str, __actual_str, ...) \
321 size_t __expected_length; \
322 size_t __actual_length; \
323 int ret= yatl_strcmp(__expected_str, __actual_str, &__expected_length, &__actual_length); \
325 size_t ask= snprintf(0, 0, __VA_ARGS__); \
327 char *buffer= (char*)alloca(sizeof(char) * ask); \
328 ask= snprintf(buffer, ask, __VA_ARGS__); \
330 FAIL("Assertion '%.*s' == '%.*s' [ %.*s ]", \
331 (int)(__expected_length), (__expected_str), \
332 (int)(__actual_length), (__actual_str), \
333 (int)(ask), buffer); \
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); \
343 #define ASSERT_NEQ(__expected, __actual) \
346 if ((__expected) == (__actual)) { \
348 FAIL("Assertion '%s' == '%s'", #__expected, #__actual); \
350 fprintf(stderr, "\n%s:%d: %s Assertion '%s' == '%s'\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expected, #__actual); \
351 exit(EXIT_FAILURE); \
355 #define ASSERT_NEQ_(__expected, __actual, ...) \
358 if ((__expected) == (__actual)) { \
359 size_t ask= snprintf(0, 0, __VA_ARGS__); \
361 char *buffer= (char*)alloca(sizeof(char) * ask); \
362 snprintf(buffer, ask, __VA_ARGS__); \
364 FAIL("Assertion '%s' == '%s' [ %s ]", #__expected, #__actual, buffer); \
366 fprintf(stderr, "\n%s:%d: %s Assertion '%s' == '%s' [ %s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expected, #__actual, buffer); \
367 exit(EXIT_FAILURE); \
371 #define ASSERT_FALSE_(__expression, ...) \
374 if ((__expression)) { \
375 size_t ask= snprintf(0, 0, __VA_ARGS__); \
377 char *buffer= (char*)alloca(sizeof(char) * ask); \
378 snprintf(buffer, ask, __VA_ARGS__); \
380 FAIL("Assertion '!%s' [ %s ]", #__expression, buffer); \
382 fprintf(stderr, "\n%s:%d: %s Assertion '!%s' [ %s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression, buffer); \
383 exit(EXIT_FAILURE); \