Framework.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. // Copyright (C) 2009-2020, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma once
  6. #include <anki/util/Singleton.h>
  7. #include <anki/Math.h>
  8. #include <anki/util/Logger.h>
  9. #include <anki/Core.h>
  10. #include <anki/Gr.h>
  11. #include <anki/Resource.h>
  12. #include <anki/Physics.h>
  13. #include <stdexcept>
  14. #include <vector>
  15. #include <string>
  16. #include <sstream>
  17. namespace anki
  18. {
  19. // Forward
  20. class TestSuite;
  21. class Test;
  22. class Tester;
  23. #define ANKI_TEST_LOGI(...) ANKI_LOG("TEST", NORMAL, __VA_ARGS__)
  24. #define ANKI_TEST_LOGE(...) ANKI_LOG("TEST", ERROR, __VA_ARGS__)
  25. #define ANKI_TEST_LOGW(...) ANKI_LOG("TEST", WARNING, __VA_ARGS__)
  26. #define ANKI_TEST_LOGF(...) ANKI_LOG("TEST", FATAL, __VA_ARGS__)
  27. /// The actual test
  28. using TestCallback = void (*)(Test&);
  29. /// Test suite
  30. class TestSuite
  31. {
  32. public:
  33. std::string name;
  34. std::vector<Test*> tests;
  35. ~TestSuite();
  36. };
  37. /// Test
  38. class Test
  39. {
  40. public:
  41. std::string name;
  42. TestSuite* suite = nullptr;
  43. TestCallback callback = nullptr;
  44. void run();
  45. };
  46. /// A container of test suites
  47. class Tester
  48. {
  49. public:
  50. std::vector<TestSuite*> suites;
  51. std::string programName;
  52. void addTest(const char* name, const char* suite, TestCallback callback);
  53. int run(int argc, char** argv);
  54. int listTests();
  55. ~Tester()
  56. {
  57. for(TestSuite* s : suites)
  58. {
  59. delete s;
  60. }
  61. }
  62. };
  63. /// Singleton so we can do the ANKI_TEST trick
  64. extern Tester& getTesterSingleton();
  65. /// Delete the instance to make valgrind a bit happy
  66. extern void deleteTesterSingleton();
  67. // Macros
  68. /// Create a new test and add it. It does a trick to add the test by using a static function
  69. #define ANKI_TEST(suiteName_, name_) \
  70. using namespace anki; \
  71. void test_##suiteName_##name_(Test&); \
  72. struct Foo##suiteName_##name_ \
  73. { \
  74. Foo##suiteName_##name_() \
  75. { \
  76. getTesterSingleton().addTest(#name_, #suiteName_, test_##suiteName_##name_); \
  77. } \
  78. }; \
  79. static Foo##suiteName_##name_ yada##suiteName_##name_; \
  80. void test_##suiteName_##name_(Test&)
  81. /// Intermediate macro
  82. #define ANKI_TEST_EXPECT_EQ_IMPL(file_, line_, func_, x, y) \
  83. do \
  84. { \
  85. if((x) != (y)) \
  86. { \
  87. std::stringstream ss; \
  88. ss << "FAILURE: " << #x << " != " << #y << " (" << file_ << ":" << line_ << ")"; \
  89. fprintf(stderr, "%s\n", ss.str().c_str()); \
  90. abort(); \
  91. } \
  92. } while(0);
  93. /// Intermediate macro
  94. #define ANKI_TEST_EXPECT_NEQ_IMPL(file_, line_, func_, x, y) \
  95. do \
  96. { \
  97. if((x) == (y)) \
  98. { \
  99. std::stringstream ss; \
  100. ss << "FAILURE: " << #x << " == " << #y << " (" << file_ << ":" << line_ << ")"; \
  101. fprintf(stderr, "%s\n", ss.str().c_str()); \
  102. abort(); \
  103. } \
  104. } while(0);
  105. /// Intermediate macro
  106. #define ANKI_TEST_EXPECT_GT_IMPL(file_, line_, func_, x, y) \
  107. do \
  108. { \
  109. if((x) <= (y)) \
  110. { \
  111. std::stringstream ss; \
  112. ss << "FAILURE: " << #x << " > " << #y << " (" << file_ << ":" << line_ << ")"; \
  113. fprintf(stderr, "%s\n", ss.str().c_str()); \
  114. abort(); \
  115. } \
  116. } while(0);
  117. /// Intermediate macro
  118. #define ANKI_TEST_EXPECT_GEQ_IMPL(file_, line_, func_, x, y) \
  119. do \
  120. { \
  121. if((x) < (y)) \
  122. { \
  123. std::stringstream ss; \
  124. ss << "FAILURE: " << #x << " >= " << #y << " (" << file_ << ":" << line_ << ")"; \
  125. fprintf(stderr, "%s\n", ss.str().c_str()); \
  126. abort(); \
  127. } \
  128. } while(0);
  129. /// Intermediate macro
  130. #define ANKI_TEST_EXPECT_LT_IMPL(file_, line_, func_, x, y) \
  131. do \
  132. { \
  133. if((x) >= (y)) \
  134. { \
  135. std::stringstream ss; \
  136. ss << "FAILURE: " << #x << " < " << #y << " (" << file_ << ":" << line_ << ")"; \
  137. fprintf(stderr, "%s\n", ss.str().c_str()); \
  138. abort(); \
  139. } \
  140. } while(0);
  141. /// Intermediate macro
  142. #define ANKI_TEST_EXPECT_LEQ_IMPL(file_, line_, func_, x, y) \
  143. do \
  144. { \
  145. if((x) > (y)) \
  146. { \
  147. std::stringstream ss; \
  148. ss << "FAILURE: " << #x << " <= " << #y << " (" << file_ << ":" << line_ << ")"; \
  149. fprintf(stderr, "%s\n", ss.str().c_str()); \
  150. abort(); \
  151. } \
  152. } while(0);
  153. /// Intermediate macro
  154. #define ANKI_TEST_EXPECT_NEAR_IMPL(file_, line_, func_, x, y, epsilon_) \
  155. do \
  156. { \
  157. auto maxVal = ((x) > (y)) ? (x) : (y); \
  158. auto minVal = ((x) < (y)) ? (x) : (y); \
  159. if((maxVal - minVal) > (epsilon_)) \
  160. { \
  161. std::stringstream ss; \
  162. ss << "FAILURE: " << #x << " != " << #y << " (" << file_ << ":" << line_ << ")"; \
  163. fprintf(stderr, "%s\n", ss.str().c_str()); \
  164. abort(); \
  165. } \
  166. } while(0);
  167. /// Macro to compare equal
  168. #define ANKI_TEST_EXPECT_EQ(x_, y_) ANKI_TEST_EXPECT_EQ_IMPL(__FILE__, __LINE__, __func__, x_, y_)
  169. /// Macro to compare equal
  170. #define ANKI_TEST_EXPECT_NEQ(x_, y_) ANKI_TEST_EXPECT_NEQ_IMPL(__FILE__, __LINE__, __func__, x_, y_)
  171. /// Macro to compare greater than
  172. #define ANKI_TEST_EXPECT_GT(x_, y_) ANKI_TEST_EXPECT_GT_IMPL(__FILE__, __LINE__, __func__, x_, y_)
  173. /// Macro to compare greater than or equal
  174. #define ANKI_TEST_EXPECT_GEQ(x_, y_) ANKI_TEST_EXPECT_GEQ_IMPL(__FILE__, __LINE__, __func__, x_, y_)
  175. /// Macro to compare less than
  176. #define ANKI_TEST_EXPECT_LT(x_, y_) ANKI_TEST_EXPECT_LT_IMPL(__FILE__, __LINE__, __func__, x_, y_)
  177. /// Macro to compare less than or equal
  178. #define ANKI_TEST_EXPECT_LEQ(x_, y_) ANKI_TEST_EXPECT_LEQ_IMPL(__FILE__, __LINE__, __func__, x_, y_)
  179. /// Compare floats with epsilon
  180. #define ANKI_TEST_EXPECT_NEAR(x_, y_, e_) ANKI_TEST_EXPECT_NEAR_IMPL(__FILE__, __LINE__, __func__, x_, y_, e_)
  181. /// Check error code.
  182. #define ANKI_TEST_EXPECT_NO_ERR(x_) ANKI_TEST_EXPECT_EQ_IMPL(__FILE__, __LINE__, __func__, x_, Error::NONE)
  183. /// Check error code.
  184. #define ANKI_TEST_EXPECT_ANY_ERR(x_) ANKI_TEST_EXPECT_NEQ_IMPL(__FILE__, __LINE__, __func__, x_, Error::NONE)
  185. /// Check error code.
  186. #define ANKI_TEST_EXPECT_ERR(x_, y_) ANKI_TEST_EXPECT_EQ_IMPL(__FILE__, __LINE__, __func__, x_, y_)
  187. void initConfig(ConfigSet& cfg);
  188. NativeWindow* createWindow(const ConfigSet& cfg);
  189. GrManager* createGrManager(const ConfigSet& cfg, NativeWindow* win);
  190. ResourceManager* createResourceManager(const ConfigSet& cfg, GrManager* gr, PhysicsWorld*& physics,
  191. ResourceFilesystem*& resourceFs);
  192. } // end namespace anki