-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclarify.h
213 lines (197 loc) · 8.78 KB
/
clarify.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
/*
Clarify is a BDD-style unit testing framework for C.
*/
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
/*
Require that the expression evaluates to true.
*/
#define REQUIRE(boolean_test) \
do { \
if (!(boolean_test)) { \
test_count_failed++; \
printf("FAIL: %s:%d\n", \
__FILE__, __LINE__); \
printf(" Given: %s\n", this_test.given); \
printf(" When: %s\n", this_test.when); \
printf(" Then: %s\n", this_test.then); \
printf("REQUIRE( %s )\n", #boolean_test); \
printf("\n"); \
return; \
} \
} while (0)
/*
Require that the actual value matches the expected value. If there is an
error, the provided printf-style format_string is used to print the value.
*/
#if 0
#define REQUIRE_EQUAL_FORMAT(expected, actual, format_string) \
do {\
if (!((expected) == (actual))) { \
test_count_failed++; \
printf("---------------------------------------------------\n"); \
printf("Test at %s:%d FAILED\n", \
__FILE__, this_test.starting_line_number); \
printf("---------------------------------------------------\n"); \
printf(" Given: %s\n", this_test.given); \
printf(" When: %s\n", this_test.when); \
printf(" Then: %s\n", this_test.then); \
printf("---------------------------------------------------\n"); \
printf("FAILED at %s:%d:\n", \
__FILE__, __LINE__); \
printf(" Expected: " #format_string "\n", expected); \
printf(" Actual : " #format_string "\n ", actual); \
printf("---------------------------------------------------\n"); \
printf("\n"); \
return; \
} \
} while (0)
#endif
#define REQUIRE_EQUAL_FORMAT(expected, actual, format_string) \
do { \
if (!((expected) == (actual))) { \
test_count_failed++; \
printf("FAIL: %s:%d \n", \
__FILE__, __LINE__); \
printf(" Given: %s\n", this_test.given); \
printf(" When: %s\n", this_test.when); \
printf(" Then: %s\n", this_test.then); \
printf("Expected: " #format_string "\n", expected); \
printf("Actual : " #format_string "\n ", actual); \
printf("\n"); \
return; \
} \
} while (0)
#define REQUIRE_EQUAL_INT(expected, actual) REQUIRE_EQUAL_FORMAT(expected, actual, %d)
#define REQUIRE_EQUAL_UINT(expected, actual) REQUIRE_EQUAL_FORMAT(expected, actual, %u)
#define REQUIRE_EQUAL_BYTE(expected, actual) REQUIRE_EQUAL_FORMAT(expected, actual, 0x%02x)
#define REQUIRE_EQUAL_STRING(expected, actual) \
do { \
if (strcmp(expected, actual) != 0) { \
test_count_failed++; \
printf("FAIL: %s:%d \n", \
__FILE__, __LINE__); \
printf(" Given: %s\n", this_test.given); \
printf(" When: %s\n", this_test.when); \
printf(" Then: %s\n", this_test.then); \
printf("Expected: %s\n", expected); \
printf("Actual : %s\n ", actual); \
printf("\n"); \
return; \
} \
} while (0)
#define REQUIRE_EQUAL_MEMORY(expected_ptr, actual_ptr, size) \
do { \
if (memcmp(expected_ptr, actual_ptr, size) != 0) { \
test_count_failed++; \
printf("FAIL: %s:%d \n", \
__FILE__, __LINE__); \
printf(" Given: %s\n", this_test.given); \
printf(" When: %s\n", this_test.when); \
printf(" Then: %s\n", this_test.then); \
printf("\n"); \
return; \
} \
} while (0)
/*
State maintined by clarify during the execution of tests.
*/
typedef struct {
char * given; // The strings describing each
char * when;
char * then;
int starting_line_number; // The starting line number of this
// test (where the GIVEN statement is).
bool test_executed_this_pass; // True when a full Given-When-Then test has
// been run on this pass through the function.
bool skip_this_clause; // Temp flag used to skip a clause.
int current_when_line; // The starting line of the WHEN clause
// currently being executed.
int last_then_line_executed; // The starting line of the most recent THEN
// clause to be executed.
} TEST_CASE;
// Create a unique test function name for each test function, based on the line
// number. We need two levels of indirection to get the line number to be
// evaluated during the string concatentation.
// http://stackoverflow.com/questions/1597007/creating-c-macro-with-and-line-token-concatenation-with-positioning-macr/1597129
#define TOKENPASTE(x, y) x ## y
#define TOKENPASTE2(x, y) TOKENPASTE(x, y)
#define UNIQUE_TEST_FUNCTION_NAME TOKENPASTE2(test_, __LINE__)
#define PRINT_HEADER() \
printf("---------------------------------------------------\n"); \
printf("%s: Running Tests...\n", __FILE__); \
printf("---------------------------------------------------\n"); \
#define PRINT_RESULTS() \
printf("\n"); \
printf("---------------------------------------------------\n"); \
printf("%s: Test Results\n", __FILE__); \
printf("---------------------------------------------------\n"); \
printf("Tested: %d\n", test_count_total); \
printf("Passed: %d\n", test_count_total - test_count_failed); \
printf("Failed: %d\n", test_count_failed);
#define RUN_TESTS() \
TEST_CASE this_test; \
int test_count_total = 0; \
int test_count_failed = 0; \
void all_tests(void); \
int main(void) {\
PRINT_HEADER(); \
all_tests(); \
PRINT_RESULTS(); \
return test_count_failed; \
}\
void all_tests(void)
#define GIVEN(precondition) \
auto void UNIQUE_TEST_FUNCTION_NAME (void); \
this_test.starting_line_number = __LINE__; \
this_test.given = precondition; \
this_test.last_then_line_executed = 0; \
this_test.current_when_line = 0; \
do { \
this_test.test_executed_this_pass = false; \
UNIQUE_TEST_FUNCTION_NAME(); \
} while (this_test.test_executed_this_pass); \
void UNIQUE_TEST_FUNCTION_NAME (void)
#define WHEN(condition) \
this_test.skip_this_clause = false; \
if (this_test.test_executed_this_pass) \
{ \
/* We just completed the last WHEN, switch to this when.*/ \
this_test.current_when_line = __LINE__; \
return; \
} \
if ((this_test.current_when_line == 0) || (this_test.current_when_line == __LINE__)) \
{ \
/* This is current when clause, so we're going to run it. */ \
this_test.when = condition; \
} \
else \
{ \
/* This is not the current WHEN clause... so don't run it. */ \
this_test.skip_this_clause = true; \
} \
if (!this_test.skip_this_clause)
#define THEN(result) \
this_test.skip_this_clause = false; \
if (this_test.test_executed_this_pass) \
{ \
return; \
} \
else if (this_test.last_then_line_executed >= __LINE__) \
{ \
/*We've already run this test, skip it. */ \
this_test.skip_this_clause = true; \
} \
else \
{ \
/* We are executing this THEN clause this pass. */ \
this_test.last_then_line_executed = __LINE__; \
this_test.test_executed_this_pass = true; \
} \
if (!this_test.skip_this_clause) \
{ \
this_test.then = result; \
test_count_total++; \
} \
if (!this_test.skip_this_clause)