]> git.lizzy.rs Git - dragonfireclient.git/blob - src/unittest/test.h
Tests: Add random unittests
[dragonfireclient.git] / src / unittest / test.h
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #ifndef TEST_HEADER
21 #define TEST_HEADER
22
23 #include <exception>
24 #include <vector>
25
26 #include "irrlichttypes_extrabloated.h"
27 #include "porting.h"
28 #include "filesys.h"
29 #include "mapnode.h"
30
31 class TestFailedException : public std::exception {
32 };
33
34 // Runs a unit test and reports results
35 #define TEST(fxn, ...) do {         \
36         u32 t1 = porting::getTime(PRECISION_MILLI); \
37         try {                           \
38                 fxn(__VA_ARGS__);           \
39                 dstream << "[PASS] ";       \
40         } catch (...) {                 \
41                 dstream << "[FAIL] ";       \
42                 num_tests_failed++;         \
43         }                               \
44         num_tests_run++;                \
45         u32 tdiff = porting::getTime(PRECISION_MILLI) - t1;     \
46         dstream << #fxn << " - " << tdiff << "ms" << std::endl; \
47 } while (0)
48
49 // Asserts the specified condition is true, or fails the current unit test
50 #define UASSERT(x) do {                                       \
51         if (!(x)) {                                               \
52                 dstream << "Test assertion failed: " #x << std::endl  \
53                         << "    at " << fs::GetFilenameFromPath(__FILE__) \
54                         << ":" << __LINE__ << std::endl;                  \
55                 throw TestFailedException();                          \
56         }                                                         \
57 } while (0)
58
59 // Asserts the specified condition is true, or fails the current unit test
60 // and prints the format specifier fmt
61 #define UTEST(x, fmt, ...) do {                                        \
62         if (!(x)) {                                                        \
63                 char utest_buf[1024];                                          \
64                 snprintf(utest_buf, sizeof(utest_buf), fmt, __VA_ARGS__);      \
65                 dstream << "Test assertion failed: " << utest_buf << std::endl \
66                         << "    at " << fs::GetFilenameFromPath(__FILE__)          \
67                         << ":" << __LINE__ << std::endl;                           \
68                 throw TestFailedException();                                   \
69         }                                                                  \
70 } while (0)
71
72 // Asserts the comparison specified by CMP is true, or fails the current unit test
73 #define UASSERTCMP(T, CMP, actual, expected) do {                             \
74         T a = (actual);                                                           \
75         T e = (expected);                                                         \
76         if (!(a CMP e)) {                                                         \
77                 dstream << "Test assertion failed: " << #actual << " " << #CMP << " " \
78                         << #expected << std::endl                                         \
79                         << "    at " << fs::GetFilenameFromPath(__FILE__) << ":"          \
80                         << __LINE__ << std::endl                                          \
81                         << "    actual:   " << a << std::endl << "    expected: "         \
82                         << e << std::endl;                                                \
83                 throw TestFailedException();                                          \
84         }                                                                         \
85 } while (0)
86
87 #define UASSERTEQ(T, actual, expected) UASSERTCMP(T, ==, actual, expected)
88
89 // UASSERTs that the specified exception occurs
90 #define EXCEPTION_CHECK(EType, code) do { \
91         bool exception_thrown = false;        \
92         try {                                 \
93                 code;                             \
94         } catch (EType &e) {                  \
95                 exception_thrown = true;          \
96         }                                     \
97         UASSERT(exception_thrown);            \
98 } while (0)
99
100 class IGameDef;
101
102 class TestBase {
103 public:
104         bool testModule(IGameDef *gamedef);
105         virtual void runTests(IGameDef *gamedef) = 0;
106         virtual const char *getName() = 0;
107
108         u32 num_tests_failed;
109         u32 num_tests_run;
110 };
111
112 class TestManager {
113 public:
114         static std::vector<TestBase *> &getTestModules()
115         {
116                 static std::vector<TestBase *> m_modules_to_test;
117                 return m_modules_to_test;
118         }
119
120         static void registerTestModule(TestBase *module)
121         {
122                 getTestModules().push_back(module);
123         }
124 };
125
126 // A few item and node definitions for those tests that need them
127 extern content_t CONTENT_STONE;
128 extern content_t CONTENT_GRASS;
129 extern content_t CONTENT_TORCH;
130
131 void run_tests();
132
133 #endif