test.h 5.6 KB

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