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

Last change on this file since 2183 was 2183, checked in by sam, 7 years ago

build: fix the WTFPL site URL in all code comments.

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://www.wtfpl.net/ 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
24namespace lol
25{
26
27using namespace std;
28
29/*
30 * This is the base class for all fixtures. It keeps track of all
31 * fixtures registered through the LOLUNIT_FIXTURE macro and puts them
32 * in a linked list.
33 */
34class FixtureBase
35{
36    friend class TextTestRunner;
37
38public:
39    virtual void SetUp(void) {};
40    virtual void TearDown(void) {};
41
42protected:
43    FixtureBase() : m_next(NULL), m_testcases(0), m_failcases(0) {}
44    virtual ~FixtureBase() {}
45
46    static void AddFixture(FixtureBase *fixture)
47    {
48        /* Protect against several instances of the same Fixture subclass */
49        if (fixture->MarkFixture())
50            return;
51        FixtureListHelper(fixture);
52    }
53    static FixtureBase *FixtureList() { return FixtureListHelper(NULL); }
54
55    virtual void RunFixture() = 0;
56    virtual bool MarkFixture() = 0;
57
58    /* Prevent compiler complaints about unreachable code */
59    static inline bool True() { return true; }
60
61    FixtureBase *m_next;
62    int m_testcases, m_failcases;
63    int m_asserts, m_failure;
64    char const *m_fixturename, *m_currentname;
65    std::stringstream m_errorlog, m_context;
66
67private:
68    /* The FixtureBase class keeps track of all instanciated children
69     * fixtures through this method. */
70    static FixtureBase *FixtureListHelper(FixtureBase *set)
71    {
72        static FixtureBase *head = NULL, *tail = NULL;
73
74        if (set)
75        {
76            if (!head) head = set;
77            if (tail) tail->m_next = set;
78            tail = set;
79        }
80        return head;
81    }
82};
83
84/*
85 * This template specialises FixtureBase and provides registration of
86 * test cases in a linked list through the LOLUNIT_TEST macro.
87 */
88template<class T> class Fixture : protected FixtureBase
89{
90public:
91    typedef T FixtureClass;
92
93    struct TestCase
94    {
95        void (FixtureClass::* m_fun)();
96        char const *m_testname;
97        TestCase *m_next;
98    };
99
100    Fixture<T>()
101    {
102        AddFixture(this);
103    }
104
105    /* Run all test cases in this fixture. */
106    virtual void RunFixture()
107    {
108        m_errorlog.str("");
109        m_testcases = 0;
110        m_failcases = 0;
111        for (TestCase *c = TestCaseList(); c; c = c->m_next)
112        {
113            m_testcases++;
114            m_asserts = 0;
115            m_failure = false;
116            m_currentname = c->m_testname;
117            m_context.str("");
118
119            std::cout << '.';
120            (static_cast<FixtureClass *>(this)->*c->m_fun)();
121            if (m_failure) std::cout << 'F';
122        }
123    }
124
125    /* Mark the current fixture type as already registered and return whether
126     * it was seen before. */
127    virtual bool MarkFixture()
128    {
129        static bool seen = false;
130        if (seen)
131        {
132            SealFixture();
133            return true;
134        }
135        seen = true;
136        return false;
137    }
138
139    /* Manage Fixture sealing. Once SealFixture() has been called, we
140     * will no longer accept TestCase registrations. */
141    static void SealFixture() { SealFixtureHelper(true); }
142    static bool IsFixtureSealed() { return SealFixtureHelper(false); }
143
144    /* Each Fixture class specialisation keeps track of its instanciated
145     * test cases. */
146    static void AddTestCase(TestCase *that, char const *name,
147                            void (FixtureClass::*fun)())
148    {
149        if (IsFixtureSealed())
150            return;
151
152        that->m_fun = fun;
153        that->m_testname = name;
154        that->m_next = NULL;
155        TestCaseListHelper(that);
156    }
157    static TestCase *TestCaseList() { return TestCaseListHelper(NULL); }
158
159private:
160    static bool SealFixtureHelper(bool set)
161    {
162        static bool sealed = false;
163        if (set) sealed = true;
164        return sealed;
165    }
166
167    static TestCase *TestCaseListHelper(TestCase *set)
168    {
169        static TestCase *head = NULL, *tail = NULL;
170        if (set)
171        {
172            if (!head) head = set;
173            if (tail) tail->m_next = set;
174            tail = set;
175        }
176        return head;
177    }
178};
179
180/*
181 * This simple class runs all automatically registered tests and reports
182 * on error and success in the standard output.
183 */
184class TextTestRunner
185{
186public:
187    bool Run()
188    {
189        bool ret = true;
190        std::stringstream errors("");
191        int failcases = 0, testcases = 0;
192
193        for (FixtureBase *f = FixtureBase::FixtureList(); f; f = f->m_next)
194        {
195            f->SetUp();
196            f->RunFixture();
197            f->TearDown();
198
199            errors << f->m_errorlog.str();
200            testcases += f->m_testcases;
201            failcases += f->m_failcases;
202        }
203        std::cout << std::endl;
204
205        std::cout << std::endl << std::endl;
206        if (failcases)
207        {
208            std::cout << "!!!FAILURES!!!" << std::endl;
209            std::cout << "Test Results:" << std::endl;
210            std::cout << "Run:  " << testcases
211                      << "  Failures: " << failcases
212                      << "  Errors: 0" << std::endl; /* TODO: handle errors */
213
214            std::cout << errors.str();
215            ret = false;
216        }
217        else
218        {
219            std::cout << "OK (" << testcases << " tests)" << std::endl;
220        }
221        std::cout << std::endl << std::endl;
222
223        return ret;
224    }
225};
226
227#define LOLUNIT_ASSERT_GENERIC(msg, cond) \
228    do { \
229        m_asserts++; \
230        if (True() && !(cond)) \
231        { \
232            m_errorlog << std::endl << std::endl; \
233            m_errorlog << ++m_failcases << ") test: " \
234                       << lol_unit_helper_name(this) << "::" << m_currentname \
235                       << " (F) line: " << __LINE__ << " " \
236                       << __FILE__ << std::endl; \
237            m_errorlog << "assertion failed" << std::endl; \
238            m_errorlog << "- Expression: " << #cond << std::endl; \
239            m_errorlog << msg; \
240            m_failure = true; \
241            return; \
242        } \
243    } while (!True())
244
245#define LOLUNIT_ASSERT_OP(op, modifier, opdesc, msg, a, b) \
246    do { \
247        m_asserts++; \
248        if (True() && !modifier((a) op (b))) \
249        { \
250            m_errorlog << std::endl << std::endl; \
251            m_errorlog << ++m_failcases << ") test: " \
252                       << lol_unit_helper_name(this) << "::" << m_currentname \
253                       << " (F) line: " << __LINE__ << " " \
254                       << __FILE__ << std::endl; \
255            m_errorlog << opdesc << " assertion failed" << std::endl; \
256            m_errorlog << "- Expected: " << #a << " = " << (a) << std::endl; \
257            m_errorlog << "- Actual  : " << #b << " = " << (b) << std::endl; \
258            m_errorlog << msg; \
259            m_errorlog << m_context.str(); \
260            m_failure = true; \
261            return; \
262        } \
263    } while (!True())
264
265#define LOLUNIT_MSG(msg) \
266    "- " << msg << std::endl
267
268#define LOLUNIT_ASSERT_DOUBLES_EQUAL_GENERIC(msg, a, b, t) \
269    do { \
270        m_asserts++; \
271        using std::fabs; \
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
Note: See TracBrowser for help on using the repository browser.