~drizzle-trunk/libdrizzle/jenkins-Libdrizzle-29

« back to all changes in this revision

Viewing changes to yatl/lite.h

  • Committer: Continuous Integration
  • Date: 2012-12-29 11:51:43 UTC
  • mfrom: (68.1.1 5.1-trunk)
  • Revision ID: ci@drizzle.org-20121229115143-b2w1xjx16neqxyu2
Merge lp:~brianaker/libdrizzle/yatl_lite_bugreport_fix Build: jenkins-Libdrizzle-17

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 
2
 *
 
3
 *  Data Differential YATL (i.e. libtest)  library
 
4
 *
 
5
 *  Copyright (C) 2012 Data Differential, http://datadifferential.com/
 
6
 *
 
7
 *  Redistribution and use in source and binary forms, with or without
 
8
 *  modification, are permitted provided that the following conditions are
 
9
 *  met:
 
10
 *
 
11
 *      * Redistributions of source code must retain the above copyright
 
12
 *  notice, this list of conditions and the following disclaimer.
 
13
 *
 
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
 
17
 *  distribution.
 
18
 *
 
19
 *      * The names of its contributors may not be used to endorse or
 
20
 *  promote products derived from this software without specific prior
 
21
 *  written permission.
 
22
 *
 
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.
 
34
 *
 
35
 */
 
36
 
 
37
#pragma once
 
38
 
 
39
#ifdef __cplusplus
 
40
# include <cstddef>
 
41
# include <cstdlib>
 
42
# include <cstring>
 
43
#include <cstdarg>
 
44
#else
 
45
# include <stddef.h>
 
46
# include <stdlib.h>
 
47
# include <stdbool.h>
 
48
# include <string.h>
 
49
# include <stdarg.h>
 
50
#endif
 
51
#ifndef __PRETTY_FUNCTION__
 
52
# define __PRETTY_FUNCTION__ __func__
 
53
#endif
 
54
 
 
55
#ifndef EXIT_SKIP
 
56
# define EXIT_SKIP 77
 
57
#endif
 
58
 
 
59
static inline bool valgrind_is_caller(void)
 
60
{
 
61
  if (getenv("TESTS_ENVIRONMENT")  && strstr(getenv("TESTS_ENVIRONMENT"), "valgrind"))
 
62
  {
 
63
    return true;
 
64
  }
 
65
 
 
66
  return false;
 
67
}
 
68
 
 
69
static inline size_t yatl_strlen(const char *s)
 
70
{
 
71
  if (s)
 
72
  {
 
73
    return strlen(s);
 
74
  }
 
75
 
 
76
  return (size_t)(0);
 
77
}
 
78
 
 
79
static inline int yatl_strcmp(const char *s1, const char *s2, size_t *s1_length, size_t *s2_length)
 
80
{
 
81
  *s1_length= yatl_strlen(s1);
 
82
  *s2_length= yatl_strlen(s2);
 
83
 
 
84
  if (*s1_length == 0 &&  *s1_length == *s2_length)
 
85
  {
 
86
    return 0;
 
87
  }
 
88
 
 
89
  if (*s1_length == 0 && *s2_length)
 
90
  {
 
91
    return 1;
 
92
  }
 
93
 
 
94
  if (*s1_length &&  *s2_length == 0)
 
95
  {
 
96
    return 1;
 
97
  }
 
98
 
 
99
  return strcmp(s1, s2);
 
100
}
 
101
 
 
102
#define SKIP_IF(__expression) \
 
103
do \
 
104
{ \
 
105
  if ((__expression)) { \
 
106
    fprintf(stderr, "\n%s:%d: %s SKIP '!%s'\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression);\
 
107
    exit(EXIT_SKIP); \
 
108
  } \
 
109
} while (0)
 
110
 
 
111
#define ASSERT_TRUE(__expression) \
 
112
do \
 
113
{ \
 
114
  if (! (__expression)) { \
 
115
    fprintf(stderr, "\n%s:%d: %s Assertion '%s'\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression);\
 
116
    exit(EXIT_FAILURE); \
 
117
  } \
 
118
} while (0)
 
119
 
 
120
#define ASSERT_FALSE(__expression) \
 
121
do \
 
122
{ \
 
123
  if ((__expression)) { \
 
124
    fprintf(stderr, "\n%s:%d: %s Assertion '!%s'\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression);\
 
125
    exit(EXIT_FAILURE); \
 
126
  } \
 
127
} while (0)
 
128
 
 
129
#define ASSERT_NULL_(__expression, ...) \
 
130
do \
 
131
{ \
 
132
  if ((__expression) != NULL) { \
 
133
    size_t ask= snprintf(0, 0, __VA_ARGS__); \
 
134
    ask++; \
 
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);\
 
138
    free(buffer); \
 
139
    exit(EXIT_FAILURE); \
 
140
  } \
 
141
} while (0)
 
142
 
 
143
#define ASSERT_NOT_NULL(__expression) \
 
144
do \
 
145
{ \
 
146
  if ((__expression) == NULL) { \
 
147
    fprintf(stderr, "\n%s:%d: %s Assertion '%s' == NULL\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression,);\
 
148
    exit(EXIT_FAILURE); \
 
149
  } \
 
150
} while (0)
 
151
 
 
152
#define ASSERT_NOT_NULL_(__expression, ...) \
 
153
do \
 
154
{ \
 
155
  if ((__expression) == NULL) { \
 
156
    size_t ask= snprintf(0, 0, __VA_ARGS__); \
 
157
    ask++; \
 
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);\
 
161
    free(buffer); \
 
162
    exit(EXIT_FAILURE); \
 
163
  } \
 
164
} while (0)
 
165
 
 
166
#define SKIP_IF_(__expression, ...) \
 
167
do \
 
168
{ \
 
169
  if ((__expression)) { \
 
170
    size_t ask= snprintf(0, 0, __VA_ARGS__); \
 
171
    ask++; \
 
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); \
 
175
    free(buffer); \
 
176
    exit(EXIT_SKIP); \
 
177
  } \
 
178
} while (0)
 
179
 
 
180
#define ASSERT_TRUE_(__expression, ...) \
 
181
do \
 
182
{ \
 
183
  if (! (__expression)) { \
 
184
    size_t ask= snprintf(0, 0, __VA_ARGS__); \
 
185
    ask++; \
 
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); \
 
189
    free(buffer); \
 
190
    exit(EXIT_FAILURE); \
 
191
  } \
 
192
} while (0)
 
