aad47cf3eb4936982fefdfcc462f84a079c130ca
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, ...)
71 # define SKIP(__message_format, ...)
74 static inline bool valgrind_is_caller(void)
76 if (getenv("TESTS_ENVIRONMENT") && strstr(getenv("TESTS_ENVIRONMENT"), "valgrind"))
84 static inline size_t yatl_strlen(const char *s
)
94 static inline int yatl_strcmp(const char *s1
, const char *s2
, size_t *s1_length
, size_t *s2_length
)
96 *s1_length
= yatl_strlen(s1
);
97 *s2_length
= yatl_strlen(s2
);
99 if (*s1_length
== 0 && *s1_length
== *s2_length
)
104 if (*s1_length
== 0 && *s2_length
)
109 if (*s1_length
&& *s2_length
== 0)
114 return strcmp(s1
, s2
);
117 #define SKIP_IF(__expression) \
120 if ((__expression)) { \
122 SKIP(#__expression); \
124 fprintf(stdout, "\n%s:%d: %s SKIP '!(%s)'\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression); \
129 #define ASSERT_TRUE(__expression) \
132 if (! (__expression)) { \
134 FAIL("Assertion '%s'", #__expression); \
136 fprintf(stderr, "\n%s:%d: %s Assertion '%s'\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression);\
137 exit(EXIT_FAILURE); \
141 #define ASSERT_FALSE(__expression) \
144 if ((__expression)) { \
146 FAIL("Assertion '!%s'", #__expression); \
148 fprintf(stderr, "\n%s:%d: %s Assertion '!%s'\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression);\
149 exit(EXIT_FAILURE); \
153 #define ASSERT_NULL_(__expression, ...) \
156 if ((__expression) != NULL) { \
157 size_t ask= snprintf(0, 0, __VA_ARGS__); \
159 char *buffer= (char*)alloca(sizeof(char) * ask); \
160 snprintf(buffer, ask, __VA_ARGS__); \
161 fprintf(stderr, "\n%s:%d: %s Assertion '%s' != NULL [ %s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression, buffer);\
162 exit(EXIT_FAILURE); \
166 #define ASSERT_NOT_NULL(__expression) \
169 if ((__expression) == NULL) { \
170 fprintf(stderr, "\n%s:%d: %s Assertion '%s' == NULL\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression,);\
171 exit(EXIT_FAILURE); \
175 #define ASSERT_NOT_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__); \
183 fprintf(stderr, "\n%s:%d: %s Assertion '%s' == NULL [ %s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression, buffer);\
184 exit(EXIT_FAILURE); \
188 #define SKIP_IF_(__expression, ...) \
191 if ((__expression)) { \
192 size_t ask= snprintf(0, 0, __VA_ARGS__); \
194 char *buffer= (char*)alloca(sizeof(char) * ask); \
195 snprintf(buffer, ask, __VA_ARGS__); \
197 SKIP(#__expression, buffer); \
199 fprintf(stdout, "\n%s:%d: %s SKIP '%s' [ %s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression, buffer); \
204 #define ASSERT_TRUE_(__expression, ...) \
207 if (! (__expression)) { \
208 size_t ask= snprintf(0, 0, __VA_ARGS__); \
210 char *buffer= (char*)alloca(sizeof(char) * ask); \
211 snprintf(buffer, ask, __VA_ARGS__); \
212 fprintf(stderr, "\n%s:%d: %s Assertion '%s' [ %s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression, buffer); \
213 exit(EXIT_FAILURE); \
217 #define ASSERT_EQ(__expected, __actual) \
220 if ((__expected) != (__actual)) { \
221 fprintf(stderr, "\n%s:%d: %s Assertion '%s' != '%s'\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expected, #__actual); \
222 exit(EXIT_FAILURE); \
226 #define ASSERT_EQ_(__expected, __actual, ...) \
229 if ((__expected) != (__actual)) { \
230 size_t ask= snprintf(0, 0, __VA_ARGS__); \
232 char *buffer= (char*)alloca(sizeof(char) * ask); \
233 snprintf(buffer, ask, __VA_ARGS__); \
234 fprintf(stderr, "\n%s:%d: %s Assertion '%s' != '%s' [ %s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expected, #__actual, buffer); \
235 exit(EXIT_FAILURE); \
239 #define ASSERT_STREQ(__expected_str, __actual_str) \
242 size_t __expected_length; \
243 size_t __actual_length; \
244 int ret= yatl_strcmp(__expected_str, __actual_str, &__expected_length, &__actual_length); \
246 fprintf(stderr, "\n%s:%d: %s Assertion '%.*s' != '%.*s'\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, \
247 (int)(__expected_length), (__expected_str), \
248 (int)__actual_length, (__actual_str)) ; \
249 exit(EXIT_FAILURE); \
253 #define ASSERT_STREQ_(__expected_str, __actual_str, ...) \
256 size_t __expected_length; \
257 size_t __actual_length; \
258 int ret= yatl_strcmp(__expected_str, __actual_str, &__expected_length, &__actual_length); \
260 size_t ask= snprintf(0, 0, __VA_ARGS__); \
262 char *buffer= (char*)alloca(sizeof(char) * ask); \
263 ask= snprintf(buffer, ask, __VA_ARGS__); \
264 fprintf(stderr, "\n%s:%d: %s Assertion '%.*s' != '%.*s' [ %.*s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, \
265 (int)(__expected_length), (__expected_str), \
266 (int)(__actual_length), (__actual_str), \
267 (int)(ask), buffer); \
268 exit(EXIT_FAILURE); \
272 #define ASSERT_STRNE(__expected_str, __actual_str) \
275 size_t __expected_length; \
276 size_t __actual_length; \
277 int ret= yatl_strcmp(__expected_str, __actual_str, &__expected_length, &__actual_length); \
279 fprintf(stderr, "\n%s:%d: %s Assertion '%.*s' == '%.*s'\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, \
280 (int)(__expected_length), (__expected_str), \
281 (int)__actual_length, (__actual_str)) ; \
282 exit(EXIT_FAILURE); \
286 #define ASSERT_STRNE_(__expected_str, __actual_str, ...) \
289 size_t __expected_length; \
290 size_t __actual_length; \
291 int ret= yatl_strcmp(__expected_str, __actual_str, &__expected_length, &__actual_length); \
293 size_t ask= snprintf(0, 0, __VA_ARGS__); \
295 char *buffer= (char*)alloca(sizeof(char) * ask); \
296 ask= snprintf(buffer, ask, __VA_ARGS__); \
297 fprintf(stderr, "\n%s:%d: %s Assertion '%.*s' == '%.*s' [ %.*s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, \
298 (int)(__expected_length), (__expected_str), \
299 (int)(__actual_length), (__actual_str), \
300 (int)(ask), buffer); \
301 exit(EXIT_FAILURE); \
305 #define ASSERT_NEQ(__expected, __actual, ...) \
308 if ((__expected) == (__actual)) { \
309 fprintf(stderr, "\n%s:%d: %s Assertion '%s' == '%s'\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expected, #__actual); \
310 exit(EXIT_FAILURE); \
314 #define ASSERT_NEQ_(__expected, __actual, ...) \
317 if ((__expected) == (__actual)) { \
318 size_t ask= snprintf(0, 0, __VA_ARGS__); \
320 char *buffer= (char*)alloca(sizeof(char) * ask); \
321 snprintf(buffer, ask, __VA_ARGS__); \
322 fprintf(stderr, "\n%s:%d: %s Assertion '%s' == '%s' [ %s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expected, #__actual, buffer); \
323 exit(EXIT_FAILURE); \
327 #define ASSERT_FALSE_(__expression, ...) \
330 if ((__expression)) { \
331 size_t ask= snprintf(0, 0, __VA_ARGS__); \
333 char *buffer= (char*)alloca(sizeof(char) * ask); \
334 snprintf(buffer, ask, __VA_ARGS__); \
336 throw libtest::__failure(__FILE__, __LINE__, __PRETTY_FUNCTION__, "Assertion '!%s' [ %s ]", #__expression, buffer); \
338 fprintf(stderr, "\n%s:%d: %s Assertion '!%s' [ %s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression, buffer); \
339 exit(EXIT_FAILURE); \