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 static inline bool valgrind_is_caller(void)
82 if (getenv("TESTS_ENVIRONMENT") && strstr(getenv("TESTS_ENVIRONMENT"), "valgrind"))
90 static inline size_t yatl_strlen(const char *s
)
100 static inline int yatl_strcmp(const char *s1
, const char *s2
, size_t *s1_length
, size_t *s2_length
)
102 *s1_length
= yatl_strlen(s1
);
103 *s2_length
= yatl_strlen(s2
);
105 if (*s1_length
== 0 && *s1_length
== *s2_length
)
110 if (*s1_length
== 0 && *s2_length
)
115 if (*s1_length
&& *s2_length
== 0)
120 return strcmp(s1
, s2
);
123 #define SKIP_IF(__expression) \
126 if ((__expression)) { \
128 SKIP(#__expression); \
130 fprintf(stdout, "\n%s:%d: %s SKIP '!(%s)'\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression); \
135 #define SKIP_IF_(__expression, ...) \
138 if ((__expression)) { \
139 size_t ask= snprintf(0, 0, __VA_ARGS__); \
141 char *buffer= (char*)alloca(sizeof(char) * ask); \
142 snprintf(buffer, ask, __VA_ARGS__); \
144 SKIP(#__expression, buffer); \
146 fprintf(stdout, "\n%s:%d: %s SKIP '%s' [ %s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression, buffer); \
151 #define ASSERT_TRUE(__expression) \
154 if (! (__expression)) { \
156 FAIL("Assertion '%s'", #__expression); \
158 fprintf(stderr, "\n%s:%d: %s Assertion '%s'\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression);\
159 exit(EXIT_FAILURE); \
163 #define ASSERT_FALSE(__expression) \
166 if ((__expression)) { \
168 FAIL("Assertion '!%s'", #__expression); \
170 fprintf(stderr, "\n%s:%d: %s Assertion '!%s'\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression);\
171 exit(EXIT_FAILURE); \
175 #define ASSERT_NULL_(__expression, ...) \
178 if ((__expression) != NULL) { \
179 size_t ask= snprintf(0, 0, __VA_ARGS__); \
181 char *buffer= (char*)alloca(sizeof(char) * ask); \
182 snprintf(buffer, ask, __VA_ARGS__); \
184 FAIL("Assertion '%s' != NULL [ %s ]", #__expression, buffer);\
186 fprintf(stderr, "\n%s:%d: %s Assertion '%s' != NULL [ %s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression, buffer);\
187 exit(EXIT_FAILURE); \
191 #define ASSERT_NOT_NULL(__expression) \
194 if ((__expression) == NULL) { \
196 FAIL("Assertion '%s' == NULL", #__expression,);\
198 fprintf(stderr, "\n%s:%d: %s Assertion '%s' == NULL\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression,);\
199 exit(EXIT_FAILURE); \
203 #define ASSERT_NOT_NULL_(__expression, ...) \
206 if ((__expression) == NULL) { \
207 size_t ask= snprintf(0, 0, __VA_ARGS__); \
209 char *buffer= (char*)alloca(sizeof(char) * ask); \
210 snprintf(buffer, ask, __VA_ARGS__); \
212 FAIL("Assertion '%s' == NULL [ %s ]", #__expression, buffer);\
214 fprintf(stderr, "\n%s:%d: %s Assertion '%s' == NULL [ %s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression, buffer);\
215 exit(EXIT_FAILURE); \
219 #define ASSERT_TRUE_(__expression, ...) \
222 if (! (__expression)) { \
223 size_t ask= snprintf(0, 0, __VA_ARGS__); \
225 char *buffer= (char*)alloca(sizeof(char) * ask); \
226 snprintf(buffer, ask, __VA_ARGS__); \
228 FAIL("Assertion '%s' [ %s ]", #__expression, buffer); \
230 fprintf(stderr, "\n%s:%d: %s Assertion '%s' [ %s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression, buffer); \
231 exit(EXIT_FAILURE); \
235 #define ASSERT_EQ(__expected, __actual) \
238 if ((__expected) != (__actual)) { \
240 FAIL("Assertion '%s' != '%s'", #__expected, #__actual); \
242 fprintf(stderr, "\n%s:%d: %s Assertion '%s' != '%s'\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expected, #__actual); \
243 exit(EXIT_FAILURE); \
247 #define ASSERT_EQ_(__expected, __actual, ...) \
250 if ((__expected) != (__actual)) { \
251 size_t ask= snprintf(0, 0, __VA_ARGS__); \
253 char *buffer= (char*)alloca(sizeof(char) * ask); \
254 snprintf(buffer, ask, __VA_ARGS__); \
256 FAIL("Assertion '%s' != '%s' [ %s ]", #__expected, #__actual, buffer); \
258 fprintf(stderr, "\n%s:%d: %s Assertion '%s' != '%s' [ %s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expected, #__actual, buffer); \
259 exit(EXIT_FAILURE); \
263 #define ASSERT_STREQ(__expected_str, __actual_str) \
266 size_t __expected_length; \
267 size_t __actual_length; \
268 int ret= yatl_strcmp(__expected_str, __actual_str, &__expected_length, &__actual_length); \
271 FAIL("Assertion '%.*s' != '%.*s'\n", \
272 (int)(__expected_length), (__expected_str), \
273 (int)__actual_length, (__actual_str)) ; \
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); \
282 #define ASSERT_STREQ_(__expected_str, __actual_str, ...) \
285 size_t __expected_length; \
286 size_t __actual_length; \
287 int ret= yatl_strcmp(__expected_str, __actual_str, &__expected_length, &__actual_length); \
289 size_t ask= snprintf(0, 0, __VA_ARGS__); \
291 char *buffer= (char*)alloca(sizeof(char) * ask); \
292 ask= snprintf(buffer, ask, __VA_ARGS__); \
294 FAIL("Assertion '%.*s' != '%.*s' [ %.*s ]", \
295 (int)(__expected_length), (__expected_str), \
296 (int)(__actual_length), (__actual_str), \
297 (int)(ask), buffer); \
299 fprintf(stderr, "\n%s:%d: %s Assertion '%.*s' != '%.*s' [ %.*s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, \
300 (int)(__expected_length), (__expected_str), \
301 (int)(__actual_length), (__actual_str), \
302 (int)(ask), buffer); \
303 exit(EXIT_FAILURE); \
307 #define ASSERT_STRNE(__expected_str, __actual_str) \
310 size_t __expected_length; \
311 size_t __actual_length; \
312 int ret= yatl_strcmp(__expected_str, __actual_str, &__expected_length, &__actual_length); \
315 FAIL("Assertion '%.*s' == '%.*s'", \
316 (int)(__expected_length), (__expected_str), \
317 (int)__actual_length, (__actual_str)) ; \
319 fprintf(stderr, "\n%s:%d: %s Assertion '%.*s' == '%.*s'\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, \
320 (int)(__expected_length), (__expected_str), \
321 (int)__actual_length, (__actual_str)) ; \
322 exit(EXIT_FAILURE); \
326 #define ASSERT_STRNE_(__expected_str, __actual_str, ...) \
329 size_t __expected_length; \
330 size_t __actual_length; \
331 int ret= yatl_strcmp(__expected_str, __actual_str, &__expected_length, &__actual_length); \
333 size_t ask= snprintf(0, 0, __VA_ARGS__); \
335 char *buffer= (char*)alloca(sizeof(char) * ask); \
336 ask= snprintf(buffer, ask, __VA_ARGS__); \
338 FAIL("Assertion '%.*s' == '%.*s' [ %.*s ]", \
339 (int)(__expected_length), (__expected_str), \
340 (int)(__actual_length), (__actual_str), \
341 (int)(ask), buffer); \
343 fprintf(stderr, "\n%s:%d: %s Assertion '%.*s' == '%.*s' [ %.*s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, \
344 (int)(__expected_length), (__expected_str), \
345 (int)(__actual_length), (__actual_str), \
346 (int)(ask), buffer); \
347 exit(EXIT_FAILURE); \
351 #define ASSERT_NEQ(__expected, __actual) \
354 if ((__expected) == (__actual)) { \
356 FAIL("Assertion '%s' == '%s'", #__expected, #__actual); \
358 fprintf(stderr, "\n%s:%d: %s Assertion '%s' == '%s'\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expected, #__actual); \
359 exit(EXIT_FAILURE); \
363 #define ASSERT_NEQ_(__expected, __actual, ...) \
366 if ((__expected) == (__actual)) { \
367 size_t ask= snprintf(0, 0, __VA_ARGS__); \
369 char *buffer= (char*)alloca(sizeof(char) * ask); \
370 snprintf(buffer, ask, __VA_ARGS__); \
372 FAIL("Assertion '%s' == '%s' [ %s ]", #__expected, #__actual, buffer); \
374 fprintf(stderr, "\n%s:%d: %s Assertion '%s' == '%s' [ %s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expected, #__actual, buffer); \
375 exit(EXIT_FAILURE); \
379 #define ASSERT_FALSE_(__expression, ...) \
382 if ((__expression)) { \
383 size_t ask= snprintf(0, 0, __VA_ARGS__); \
385 char *buffer= (char*)alloca(sizeof(char) * ask); \
386 snprintf(buffer, ask, __VA_ARGS__); \
388 FAIL("Assertion '!%s' [ %s ]", #__expression, buffer); \
390 fprintf(stderr, "\n%s:%d: %s Assertion '!%s' [ %s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression, buffer); \
391 exit(EXIT_FAILURE); \