56ea4c1774652fade9717f89addef1f5d5f74ca3
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.
54 #ifndef __PRETTY_FUNCTION__
55 # define __PRETTY_FUNCTION__ __func__
67 # define FAIL(__message_format, ...)
70 static inline bool valgrind_is_caller(void)
72 if (getenv("TESTS_ENVIRONMENT") && strstr(getenv("TESTS_ENVIRONMENT"), "valgrind"))
80 static inline size_t yatl_strlen(const char *s
)
90 static inline int yatl_strcmp(const char *s1
, const char *s2
, size_t *s1_length
, size_t *s2_length
)
92 *s1_length
= yatl_strlen(s1
);
93 *s2_length
= yatl_strlen(s2
);
95 if (*s1_length
== 0 && *s1_length
== *s2_length
)
100 if (*s1_length
== 0 && *s2_length
)
105 if (*s1_length
&& *s2_length
== 0)
110 return strcmp(s1
, s2
);
113 #define SKIP_IF(__expression) \
116 if ((__expression)) { \
117 fprintf(stderr, "\n%s:%d: %s SKIP '!%s'\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression); \
122 #define ASSERT_TRUE(__expression) \
125 if (! (__expression)) { \
127 FAIL("Assertion '%s'", #__expression); \
129 fprintf(stderr, "\n%s:%d: %s Assertion '%s'\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression);\
130 exit(EXIT_FAILURE); \
134 #define ASSERT_FALSE(__expression) \
137 if ((__expression)) { \
139 FAIL("Assertion '!%s'", #__expression); \
141 fprintf(stderr, "\n%s:%d: %s Assertion '!%s'\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression);\
142 exit(EXIT_FAILURE); \
146 #define ASSERT_NULL_(__expression, ...) \
149 if ((__expression) != NULL) { \
150 size_t ask= snprintf(0, 0, __VA_ARGS__); \
152 char *buffer= (char*)malloc(sizeof(char) * ask); \
153 snprintf(buffer, ask, __VA_ARGS__); \
154 fprintf(stderr, "\n%s:%d: %s Assertion '%s' != NULL [ %s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression, buffer);\
156 exit(EXIT_FAILURE); \
160 #define ASSERT_NOT_NULL(__expression) \
163 if ((__expression) == NULL) { \
164 fprintf(stderr, "\n%s:%d: %s Assertion '%s' == NULL\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression,);\
165 exit(EXIT_FAILURE); \
169 #define ASSERT_NOT_NULL_(__expression, ...) \
172 if ((__expression) == NULL) { \
173 size_t ask= snprintf(0, 0, __VA_ARGS__); \
175 char *buffer= (char*)malloc(sizeof(char) * ask); \
176 snprintf(buffer, ask, __VA_ARGS__); \
177 fprintf(stderr, "\n%s:%d: %s Assertion '%s' == NULL [ %s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression, buffer);\
179 exit(EXIT_FAILURE); \
183 #define SKIP_IF_(__expression, ...) \
186 if ((__expression)) { \
187 size_t ask= snprintf(0, 0, __VA_ARGS__); \
189 char *buffer= (char*)malloc(sizeof(char) * ask); \
190 snprintf(buffer, ask, __VA_ARGS__); \
191 fprintf(stdout, "\n%s:%d: %s SKIP '%s' [ %s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression, buffer); \
197 #define ASSERT_TRUE_(__expression, ...) \
200 if (! (__expression)) { \
201 size_t ask= snprintf(0, 0, __VA_ARGS__); \
203 char *buffer= (char*)malloc(sizeof(char) * ask); \
204 snprintf(buffer, ask, __VA_ARGS__); \
205 fprintf(stderr, "\n%s:%d: %s Assertion '%s' [ %s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression, buffer); \
207 exit(EXIT_FAILURE); \
211 #define ASSERT_EQ(__expected, __actual) \
214 if ((__expected) != (__actual)) { \
215 fprintf(stderr, "\n%s:%d: %s Assertion '%s' != '%s'\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expected, #__actual); \
216 exit(EXIT_FAILURE); \
220 #define ASSERT_EQ_(__expected, __actual, ...) \
223 if ((__expected) != (__actual)) { \
224 size_t ask= snprintf(0, 0, __VA_ARGS__); \
226 char *buffer= (char*)malloc(sizeof(char) * ask); \
227 snprintf(buffer, ask, __VA_ARGS__); \
228 fprintf(stderr, "\n%s:%d: %s Assertion '%s' != '%s' [ %s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expected, #__actual, buffer); \
230 exit(EXIT_FAILURE); \
234 #define ASSERT_STREQ(__expected_str, __actual_str) \
237 size_t __expected_length; \
238 size_t __actual_length; \
239 int ret= yatl_strcmp(__expected_str, __actual_str, &__expected_length, &__actual_length); \
241 fprintf(stderr, "\n%s:%d: %s Assertion '%.*s' != '%.*s'\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, \
242 (int)(__expected_length), (__expected_str), \
243 (int)__actual_length, (__actual_str)) ; \
244 exit(EXIT_FAILURE); \
248 #define ASSERT_STREQ_(__expected_str, __actual_str, ...) \
251 size_t __expected_length; \
252 size_t __actual_length; \
253 int ret= yatl_strcmp(__expected_str, __actual_str, &__expected_length, &__actual_length); \
255 size_t ask= snprintf(0, 0, __VA_ARGS__); \
257 char *buffer= (char*)malloc(sizeof(char) * ask); \
258 ask= snprintf(buffer, ask, __VA_ARGS__); \
259 fprintf(stderr, "\n%s:%d: %s Assertion '%.*s' != '%.*s' [ %.*s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, \
260 (int)(__expected_length), (__expected_str), \
261 (int)(__actual_length), (__actual_str), \
262 (int)(ask), buffer); \
264 exit(EXIT_FAILURE); \
268 #define ASSERT_STRNE(__expected_str, __actual_str) \
271 size_t __expected_length; \
272 size_t __actual_length; \
273 int ret= yatl_strcmp(__expected_str, __actual_str, &__expected_length, &__actual_length); \
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_STRNE_(__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*)malloc(sizeof(char) * ask); \
292 ask= snprintf(buffer, ask, __VA_ARGS__); \
293 fprintf(stderr, "\n%s:%d: %s Assertion '%.*s' == '%.*s' [ %.*s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, \
294 (int)(__expected_length), (__expected_str), \
295 (int)(__actual_length), (__actual_str), \
296 (int)(ask), buffer); \
298 exit(EXIT_FAILURE); \
302 #define ASSERT_NEQ(__expected, __actual, ...) \
305 if ((__expected) == (__actual)) { \
306 fprintf(stderr, "\n%s:%d: %s Assertion '%s' == '%s'\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expected, #__actual); \
307 exit(EXIT_FAILURE); \
311 #define ASSERT_NEQ_(__expected, __actual, ...) \
314 if ((__expected) == (__actual)) { \
315 size_t ask= snprintf(0, 0, __VA_ARGS__); \
317 char *buffer= (char*)malloc(sizeof(char) * ask); \
318 snprintf(buffer, ask, __VA_ARGS__); \
319 fprintf(stderr, "\n%s:%d: %s Assertion '%s' == '%s' [ %s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expected, #__actual, buffer); \
321 exit(EXIT_FAILURE); \
325 #define ASSERT_FALSE_(__expression, ...) \
328 if ((__expression)) { \
329 size_t ask= snprintf(0, 0, __VA_ARGS__); \
331 char *buffer= (char*)alloca(sizeof(char) * ask); \
332 snprintf(buffer, ask, __VA_ARGS__); \
334 throw libtest::__failure(__FILE__, __LINE__, __PRETTY_FUNCTION__, "Assertion '!%s' [ %s ]", #__expression, buffer); \
336 fprintf(stderr, "\n%s:%d: %s Assertion '!%s' [ %s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression, buffer); \
337 exit(EXIT_FAILURE); \