-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsput.h
319 lines (264 loc) · 15 KB
/
sput.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
/*
* sput - Simple, Portable Unit Testing Framework for C/C++ v1.4.0
*
* http://www.use-strict.de/sput-unit-testing/
*
*
* Copyright (C) 2011-2015 Lingua-Systems Software GmbH
* Copyright (C) 2016 Alex Linke <[email protected]>
*
* All rights reserved.
*
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * 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 COPYRIGHT HOLDERS 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 COPYRIGHT HOLDER 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.
*/
#ifndef HAVE_SPUT_H
#define HAVE_SPUT_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
/* ===================================================================
* definitions
* =================================================================== */
#define SPUT_VERSION_MAJOR 1
#define SPUT_VERSION_MINOR 4
#define SPUT_VERSION_PATCH 0
#define SPUT_VERSION_STRING "1.4.0"
#define SPUT_DEFAULT_SUITE_NAME "Unlabeled Suite"
#define SPUT_DEFAULT_CHECK_NAME "Unlabeled Check"
#define SPUT_INITIALIZED 0x06 /* ACK */
/* ===================================================================
* sput global variable
* =================================================================== */
static struct sput
{
FILE *out;
char initialized;
struct sput_overall
{
unsigned long checks;
unsigned long suites;
unsigned long ok;
unsigned long nok;
} overall;
struct sput_suite
{
const char *name;
unsigned long nr;
unsigned long checks;
unsigned long ok;
unsigned long nok;
} suite;
struct sput_test
{
const char *name;
unsigned long nr;
} test;
struct sput_check
{
const char *name;
const char *cond;
const char *type;
unsigned long line;
} check;
struct sput_time
{
time_t start;
time_t end;
} time;
} __sput;
/* ==================================================================
* sput internal macros
* ================================================================== */
#define _sput_die_unless_initialized() \
if (__sput.initialized != SPUT_INITIALIZED) \
{ \
fputs("sput_start_testing() omitted\n", stderr); \
exit(EXIT_FAILURE); \
}
#define _sput_die_unless_suite_set() \
if (! __sput.suite.name) \
{ \
fputs("sput_enter_suite() omitted\n", __sput.out); \
exit(EXIT_FAILURE); \
}
#define _sput_die_unless_test_set() \
if (! __sput.test.name) \
{ \
fputs("sput_run_test() omitted\n", __sput.out); \
exit(EXIT_FAILURE); \
}
#define _sput_check_failed() \
{ \
_sput_die_unless_initialized(); \
_sput_die_unless_suite_set(); \
__sput.suite.nok++; \
fprintf(__sput.out, \
"[%lu:%lu] %s:#%lu \"%s\" FAIL\n" \
"! Type: %s\n" \
"! Condition: %s\n" \
"! Line: %lu\n", \
__sput.suite.nr, __sput.suite.checks, __sput.test.name, \
__sput.test.nr, __sput.check.name, __sput.check.type, \
__sput.check.cond, __sput.check.line); \
}
#define _sput_check_succeeded() \
{ \
_sput_die_unless_initialized(); \
_sput_die_unless_suite_set(); \
__sput.suite.ok++; \
fprintf(__sput.out, \
"[%lu:%lu] %s:#%lu \"%s\" pass\n", \
__sput.suite.nr, __sput.suite.checks, \
__sput.test.name, \
__sput.test.nr, \
__sput.check.name); \
}
/* ==================================================================
* user macros
* ================================================================== */
#define sput_start_testing() \
do { \
memset(&__sput, 0, sizeof(__sput)); \
__sput.out = stdout; \
__sput.time.start = time(NULL); \
__sput.initialized = SPUT_INITIALIZED; \
} while (0)
#define sput_leave_suite() \
do { \
float failpls = 0.0f; \
_sput_die_unless_initialized(); \
_sput_die_unless_suite_set(); \
failpls = __sput.suite.checks ? (float) \
((__sput.suite.nok * 100.0) / __sput.suite.checks) : \
0.0f; \
fprintf(__sput.out, \
"\n--> %lu check(s), %lu ok, %lu failed (%.2f%%)\n", \
__sput.suite.checks, __sput.suite.ok, __sput.suite.nok, \
failpls); \
__sput.overall.checks += __sput.suite.checks; \
__sput.overall.ok += __sput.suite.ok; \
__sput.overall.nok += __sput.suite.nok; \
memset(&__sput.suite, 0, sizeof(__sput.suite)); \
} while (0)
#define sput_get_return_value() \
(__sput.overall.nok > 0 ? EXIT_FAILURE : EXIT_SUCCESS)
#define sput_enter_suite(_name) \
do { \
_sput_die_unless_initialized(); \
if (__sput.suite.name) \
{ \
sput_leave_suite(); \
} \
__sput.suite.name = _name != NULL ? \
_name : SPUT_DEFAULT_SUITE_NAME; \
__sput.suite.nr = ++__sput.overall.suites; \
fprintf(__sput.out, "\n== Entering suite #%lu, \"%s\" ==\n\n", \
__sput.suite.nr, __sput.suite.name); \
} while (0)
#define sput_finish_testing() \
do { \
float failpft = 0.0f; \
_sput_die_unless_initialized(); \
if (__sput.suite.name) \
{ \
sput_leave_suite(); \
} \
failpft = __sput.overall.checks ? (float) \
((__sput.overall.nok * 100.0) / __sput.overall.checks) : \
0.0f; \
__sput.time.end = time(NULL); \
fprintf(__sput.out, \
"\n==> %lu check(s) in %lu suite(s) finished after %.2f " \
"second(s),\n" \
" %lu succeeded, %lu failed (%.2f%%)\n" \
"\n[%s]\n", \
__sput.overall.checks, __sput.overall.suites, \
difftime(__sput.time.end, __sput.time.start), \
__sput.overall.ok, __sput.overall.nok, failpft, \
(sput_get_return_value() == EXIT_SUCCESS) ? \
"SUCCESS" : "FAILURE"); \
} while (0)
#define sput_set_output_stream(_fp) \
do { \
__sput.out = _fp != NULL ? _fp : stdout; \
} while (0)
#define sput_fail_if(_cond, _name) \
do { \
_sput_die_unless_initialized(); \
_sput_die_unless_suite_set(); \
_sput_die_unless_test_set(); \
__sput.check.name = _name != NULL ? \
_name : SPUT_DEFAULT_CHECK_NAME; \
__sput.check.line = __LINE__; \
__sput.check.cond = #_cond; \
__sput.check.type = "fail-if"; \
__sput.test.nr++; \
__sput.suite.checks++; \
if ((_cond)) \
{ \
_sput_check_failed(); \
} \
else \
{ \
_sput_check_succeeded(); \
} \
} while (0)
#define sput_fail_unless(_cond, _name) \
do { \
_sput_die_unless_initialized(); \
_sput_die_unless_suite_set(); \
_sput_die_unless_test_set(); \
__sput.check.name = _name != NULL ? \
_name : SPUT_DEFAULT_CHECK_NAME; \
__sput.check.line = __LINE__; \
__sput.check.cond = #_cond; \
__sput.check.type = "fail-unless"; \
__sput.test.nr++; \
__sput.suite.checks++; \
if (! (_cond)) \
{ \
_sput_check_failed(); \
} \
else \
{ \
_sput_check_succeeded(); \
} \
} while (0)
#define sput_run_test(_func) \
do { \
_sput_die_unless_initialized(); \
_sput_die_unless_suite_set(); \
memset(&__sput.test, 0, sizeof(__sput.test)); \
__sput.test.name = #_func; \
_func(); \
} while (0)
#ifdef __cplusplus
}
#endif
#endif /* HAVE_SPUT_H */
/* vim: set ft=c sts=4 sw=4 ts=4 ai et: */