forked from mattiasgustavsson/libs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestfw.h
438 lines (355 loc) · 16 KB
/
testfw.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
/*
------------------------------------------------------------------------------
Licensing information can be found at the end of the file.
------------------------------------------------------------------------------
testfw.h - v1.0 - Basic test framwework for C/C++.
Do this:
#define TESTFW_IMPLEMENTATION
before you include this file in *one* C/C++ file to create the implementation.
*/
#ifndef testfw_h
#define testfw_h
#define TESTFW_INIT() testfw_init()
#define TESTFW_SUMMARY() testfw_summary( __FILE__, __func__, __LINE__ )
#define TESTFW_TEST_BEGIN( desc ) testfw_test_begin( desc, __FILE__, __func__, __LINE__ );
#define TESTFW_TEST_END() testfw_test_end( __FILE__, __func__, __LINE__ )
#define TESTFW_EXPECTED( expression ) testfw_expected( (expression) ? 1 : 0, #expression, __FILE__, __func__, __LINE__ )
void testfw_init();
void testfw_summary( char const* filename, char const* funcname, int line );
void testfw_test_begin( char const* desc, char const* filename, char const* funcname, int line );
void testfw_test_end( char const* filename, char const* funcname, int line );
void testfw_expected( int expression, char const* expression_str, char const* filename, char const* funcname, int line );
void testfw_print_test_desc();
void testfw_print_failure( char const* filename, int line );
void testfw_assertion_count_inc();
void testfw_current_test_assertion_failed();
#endif /* testfw_h */
#ifdef TESTFW_IMPLEMENTATION
#undef TESTFW_IMPLEMENTATION
#define _CRT_NONSTDC_NO_DEPRECATE
#define _CRT_SECURE_NO_WARNINGS
#include <string.h>
#ifdef _WIN32
#pragma warning( push )
#pragma warning( disable: 4619 ) // pragma warning : there is no warning number 'number'
#pragma warning( disable: 4668 ) // 'symbol' is not defined as a preprocessor macro, replacing with '0'
#include <crtdbg.h>
#pragma warning( pop )
#endif /* _WIN32 */
#ifndef TESTFW_PRINTF
#define _CRT_NONSTDC_NO_DEPRECATE
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#define TESTFW_PRINTF printf
#endif
#ifdef TESTFW_NO_ANSI
#define TESTFW_ANSI_BLACK ""
#define TESTFW_ANSI_BLUE ""
#define TESTFW_ANSI_GREEN ""
#define TESTFW_ANSI_CYAN ""
#define TESTFW_ANSI_RED ""
#define TESTFW_ANSI_MAGENTA ""
#define TESTFW_ANSI_BROWN ""
#define TESTFW_ANSI_LIGHT_GREY ""
#define TESTFW_ANSI_GREY ""
#define TESTFW_ANSI_LIGHT_BLUE ""
#define TESTFW_ANSI_LIGHT_GREEN ""
#define TESTFW_ANSI_LIGHT_CYAN ""
#define TESTFW_ANSI_LIGHT_RED ""
#define TESTFW_ANSI_LIGHT_MAGENTA ""
#define TESTFW_ANSI_YELLOW ""
#define TESTFW_ANSI_WHITE ""
#define TESTFW_ANSI_RESET ""
#else
#ifndef TESTFW_ANSI_BLACK
#define TESTFW_ANSI_BLACK "\x1b[30m"
#endif
#ifndef TESTFW_ANSI_BLUE
#define TESTFW_ANSI_BLUE "\x1b[34m"
#endif
#ifndef TESTFW_ANSI_GREEN
#define TESTFW_ANSI_GREEN "\x1b[32m"
#endif
#ifndef TESTFW_ANSI_CYAN
#define TESTFW_ANSI_CYAN "\x1b[36m"
#endif
#ifndef TESTFW_ANSI_RED
#define TESTFW_ANSI_RED "\x1b[31m"
#endif
#ifndef TESTFW_ANSI_MAGENTA
#define TESTFW_ANSI_MAGENTA "\x1b[35m"
#endif
#ifndef TESTFW_ANSI_BROWN
#define TESTFW_ANSI_BROWN "\x1b[33m"
#endif
#ifndef TESTFW_ANSI_LIGHT_GREY
#define TESTFW_ANSI_LIGHT_GREY "\x1b[37m"
#endif
#ifndef TESTFW_ANSI_GREY
#define TESTFW_ANSI_GREY "\x1b[30;1m"
#endif
#ifndef TESTFW_ANSI_LIGHT_BLUE
#define TESTFW_ANSI_LIGHT_BLUE "\x1b[34;1m"
#endif
#ifndef TESTFW_ANSI_LIGHT_GREEN
#define TESTFW_ANSI_LIGHT_GREEN "\x1b[32;1m"
#endif
#ifndef TESTFW_ANSI_LIGHT_CYAN
#define TESTFW_ANSI_LIGHT_CYAN "\x1b[36;1m"
#endif
#ifndef TESTFW_ANSI_LIGHT_RED
#define TESTFW_ANSI_LIGHT_RED "\x1b[31;1m"
#endif
#ifndef TESTFW_ANSI_LIGHT_MAGENTA
#define TESTFW_ANSI_LIGHT_MAGENTA "\x1b[35;1m"
#endif
#ifndef TESTFW_ANSI_YELLOW
#define TESTFW_ANSI_YELLOW "\x1b[33;1m"
#endif
#ifndef TESTFW_ANSI_WHITE
#define TESTFW_ANSI_WHITE "\x1b[37;1m"
#endif
#ifndef TESTFW_ANSI_RESET
#define TESTFW_ANSI_RESET "\x1b[0m"
#endif
#endif
struct testfw_internal_current_test_state_t
{
int is_active;
char const* file;
char const* func;
int line;
char const* desc;
int desc_printed;
int counted_as_failed;
};
static struct
{
int tests_total;
int tests_failed;
int assertions_total;
int assertions_failed;
testfw_internal_current_test_state_t current_test;
} testfw_internal_state = {};
static void testfw_internal_print_progress_divider( char ch, int fail, int total )
{
int width = 79;
int first = (int)( ( width * fail ) / total );
int second = width - first;
if( fail > 0 && first == 0 )
{
++first;
--second;
}
TESTFW_PRINTF( "%s", TESTFW_ANSI_LIGHT_RED );
for( int i = 0; i < first; ++i ) TESTFW_PRINTF( "%c", ch );
TESTFW_PRINTF( "%s", TESTFW_ANSI_LIGHT_GREEN );
for( int i = 0; i < second; ++i ) TESTFW_PRINTF( "%c", ch );
TESTFW_PRINTF( "%s", TESTFW_ANSI_RESET );
TESTFW_PRINTF( "\n" );
}
#if defined( _WIN32 ) && defined( _DEBUG )
static int testfw_internal_debug_report_hook( int report_type, char* message, int* return_value )
{
static int total_leaks = 0;
*return_value = 0; // Don't break to debugger
if( stricmp( message, "Detected memory leaks!\n" ) == 0 )
{
TESTFW_PRINTF( "-------------------------------------------------------------------------------\n" );
TESTFW_PRINTF( "%sMEMORY CHECKS FAILED:%s Detected memory leaks\n", TESTFW_ANSI_LIGHT_RED,
TESTFW_ANSI_RESET );
return 1; // Tell CRT not to print the message
}
else if( stricmp( message, "Dumping objects ->\n" ) == 0 )
{
TESTFW_PRINTF( "-------------------------------------------------------------------------------\n" );
TESTFW_PRINTF( "\n" );
return 1; // Tell CRT not to print the message
}
else if( stricmp( message, "Object dump complete.\n" ) == 0 )
{
TESTFW_PRINTF( "\n" );
_CrtMemState state;
_CrtMemCheckpoint( &state );
static char const* const block_use_names[ 5 ] = { "Free", "Normal", "CRT", "Ignore", "Client", };
for( unsigned int i = 0; i < 5; ++i )
{
if( i != 2 && state.lCounts[ i ] )
TESTFW_PRINTF( "%Id bytes in %Id %hs Blocks.\n", (long long) state.lSizes[ i ],
(long long) state.lCounts[ i ], block_use_names[ i ] );
}
TESTFW_PRINTF( "\n" );
TESTFW_PRINTF( "High water mark: %Id bytes.\n", (long long)state.lHighWaterCount);
TESTFW_PRINTF( "All allocations: %Id bytes.\n", (long long)state.lTotalCount);
TESTFW_PRINTF( "\n" );
TESTFW_PRINTF( "===============================================================================\n" );
TESTFW_PRINTF( "%sMEMORY LEAKS: %d%s\n\n", TESTFW_ANSI_LIGHT_RED, total_leaks, TESTFW_ANSI_RESET );
return 1; // Tell CRT not to print the message
}
else if( strnicmp( message, " Data: <", 8 ) == 0 )
{
++total_leaks;
return 1; // Tell CRT not to print the message
}
return 0; // Tell CRT it can print the message, as we didn't handle it
}
#endif /* _WIN32 && _DEBUG */
void testfw_init()
{
memset( &testfw_internal_state, 0, sizeof( testfw_internal_state ) );
#ifdef _WIN32
int flag = _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG ); // Get current flag
flag ^= _CRTDBG_LEAK_CHECK_DF; // Turn on leak-checking bit
_CrtSetDbgFlag( flag ); // Set flag to the new value
_CrtSetReportMode( _CRT_WARN, _CRTDBG_MODE_FILE );
_CrtSetReportFile( _CRT_WARN, _CRTDBG_FILE_STDOUT );
_CrtSetReportHook2( _CRT_RPTHOOK_INSTALL, testfw_internal_debug_report_hook );
#endif /* _WIN32 */
}
static int testfw_internal_must_be_in_test()
{
if( !testfw_internal_state.current_test.is_active )
TESTFW_PRINTF( "\n\n%sEXPECTED TO BE IN AN ACTIVE TEST, BUT NO TEST IS CURRENTLY ACTIVE.%s\n\n",
TESTFW_ANSI_LIGHT_RED, TESTFW_ANSI_RESET );
return !testfw_internal_state.current_test.is_active;
}
static int testfw_internal_can_not_be_in_test()
{
if( testfw_internal_state.current_test.is_active )
TESTFW_PRINTF( "\n\n%sEXPECTED TO NOT BE IN AN ACTIVE TEST, BUT A TEST IS CURRENTLY ACTIVE.%s\n\n",
TESTFW_ANSI_LIGHT_RED, TESTFW_ANSI_RESET );
return testfw_internal_state.current_test.is_active;
}
void testfw_print_test_desc()
{
if( testfw_internal_must_be_in_test() ) return;
if( !testfw_internal_state.current_test.desc_printed )
{
testfw_internal_state.current_test.desc_printed = 1;
TESTFW_PRINTF( "\n" );
TESTFW_PRINTF( "-------------------------------------------------------------------------------\n" );
TESTFW_PRINTF( "%s\n", testfw_internal_state.current_test.desc ? testfw_internal_state.current_test.desc :
"<NO DESCRIPTION>" );
TESTFW_PRINTF( "-------------------------------------------------------------------------------\n" );
TESTFW_PRINTF( "%s%s(%d): %s%s\n", TESTFW_ANSI_GREY, testfw_internal_state.current_test.file,
testfw_internal_state.current_test.line, TESTFW_ANSI_RESET, testfw_internal_state.current_test.func );
TESTFW_PRINTF( "%s", TESTFW_ANSI_GREY );
TESTFW_PRINTF( "...............................................................................\n" );
TESTFW_PRINTF( "%s", TESTFW_ANSI_RESET );
}
}
void testfw_summary( char const* filename, char const* funcname, int line )
{
if( testfw_internal_can_not_be_in_test() ) return;
TESTFW_PRINTF( "\n" );
testfw_internal_print_progress_divider( '=', testfw_internal_state.tests_failed, testfw_internal_state.tests_total );
if( testfw_internal_state.tests_failed == 0 && testfw_internal_state.assertions_failed == 0 )
{
TESTFW_PRINTF( "%sAll tests passed%s (%d assertions in %d test cases)\n", TESTFW_ANSI_LIGHT_GREEN,
TESTFW_ANSI_RESET, testfw_internal_state.assertions_total, testfw_internal_state.tests_total );
}
else
{
TESTFW_PRINTF( "test cases: %4d |%s %4d passed %s|%s %4d failed%s\n", testfw_internal_state.tests_total,
TESTFW_ANSI_LIGHT_GREEN, testfw_internal_state.tests_total - testfw_internal_state.tests_failed,
TESTFW_ANSI_RESET, TESTFW_ANSI_LIGHT_RED, testfw_internal_state.tests_failed, TESTFW_ANSI_RESET );
TESTFW_PRINTF( "assertions: %4d |%s %4d passed %s|%s %4d failed%s\n", testfw_internal_state.assertions_total,
TESTFW_ANSI_LIGHT_GREEN, testfw_internal_state.assertions_total - testfw_internal_state.assertions_failed,
TESTFW_ANSI_RESET, TESTFW_ANSI_LIGHT_RED, testfw_internal_state.assertions_failed, TESTFW_ANSI_RESET );
}
TESTFW_PRINTF( "\n\n" );
}
void testfw_test_begin( char const* desc, char const* filename, char const* funcname, int line )
{
if( testfw_internal_can_not_be_in_test() ) return;
memset( &testfw_internal_state.current_test, 0, sizeof( testfw_internal_state.current_test ) );
testfw_internal_state.current_test.is_active = 1;
testfw_internal_state.current_test.file = filename;
testfw_internal_state.current_test.func = funcname;
testfw_internal_state.current_test.line = line;
testfw_internal_state.current_test.desc = desc;
testfw_internal_state.current_test.desc_printed = 0;
testfw_internal_state.current_test.counted_as_failed = 0;
++testfw_internal_state.tests_total;
}
void testfw_test_end( char const* filename, char const* funcname, int line )
{
if( testfw_internal_must_be_in_test() ) return;
memset( &testfw_internal_state.current_test, 0, sizeof( testfw_internal_state.current_test ) );
}
void testfw_current_test_assertion_failed()
{
if( testfw_internal_must_be_in_test() ) return;
++testfw_internal_state.assertions_failed;
if( !testfw_internal_state.current_test.counted_as_failed )
{
testfw_internal_state.current_test.counted_as_failed = 1;
++testfw_internal_state.tests_failed;
}
}
void testfw_assertion_count_inc()
{
if( testfw_internal_must_be_in_test() ) return;
++testfw_internal_state.assertions_total;
}
void testfw_print_failure( char const* filename, int line )
{
TESTFW_PRINTF( "\n%s%s(%d): %sFAILED:%s\n", TESTFW_ANSI_GREY, filename, line, TESTFW_ANSI_LIGHT_RED,
TESTFW_ANSI_RESET );
}
void testfw_expected( int expression, char const* expression_str, char const* filename,
char const* funcname, int line )
{
if( testfw_internal_must_be_in_test() ) return;
testfw_assertion_count_inc();
if( !expression )
{
testfw_current_test_assertion_failed();
testfw_print_test_desc();
testfw_print_failure( filename, line );
TESTFW_PRINTF( "\n %sTESTFW_EXPECTED( %s%s%s )%s\n", TESTFW_ANSI_CYAN, TESTFW_ANSI_WHITE, expression_str,
TESTFW_ANSI_CYAN, TESTFW_ANSI_RESET );
}
}
#endif /* TESTFW_IMPLEMENTATION */
/*
------------------------------------------------------------------------------
This software is available under 2 licenses - you may choose the one you like.
------------------------------------------------------------------------------
ALTERNATIVE A - MIT License
Copyright (c) 2019 Mattias Gustavsson
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
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 OR COPYRIGHT HOLDERS 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.
------------------------------------------------------------------------------
ALTERNATIVE B - Public Domain (www.unlicense.org)
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.
------------------------------------------------------------------------------
*/