LCOV - code coverage report
Current view: top level - src/unittest - test.h (source / functions) Hit Total Coverage
Test: report Lines: 7 8 87.5 %
Date: 2015-07-11 18:23:49 Functions: 4 7 57.1 %

          Line data    Source code
       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           0 : 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          38 : class TestBase {
     103             : public:
     104             :         bool testModule(IGameDef *gamedef);
     105             :         std::string getTestTempDirectory();
     106             :         std::string getTestTempFile();
     107             : 
     108             :         virtual void runTests(IGameDef *gamedef) = 0;
     109             :         virtual const char *getName() = 0;
     110             : 
     111             :         u32 num_tests_failed;
     112             :         u32 num_tests_run;
     113             : 
     114             : private:
     115             :         std::string m_test_dir;
     116             : };
     117             : 
     118             : class TestManager {
     119             : public:
     120          19 :         static std::vector<TestBase *> &getTestModules()
     121             :         {
     122          19 :                 static std::vector<TestBase *> m_modules_to_test;
     123          19 :                 return m_modules_to_test;
     124             :         }
     125             : 
     126          19 :         static void registerTestModule(TestBase *module)
     127             :         {
     128          19 :                 getTestModules().push_back(module);
     129          19 :         }
     130             : };
     131             : 
     132             : // A few item and node definitions for those tests that need them
     133             : extern content_t t_CONTENT_STONE;
     134             : extern content_t t_CONTENT_GRASS;
     135             : extern content_t t_CONTENT_TORCH;
     136             : extern content_t t_CONTENT_WATER;
     137             : extern content_t t_CONTENT_LAVA;
     138             : extern content_t t_CONTENT_BRICK;
     139             : 
     140             : void run_tests();
     141             : 
     142             : #endif

Generated by: LCOV version 1.11