1 | // |
---|
2 | // Lol Engine |
---|
3 | // |
---|
4 | // Copyright: (c) 2010-2011 Sam Hocevar <sam@hocevar.net> |
---|
5 | // This program is free software; you can redistribute it and/or |
---|
6 | // modify it under the terms of the Do What The Fuck You Want To |
---|
7 | // Public License, Version 2, as published by Sam Hocevar. See |
---|
8 | // http://sam.zoy.org/projects/COPYING.WTFPL for more details. |
---|
9 | // |
---|
10 | |
---|
11 | // |
---|
12 | // The Unit test framework |
---|
13 | // ----------------------- |
---|
14 | // |
---|
15 | |
---|
16 | #if !defined __LOL_UNIT_H__ |
---|
17 | #define __LOL_UNIT_H__ |
---|
18 | |
---|
19 | #include <iostream> |
---|
20 | #include <iomanip> |
---|
21 | #include <sstream> |
---|
22 | #include <cstdio> |
---|
23 | #include <cmath> |
---|
24 | |
---|
25 | namespace lol |
---|
26 | { |
---|
27 | |
---|
28 | using namespace std; |
---|
29 | |
---|
30 | /* |
---|
31 | * This is the base class for all fixtures. It keeps track of all |
---|
32 | * fixtures registered through the LOLUNIT_FIXTURE macro and puts them |
---|
33 | * in a linked list. |
---|
34 | */ |
---|
35 | class FixtureBase |
---|
36 | { |
---|
37 | friend class TextTestRunner; |
---|
38 | |
---|
39 | public: |
---|
40 | virtual void SetUp(void) {}; |
---|
41 | virtual void TearDown(void) {}; |
---|
42 | |
---|
43 | protected: |
---|
44 | FixtureBase() : m_next(NULL), m_testcases(0), m_failcases(0) {} |
---|
45 | virtual ~FixtureBase() {} |
---|
46 | |
---|
47 | static void AddFixture(FixtureBase *fixture) |
---|
48 | { |
---|
49 | /* Protect against several instances of the same Fixture subclass */ |
---|
50 | if (fixture->MarkFixture()) |
---|
51 | return; |
---|
52 | FixtureListHelper(fixture); |
---|
53 | } |
---|
54 | static FixtureBase *FixtureList() { return FixtureListHelper(NULL); } |
---|
55 | |
---|
56 | virtual void RunFixture() = 0; |
---|
57 | virtual bool MarkFixture() = 0; |
---|
58 | |
---|
59 | /* Prevent compiler complaints about unreachable code */ |
---|
60 | static inline bool True() { return true; } |
---|
61 | |
---|
62 | FixtureBase *m_next; |
---|
63 | int m_testcases, m_failcases; |
---|
64 | int m_asserts, m_failure; |
---|
65 | char const *m_fixturename, *m_currentname; |
---|
66 | std::stringstream m_errorlog, m_context; |
---|
67 | |
---|
68 | private: |
---|
69 | /* The FixtureBase class keeps track of all instanciated children |
---|
70 | * fixtures through this method. */ |
---|
71 | static FixtureBase *FixtureListHelper(FixtureBase *set) |
---|
72 | { |
---|
73 | static FixtureBase *head = NULL, *tail = NULL; |
---|
74 | |
---|
75 | if (set) |
---|
76 | { |
---|
77 | if (!head) head = set; |
---|
78 | if (tail) tail->m_next = set; |
---|
79 | tail = set; |
---|
80 | } |
---|
81 | return head; |
---|
82 | } |
---|
83 | }; |
---|
84 | |
---|
85 | /* |
---|
86 | * This template specialises FixtureBase and provides registration of |
---|
87 | * test cases in a linked list through the LOLUNIT_TEST macro. |
---|
88 | */ |
---|
89 | template<class T> class Fixture : protected FixtureBase |
---|
90 | { |
---|
91 | public: |
---|
92 | typedef T FixtureClass; |
---|
93 | |
---|
94 | struct TestCase |
---|
95 | { |
---|
96 | void (FixtureClass::* m_fun)(); |
---|
97 | char const *m_testname; |
---|
98 | TestCase *m_next; |
---|
99 | }; |
---|
100 | |
---|
101 | Fixture<T>() |
---|
102 | { |
---|
103 | AddFixture(this); |
---|
104 | } |
---|
105 | |
---|
106 | /* Run all test cases in this fixture. */ |
---|
107 | virtual void RunFixture() |
---|
108 | { |
---|
109 | m_errorlog.str(""); |
---|
110 | m_testcases = 0; |
---|
111 | m_failcases = 0; |
---|
112 | for (TestCase *c = TestCaseList(); c; c = c->m_next) |
---|
113 | { |
---|
114 | m_testcases++; |
---|
115 | m_asserts = 0; |
---|
116 | m_failure = false; |
---|
117 | m_currentname = c->m_testname; |
---|
118 | m_context.str(""); |
---|
119 | |
---|
120 | std::cout << '.'; |
---|
121 | (static_cast<FixtureClass *>(this)->*c->m_fun)(); |
---|
122 | if (m_failure) std::cout << 'F'; |
---|
123 | } |
---|
124 | } |
---|
125 | |
---|
126 | /* Mark the current fixture type as already registered and return whether |
---|
127 | * it was seen before. */ |
---|
128 | virtual bool MarkFixture() |
---|
129 | { |
---|
130 | static bool seen = false; |
---|
131 | if (seen) |
---|
132 | { |
---|
133 | SealFixture(); |
---|
134 | return true; |
---|
135 | } |
---|
136 | seen = true; |
---|
137 | return false; |
---|
138 | } |
---|
139 | |
---|
140 | /* Manage Fixture sealing. Once SealFixture() has been called, we |
---|
141 | * will no longer accept TestCase registrations. */ |
---|
142 | static void SealFixture() { SealFixtureHelper(true); } |
---|
143 | static bool IsFixtureSealed() { return SealFixtureHelper(false); } |
---|
144 | |
---|
145 | /* Each Fixture class specialisation keeps track of its instanciated |
---|
146 | * test cases. */ |
---|
147 | static void AddTestCase(TestCase *that, char const *name, |
---|
148 | void (FixtureClass::*fun)()) |
---|
149 | { |
---|
150 | if (IsFixtureSealed()) |
---|
151 | return; |
---|
152 | |
---|
153 | that->m_fun = fun; |
---|
154 | that->m_testname = name; |
---|
155 | that->m_next = NULL; |
---|
156 | TestCaseListHelper(that); |
---|
157 | } |
---|
158 | static TestCase *TestCaseList() { return TestCaseListHelper(NULL); } |
---|
159 | |
---|
160 | private: |
---|
161 | static bool SealFixtureHelper(bool set) |
---|
162 | { |
---|
163 | static bool sealed = false; |
---|
164 | if (set) sealed = true; |
---|
165 | return sealed; |
---|
166 | } |
---|
167 | |
---|
168 | static TestCase *TestCaseListHelper(TestCase *set) |
---|
169 | { |
---|
170 | static TestCase *head = NULL, *tail = NULL; |
---|
171 | if (set) |
---|
172 | { |
---|
173 | if (!head) head = set; |
---|
174 | if (tail) tail->m_next = set; |
---|
175 | tail = set; |
---|
176 | } |
---|
177 | return head; |
---|
178 | } |
---|
179 | }; |
---|
180 | |
---|
181 | /* |
---|
182 | * This simple class runs all automatically registered tests and reports |
---|
183 | * on error and success in the standard output. |
---|
184 | */ |
---|
185 | class TextTestRunner |
---|
186 | { |
---|
187 | public: |
---|
188 | bool Run() |
---|
189 | { |
---|
190 | bool ret = true; |
---|
191 | std::stringstream errors(""); |
---|
192 | int failcases = 0, testcases = 0; |
---|
193 | |
---|
194 | for (FixtureBase *f = FixtureBase::FixtureList(); f; f = f->m_next) |
---|
195 | { |
---|
196 | f->SetUp(); |
---|
197 | f->RunFixture(); |
---|
198 | f->TearDown(); |
---|
199 | |
---|
200 | errors << f->m_errorlog.str(); |
---|
201 | testcases += f->m_testcases; |
---|
202 | failcases += f->m_failcases; |
---|
203 | } |
---|
204 | std::cout << std::endl; |
---|
205 | |
---|
206 | std::cout << std::endl << std::endl; |
---|
207 | if (failcases) |
---|
208 | { |
---|
209 | std::cout << "!!!FAILURES!!!" << std::endl; |
---|
210 | std::cout << "Test Results:" << std::endl; |
---|
211 | std::cout << "Run: " << testcases |
---|
212 | << " Failures: " << failcases |
---|
213 | << " Errors: 0" << std::endl; /* TODO: handle errors */ |
---|
214 | |
---|
215 | std::cout << errors.str(); |
---|
216 | ret = false; |
---|
217 | } |
---|
218 | else |
---|
219 | { |
---|
220 | std::cout << "OK (" << testcases << " tests)" << std::endl; |
---|
221 | } |
---|
222 | std::cout << std::endl << std::endl; |
---|
223 | |
---|
224 | return ret; |
---|
225 | } |
---|
226 | }; |
---|
227 | |
---|
228 | #define LOLUNIT_ASSERT_GENERIC(msg, cond) \ |
---|
229 | do { \ |
---|
230 | m_asserts++; \ |
---|
231 | if (True() && !(cond)) \ |
---|
232 | { \ |
---|
233 | m_errorlog << std::endl << std::endl; \ |
---|
234 | m_errorlog << ++m_failcases << ") test: " \ |
---|
235 | << lol_unit_helper_name(this) << "::" << m_currentname \ |
---|
236 | << " (F) line: " << __LINE__ << " " \ |
---|
237 | << __FILE__ << std::endl; \ |
---|
238 | m_errorlog << "assertion failed" << std::endl; \ |
---|
239 | m_errorlog << "- Expression: " << #cond << std::endl; \ |
---|
240 | m_errorlog << msg; \ |
---|
241 | m_failure = true; \ |
---|
242 | return; \ |
---|
243 | } \ |
---|
244 | } while (!True()) |
---|
245 | |
---|
246 | #define LOLUNIT_ASSERT_OP(op, modifier, opdesc, msg, a, b) \ |
---|
247 | do { \ |
---|
248 | m_asserts++; \ |
---|
249 | if (True() && !modifier((a) op (b))) \ |
---|
250 | { \ |
---|
251 | m_errorlog << std::endl << std::endl; \ |
---|
252 | m_errorlog << ++m_failcases << ") test: " \ |
---|
253 | << lol_unit_helper_name(this) << "::" << m_currentname \ |
---|
254 | << " (F) line: " << __LINE__ << " " \ |
---|
255 | << __FILE__ << std::endl; \ |
---|
256 | m_errorlog << opdesc << " assertion failed" << std::endl; \ |
---|
257 | m_errorlog << "- Expected: " << #a << " = " << (a) << std::endl; \ |
---|
258 | m_errorlog << "- Actual : " << #b << " = " << (b) << std::endl; \ |
---|
259 | m_errorlog << msg; \ |
---|
260 | m_errorlog << m_context.str(); \ |
---|
261 | m_failure = true; \ |
---|
262 | return; \ |
---|
263 | } \ |
---|
264 | } while (!True()) |
---|
265 | |
---|
266 | #define LOLUNIT_MSG(msg) \ |
---|
267 | "- " << msg << std::endl |
---|
268 | |
---|
269 | #define LOLUNIT_ASSERT_DOUBLES_EQUAL_GENERIC(msg, a, b, t) \ |
---|
270 | do { \ |
---|
271 | m_asserts++; \ |
---|
272 | if (True() && fabs((a) - (b)) > fabs((t))) \ |
---|
273 | { \ |
---|
274 | m_errorlog << std::endl << std::endl; \ |
---|
275 | m_errorlog << ++m_failcases << ") test: " \ |
---|
276 | << lol_unit_helper_name(this) << "::" << m_currentname \ |
---|
277 | << " (F) line: " << __LINE__ << " " \ |
---|
278 | << __FILE__ << std::endl; \ |
---|
279 | m_errorlog << "double equality assertion failed" << std::endl; \ |
---|
280 | std::streamsize old_prec = m_errorlog.precision(); \ |
---|
281 | m_errorlog << std::setprecision(16); \ |
---|
282 | m_errorlog << "- Expected: " << #a << " = " << (a) << std::endl; \ |
---|
283 | m_errorlog << "- Actual : " << #b << " = " << (b) << std::endl; \ |
---|
284 | m_errorlog << "- Delta : " << (t) << std::endl; \ |
---|
285 | m_errorlog << std::setprecision(old_prec); \ |
---|
286 | m_errorlog << msg; \ |
---|
287 | m_errorlog << m_context.str(); \ |
---|
288 | m_failure = true; \ |
---|
289 | return; \ |
---|
290 | } \ |
---|
291 | } while (!True()) |
---|
292 | |
---|
293 | /* |
---|
294 | * Public helper macros |
---|
295 | */ |
---|
296 | |
---|
297 | #define LOLUNIT_FIXTURE(N) \ |
---|
298 | class N; \ |
---|
299 | /* This pattern allows us to statically create a Fixture instance \ |
---|
300 | * before its exact implementation was defined. */ \ |
---|
301 | template<typename T> struct lol_unit_helper_fixture_##N \ |
---|
302 | { \ |
---|
303 | lol_unit_helper_fixture_##N() { p = new T(); } \ |
---|
304 | ~lol_unit_helper_fixture_##N() { delete p; } \ |
---|
305 | T *p; \ |
---|
306 | }; \ |
---|
307 | lol_unit_helper_fixture_##N<N> lol_unit_helper_fixture_##N##_instance; \ |
---|
308 | /* Allow to retrieve the class name without using RTTI and without \ |
---|
309 | * knowing the type of "this". */ \ |
---|
310 | static inline char const *lol_unit_helper_name(N *p) \ |
---|
311 | { \ |
---|
312 | (void)p; \ |
---|
313 | return #N; \ |
---|
314 | } \ |
---|
315 | /* Now the user can define the implementation */ \ |
---|
316 | class N : public lol::Fixture<N> |
---|
317 | |
---|
318 | #define LOLUNIT_TEST(N) \ |
---|
319 | /* For each test in the fixture, we create an object that will \ |
---|
320 | * automatically register the test method in a list global to the \ |
---|
321 | * specialised fixture. */ \ |
---|
322 | class lol_unit_helper_test_##N : public TestCase \ |
---|
323 | { \ |
---|
324 | public: \ |
---|
325 | lol_unit_helper_test_##N() \ |
---|
326 | { \ |
---|
327 | AddTestCase(this, #N, \ |
---|
328 | (void (FixtureClass::*)()) &FixtureClass::N); \ |
---|
329 | } \ |
---|
330 | }; \ |
---|
331 | lol_unit_helper_test_##N lol_unit_helper_test_instance_##N; \ |
---|
332 | void N() |
---|
333 | |
---|
334 | /* |
---|
335 | * Provide context for error messages |
---|
336 | */ |
---|
337 | |
---|
338 | #define LOLUNIT_SET_CONTEXT(n) \ |
---|
339 | do { \ |
---|
340 | m_context.str(""); \ |
---|
341 | m_context << "- Context : " << #n << " = " << (n) << std::endl; \ |
---|
342 | } while (!True()) |
---|
343 | |
---|
344 | #define LOLUNIT_UNSET_CONTEXT(n) \ |
---|
345 | m_context.str("") |
---|
346 | |
---|
347 | /* |
---|
348 | * Public assert macros |
---|
349 | */ |
---|
350 | |
---|
351 | #define LOLUNIT_FAIL(msg) \ |
---|
352 | do { \ |
---|
353 | m_asserts++; \ |
---|
354 | m_errorlog << std::endl << std::endl; \ |
---|
355 | m_errorlog << ++m_failcases << ") test: " \ |
---|
356 | << lol_unit_helper_name(this) << "::" << m_currentname \ |
---|
357 | << " (F) line: " << __LINE__ << " " \ |
---|
358 | << __FILE__ << std::endl; \ |
---|
359 | m_errorlog << "forced failure" << std::endl; \ |
---|
360 | m_errorlog << LOLUNIT_MSG(msg); \ |
---|
361 | m_errorlog << m_context.str(); \ |
---|
362 | m_failure = true; \ |
---|
363 | return; \ |
---|
364 | } while (!True()) |
---|
365 | |
---|
366 | #define LOLUNIT_ASSERT(cond) \ |
---|
367 | LOLUNIT_ASSERT_GENERIC("", cond) |
---|
368 | #define LOLUNIT_ASSERT_MESSAGE(m, cond) \ |
---|
369 | LOLUNIT_ASSERT_GENERIC(LOLUNIT_MSG(m), cond) |
---|
370 | |
---|
371 | |
---|
372 | #define LOLUNIT_ASSERT_EQUAL(a, b) \ |
---|
373 | LOLUNIT_ASSERT_OP(==, !!, "equality", "", a, b) |
---|
374 | #define LOLUNIT_ASSERT_EQUAL_MESSAGE(m, a, b) \ |
---|
375 | LOLUNIT_ASSERT_OP(==, !!, "equality", LOLUNIT_MSG(m), a, b) |
---|
376 | #define LOLUNIT_ASSERT_DIFFERENT(a, b) \ |
---|
377 | LOLUNIT_ASSERT_OP(!=, !!, "inequality", "", a, b) |
---|
378 | #define LOLUNIT_ASSERT_DIFFERENT_MESSAGE(m, a, b) \ |
---|
379 | LOLUNIT_ASSERT_OP(!=, !!, "inequality", LOLUNIT_MSG(m), a, b) |
---|
380 | #define LOLUNIT_ASSERT_LESS(a, b) \ |
---|
381 | LOLUNIT_ASSERT_OP(<, !!, "less than", "", a, b) |
---|
382 | #define LOLUNIT_ASSERT_LESS_MESSAGE(m, a, b) \ |
---|
383 | LOLUNIT_ASSERT_OP(<, !!, "less than", LOLUNIT_MSG(m), a, b) |
---|
384 | #define LOLUNIT_ASSERT_LEQUAL(a, b) \ |
---|
385 | LOLUNIT_ASSERT_OP(<=, !!, "less than or equal", "", a, b) |
---|
386 | #define LOLUNIT_ASSERT_LEQUAL_MESSAGE(m, a, b) \ |
---|
387 | LOLUNIT_ASSERT_OP(<=, !!, "less than or equal", LOLUNIT_MSG(m), a, b) |
---|
388 | #define LOLUNIT_ASSERT_GREATER(a, b) \ |
---|
389 | LOLUNIT_ASSERT_OP(>, !!, "greater than", "", a, b) |
---|
390 | #define LOLUNIT_ASSERT_GREATER_MESSAGE(m, a, b) \ |
---|
391 | LOLUNIT_ASSERT_OP(>, !!, "greater than", LOLUNIT_MSG(m), a, b) |
---|
392 | #define LOLUNIT_ASSERT_GEQUAL(a, b) \ |
---|
393 | LOLUNIT_ASSERT_OP(>=, !!, "greater than or equal", "", a, b) |
---|
394 | #define LOLUNIT_ASSERT_GEQUAL_MESSAGE(m, a, b) \ |
---|
395 | LOLUNIT_ASSERT_OP(>=, !!, "greater than or equal", LOLUNIT_MSG(m), a, b) |
---|
396 | |
---|
397 | |
---|
398 | #define LOLUNIT_ASSERT_NOT_EQUAL(a, b) \ |
---|
399 | LOLUNIT_ASSERT_OP(==, !, "not equality", "", a, b) |
---|
400 | #define LOLUNIT_ASSERT_NOT_EQUAL_MESSAGE(m, a, b) \ |
---|
401 | LOLUNIT_ASSERT_OP(==, !, "not equality", LOLUNIT_MSG(m), a, b) |
---|
402 | #define LOLUNIT_ASSERT_NOT_DIFFERENT(a, b) \ |
---|
403 | LOLUNIT_ASSERT_OP(!=, !, "not inequality", "", a, b) |
---|
404 | #define LOLUNIT_ASSERT_NOT_DIFFERENT_MESSAGE(m, a, b) \ |
---|
405 | LOLUNIT_ASSERT_OP(!=, !, "not inequality", LOLUNIT_MSG(m), a, b) |
---|
406 | #define LOLUNIT_ASSERT_NOT_LESS(a, b) \ |
---|
407 | LOLUNIT_ASSERT_OP(<, !, "not less than", "", a, b) |
---|
408 | #define LOLUNIT_ASSERT_NOT_LESS_MESSAGE(m, a, b) \ |
---|
409 | LOLUNIT_ASSERT_OP(<, !, "not less than", LOLUNIT_MSG(m), a, b) |
---|
410 | #define LOLUNIT_ASSERT_NOT_LEQUAL(a, b) \ |
---|
411 | LOLUNIT_ASSERT_OP(<=, !, "not less than or equal", "", a, b) |
---|
412 | #define LOLUNIT_ASSERT_NOT_LEQUAL_MESSAGE(m, a, b) \ |
---|
413 | LOLUNIT_ASSERT_OP(<=, !, "not less than or equal", LOLUNIT_MSG(m), a, b) |
---|
414 | #define LOLUNIT_ASSERT_NOT_GREATER(a, b) \ |
---|
415 | LOLUNIT_ASSERT_OP(>, !, "not greater than", "", a, b) |
---|
416 | #define LOLUNIT_ASSERT_NOT_GREATER_MESSAGE(m, a, b) \ |
---|
417 | LOLUNIT_ASSERT_OP(>, !, "not greater than", LOLUNIT_MSG(m), a, b) |
---|
418 | #define LOLUNIT_ASSERT_NOT_GEQUAL(a, b) \ |
---|
419 | LOLUNIT_ASSERT_OP(>=, !, "not greater than or equal", "", a, b) |
---|
420 | #define LOLUNIT_ASSERT_NOT_GEQUAL_MESSAGE(m, a, b) \ |
---|
421 | LOLUNIT_ASSERT_OP(>=, !, "not greater than or equal", LOLUNIT_MSG(m), a, b) |
---|
422 | |
---|
423 | #define LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, t) \ |
---|
424 | LOLUNIT_ASSERT_DOUBLES_EQUAL_GENERIC("", a, b, t) |
---|
425 | #define LOLUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(msg, a, b, t) \ |
---|
426 | LOLUNIT_ASSERT_DOUBLES_EQUAL_GENERIC(LOLUNIT_MSG(msg), a, b, t) |
---|
427 | |
---|
428 | } /* namespace lol */ |
---|
429 | |
---|
430 | #endif // __LOL_UNIT_H__ |
---|
431 | |
---|