/
testing.h
287 lines (254 loc) · 11.2 KB
/
testing.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
// Copyright (c) 2015-2017 Nuxi, https://nuxi.nl/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
// OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
// OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
// SUCH DAMAGE.
// <testing.h> - Unit testing
#ifndef _TESTING_H_
#define _TESTING_H_
#include <_/types.h>
// clang-format off
// Instantiate the same macro for all separate types.
#define _MACRO_FOREACH_INEQUALITY_TYPE(macro, ...) \
macro(_Bool, bool, __VA_ARGS__) \
macro(char, char, __VA_ARGS__) \
macro(signed char, schar, __VA_ARGS__) \
macro(unsigned char, uchar, __VA_ARGS__) \
macro(short, short, __VA_ARGS__) \
macro(unsigned short, ushort, __VA_ARGS__) \
macro(int, int, __VA_ARGS__) \
macro(unsigned int, uint, __VA_ARGS__) \
macro(long, long, __VA_ARGS__) \
macro(unsigned long, ulong, __VA_ARGS__) \
macro(long long, llong, __VA_ARGS__) \
macro(unsigned long long, ullong, __VA_ARGS__) \
macro(float, float, __VA_ARGS__) \
macro(double, double, __VA_ARGS__) \
macro(long double, ldouble, __VA_ARGS__) \
macro(const void *, ptr, __VA_ARGS__)
#define _MACRO_FOREACH_TYPE(macro, ...) \
macro(float _Complex, cfloat, __VA_ARGS__) \
macro(double _Complex, cdouble, __VA_ARGS__) \
macro(long double _Complex, cldouble, __VA_ARGS__) \
_MACRO_FOREACH_INEQUALITY_TYPE(macro, __VA_ARGS__)
// Evaluate to a different identifier name based on the type.
#define _EXPRESSION_FOREACH_CASE(type, stype, prefix) \
type: prefix##_##stype, \
type const: prefix##_##stype, \
type volatile: prefix##_##stype, \
type const volatile: prefix##_##stype,
#define _EXPRESSION_FOREACH_INEQUALITY_TYPE(prefix, expression) \
_Generic(expression, \
_MACRO_FOREACH_INEQUALITY_TYPE(_EXPRESSION_FOREACH_CASE, prefix) \
default: prefix##_ptr)
#define _EXPRESSION_FOREACH_TYPE(prefix, expression) \
_Generic(expression, \
_MACRO_FOREACH_TYPE(_EXPRESSION_FOREACH_CASE, prefix) \
default: prefix##_ptr)
// clang-format on
// Test registration.
//
// Syntax:
//
// TEST(module, name) {
// ...
// }
//
// TEST_SINGLE_THREADED(module, name) {
// ...
// }
struct __test {
const char *__name;
void (*__func)(int);
_Bool __single_threaded;
};
#define TEST(module, name) \
static void __test_func_##module##_##name(int); \
static struct __test __test_obj_##module##_##name __section("__tests") \
__used = {#module "::" #name, __test_func_##module##_##name, 0}; \
static void __test_func_##module##_##name(int fd_tmp)
#define TEST_SINGLE_THREADED(module, name) \
static void __test_func_##module##_##name(int); \
static struct __test __test_obj_##module##_##name __section("__tests") \
__used = {#module "::" #name, __test_func_##module##_##name, 1}; \
static void __test_func_##module##_##name(int fd_tmp)
// Test execution.
//
// Syntax:
//
// int main(void) {
// int logfile = openat(...);
// int tmpdir = openat(...);
// testing_execute(tmpdir, logfile, nthreads);
// return 0;
// }
__BEGIN_DECLS
void testing_execute(int, int, unsigned int);
__END_DECLS
// Numerical comparison.
//
// Syntax:
//
// ASSERT_EQ(expected, actual);
// ASSERT_GE(expected, actual);
// ASSERT_GT(expected, actual);
// ASSERT_LE(expected, actual);
// ASSERT_LT(expected, actual);
// ASSERT_NE(expected, actual);
#define _GENERATE_TEST_FAILED(type, stype, unused) \
_Noreturn void __test_failed_##stype(const char *, type, const char *, type, \
const char *, const char *, int);
__BEGIN_DECLS
_MACRO_FOREACH_TYPE(_GENERATE_TEST_FAILED, unused)
__END_DECLS
#undef _GENERATE_TEST_FAILED
#define _GENERATE_COMPARE(type, stype, suffix, op) \
static __inline void __test_compare_##suffix##_##stype( \
type expected, const char *expected_str, type actual, \
const char *actual_str, const char *file, int line) { \
if (!(expected op actual)) \
__test_failed_##stype(#suffix, expected, expected_str, actual, \
actual_str, file, line); \
}
_MACRO_FOREACH_TYPE(_GENERATE_COMPARE, EQ, ==)
_MACRO_FOREACH_TYPE(_GENERATE_COMPARE, NE, !=)
_MACRO_FOREACH_INEQUALITY_TYPE(_GENERATE_COMPARE, GE, >=)
_MACRO_FOREACH_INEQUALITY_TYPE(_GENERATE_COMPARE, GT, >)
_MACRO_FOREACH_INEQUALITY_TYPE(_GENERATE_COMPARE, LE, <=)
_MACRO_FOREACH_INEQUALITY_TYPE(_GENERATE_COMPARE, LT, <)
#undef _GENERATE_COMPARE
#define ASSERT_EQ(expected, actual) \
_EXPRESSION_FOREACH_TYPE(__test_compare_EQ, actual) \
(expected, #expected, actual, #actual, __FILE__, __LINE__)
#define ASSERT_NE(expected, actual) \
_EXPRESSION_FOREACH_TYPE(__test_compare_NE, actual) \
(expected, #expected, actual, #actual, __FILE__, __LINE__)
#define ASSERT_GE(expected, actual) \
_EXPRESSION_FOREACH_INEQUALITY_TYPE(__test_compare_GE, actual) \
(expected, #expected, actual, #actual, __FILE__, __LINE__)
#define ASSERT_GT(expected, actual) \
_EXPRESSION_FOREACH_INEQUALITY_TYPE(__test_compare_GT, actual) \
(expected, #expected, actual, #actual, __FILE__, __LINE__)
#define ASSERT_LE(expected, actual) \
_EXPRESSION_FOREACH_INEQUALITY_TYPE(__test_compare_LE, actual) \
(expected, #expected, actual, #actual, __FILE__, __LINE__)
#define ASSERT_LT(expected, actual) \
_EXPRESSION_FOREACH_INEQUALITY_TYPE(__test_compare_LT, actual) \
(expected, #expected, actual, #actual, __FILE__, __LINE__)
// Boolean comparison.
//
// Syntax:
//
// ASSERT_TRUE(expr);
// ASSERT_FALSE(expr);
#define ASSERT_TRUE(actual) \
__test_compare_EQ_bool(1, "true", !!(actual), #actual, __FILE__, __LINE__)
#define ASSERT_FALSE(actual) \
__test_compare_EQ_bool(0, "false", !!(actual), #actual, __FILE__, __LINE__)
// Array comparison.
//
// Syntax:
//
// ASSERT_ARREQ(expected, actual, length);
#define _GENERATE_COMPARE_ARREQ(type, stype, unused) \
void __test_compare_ARREQ_##stype(type const *, const char *, type const *, \
const char *, __size_t, const char *, \
int);
__BEGIN_DECLS
_MACRO_FOREACH_TYPE(_GENERATE_COMPARE_ARREQ, unused)
__END_DECLS
#undef _GENERATE_COMPARE_ARREQ
#define ASSERT_ARREQ(expected, actual, length) \
_EXPRESSION_FOREACH_TYPE(__test_compare_ARREQ, *(actual)) \
(expected, #expected, actual, #actual, length, __FILE__, __LINE__)
// String comparison.
//
// Syntax:
//
// ASSERT_STREQ(expected, actual);
// ASSERT_STRNE(expected, actual);
#define _GENERATE_COMPARE_STR(type, stype, op) \
void __test_compare_STR##op##_##stype(type const *, const char *, \
type const *, const char *, \
const char *, int);
__BEGIN_DECLS
_MACRO_FOREACH_TYPE(_GENERATE_COMPARE_STR, EQ)
_MACRO_FOREACH_TYPE(_GENERATE_COMPARE_STR, NE)
__END_DECLS
#undef _GENERATE_COMPARE_STREQ
#define ASSERT_STREQ(expected, actual) \
_EXPRESSION_FOREACH_TYPE(__test_compare_STREQ, *(actual)) \
(expected, #expected, actual, #actual, __FILE__, __LINE__)
#define ASSERT_STRNE(expected, actual) \
_EXPRESSION_FOREACH_TYPE(__test_compare_STRNE, *(actual)) \
(expected, #expected, actual, #actual, __FILE__, __LINE__)
// Notes: print additional values upon failed assertions.
//
// Syntax:
//
// TEST(module, name) {
// for (int i = 0; i < 100; ++i) {
// SCOPED_NOTE(i, {
// ...
// });
// }
// }
enum __test_note_kind {
#define _TEST_NOTE_KIND(type, stype, unused) __test_note_kind_##stype,
_MACRO_FOREACH_TYPE(_TEST_NOTE_KIND, unused)
#undef _TEST_NOTE_KIND
};
struct __test_note {
enum __test_note_kind __kind;
union {
#define _TEST_NOTE_VALUE(type, stype, unused) type __value_##stype;
_MACRO_FOREACH_TYPE(_TEST_NOTE_VALUE, unused)
#undef _TEST_NOTE_VALUE
};
const char *__expression;
const char *__file;
int __line;
const struct __test_note *__parent;
};
extern _Thread_local const struct __test_note *__test_note_stack;
#define _GENERATE_NOTE_SET(type, stype, unused) \
static __inline void __note_set_##stype(struct __test_note *__note, \
type __value) { \
__note->__kind = __test_note_kind_##stype; \
__note->__value_##stype = __value; \
}
_MACRO_FOREACH_TYPE(_GENERATE_NOTE_SET, unused)
#undef _GENERATE_NOTE_SET
#define SCOPED_NOTE(exp, block) _SCOPED_NOTE(exp, block, __COUNTER__)
#define _SCOPED_NOTE(exp, block, counter) __SCOPED_NOTE(exp, block, counter)
#define __SCOPED_NOTE(exp, block, counter) \
do { \
struct __test_note __note_##counter; \
_EXPRESSION_FOREACH_TYPE(__note_set, exp)(&__note_##counter, exp); \
__note_##counter.__expression = #exp; \
__note_##counter.__file = __FILE__; \
__note_##counter.__line = __LINE__; \
__note_##counter.__parent = __test_note_stack; \
__test_note_stack = &__note_##counter; \
do \
block while (0); \
__test_note_stack = __note_##counter.__parent; \
} while (0)
#endif