-
Notifications
You must be signed in to change notification settings - Fork 1
/
unit.h
268 lines (225 loc) · 10.7 KB
/
unit.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
/**
zpl - Unit testing framework
Usage:
#include "unit.h" in EXACTLY one source file, usually the one containing your testing app's entry point.
There really is no need to include this file multiple times within a project, unless you wish to run
multiple tests within a single executable or split test cases to multiple compilation units, in such case
define UNIT_STATIC to ensure the library won't leak symbols outside compilation units.
and cover your beautiful code already!
GitHub:
https://github.com/zpl-c/tester
Version History:
1.1.1 - ensure memory arena only works with zpl present
1.1.0 - introduce memory arena for per-module allocations
1.0.1 - Small tweaks
1.0.0 - Where it all started... (not really)
License:
This Software is dual licensed under the following licenses:
Unlicense
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org/>
BSD 3-Clause
Copyright (c) 2016-2021 Dominik Madarász. All rights reserved.
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.
3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
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.
*/
/* Adjust it to your needs, preferably to the number of modules you wish to test against. */
#ifndef UNIT_MAX_MODULES
#define UNIT_MAX_MODULES 256
#endif
#ifndef UNIT_SKIP_MAGIC
#define UNIT_SKIP_MAGIC 0xFF
#endif
#ifndef UNIT_JOIN2
#define UNIT_JOIN2(a,b) a##b
#endif
#ifndef UNIT_TEST_PREFIX
#define UNIT_TEST_PREFIX "It "
#endif
/* Isolates test results within compilation units, this allows for running
multiple test suites per single binary. */
#ifdef UNIT_STATIC
#define UNIT_DEF static
#else
#define UNIT_DEF
#endif
#include <string.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdint.h>
/* zpl specific */
/* verify if zpl is present */
#if defined(ZPL_H) && !defined(UNIT_ZPL_DISABLE_ARENA)
/* specifies memory limit for per-module memory allocator */
#ifndef UNIT_ARENA_MEM
#define UNIT_ARENA_MEM (512*1024)
#endif
#define UNIT_ZPL_INITIALIZE_MEM_ARENA() \
zpl_arena hunk_mem={0}; \
zpl_arena_init_from_allocator(&hunk_mem, zpl_heap(), UNIT_ARENA_MEM); \
zpl_allocator mem_alloc = zpl_arena_allocator(&hunk_mem); \
zpl_unused(mem_alloc);
#define UNIT_ZPL_DESTROY_MEM_ARENA() \
zpl_arena_free(&hunk_mem);
#define UNIT_ZPL_CAPTURE_MEMORY() \
zpl_temp_arena_memory mem_snapshot = zpl_temp_arena_memory_begin(&hunk_mem);
#define UNIT_ZPL_RESTORE_MEMORY() \
zpl_temp_arena_memory_end(mem_snapshot);
#else
#define UNIT_ZPL_INITIALIZE_MEM_ARENA()
#define UNIT_ZPL_DESTROY_MEM_ARENA()
#define UNIT_ZPL_CAPTURE_MEMORY()
#define UNIT_ZPL_RESTORE_MEMORY()
#endif
#define MODULE(name, ...) \
int32_t UNIT_JOIN2(module__,name)() { \
printf("--------------------------------------\n"); \
printf(" module: %s\n", #name); \
printf("--------------------------------------\n"); \
fflush(stdout); \
_g_modules++; \
int32_t _total = 0; \
int32_t _errors = 0; \
int32_t _lasterr = 0; \
char *_errstr = 0; \
UNIT_ZPL_INITIALIZE_MEM_ARENA(); \
{__VA_ARGS__}; \
UNIT_ZPL_DESTROY_MEM_ARENA(); \
fflush(stdout); \
printf("\n results: %d total, %s%d failed\x1B[0m, %s%d passed\x1B[0m\n", _total, _errors>0?"\x1B[31m":"", _errors, _errors==0?"\x1B[32m":"", _total - _errors); \
_g_total += _total; \
_g_errors += _errors; \
if (_errors) _g_modules_err++; \
return (_errors); \
}
#define IT(desc, ...) \
_lasterr = 0; \
_errstr = ""; \
_total += 1; \
{ \
UNIT_ZPL_CAPTURE_MEMORY(); \
do {__VA_ARGS__} while(0); \
UNIT_ZPL_RESTORE_MEMORY(); \
} \
if (_lasterr != UNIT_SKIP_MAGIC) _errors += _lasterr; \
printf(" * [%s]: " UNIT_TEST_PREFIX "%s %s\n", (_lasterr == UNIT_SKIP_MAGIC) ? "\x1B[33mSKIP\x1B[0m" : (_lasterr) ? "\x1B[31mFAIL\x1B[0m" : "\x1B[32mPASS\x1B[0m", desc, _errstr);
/* TEST CHECKS */
#define FAIL(a, b) { _errstr = unit__bprintf("\n\n\tassert: \x1B[31m%s:%lld %s %s:%lld\x1B[0m\n\tat %s:%d\n", #a, a, (a == b)?"==":"!=", #b, b, __FILE__, __LINE__); _lasterr = 1; break; }
#define UFAIL(a, b) { _errstr = unit__bprintf("\n\n\tassert: \x1B[31m%s:%llu %s %s:%llu\x1B[0m\n\tat %s:%d\n", #a, a, (a == b)?"==":"!=", #b, b, __FILE__, __LINE__); _lasterr = 1; break; }
#define FFAIL(a, b) { _errstr = unit__bprintf("\n\n\tassert: \x1B[31m%s:%g %s %s:%g\x1B[0m\n\tat %s:%d\n", #a, a, (a == b)?"==":"!=", #b, b, __FILE__, __LINE__); _lasterr = 1; break; }
#define STRFAIL(a, b) { _errstr = unit__bprintf("\n\n\tassert: \x1B[31m%s:%s %s %s:%s\x1B[0m\n\tat %s:%d\n", #a, (char *)a, (!strcmp(a,b))?"==":"!=", #b, b, __FILE__, __LINE__); _lasterr = 1; break; }
#define EQUALS(a, b) if (a != b) { FAIL(a, b); }
#define UEQUALS(a, b) if (a != b) { UFAIL(a, b); }
#define FEQUALS(a, b) if (a != b) { FFAIL(a, b); }
#define STREQUALS(a, b) if (!!strcmp(a,b)) { STRFAIL(a, b); }
#define STRCEQUALS(a, b, c) if (!!strncmp(a,b, c)) { STRFAIL(a, b); }
#define STRCNEQUALS(a, b, c) if (!strncmp(a,b, c)) { STRFAIL(a, b); }
#define STRNEQUALS(a, b) if (!strcmp(a,b)) { STRFAIL(a, b); }
#define NEQUALS(a, b) if (a == b) { FAIL(a, b); }
#define LESSER(a, b) if (a >= b) { FAIL(a, b); }
#define GREATER(a, b) if (a <=b) { FAIL(a, b); }
#define LESSEREQ(a, b) if (a < b) { FAIL(a, b); }
#define GREATEREQ(a, b) if (a > b) { FAIL(a, b); }
#define SKIP() { _lasterr = UNIT_SKIP_MAGIC; break; }
#if defined(__GCC__) || defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wattributes"
#pragma GCC diagnostic ignored "-Wunused-value"
#pragma GCC diagnostic ignored "-Wignored-qualifiers"
#pragma GCC diagnostic ignored "-Wunused-variable"
#pragma GCC diagnostic ignored "-Wunused-function"
#pragma GCC diagnostic ignored "-Wwrite-strings"
#pragma GCC diagnostic ignored "-Wunused-parameter"
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#pragma GCC diagnostic ignored "-Wmissing-braces"
#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
#pragma GCC diagnostic ignored "-Wpointer-to-int-cast"
#endif
#if defined(_MSC_VER)
#pragma warning(push)
#pragma warning(disable : 4201)
#pragma warning(disable : 4127) // Conditional expression is constant
#endif
typedef int32_t (*unit_case)();
#define UNIT_CREATE(name) \
const char *unit_name = name; \
unit_case unit_modules[UNIT_MAX_MODULES] = {0}; \
int32_t unit_count = 0;
#define UNIT_MODULE(name) \
unit_modules[unit_count++] = UNIT_JOIN2(module__,name)
#define UNIT_RUN() \
unit_main(unit_name, unit_modules, unit_count)
/* INTERNALS */
UNIT_DEF int32_t _g_modules = 0;
UNIT_DEF int32_t _g_modules_err = 0;
UNIT_DEF int32_t _g_total = 0;
UNIT_DEF int32_t _g_errors = 0;
UNIT_DEF int32_t unit_main(const char *name, unit_case *cases, int32_t count) {
int32_t err = 0, cnt = count;
printf("> testing suite:\n\n");
printf(" * suite: %s\n", name);
printf(" * modules: %d\n", cnt);
printf("\n");
for (int32_t i = 0; i < count; ++i) {
err += cases[i]();
}
fflush(stdout);
printf("--------------------------------------\n"); \
printf("> total:\n\n");
printf(" * modules: %d total, %s%d failed\x1B[0m, %s%d passed\x1B[0m\n", _g_modules, _g_modules_err>0?"\x1B[31m":"" ,_g_modules_err, _g_modules_err==0?"\x1B[32m":"", _g_modules - _g_modules_err);
printf(" * tests: %d total, %s%d failed\x1B[0m, %s%d passed\x1B[0m\n", _g_total, _g_errors>0?"\x1B[31m":"" ,_g_errors, _g_errors==0?"\x1B[32m":"", _g_total - _g_errors);
printf("\n");
return -err;
}
// Locally persisting buffer
static inline char* unit__bprintf(const char* fmt, ...)
{
static char buf[128];
va_list args;
va_start(args, fmt);
vsprintf(buf, fmt, args);
va_end(args);
return buf;
}
#if defined(ZPL_COMPILER_MSVC)
#pragma warning(pop)
#endif
#if defined(__GCC__) || defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic pop
#endif