-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathtinytest.h
More file actions
219 lines (185 loc) · 10.2 KB
/
Copy pathtinytest.h
File metadata and controls
219 lines (185 loc) · 10.2 KB
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
/*!
Simple unit testing for c/c++
Copyright Cosmin Cremarenco.
Licence: Apache 2.0
Purpose: facilitate the unit testing for programs written in c/c++
Use:
Define your tests as functions that don't take any arguments
but return "int". Then add them to a test suite. Finally,
if all you want to run are the tests inside the same .c/.cpp translation
unit add call to TINYTEST_MAIN_SINGLE_SUITE which takes as argument
the name of the test suite to run.
Otherwise, if you wish to gather test suites across translation units
you need to declare your test suites using TINYTEST_DECLARE_SUITE
and include the .h with the declarations inside a .c/.cpp file
which will call TINYTEST_START_MAIN/TINYTEST_END_MAIN in which
you will put all test suites.
Please see examples for more details.
Tests are meant to be in the same place as the tested code.
Declare TINYTEST_NOTEST if you don't want to compile the test code.
Usually you will declare TINYTEST_NOTEST together with NDEBUG.
*/
#ifndef __TINYTEST_H__
#define __TINYTEST_H__
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
typedef int (*TinyTestFunc)(void);
typedef struct TinyTestStruct
{
TinyTestFunc m_func;
const char* m_name;
struct TinyTestStruct* m_next;
} TinyTest;
typedef struct TinyTestSuiteStruct
{
struct TinyTestStruct* m_head;
const char* m_name;
struct TinyTestStruct* m_headTest;
struct TinyTestSuiteStruct* m_next;
} TinyTestSuite;
typedef struct TinyTestRegistryStruct
{
TinyTestSuite* m_headSuite;
} TinyTestRegistry;
#ifndef TINYTEST_NOTESTING
#define EPSILON 0.0000001
#define TINYTEST_EQUAL_EPSILON_MSG(expected, actual, msg) \
if ( fabs((expected) - (actual)) > EPSILON) \
{ \
printf("%s:%d expected %s = %f, actual: %s = %f\n", \
__FILE__, __LINE__, #expected, expected, #actual, actual); \
if ( msg ) printf(msg); \
return 0; \
}
#define TINYTEST_EQUAL_EPSILON(expected, actual) \
TINYTEST_EQUAL_EPSILON_MSG(expected, actual, " ")
#define TINYTEST_EQUAL_MSG(expected, actual, msg) \
if ( (expected) != (actual) ) \
{ \
printf("%s:%d expected %s, actual: %s\n", \
__FILE__, __LINE__, #expected, #actual); \
if ( msg ) printf(msg); \
return 0; \
}
#define TINYTEST_EQUAL(expected, actual) \
TINYTEST_EQUAL_MSG(expected, actual, " ")
#define TINYTEST_STR_EQUAL_MSG(expected, actual, msg) \
if ( strcmp((expected), (actual)) ) \
{ \
printf("%s:%d expected \"%s\", actual: \"%s\"\n", \
__FILE__, __LINE__, expected, actual); \
if ( msg ) printf(msg); \
return 0; \
}
#define TINYTEST_STR_EQUAL(expected, actual) \
TINYTEST_STR_EQUAL_MSG(expected, actual, " ")
#define TINYTEST_ASSERT_MSG(assertion, msg) \
if ( !(assertion) ) \
{ \
printf("%s:%d assertion failed: \"%s\"\n", \
__FILE__, __LINE__, #assertion); \
if ( msg ) printf(msg); \
return 0; \
}
#define TINYTEST_ASSERT(assertion) \
TINYTEST_ASSERT_MSG(assertion, " ")
#define TINYTEST_DECLARE_SUITE(suiteName) \
void Suite##suiteName(TinyTestRegistry* registry)
#define TINYTEST_START_SUITE(suiteName) \
void Suite##suiteName(TinyTestRegistry* registry) \
{ \
TinyTestSuite* suite = (TinyTestSuite*)malloc(sizeof(TinyTestSuite)); \
suite->m_name = #suiteName; \
suite->m_headTest = NULL; \
suite->m_next = NULL
#define TINYTEST_ADD_TEST(test) \
TinyTest* test##decl = (TinyTest*)malloc(sizeof(TinyTest)); \
test##decl->m_func = test; \
test##decl->m_name = #test; \
test##decl->m_next = suite->m_headTest; \
suite->m_headTest = test##decl
#define TINYTEST_END_SUITE() \
suite->m_next = registry->m_headSuite; \
registry->m_headSuite = suite; \
}
#define TINYTEST_START_MAIN() \
int main(int argc, char* argv[]) \
{ \
TinyTestRegistry registry; \
registry.m_headSuite = NULL
#define TINYTEST_RUN_SUITE(suiteName) \
Suite##suiteName(®istry)
#define TINYTEST_INTERNAL_FREE_TESTS() \
{ \
TinyTestSuite* s = registry.m_headSuite; \
TinyTestSuite* ss = NULL; \
for ( ; s; s = ss ) \
{ \
ss = s->m_next; \
TinyTest* t = s->m_headTest; \
TinyTest* tt = NULL; \
for ( ; t; t = tt ) \
{ \
tt = t->m_next; \
free(t); \
} \
free(s); \
} \
}
#define TINYTEST_INTERNAL_RUN_TESTS() \
{ \
int okTests = 0; \
int failedTests = 0; \
TinyTestSuite* s = registry.m_headSuite; \
for ( ; s; s = s->m_next ) \
{ \
TinyTest* t = s->m_headTest; \
for ( ; t; t = t->m_next ) \
{ \
if ( (*t->m_func)() ) \
{ \
printf("."); \
++okTests; \
} \
else \
{ \
printf("x"); \
++failedTests; \
} \
} \
} \
printf("\nOK: %d", okTests); \
if ( failedTests ) \
{ \
printf(" FAILED: %d", failedTests); \
} \
printf("\n"); \
}
#define TINYTEST_END_MAIN() \
TINYTEST_INTERNAL_RUN_TESTS(); \
printf("\n"); \
TINYTEST_INTERNAL_FREE_TESTS() \
}
#define TINYTEST_MAIN_SINGLE_SUITE(suiteName) \
TINYTEST_START_MAIN(); \
TINYTEST_RUN_SUITE(suiteName); \
TINYTEST_END_MAIN();
#else // TINYTEST_NOTESTING
#define TINYTEST_EQUAL_MSG(expected, actual, msg) (void)0
#define TINYTEST_EQUAL(expected, actual) (void)0
#define TINYTEST_STR_EQUAL_MSG(expected, actual, msg) (void)0
#define TINYTEST_STR_EQUAL(expected, actual) (void)0
#define TINYTEST_ASSERT_MSG(assertion, msg) (void)0
#define TINYTEST_ASSERT(assertion) (void)0
#define TINYTEST_START_SUITE(suiteName)
#define TINYTEST_ADD_TEST(test)
#define TINYTEST_END_SUITE()
#define TINYTEST_START_MAIN()
#define TINYTEST_RUN_SUITE(suiteName)
#define TINYTEST_INTERNAL_FREE_TESTS()
#define TINYTEST_INTERNAL_RUN_TESTS()
#define TINYTEST_END_MAIN()
#define TINYTEST_MAIN_SINGLE_SUITE(suiteName)
#endif // TINYTEST_NOTESTING
#endif