3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
26 #include "irrlichttypes_extrabloated.h"
31 class TestFailedException : public std::exception {
34 // Runs a unit test and reports results
35 #define TEST(fxn, ...) do { \
36 u32 t1 = porting::getTime(PRECISION_MILLI); \
39 dstream << "[PASS] "; \
41 dstream << "[FAIL] "; \
45 u32 tdiff = porting::getTime(PRECISION_MILLI) - t1; \
46 dstream << #fxn << " - " << tdiff << "ms" << std::endl; \
49 // Asserts the specified condition is true, or fails the current unit test
50 #define UASSERT(x) do { \
52 dstream << "Test assertion failed: " #x << std::endl \
53 << " at " << fs::GetFilenameFromPath(__FILE__) \
54 << ":" << __LINE__ << std::endl; \
55 throw TestFailedException(); \
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 { \
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(); \
72 // Asserts the comparison specified by CMP is true, or fails the current unit test
73 #define UASSERTCMP(T, CMP, actual, expected) do { \
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: " \
83 throw TestFailedException(); \
87 #define UASSERTEQ(T, actual, expected) UASSERTCMP(T, ==, actual, expected)
89 // UASSERTs that the specified exception occurs
90 #define EXCEPTION_CHECK(EType, code) do { \
91 bool exception_thrown = false; \
94 } catch (EType &e) { \
95 exception_thrown = true; \
97 UASSERT(exception_thrown); \
104 bool testModule(IGameDef *gamedef);
105 virtual void runTests(IGameDef *gamedef) = 0;
106 virtual const char *getName() = 0;
108 u32 num_tests_failed;
114 static std::vector<TestBase *> &getTestModules()
116 static std::vector<TestBase *> m_modules_to_test;
117 return m_modules_to_test;
120 static void registerTestModule(TestBase *module)
122 getTestModules().push_back(module);
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;