-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathctest.h
157 lines (140 loc) · 7.76 KB
/
ctest.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
#pragma once
// This file defines macros for a simple test framework.
#ifndef CTEST_FAILED_ASSERTION_ABORTS
#define CTEST_FAILED_ASSERTION_ABORTS false
#endif
// If an assertion fails, return from the test function. In practice, we sill
// run the code, but we skip the assertions.
#ifndef CTEST_FAILED_ASSERTION_SKIPS
#define CTEST_FAILED_ASSERTION_SKIPS true
#endif
#ifndef CTEST_VERBOSE_EXPECT
#define CTEST_VERBOSE_EXPECT false
#endif
#ifdef CI_TEST
#define BOLD ""
#define RESET ""
#define RED ""
#define GREEN ""
#define BOLD_RED ""
#define BOLD_GREEN ""
#define BOLD_YELLOW ""
#define BOLD_UNDERLINE ""
#else
#define BOLD "\033[1m"
#define RESET "\033[0m"
#define RED "\033[31m"
#define GREEN "\033[32m"
#define BOLD_RED "\033[1;31m"
#define BOLD_GREEN "\033[1;32m"
#define BOLD_YELLOW "\033[1;33m"
#define BOLD_UNDERLINE "\033[1;4m"
#endif
#define RESULT_FAIL 1
// Test framework ==============================================================
static int _ctest_result = 0;
static int assertions = 0;
static int failed_assertions = 0;
static int function_assertions = 0;
static int function_skipped_assertions = 0;
static int function_failed_assertions = 0;
static bool function_skip = false;
#define begin_test(func) \
do { \
function_assertions = 0; \
function_skipped_assertions = 0; \
function_failed_assertions = 0; \
function_skip = false; \
if (!CTEST_VERBOSE_EXPECT) { \
fprintf(stdout, "Test: " BOLD_UNDERLINE "%s" RESET, #func); \
} else { \
fprintf(stdout, "Test: " BOLD_UNDERLINE "%s\n" RESET, #func); \
} \
func(); \
} while (0)
#define end_test() \
do { \
if (CTEST_VERBOSE_EXPECT) { \
fprintf(stdout, BOLD_UNDERLINE "%s " RESET "completed", __func__); \
} \
fprintf(stdout, \
" %s" RESET " (%d total assertions, %d failed assertions)\n" RESET, \
function_skip ? BOLD_YELLOW "SKIP" \
: function_failed_assertions == 0 ? BOLD_GREEN "OK" \
: BOLD_RED "FAIL", \
function_assertions, function_failed_assertions); \
} while (0)
// We don't need to do anything here, but we need to define these macros to
// future-proof the framework.
#define begin_suite() \
do { \
} while (0)
#define end_suite() \
({ \
fprintf(stdout, "\nTest suite completed %s\n" RESET, \
_ctest_result == 0 ? BOLD_GREEN "successfully" \
: BOLD_RED "with failures"); \
fprintf(stdout, "Total assertions: %d\n", assertions); \
fprintf(stdout, "Failed assertions: %d\n", failed_assertions); \
_ctest_result; \
})
/**
* @brief Assert that a condition is true. Since expect calls may be skipped,
* it is recommended that your tests don't rely on conditions being evaluated.
*
* @example
*
* // This is fine.
* autor result = my_function();
* expect(result == 0);
*
* // This is might cause issues!
* expect(my_function() == 0);
*/
#define expect(cond) \
do { \
function_assertions++; \
if ((CTEST_FAILED_ASSERTION_SKIPS && function_failed_assertions > 0) \
|| function_skip) { \
function_skipped_assertions++; \
fprintf(stderr, \
BOLD_YELLOW "[ SKIP ] " RESET "Assertion skipped (" __FILE__ \
":%d %s): " #cond "\n", \
__LINE__, __func__); \
break; \
} \
if (!(cond)) { \
fprintf(stderr, \
BOLD_RED "[ FAIL ] " RESET "Assertion failed (" __FILE__ \
":%d %s): " #cond "\n", \
__LINE__, __func__); \
failed_assertions++; \
function_failed_assertions++; \
_ctest_result = RESULT_FAIL; \
if (CTEST_FAILED_ASSERTION_ABORTS) { \
abort(); \
} \
} else { \
if (CTEST_VERBOSE_EXPECT) { \
fprintf( \
stdout, BOLD_GREEN "[ OK ]" RESET " %s " #cond "\n", __func__); \
} \
} \
assertions++; \
} while (0)
#define test_skip() \
do { \
if (function_assertions > 0) { \
fprintf(stderr, \
BOLD_YELLOW "Warning: " RESET \
"You are skipping a test after calling expect() in it. " \
"This is likely a mistake.\n"); \
} \
function_skip = true; \
} while (0)
#define test(func) \
do { \
begin_test(func); \
end_test(); \
} while (0)
#define bool_to_str(b) ((b) ? "true" : "false")