193
 
 
194
#define ASSERT_EQ(__expected, __actual) \
 
195
do \
 
196
{ \
 
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); \
 
200
  } \
 
201
} while (0)
 
202
 
 
203
#define ASSERT_EQ_(__expected, __actual, ...) \
 
204
do \
 
205
{ \
 
206
  if ((__expected) != (__actual)) { \
 
207
    size_t ask= snprintf(0, 0, __VA_ARGS__); \
 
208
    ask++; \
 
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); \
 
212
    free(buffer); \
 
213
    exit(EXIT_FAILURE); \
 
214
  } \
 
215
} while (0)
 
216
 
 
217
#define ASSERT_STREQ(__expected_str, __actual_str) \
 
218
do \
 
219
{ \
 
220
  size_t __expected_length; \
 
221
  size_t __actual_length; \
 
222
  int ret= yatl_strcmp(__expected_str, __actual_str, &__expected_length, &__actual_length); \
 
223
  if (ret) { \
 
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); \
 
228
  } \
 
229
} while (0)
 
230
 
 
231
#define ASSERT_STREQ_(__expected_str, __actual_str, ...) \
 
232
do \
 
233
{ \
 
234
  size_t __expected_length; \
 
235
  size_t __actual_length; \
 
236
  int ret= yatl_strcmp(__expected_str, __actual_str, &__expected_length, &__actual_length); \
 
237
  if (ret) { \
 
238
    size_t ask= snprintf(0, 0, __VA_ARGS__); \
 
239
    ask++; \
 
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); \
 
246
    free(buffer); \
 
247
    exit(EXIT_FAILURE); \
 
248
  } \
 
249
} while (0)
 
250
 
 
251
#define ASSERT_STRNE(__expected_str, __actual_str) \
 
252
do \
 
253
{ \
 
254
  size_t __expected_length; \
 
255
  size_t __actual_length; \
 
256
  int ret= yatl_strcmp(__expected_str, __actual_str, &__expected_length, &__actual_length); \
 
257
  if (ret == 0) { \
 
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); \
 
262
  } \
 
263
} while (0)
 
264
 
 
265
#define ASSERT_STRNE_(__expected_str, __actual_str, ...) \
 
266
do \
 
267
{ \
 
268
  size_t __expected_length; \
 
269
  size_t __actual_length; \
 
270
  int ret= yatl_strcmp(__expected_str, __actual_str, &__expected_length, &__actual_length); \
 
271
  if (ret == 0) { \
 
272
    size_t ask= snprintf(0, 0, __VA_ARGS__); \
 
273
    ask++; \
 
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); \
 
280
    free(buffer); \
 
281
    exit(EXIT_FAILURE); \
 
282
  } \
 
283
} while (0)
 
284
 
 
285
#define ASSERT_NEQ(__expected, __actual, ...) \
 
286
do \
 
287
{ \
 
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); \
 
291
  } \
 
292
} while (0)
 
293
 
 
294
#define ASSERT_NEQ_(__expected, __actual, ...) \
 
295
do \
 
296
{ \
 
297
  if ((__expected) == (__actual)) { \
 
298
    size_t ask= snprintf(0, 0, __VA_ARGS__); \
 
299
    ask++; \
 
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); \
 
303
    free(buffer); \
 
304
    exit(EXIT_FAILURE); \
 
305
  } \
 
306
} while (0)
 
307
 
 
308
#define ASSERT_FALSE_(__expression, ...) \
 
309
do \
 
310
{ \
 
311
  if ((__expression)) { \
 
312
    size_t ask= snprintf(0, 0, __VA_ARGS__); \
 
313
    ask++; \
 
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); \
 
317
    free(buffer); \
 
318
    exit(EXIT_FAILURE); \
 
319
  } \
 
320
} while (0)