source: trunk/src/lol/unit.h @ 1163

Last change on this file since 1163 was 1163, checked in by sam, 9 years ago

math: ensure real::fabs() is never chosen over std::fabs() for arguments
that are not explicitly real, even with namespace mistakes.

File size: 13.4 KB
Line 
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
25namespace lol
26{
27
28using 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 */
35class FixtureBase
36{
37    friend class TextTestRunner;
38
39public:
40    virtual void SetUp(void) {};
41    virtual void TearDown(void) {};
42
43protected:
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
68private:
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 */
89template<class T> class Fixture : protected FixtureBase
90{
91public:
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
160private:
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 */
185class TextTestRunner
186{
187public:
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        using std::fabs; \
273        if (True() && fabs((a) - (b)) > fabs((t))) \
274        { \
275            m_errorlog << std::endl << std::endl; \
276            m_errorlog << ++m_failcases << ") test: " \
277                       << lol_unit_helper_name(this) << "::" << m_currentname \
278                       << " (F) line: " << __LINE__ << " " \
279                       << __FILE__ << std::endl; \
280            m_errorlog << "double equality assertion failed" << std::endl; \
281            std::streamsize old_prec = m_errorlog.precision(); \
282            m_errorlog << std::setprecision(16); \
283            m_errorlog << "- Expected: " << #a << " = " << (a) << std::endl; \
284            m_errorlog << "- Actual  : " << #b << " = " << (b) << std::endl; \
285            m_errorlog << "- Delta   : " << (t) << std::endl; \
286            m_errorlog << std::setprecision(old_prec); \
287            m_errorlog << msg; \
288            m_errorlog << m_context.str(); \
289            m_failure = true; \
290            return; \
291        } \
292    } while (!True())
293
294/*
295 * Public helper macros
296 */
297
298#define LOLUNIT_FIXTURE(N) \
299    class N; \
300    /* This pattern allows us to statically create a Fixture instance \
301     * before its exact implementation was defined. */ \
302    template<typename T> struct lol_unit_helper_fixture_##N \
303    { \
304        lol_unit_helper_fixture_##N() { p = new T(); } \
305        ~lol_unit_helper_fixture_##N() { delete p; } \
306        T *p; \
307    }; \
308    lol_unit_helper_fixture_##N<N> lol_unit_helper_fixture_##N##_instance; \
309    /* Allow to retrieve the class name without using RTTI and without \
310     * knowing the type of "this". */ \
311    static inline char const *lol_unit_helper_name(N *p) \
312    { \
313        (void)p; \
314        return #N; \
315    } \
316    /* Now the user can define the implementation */ \
317    class N : public lol::Fixture<N>
318
319#define LOLUNIT_TEST(N) \
320    /* For each test in the fixture, we create an object that will \
321     * automatically register the test method in a list global to the \
322     * specialised fixture. */ \
323    class lol_unit_helper_test_##N : public TestCase \
324    { \
325    public: \
326        lol_unit_helper_test_##N() \
327        { \
328            AddTestCase(this, #N, \
329                        (void (FixtureClass::*)()) &FixtureClass::N); \
330        } \
331    }; \
332    lol_unit_helper_test_##N lol_unit_helper_test_instance_##N; \
333    void N()
334
335/*
336 * Provide context for error messages
337 */
338
339#define LOLUNIT_SET_CONTEXT(n) \
340    do { \
341        m_context.str(""); \
342        m_context << "- Context : " << #n << " = " << (n) << std::endl; \
343    } while (!True())
344
345#define LOLUNIT_UNSET_CONTEXT(n) \
346    m_context.str("")
347
348/*
349 * Public assert macros
350 */
351
352#define LOLUNIT_FAIL(msg) \
353    do { \
354        m_asserts++; \
355        m_errorlog << std::endl << std::endl; \
356        m_errorlog << ++m_failcases << ") test: " \
357                   << lol_unit_helper_name(this) << "::" << m_currentname \
358                   << " (F) line: " << __LINE__ << " " \
359                   << __FILE__ << std::endl; \
360        m_errorlog << "forced failure" << std::endl; \
361        m_errorlog << LOLUNIT_MSG(msg); \
362        m_errorlog << m_context.str(); \
363        m_failure = true; \
364        return; \
365    } while (!True())
366
367#define LOLUNIT_ASSERT(cond) \
368    LOLUNIT_ASSERT_GENERIC("", cond)
369#define LOLUNIT_ASSERT_MESSAGE(m, cond) \
370    LOLUNIT_ASSERT_GENERIC(LOLUNIT_MSG(m), cond)
371
372
373#define LOLUNIT_ASSERT_EQUAL(a, b) \
374    LOLUNIT_ASSERT_OP(==, !!, "equality", "", a, b)
375#define LOLUNIT_ASSERT_EQUAL_MESSAGE(m, a, b) \
376    LOLUNIT_ASSERT_OP(==, !!, "equality", LOLUNIT_MSG(m), a, b)
377#define LOLUNIT_ASSERT_DIFFERENT(a, b) \
378    LOLUNIT_ASSERT_OP(!=, !!, "inequality", "", a, b)
379#define LOLUNIT_ASSERT_DIFFERENT_MESSAGE(m, a, b) \
380    LOLUNIT_ASSERT_OP(!=, !!, "inequality", LOLUNIT_MSG(m), a, b)
381#define LOLUNIT_ASSERT_LESS(a, b) \
382    LOLUNIT_ASSERT_OP(<, !!, "less than", "", a, b)
383#define LOLUNIT_ASSERT_LESS_MESSAGE(m, a, b) \
384    LOLUNIT_ASSERT_OP(<, !!, "less than", LOLUNIT_MSG(m), a, b)
385#define LOLUNIT_ASSERT_LEQUAL(a, b) \
386    LOLUNIT_ASSERT_OP(<=, !!, "less than or equal", "", a, b)
387#define LOLUNIT_ASSERT_LEQUAL_MESSAGE(m, a, b) \
388    LOLUNIT_ASSERT_OP(<=, !!, "less than or equal", LOLUNIT_MSG(m), a, b)
389#define LOLUNIT_ASSERT_GREATER(a, b) \
390    LOLUNIT_ASSERT_OP(>, !!, "greater than", "", a, b)
391#define LOLUNIT_ASSERT_GREATER_MESSAGE(m, a, b) \
392    LOLUNIT_ASSERT_OP(>, !!, "greater than", LOLUNIT_MSG(m), a, b)
393#define LOLUNIT_ASSERT_GEQUAL(a, b) \
394    LOLUNIT_ASSERT_OP(>=, !!, "greater than or equal", "", a, b)
395#define LOLUNIT_ASSERT_GEQUAL_MESSAGE(m, a, b) \
396    LOLUNIT_ASSERT_OP(>=, !!, "greater than or equal", LOLUNIT_MSG(m), a, b)
397
398
399#define LOLUNIT_ASSERT_NOT_EQUAL(a, b) \
400    LOLUNIT_ASSERT_OP(==, !, "not equality", "", a, b)
401#define LOLUNIT_ASSERT_NOT_EQUAL_MESSAGE(m, a, b) \
402    LOLUNIT_ASSERT_OP(==, !, "not equality", LOLUNIT_MSG(m), a, b)
403#define LOLUNIT_ASSERT_NOT_DIFFERENT(a, b) \
404    LOLUNIT_ASSERT_OP(!=, !, "not inequality", "", a, b)
405#define LOLUNIT_ASSERT_NOT_DIFFERENT_MESSAGE(m, a, b) \
406    LOLUNIT_ASSERT_OP(!=, !, "not inequality", LOLUNIT_MSG(m), a, b)
407#define LOLUNIT_ASSERT_NOT_LESS(a, b) \
408    LOLUNIT_ASSERT_OP(<, !, "not less than", "", a, b)
409#define LOLUNIT_ASSERT_NOT_LESS_MESSAGE(m, a, b) \
410    LOLUNIT_ASSERT_OP(<, !, "not less than", LOLUNIT_MSG(m), a, b)
411#define LOLUNIT_ASSERT_NOT_LEQUAL(a, b) \
412    LOLUNIT_ASSERT_OP(<=, !, "not less than or equal", "", a, b)
413#define LOLUNIT_ASSERT_NOT_LEQUAL_MESSAGE(m, a, b) \
414    LOLUNIT_ASSERT_OP(<=, !, "not less than or equal", LOLUNIT_MSG(m), a, b)
415#define LOLUNIT_ASSERT_NOT_GREATER(a, b) \
416    LOLUNIT_ASSERT_OP(>, !, "not greater than", "", a, b)
417#define LOLUNIT_ASSERT_NOT_GREATER_MESSAGE(m, a, b) \
418    LOLUNIT_ASSERT_OP(>, !, "not greater than", LOLUNIT_MSG(m), a, b)
419#define LOLUNIT_ASSERT_NOT_GEQUAL(a, b) \
420    LOLUNIT_ASSERT_OP(>=, !, "not greater than or equal", "", a, b)
421#define LOLUNIT_ASSERT_NOT_GEQUAL_MESSAGE(m, a, b) \
422    LOLUNIT_ASSERT_OP(>=, !, "not greater than or equal", LOLUNIT_MSG(m), a, b)
423
424#define LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, t) \
425    LOLUNIT_ASSERT_DOUBLES_EQUAL_GENERIC("", a, b, t)
426#define LOLUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(msg, a, b, t) \
427    LOLUNIT_ASSERT_DOUBLES_EQUAL_GENERIC(LOLUNIT_MSG(msg), a, b, t)
428
429} /* namespace lol */
430
431#endif // __LOL_UNIT_H__
432
Note: See TracBrowser for help on using the repository browser.