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.
51
#ifndef __PRETTY_FUNCTION__
52
# define __PRETTY_FUNCTION__ __func__
59
static inline bool valgrind_is_caller(void)
61
if (getenv("TESTS_ENVIRONMENT") && strstr(getenv("TESTS_ENVIRONMENT"), "valgrind"))
69
static inline size_t yatl_strlen(const char *s)
79
static inline int yatl_strcmp(const char *s1, const char *s2, size_t *s1_length, size_t *s2_length)
81
*s1_length= yatl_strlen(s1);
82
*s2_length= yatl_strlen(s2);
84
if (*s1_length == 0 && *s1_length == *s2_length)
89
if (*s1_length == 0 && *s2_length)
94
if (*s1_length && *s2_length == 0)
99
return strcmp(s1, s2);
102
#define SKIP_IF(__expression) \
105
if ((__expression)) { \
106
fprintf(stderr, "\n%s:%d: %s SKIP '!%s'\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression);\
111
#define ASSERT_TRUE(__expression) \
114
if (! (__expression)) { \
115
fprintf(stderr, "\n%s:%d: %s Assertion '%s'\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression);\
116
exit(EXIT_FAILURE); \
120
#define ASSERT_FALSE(__expression) \
123
if ((__expression)) { \
124
fprintf(stderr, "\n%s:%d: %s Assertion '!%s'\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression);\
125
exit(EXIT_FAILURE); \
129
#define ASSERT_NULL_(__expression, ...) \
132
if ((__expression) != NULL) { \
133
size_t ask= snprintf(0, 0, __VA_ARGS__); \
135
char *buffer= (char*)malloc(sizeof(char) * ask); \
136
snprintf(buffer, ask, __VA_ARGS__); \
137
fprintf(stderr, "\n%s:%d: %s Assertion '%s' != NULL [ %s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression, buffer);\
139
exit(EXIT_FAILURE); \
143
#define ASSERT_NOT_NULL(__expression) \
146
if ((__expression) == NULL) { \
147
fprintf(stderr, "\n%s:%d: %s Assertion '%s' == NULL\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression,);\
148
exit(EXIT_FAILURE); \
152
#define ASSERT_NOT_NULL_(__expression, ...) \
155
if ((__expression) == NULL) { \
156
size_t ask= snprintf(0, 0, __VA_ARGS__); \
158
char *buffer= (char*)malloc(sizeof(char) * ask); \
159
snprintf(buffer, ask, __VA_ARGS__); \
160
fprintf(stderr, "\n%s:%d: %s Assertion '%s' == NULL [ %s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression, buffer);\
162
exit(EXIT_FAILURE); \
166
#define SKIP_IF_(__expression, ...) \
169
if ((__expression)) { \
170
size_t ask= snprintf(0, 0, __VA_ARGS__); \
172
char *buffer= (char*)malloc(sizeof(char) * ask); \
173
snprintf(buffer, ask, __VA_ARGS__); \
174
fprintf(stdout, "\n%s:%d: %s SKIP '%s' [ %s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression, buffer); \
180
#define ASSERT_TRUE_(__expression, ...) \
183
if (! (__expression)) { \
184
size_t ask= snprintf(0, 0, __VA_ARGS__); \
186
char *buffer= (char*)malloc(sizeof(char) * ask); \
187
snprintf(buffer, ask, __VA_ARGS__); \
188
fprintf(stderr, "\n%s:%d: %s Assertion '%s' [ %s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression, buffer); \
190
exit(EXIT_FAILURE); \
194
#define ASSERT_EQ(__expected, __actual) \
197
if ((__expected) != (__actual)) { \
198
fprintf(stderr, "\n%s:%d: %s Assertion '%s' != '%s'\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expected, #__actual); \
199
exit(EXIT_FAILURE); \
203
#define ASSERT_EQ_(__expected, __actual, ...) \
206
if ((__expected) != (__actual)) { \
207
size_t ask= snprintf(0, 0, __VA_ARGS__); \
209
char *buffer= (char*)malloc(sizeof(char) * ask); \
210
snprintf(buffer, ask, __VA_ARGS__); \
211
fprintf(stderr, "\n%s:%d: %s Assertion '%s' != '%s' [ %s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expected, #__actual, buffer); \
213
exit(EXIT_FAILURE); \
217
#define ASSERT_STREQ(__expected_str, __actual_str) \
220
size_t __expected_length; \
221
size_t __actual_length; \
222
int ret= yatl_strcmp(__expected_str, __actual_str, &__expected_length, &__actual_length); \
224
fprintf(stderr, "\n%s:%d: %s Assertion '%.*s' != '%.*s'\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, \
225
(int)(__expected_length), (__expected_str), \
226
(int)__actual_length, (__actual_str)) ; \
227
exit(EXIT_FAILURE); \
231
#define ASSERT_STREQ_(__expected_str, __actual_str, ...) \
234
size_t __expected_length; \
235
size_t __actual_length; \
236
int ret= yatl_strcmp(__expected_str, __actual_str, &__expected_length, &__actual_length); \
238
size_t ask= snprintf(0, 0, __VA_ARGS__); \
240
char *buffer= (char*)malloc(sizeof(char) * ask); \
241
ask= snprintf(buffer, ask, __VA_ARGS__); \
242
fprintf(stderr, "\n%s:%d: %s Assertion '%.*s' != '%.*s' [ %.*s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, \
243
(int)(__expected_length), (__expected_str), \
244
(int)(__actual_length), (__actual_str), \
245
(int)(ask), buffer); \
247
exit(EXIT_FAILURE); \
251
#define ASSERT_STRNE(__expected_str, __actual_str) \
254
size_t __expected_length; \
255
size_t __actual_length; \
256
int ret= yatl_strcmp(__expected_str, __actual_str, &__expected_length, &__actual_length); \
258
fprintf(stderr, "\n%s:%d: %s Assertion '%.*s' == '%.*s'\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, \
259
(int)(__expected_length), (__expected_str), \
260
(int)__actual_length, (__actual_str)) ; \
261
exit(EXIT_FAILURE); \
265
#define ASSERT_STRNE_(__expected_str, __actual_str, ...) \
268
size_t __expected_length; \
269
size_t __actual_length; \
270
int ret= yatl_strcmp(__expected_str, __actual_str, &__expected_length, &__actual_length); \
272
size_t ask= snprintf(0, 0, __VA_ARGS__); \
274
char *buffer= (char*)malloc(sizeof(char) * ask); \
275
ask= snprintf(buffer, ask, __VA_ARGS__); \
276
fprintf(stderr, "\n%s:%d: %s Assertion '%.*s' == '%.*s' [ %.*s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, \
277
(int)(__expected_length), (__expected_str), \
278
(int)(__actual_length), (__actual_str), \
279
(int)(ask), buffer); \
281
exit(EXIT_FAILURE); \
285
#define ASSERT_NEQ(__expected, __actual, ...) \
288
if ((__expected) == (__actual)) { \
289
fprintf(stderr, "\n%s:%d: %s Assertion '%s' == '%s'\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expected, #__actual); \
290
exit(EXIT_FAILURE); \
294
#define ASSERT_NEQ_(__expected, __actual, ...) \
297
if ((__expected) == (__actual)) { \
298
size_t ask= snprintf(0, 0, __VA_ARGS__); \
300
char *buffer= (char*)malloc(sizeof(char) * ask); \
301
snprintf(buffer, ask, __VA_ARGS__); \
302
fprintf(stderr, "\n%s:%d: %s Assertion '%s' == '%s' [ %s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expected, #__actual, buffer); \
304
exit(EXIT_FAILURE); \
308
#define ASSERT_FALSE_(__expression, ...) \
311
if ((__expression)) { \
312
size_t ask= snprintf(0, 0, __VA_ARGS__); \
314
char *buffer= (char*)malloc(sizeof(char) * ask); \
315
snprintf(buffer, ask, __VA_ARGS__); \
316
fprintf(stderr, "\n%s:%d: %s Assertion '!%s' [ %s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression, buffer); \
318
exit(EXIT_FAILURE); \