This repository was archived by the owner on Oct 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 180
/
Copy pathTestSuite.h
351 lines (301 loc) · 15.4 KB
/
TestSuite.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
* Copyright 2016 MongoDB, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef TEST_SUITE_H
#define TEST_SUITE_H
#include <stdio.h>
#ifdef __cplusplus
extern "C" {
#endif
#ifndef BINARY_DIR
#define BINARY_DIR "tests/binary"
#endif
#ifdef ASSERT
#undef ASSERT
#endif
#define ASSERT BSON_ASSERT
#ifdef ASSERT_OR_PRINT
#undef ASSERT_OR_PRINT
#endif
#define ASSERT_OR_PRINT(_statement, _err) \
do { \
if (!(_statement)) { \
fprintf (stderr, \
"FAIL:%s:%d %s()\n %s\n %s\n\n", \
__FILE__, \
__LINE__, \
BSON_FUNC, \
#_statement, \
_err.message); \
fflush (stderr); \
abort (); \
} \
} while (0)
#define ASSERT_CMPINT_HELPER(a, eq, b, fmt) \
do { \
if (!((a) eq (b))) { \
fprintf (stderr, \
"FAIL\n\nAssert Failure: %" fmt " %s %" fmt "\n" \
"%s:%d %s()\n", \
a, \
#eq, \
b, \
__FILE__, \
__LINE__, \
BSON_FUNC); \
abort (); \
} \
} while (0)
#define ASSERT_CMPINT(a, eq, b) ASSERT_CMPINT_HELPER (a, eq, b, "d")
#define ASSERT_CMPUINT(a, eq, b) ASSERT_CMPINT_HELPER (a, eq, b, "u")
#define ASSERT_CMPLONG(a, eq, b) ASSERT_CMPINT_HELPER (a, eq, b, "ld")
#define ASSERT_CMPULONG(a, eq, b) ASSERT_CMPINT_HELPER (a, eq, b, "lu")
#define ASSERT_CMPINT32(a, eq, b) ASSERT_CMPINT_HELPER (a, eq, b, PRId32)
#define ASSERT_CMPINT64(a, eq, b) ASSERT_CMPINT_HELPER (a, eq, b, PRId64)
#define ASSERT_CMPUINT16(a, eq, b) ASSERT_CMPINT_HELPER (a, eq, b, "hu")
#define ASSERT_CMPUINT32(a, eq, b) ASSERT_CMPINT_HELPER (a, eq, b, PRIu32)
#define ASSERT_CMPUINT64(a, eq, b) ASSERT_CMPINT_HELPER (a, eq, b, PRIu64)
#define ASSERT_CMPSIZE_T(a, eq, b) ASSERT_CMPINT_HELPER (a, eq, b, "zd")
#define ASSERT_CMPSSIZE_T(a, eq, b) ASSERT_CMPINT_HELPER (a, eq, b, "zx")
#define ASSERT_CMPDOUBLE(a, eq, b) ASSERT_CMPINT_HELPER (a, eq, b, "f")
#define ASSERT_MEMCMP(a, b, n) \
do { \
if (0 != memcmp (a, b, n)) { \
fprintf (stderr, \
"Failed comparing %d bytes: \"%.*s\" != \"%.*s\"", \
n, \
n, \
(char *) a, \
n, \
(char *) b); \
abort (); \
} \
} while (0)
#ifdef ASSERT_ALMOST_EQUAL
#undef ASSERT_ALMOST_EQUAL
#endif
#define ASSERT_ALMOST_EQUAL(a, b) \
do { \
/* evaluate once */ \
int64_t _a = (a); \
int64_t _b = (b); \
if (!(_a > (_b * 2) / 3 && (_a < (_b * 3) / 2))) { \
fprintf (stderr, \
"FAIL\n\nAssert Failure: %" PRId64 \
" not within 50%% of %" PRId64 "\n" \
"%s:%d %s()\n", \
_a, \
_b, \
__FILE__, \
__LINE__, \
BSON_FUNC); \
abort (); \
} \
} while (0)
#define ASSERT_CMPSTR(a, b) \
do { \
if (((a) != (b)) && !!strcmp ((a), (b))) { \
fprintf (stderr, "FAIL\n\nAssert Failure: \"%s\" != \"%s\"\n", a, b); \
abort (); \
} \
} while (0)
#define ASSERT_CMPUINT8(a, b) ASSERT_CMPSTR ((const char *) a, (const char *) b)
#define ASSERT_CMPJSON(_a, _b) \
do { \
size_t i = 0; \
char *__a = (char *) (_a); \
char *__b = (char *) (_b); \
char *__aa = bson_malloc0 (strlen (__a) + 1); \
char *__bb = bson_malloc0 (strlen (__b) + 1); \
char *f = __a; \
do { \
while (isspace (*__a)) \
__a++; \
__aa[i++] = *__a++; \
} while (*__a); \
i = 0; \
do { \
while (isspace (*__b)) \
__b++; \
__bb[i++] = *__b++; \
} while (*__b); \
if (!!strcmp ((__aa), (__bb))) { \
fprintf (stderr, \
"FAIL\n\nAssert Failure: \"%s\" != \"%s\"\n" \
"%s:%d %s()\n", \
__aa, \
__bb, \
__FILE__, \
__LINE__, \
BSON_FUNC); \
abort (); \
} \
bson_free (__aa); \
bson_free (__bb); \
bson_free (f); \
} while (0)
#define ASSERT_CMPOID(a, b) \
do { \
if (bson_oid_compare ((a), (b))) { \
char oid_a[25]; \
char oid_b[25]; \
bson_oid_to_string ((a), oid_a); \
bson_oid_to_string ((b), oid_b); \
fprintf (stderr, \
"FAIL\n\nAssert Failure: " \
"ObjectId(\"%s\") != ObjectId(\"%s\")\n", \
oid_a, \
oid_b); \
abort (); \
} \
} while (0)
#define ASSERT_CONTAINS(a, b) \
do { \
if (NULL == strstr ((a), (b))) { \
fprintf (stderr, \
"FAIL\n\nAssert Failure: \"%s\" does not contain \"%s\"\n", \
a, \
b); \
abort (); \
} \
} while (0)
#define ASSERT_STARTSWITH(a, b) \
do { \
if ((a) != strstr ((a), (b))) { \
fprintf ( \
stderr, \
"FAIL\n\nAssert Failure: \"%s\" does not start with \"%s\"\n", \
a, \
b); \
abort (); \
} \
} while (0)
#define AWAIT(_condition) \
do { \
int64_t _start = bson_get_monotonic_time (); \
while (!(_condition)) { \
if (bson_get_monotonic_time () - _start > 1000 * 1000) { \
fprintf (stderr, \
"%s:%d %s(): \"%s\" still false after 1 second\n", \
__FILE__, \
__LINE__, \
BSON_FUNC, \
#_condition); \
abort (); \
} \
} \
} while (0)
#define ASSERT_ERROR_CONTAINS(error, _domain, _code, _message) \
do { \
ASSERT_CMPINT (error.domain, ==, _domain); \
ASSERT_CMPINT (error.code, ==, _code); \
ASSERT_CONTAINS (error.message, _message); \
} while (0);
#define ASSERT_HAS_FIELD(_bson, _field) \
do { \
if (!bson_has_field ((_bson), (_field))) { \
fprintf (stderr, \
"FAIL\n\nAssert Failure: No field \"%s\" in \"%s\"\n", \
(_field), \
bson_as_canonical_extended_json (_bson, NULL)); \
abort (); \
} \
} while (0)
/* don't check durations when testing with valgrind */
#define ASSERT_CMPTIME(actual, maxduration) \
do { \
if (!test_suite_valgrind ()) { \
ASSERT_CMPINT (actual, <, maxduration); \
} \
} while (0)
#ifdef _WIN32
#define gettestpid _getpid
#else
#define gettestpid getpid
#endif
#define ASSERT_OR_PRINT_ERRNO(_statement, _errcode) \
do { \
if (!(_statement)) { \
fprintf ( \
stderr, \
"FAIL:%s:%d %s()\n %s\n Failed with error code: %d (%s)\n\n", \
__FILE__, \
__LINE__, \
BSON_FUNC, \
#_statement, \
_errcode, \
strerror (_errcode)); \
fflush (stderr); \
abort (); \
} \
} while (0)
#define MAX_TEST_NAME_LENGTH 500
#define MAX_TEST_CHECK_FUNCS 10
typedef void (*TestFunc) (void);
typedef void (*TestFuncWC) (void *);
typedef void (*TestFuncDtor) (void *);
typedef int (*CheckFunc) (void);
typedef struct _Test Test;
typedef struct _TestSuite TestSuite;
struct _Test {
Test *next;
char *name;
TestFuncWC func;
TestFuncDtor dtor;
void *ctx;
int exit_code;
unsigned seed;
CheckFunc checks[MAX_TEST_CHECK_FUNCS];
size_t num_checks;
};
struct _TestSuite {
char *prgname;
char *name;
char *testname;
Test *tests;
FILE *outfile;
int flags;
};
void
TestSuite_Init (TestSuite *suite, const char *name, int argc, char **argv);
void
TestSuite_Add (TestSuite *suite, const char *name, TestFunc func);
void
TestSuite_AddWC (TestSuite *suite,
const char *name,
TestFuncWC func,
TestFuncDtor dtor,
void *ctx);
void
_TestSuite_AddFull (TestSuite *suite,
const char *name,
TestFuncWC func,
TestFuncDtor dtor,
void *ctx,
...);
#define TestSuite_AddFull(_suite, _name, _func, _dtor, _ctx, ...) \
_TestSuite_AddFull (_suite, _name, _func, _dtor, _ctx, __VA_ARGS__, NULL)
int
TestSuite_Run (TestSuite *suite);
void
TestSuite_Destroy (TestSuite *suite);
int
test_suite_debug_output (void);
int
test_suite_valgrind (void);
#ifdef __cplusplus
}
#endif
#endif /* TEST_SUITE_H */