-
Notifications
You must be signed in to change notification settings - Fork 3
/
utest.h
564 lines (456 loc) · 16.2 KB
/
utest.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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
/*
utest - The micro unit test framework for C99
Author - Oli Wilkinson (https://github.com/evolutional/utest)
This file provides both the interface and the implementation.
To provide the implementation for this library, you MUST define
UTEST_C_IMPLEMENTATION in exactly on source file.
#define UTEST_C_IMPLEMENTATION
#include "utest.h"
USAGE
-----
See README.md for details
LICENSE
-------
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>
*/
#ifndef UTESTI_INCLUDE_UTEST_H_
#define UTESTI_INCLUDE_UTEST_H_
#include <stdint.h>
#include <setjmp.h>
#ifndef UTEST_MAX_TESTS_PER_FIXTURE
#define UTEST_MAX_TESTS_PER_FIXTURE 265
#endif
#ifndef UTEST_MSG_BUFFER_SIZE
#define UTEST_MSG_BUFFER_SIZE 512
#endif
typedef void(*_utest_func)();
typedef struct _utest_entry
{
// name of the test
const char* name;
// Function to run for the actual test
_utest_func test;
const char* file;
const int line;
#ifdef __cplusplus
_utest_entry() : name(0), test(0), file(0), line(0) {}
_utest_entry(const char* a, _utest_func b, const char* c, const int d) : name(a), test(b), file(c), line(d) {}
#endif
} utest_entry;
typedef struct _utest_fixture
{
// name of the fixture
const char* name;
// Fixture set up (run before any tests)
_utest_func f_setup;
// Fixture teardown (run after all tests)
_utest_func f_teardown;
// Set up (run before each test)
_utest_func t_setup;
// Teardown (run after each test)
_utest_func t_teardown;
utest_entry tests[UTEST_MAX_TESTS_PER_FIXTURE];
} utest_fixture;
typedef enum _utest_test_result
{
TEST_RESULT_SUCCESS = 0,
TEST_RESULT_FAILED = -1
} utest_test_result;
// handler called when a test completes (pass/fail)
typedef void(*utest_result_func)(const utest_fixture*, const utest_entry*, utest_test_result result, const char*);
typedef void(*utest_print_func)(const utest_entry*, const char*);
typedef struct _utest_state
{
void* user_state;
int run_count;
int pass_count;
int fail_count;
// working state
utest_fixture* current_fixture;
utest_entry* current_test;
jmp_buf restore_env;
char msgbuffer[UTEST_MSG_BUFFER_SIZE];
} utest_state;
typedef struct _utest_cfg
{
utest_result_func result_func;
utest_print_func print_func;
} utest_cfg;
#ifdef __cplusplus
extern "C" {
#endif
extern void utest_init();
extern utest_test_result utest_run_fixture(utest_fixture* fixture);
extern utest_test_result utest_run_test(utest_entry* test);
// assert stuff
extern void utest_assert_integer_equal(signed expected, signed actual, const char* message);
extern void utest_assert_null(void* actual, char expect_null, const char* message);
extern void utest_assert_float_equal(double expected, double actual, double epsilon, const char* message);
extern void utest_assert_string_null(const char* actual, const char* message);
extern void utest_assert_string_equal(const char* expected, const char* actual, const char* message, char ignore_case);
extern void utest_assert_pointer_equal(void* expected, void* actual, const char* message);
extern void utest_fail(const char* format, ...);
#ifndef UTEST_C_IMPLEMENTATION
extern utest_state g_utest_state;
extern utest_cfg g_utest_cfg;
#endif
// Utility
extern void* utest_get_user();
extern void utest_set_user(void*);
extern void utest_test_result_func(const utest_fixture* fixture, const utest_entry* test, utest_test_result result, const char* message);
extern void utest_test_print_func(const utest_entry* test, const char* message);
extern void utest_set_result_func(utest_result_func func);
extern void utest_set_print_func(utest_print_func func);
extern void utest_reset_result_func();
extern void utest_reset_print_func();
extern const char* utest_last_msg();
#ifdef __cplusplus
}
#endif
/////////////
// Assert helpers
#define TEST_FAIL_MESSAGE(message) utest_fail(message);
#define TEST_FAIL TEST_FAIL_MESSAGE(0)
#define TEST_INCONCLUSIVE_MESSAGE(message) utest_inconclusive(message);
#define TEST_INCONCLUSIVE TEST_INCONCLUSIVE_MESSAGE(0)
#define TEST_MESSAGE(message) utest_print(message);
#define TEST_ASSERT_MESSAGE(expr, message) if(!(expr)) { TEST_FAIL_MESSAGE(message); }
#define TEST_ASSERT(expr) TEST_ASSERT_MESSAGE(expr, "Expression is false")
#define TEST_ASSERT_EQUAL_P(expected, actual, message) utest_assert_pointer_equal(expected, actual, message)
#define TEST_ASSERT_EQUAL_I(type, expected, actual, message) utest_assert_integer_equal((type)expected, (type)actual, message)
#define TEST_ASSERT_EQUAL_F(type, expected, actual, epsilon, message) utest_assert_float_equal((type)expected, (type)actual, epsilon, message)
#define TEST_ASSERT_EQUAL_INT(expected, actual) TEST_ASSERT_EQUAL_I(signed int, expected, actual, 0)
#define TEST_ASSERT_EQUAL_INT8(expected, actual) TEST_ASSERT_EQUAL_I(int8_t, expected, actual, 0)
#define TEST_ASSERT_EQUAL_INT16(expected, actual) TEST_ASSERT_EQUAL_I(int16_t, expected, actual, 0)
#define TEST_ASSERT_EQUAL_INT32(expected, actual) TEST_ASSERT_EQUAL_I(int32_t, expected, actual, 0)
#define TEST_ASSERT_EQUAL_INT64(expected, actual) TEST_ASSERT_EQUAL_I(int64_t, expected, actual, 0)
#define TEST_ASSERT_EQUAL_UINT(expected, actual) TEST_ASSERT_EQUAL_I(unsigned int, expected, actual, 0)
#define TEST_ASSERT_EQUAL_UINT8(expected, actual) TEST_ASSERT_EQUAL_I(uint8_t, expected, actual, 0)
#define TEST_ASSERT_EQUAL_UINT16(expected, actual) TEST_ASSERT_EQUAL_I(uint16_t, expected, actual, 0)
#define TEST_ASSERT_EQUAL_UINT32(expected, actual) TEST_ASSERT_EQUAL_I(uint32_t, expected, actual, 0)
#define TEST_ASSERT_EQUAL_UINT64(expected, actual) TEST_ASSERT_EQUAL_I(uint64_t, expected, actual, 0)
#define TEST_ASSERT_EQUAL_PTR(expected, actual) TEST_ASSERT_EQUAL_P(expected, actual, 0)
#define TEST_ASSERT_EQUAL_PTR_MESSAGE(expected, actual, message) TEST_ASSERT_EQUAL_P(expected, actual, message)
#define TEST_ASSERT_NULL(actual) utest_assert_null(actual, 1, 0)
#define TEST_ASSERT_NULL_MESSAGE(actual, message) utest_assert_null(actual, 1, message)
#define TEST_ASSERT_NOT_NULL(actual) utest_assert_null(actual, 0, 0)
#define TEST_ASSERT_NOT_NULL_MESSAGE(actual, message) utest_assert_null(actual, 0, message)
#define TEST_ASSERT_EQUAL_FLOAT(expected, actual, epsilon) TEST_ASSERT_EQUAL_F(float, expected, actual, epsilon, 0)
#define TEST_ASSERT_EQUAL_DOUBLE(expected, actual, epsilon) TEST_ASSERT_EQUAL_F(double, expected, actual, epsilon, 0)
#define TEST_ASSERT_EQUAL_INT_MESSAGE(expected, actual, message) TEST_ASSERT_EQUAL_I(signed int, expected, actual, message)
#define TEST_ASSERT_EQUAL_INT8_MESSAGE(expected, actual, message) TEST_ASSERT_EQUAL_I(int8_t, expected, actual, message)
#define TEST_ASSERT_EQUAL_INT16_MESSAGE(expected, actual, message) TEST_ASSERT_EQUAL_I(int16_t, expected, actual, message)
#define TEST_ASSERT_EQUAL_INT32_MESSAGE(expected, actual, message) TEST_ASSERT_EQUAL_I(int32_t, expected, actual, message)
#define TEST_ASSERT_EQUAL_INT64_MESSAGE(expected, actual, message) TEST_ASSERT_EQUAL_I(int64_t, expected, actual, message)
#define TEST_ASSERT_EQUAL_UINT_MESSAGE(expected, actual, message) TEST_ASSERT_EQUAL_I(unsigned int, expected, actual, message)
#define TEST_ASSERT_EQUAL_UINT8_MESSAGE(expected, actual, message) TEST_ASSERT_EQUAL_I(uint8_t, expected, actual, message)
#define TEST_ASSERT_EQUAL_UINT16_MESSAGE(expected, actual, message) TEST_ASSERT_EQUAL_I(uint16_t, expected, actual, message)
#define TEST_ASSERT_EQUAL_UINT32_MESSAGE(expected, actual, message) TEST_ASSERT_EQUAL_I(uint32_t, expected, actual, message)
#define TEST_ASSERT_EQUAL_UINT64_MESSAGE(expected, actual, message) TEST_ASSERT_EQUAL_I(uint64_t, expected, actual, message)
#define TEST_ASSERT_EQUAL_FLOAT_MESSAGE(expected, actual, epsilon, message) TEST_ASSERT_EQUAL_F(float, expected, actual, epsilon, message)
#define TEST_ASSERT_EQUAL_DOUBLE_MESSAGE(expected, actual, epsilon, message) TEST_ASSERT_EQUAL_F(double, expected, actual, epsilon, message)
#define TEST_ASSERT_EQUAL_NOCASE_STRING_MESSAGE(expected, actual, message) utest_assert_string_equal(expected, actual, message, 1)
#define TEST_ASSERT_EQUAL_NOCASE_STRING(expected, actual) TEST_ASSERT_EQUAL_NOCASE_STRING_MESSAGE(expected, actual, 0)
#define TEST_ASSERT_EQUAL_STRING_MESSAGE(expected, actual, message) utest_assert_string_equal(expected, actual, message, 0)
#define TEST_ASSERT_EQUAL_STRING(expected, actual) TEST_ASSERT_EQUAL_STRING_MESSAGE(expected, actual, 0)
#define TEST_ASSERT_NULL_STRING_MESSAGE(actual, message) utest_assert_string_null(actual, message)
#define TEST_ASSERT_NULL_STRING(actual) TEST_ASSERT_NULL_STRING_MESSAGE(actual, 0)
//////////////
// Test specs
#define TEST(test_name) \
void test_name(void)
#define DECLARE_TEST(test_name) \
void test_name(void);\
static utest_entry test_name ## _test = { #test_name, test_name, __FILE__, __LINE__ }
#define TEST_FIXTURE_BEGIN_A(fixture_name, fixture_setup, fixture_teardown, test_setup, test_teardown) \
static utest_fixture fixture_name = { #fixture_name, fixture_setup, fixture_teardown, test_setup, test_teardown, {
#define TEST_FIXTURE_BEGIN_G(fixture_name, fixture_setup, fixture_teardown) \
TEST_FIXTURE_BEGIN_A(fixture_name, fixture_setup, fixture_teardown, 0, 0)
#define TEST_FIXTURE_BEGIN_F(fixture_name, test_setup, test_teardown) \
TEST_FIXTURE_BEGIN_A(fixture_name, 0, 0, test_setup, test_teardown)
#define TEST_FIXTURE_BEGIN(fixture_name) TEST_FIXTURE_BEGIN_F(fixture_name, 0, 0)
// Adds a test to a fixture
#define TEST_FIXTURE_TEST(test_name) \
{ #test_name, test_name, __FILE__, __LINE__ },
#define TEST_FIXTURE_END() \
{ 0, 0, 0, 0 } \
} \
};
#define TEST_RUN(test_name) \
utest_run_test( &test_name ## _test )
#define TEST_RUN_FIXTURE(fixture_name) \
utest_run_fixture( &fixture_name )
#endif
#ifdef UTEST_C_IMPLEMENTATION
#include <math.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define __UTEST_FAIL_TEST_EXIT() \
longjmp(g_utest_state.restore_env, 1); \
return;
// Global state
static utest_state g_utest_state;
static utest_cfg g_utest_cfg;
void utest_init()
{
memset(&g_utest_state, 0, sizeof(g_utest_state));
g_utest_cfg.result_func = &utest_test_result_func;
g_utest_cfg.print_func = &utest_test_print_func;
}
void utest_print(const char* message)
{
if (g_utest_cfg.print_func)
{
g_utest_cfg.print_func(g_utest_state.current_test, message);
}
}
utest_test_result utest_run_fixture(utest_fixture* fixture)
{
utest_entry* test = fixture->tests;
g_utest_state.run_count = 0;
g_utest_state.pass_count = 0;
g_utest_state.fail_count = 0;
g_utest_state.current_fixture = fixture;
if (fixture->f_setup)
{
(*fixture->f_setup)();
}
while (test->test)
{
g_utest_state.current_test = test;
g_utest_state.run_count++;
if (fixture->t_setup)
{
(*fixture->t_setup)();
}
utest_test_result result = utest_run_test(test++);
if (fixture->t_teardown)
{
(*fixture->t_teardown)();
}
if (!result)
{
g_utest_state.pass_count++;
g_utest_cfg.result_func(fixture, g_utest_state.current_test, result, 0);
}
else
{
g_utest_state.fail_count++;
g_utest_cfg.result_func(fixture, g_utest_state.current_test, result, g_utest_state.msgbuffer);
}
}
if (fixture->f_teardown)
{
(*fixture->f_teardown)();
}
return g_utest_state.fail_count ? TEST_RESULT_FAILED : TEST_RESULT_SUCCESS;
}
utest_test_result utest_run_test(utest_entry* test)
{
// Grab current environment
int result = setjmp(g_utest_state.restore_env);
g_utest_state.current_test = test;
if (result == 0)
{
// Run the test
(*test->test)();
return TEST_RESULT_SUCCESS;
}
return TEST_RESULT_FAILED;
}
//////////////////////////
// Assert implementations
//
void utest_assert_integer_equal(signed expected, signed actual, const char* message)
{
if (expected == actual)
{
return;
}
if (message)
{
utest_fail("Values not equal. Expected [%i], Actual [%i] - %s", expected, actual, message);
}
else
{
utest_fail("Values not equal. Expected [%i], Actual [%i]", expected, actual);
}
}
void utest_assert_null(void* actual, char expect_null, const char* message)
{
if (expect_null)
{
if(!actual)
{
return;
}
if (message)
{
utest_fail("Value non-null. Expected null. %s", message);
}
else
{
utest_fail("Value non-null. Expected null.");
}
}
// otherwise we're expecting null
if(actual)
{
return;
}
if (message)
{
utest_fail("Value null. Expected non-null. %s", message);
}
else
{
utest_fail("Value null. Expected non-null.");
}
}
void utest_assert_float_equal(double expected, double actual, double epsilon, const char* message)
{
if (fabs(expected - actual) < epsilon)
{
return;
}
if (message)
{
utest_fail("Values not equal. Expected [%lf], Actual [%lf] - %s", expected, actual, message);
}
else
{
utest_fail("Values not equal. Expected [%lf], Actual [%lf]", expected, actual);
}
}
void utest_assert_string_null(const char* actual, const char* message)
{
if (!actual)
{
return;
}
if (message)
{
utest_fail("String not null. Actual [%s] - %s", actual, message);
}
else
{
utest_fail("String not null. Actual [%s]", actual);
}
}
void utest_assert_string_equal(const char* expected, const char* actual, const char* message, char ignore_case)
{
if (ignore_case)
{
if (_stricmp(expected, actual) == 0)
{
return;
}
}
else
{
if (strcmp(expected, actual) == 0)
{
return;
}
}
if (message)
{
utest_fail("Strings not equal. Expected [%s], Actual [%s] - %s", expected, actual, message);
}
else
{
utest_fail("Strings not equal. Expected [%s], Actual [%s]", expected, actual);
}
}
void utest_assert_pointer_equal(void* expected, void* actual, const char* message)
{
if (expected == actual)
{
return;
}
if (message)
{
utest_fail("Pointers not equal. Expected [0x%016x], Actual [0x%016x] - %s", expected, actual, message);
}
else
{
utest_fail("Pointers not equal. Expected [0x%016x], Actual [0x%016x]", expected, actual);
}
}
void utest_fail(const char* format, ...)
{
va_list args;
va_start(args, format);
#if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS)
vsnprintf_s(g_utest_state.msgbuffer, UTEST_MSG_BUFFER_SIZE, UTEST_MSG_BUFFER_SIZE, format, args);
#else
vsprintf(g_utest_state.msgbuffer, format, args);
#endif
va_end(args);
__UTEST_FAIL_TEST_EXIT();
}
void utest_test_result_func(const utest_fixture* fixture, const utest_entry* test, utest_test_result result, const char* message)
{
if (result == TEST_RESULT_FAILED)
{
printf("%s(%i): Test failed: '%s': %s\r\n",
test->file, test->line,
test->name, message);
}
}
void utest_test_print_func(const utest_entry* test, const char* message)
{
printf("%s(%i): '%s': %s\r\n",
test->file, test->line,
test->name, message);
}
void* utest_get_user()
{
return g_utest_state.user_state;
}
void utest_set_user(void* state)
{
g_utest_state.user_state = state;
}
void utest_set_result_func(utest_result_func func)
{
g_utest_cfg.result_func = func;
}
void utest_set_print_func(utest_print_func func)
{
g_utest_cfg.print_func = func;
}
void utest_reset_result_func()
{
g_utest_cfg.result_func = utest_test_result_func;
}
void utest_reset_print_func()
{
g_utest_cfg.print_func = utest_test_print_func;
}
const char* utest_last_msg()
{
return g_utest_state.msgbuffer;
}
#endif // UTEST_C_IMPLEMENTATION