Framework.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. // Copyright (C) 2009-2016, 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. \
  73. struct Foo##suiteName_##name_ \
  74. { \
  75. Foo##suiteName_##name_() \
  76. { \
  77. getTesterSingleton().addTest(#name_, #suiteName_, test_##suiteName_##name_); \
  78. } \
  79. }; \
  80. static Foo##suiteName_##name_ yada##suiteName_##name_; \
  81. void test_##suiteName_##name_(Test&)
  82. /// Intermediate macro
  83. #define ANKI_TEST_EXPECT_EQ_IMPL(file_, line_, func_, x, y) \
  84. do \
  85. { \
  86. if((x) != (y)) \
  87. { \
  88. std::stringstream ss; \
  89. ss << "FAILURE: " << #x << " != " << #y << " (" << file_ << ":" << line_ << ")"; \
  90. fprintf(stderr, "%s\n", ss.str().c_str()); \
  91. abort(); \
  92. } \
  93. } while(0);
  94. /// Intermediate macro
  95. #define ANKI_TEST_EXPECT_NEQ_IMPL(file_, line_, func_, x, y) \
  96. do \
  97. { \
  98. if((x) == (y)) \
  99. { \
  100. std::stringstream ss; \
  101. ss << "FAILURE: " << #x << " == " << #y << " (" << file_ << ":" << line_ << ")"; \
  102. fprintf(stderr, "%s\n", ss.str().c_str()); \
  103. abort(); \
  104. } \
  105. } while(0);
  106. /// Intermediate macro
  107. #define ANKI_TEST_EXPECT_GT_IMPL(file_, line_, func_, x, y) \
  108. do \
  109. { \
  110. if((x) <= (y)) \
  111. { \
  112. std::stringstream ss; \
  113. ss << "FAILURE: " << #x << " > " << #y << " (" << file_ << ":" << line_ << ")"; \
  114. fprintf(stderr, "%s\n", ss.str().c_str()); \
  115. abort(); \
  116. } \
  117. } while(0);
  118. /// Intermediate macro
  119. #define ANKI_TEST_EXPECT_GEQ_IMPL(file_, line_, func_, x, y) \
  120. do \
  121. { \
  122. if((x) < (y)) \
  123. { \
  124. std::stringstream ss; \
  125. ss << "FAILURE: " << #x << " >= " << #y << " (" << file_ << ":" << line_ << ")"; \
  126. fprintf(stderr, "%s\n", ss.str().c_str()); \
  127. abort(); \
  128. } \
  129. } while(0);
  130. /// Intermediate macro
  131. #define ANKI_TEST_EXPECT_LT_IMPL(file_, line_, func_, x, y) \
  132. do \
  133. { \
  134. if((x) >= (y)) \
  135. { \
  136. std::stringstream ss; \
  137. ss << "FAILURE: " << #x << " < " << #y << " (" << file_ << ":" << line_ << ")"; \
  138. fprintf(stderr, "%s\n", ss.str().c_str()); \
  139. abort(); \
  140. } \
  141. } while(0);
  142. /// Intermediate macro
  143. #define ANKI_TEST_EXPECT_LEQ_IMPL(file_, line_, func_, x, y) \
  144. do \
  145. { \
  146. if((x) > (y)) \
  147. { \
  148. std::stringstream ss; \
  149. ss << "FAILURE: " << #x << " <= " << #y << " (" << file_ << ":" << line_ << ")"; \
  150. fprintf(stderr, "%s\n", ss.str().c_str()); \
  151. abort(); \
  152. } \
  153. } while(0);
  154. /// Intermediate macro
  155. #define ANKI_TEST_EXPECT_NEAR_IMPL(file_, line_, func_, x, y, epsilon_) \
  156. do \
  157. { \
  158. if(absolute((x) - (y)) > (epsilon_)) \
  159. { \
  160. std::stringstream ss; \
  161. ss << "FAILURE: " << #x << " != " << #y << " (" << file_ << ":" << line_ << ")"; \
  162. fprintf(stderr, "%s\n", ss.str().c_str()); \
  163. abort(); \
  164. } \
  165. } while(0);
  166. /// Macro to compare equal
  167. #define ANKI_TEST_EXPECT_EQ(x_, y_) ANKI_TEST_EXPECT_EQ_IMPL(__FILE__, __LINE__, __func__, x_, y_)
  168. /// Macro to compare equal
  169. #define ANKI_TEST_EXPECT_NEQ(x_, y_) ANKI_TEST_EXPECT_NEQ_IMPL(__FILE__, __LINE__, __func__, x_, y_)
  170. /// Macro to compare greater than
  171. #define ANKI_TEST_EXPECT_GT(x_, y_) ANKI_TEST_EXPECT_GT_IMPL(__FILE__, __LINE__, __func__, x_, y_)
  172. /// Macro to compare greater than or equal
  173. #define ANKI_TEST_EXPECT_GEQ(x_, y_) ANKI_TEST_EXPECT_GEQ_IMPL(__FILE__, __LINE__, __func__, x_, y_)
  174. /// Macro to compare less than
  175. #define ANKI_TEST_EXPECT_LT(x_, y_) ANKI_TEST_EXPECT_LT_IMPL(__FILE__, __LINE__, __func__, x_, y_)
  176. /// Macro to compare less than or equal
  177. #define ANKI_TEST_EXPECT_LEQ(x_, y_) ANKI_TEST_EXPECT_LEQ_IMPL(__FILE__, __LINE__, __func__, x_, y_)
  178. /// Compare floats with epsilon
  179. #define ANKI_TEST_EXPECT_NEAR(x_, y_, e_) ANKI_TEST_EXPECT_NEAR_IMPL(__FILE__, __LINE__, __func__, x_, y_, e_)
  180. /// Check error code.
  181. #define ANKI_TEST_EXPECT_NO_ERR(x_) ANKI_TEST_EXPECT_EQ_IMPL(__FILE__, __LINE__, __func__, x_, ErrorCode::NONE)
  182. /// Check error code.
  183. #define ANKI_TEST_EXPECT_ANY_ERR(x_) ANKI_TEST_EXPECT_NEQ_IMPL(__FILE__, __LINE__, __func__, x_, ErrorCode::NONE)
  184. /// Check error code.
  185. #define ANKI_TEST_EXPECT_ERR(x_, y_) ANKI_TEST_EXPECT_EQ_IMPL(__FILE__, __LINE__, __func__, x_, y_)
  186. void initConfig(Config& cfg);
  187. NativeWindow* createWindow(const Config& cfg);
  188. GrManager* createGrManager(const Config& cfg, NativeWindow* win);
  189. ResourceManager* createResourceManager(
  190. const Config& cfg, GrManager* gr, PhysicsWorld*& physics, ResourceFilesystem*& resourceFs);
  191. } // end namespace anki