Framework.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. // Copyright (C) 2009-2022, 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. // Forward
  19. class TestSuite;
  20. class Test;
  21. class Tester;
  22. #define ANKI_TEST_LOGI(...) ANKI_LOG("TEST", kNormal, __VA_ARGS__)
  23. #define ANKI_TEST_LOGE(...) ANKI_LOG("TEST", kError, __VA_ARGS__)
  24. #define ANKI_TEST_LOGW(...) ANKI_LOG("TEST", kWarning, __VA_ARGS__)
  25. #define ANKI_TEST_LOGF(...) ANKI_LOG("TEST", kFatal, __VA_ARGS__)
  26. /// The actual test
  27. using TestCallback = void (*)(Test&);
  28. /// Test suite
  29. class TestSuite
  30. {
  31. public:
  32. std::string name;
  33. std::vector<Test*> tests;
  34. ~TestSuite();
  35. };
  36. /// Test
  37. class Test
  38. {
  39. public:
  40. std::string name;
  41. TestSuite* suite = nullptr;
  42. TestCallback callback = nullptr;
  43. void run();
  44. };
  45. /// A container of test suites
  46. class Tester
  47. {
  48. public:
  49. std::vector<TestSuite*> suites;
  50. std::string programName;
  51. void addTest(const char* name, const char* suite, TestCallback callback);
  52. int run(int argc, char** argv);
  53. int listTests();
  54. ~Tester()
  55. {
  56. for(TestSuite* s : suites)
  57. {
  58. delete s;
  59. }
  60. }
  61. };
  62. /// Singleton so we can do the ANKI_TEST trick
  63. extern Tester& getTesterSingleton();
  64. /// Delete the instance to make valgrind a bit happy
  65. extern void deleteTesterSingleton();
  66. // Macros
  67. /// Create a new test and add it. It does a trick to add the test by using a static function
  68. #define ANKI_TEST(suiteName_, name_) \
  69. using namespace anki; \
  70. void test_##suiteName_##name_(Test&); \
  71. struct Foo##suiteName_##name_ \
  72. { \
  73. Foo##suiteName_##name_() \
  74. { \
  75. getTesterSingleton().addTest(#name_, #suiteName_, test_##suiteName_##name_); \
  76. } \
  77. }; \
  78. static Foo##suiteName_##name_ yada##suiteName_##name_; \
  79. void test_##suiteName_##name_(Test&)
  80. /// Intermediate macro
  81. #define ANKI_TEST_EXPECT_EQ_IMPL(file_, line_, func_, x, y) \
  82. do \
  83. { \
  84. if((x) != (y)) \
  85. { \
  86. std::stringstream ss; \
  87. ss << "FAILURE: " << #x << " != " << #y << " (" << file_ << ":" << line_ << ")"; \
  88. fprintf(stderr, "%s\n", ss.str().c_str()); \
  89. ANKI_DEBUG_BREAK(); \
  90. } \
  91. } while(0);
  92. /// Intermediate macro
  93. #define ANKI_TEST_EXPECT_NEQ_IMPL(file_, line_, func_, x, y) \
  94. do \
  95. { \
  96. if((x) == (y)) \
  97. { \
  98. std::stringstream ss; \
  99. ss << "FAILURE: " << #x << " == " << #y << " (" << file_ << ":" << line_ << ")"; \
  100. fprintf(stderr, "%s\n", ss.str().c_str()); \
  101. ANKI_DEBUG_BREAK(); \
  102. } \
  103. } while(0);
  104. /// Intermediate macro
  105. #define ANKI_TEST_EXPECT_GT_IMPL(file_, line_, func_, x, y) \
  106. do \
  107. { \
  108. if((x) <= (y)) \
  109. { \
  110. std::stringstream ss; \
  111. ss << "FAILURE: " << #x << " > " << #y << " (" << file_ << ":" << line_ << ")"; \
  112. fprintf(stderr, "%s\n", ss.str().c_str()); \
  113. ANKI_DEBUG_BREAK(); \
  114. } \
  115. } while(0);
  116. /// Intermediate macro
  117. #define ANKI_TEST_EXPECT_GEQ_IMPL(file_, line_, func_, x, y) \
  118. do \
  119. { \
  120. if((x) < (y)) \
  121. { \
  122. std::stringstream ss; \
  123. ss << "FAILURE: " << #x << " >= " << #y << " (" << file_ << ":" << line_ << ")"; \
  124. fprintf(stderr, "%s\n", ss.str().c_str()); \
  125. ANKI_DEBUG_BREAK(); \
  126. } \
  127. } while(0);
  128. /// Intermediate macro
  129. #define ANKI_TEST_EXPECT_LT_IMPL(file_, line_, func_, x, y) \
  130. do \
  131. { \
  132. if((x) >= (y)) \
  133. { \
  134. std::stringstream ss; \
  135. ss << "FAILURE: " << #x << " < " << #y << " (" << file_ << ":" << line_ << ")"; \
  136. fprintf(stderr, "%s\n", ss.str().c_str()); \
  137. ANKI_DEBUG_BREAK(); \
  138. } \
  139. } while(0);
  140. /// Intermediate macro
  141. #define ANKI_TEST_EXPECT_LEQ_IMPL(file_, line_, func_, x, y) \
  142. do \
  143. { \
  144. if((x) > (y)) \
  145. { \
  146. std::stringstream ss; \
  147. ss << "FAILURE: " << #x << " <= " << #y << " (" << file_ << ":" << line_ << ")"; \
  148. fprintf(stderr, "%s\n", ss.str().c_str()); \
  149. ANKI_DEBUG_BREAK(); \
  150. } \
  151. } while(0);
  152. /// Intermediate macro
  153. #define ANKI_TEST_EXPECT_NEAR_IMPL(file_, line_, func_, x, y, epsilon_) \
  154. do \
  155. { \
  156. auto maxVal = ((x) > (y)) ? (x) : (y); \
  157. auto minVal = ((x) < (y)) ? (x) : (y); \
  158. if((maxVal - minVal) > (epsilon_)) \
  159. { \
  160. std::stringstream ss; \
  161. ss << "FAILURE: " << #x << " != " << #y << " (" << file_ << ":" << line_ << ")"; \
  162. fprintf(stderr, "%s\n", ss.str().c_str()); \
  163. ANKI_DEBUG_BREAK(); \
  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_, Error::kNone)
  182. /// Check error code.
  183. #define ANKI_TEST_EXPECT_ANY_ERR(x_) ANKI_TEST_EXPECT_NEQ_IMPL(__FILE__, __LINE__, __func__, x_, Error::kNone)
  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(ConfigSet& cfg);
  187. NativeWindow* createWindow(ConfigSet& cfg);
  188. GrManager* createGrManager(ConfigSet* cfg, NativeWindow* win);
  189. ResourceManager* createResourceManager(ConfigSet* cfg, GrManager* gr, PhysicsWorld*& physics,
  190. ResourceFilesystem*& resourceFs);
  191. /// Stolen from https://en.cppreference.com/w/cpp/algorithm/random_shuffle because std::random_suffle got deprecated
  192. template<class TRandomIt>
  193. static void randomShuffle(TRandomIt first, TRandomIt last)
  194. {
  195. typename std::iterator_traits<TRandomIt>::difference_type i, n;
  196. n = last - first;
  197. for(i = n - 1; i > 0; --i)
  198. {
  199. using std::swap;
  200. swap(first[i], first[std::rand() % (i + 1)]);
  201. // rand() % (i+1) isn't actually correct, because the generated number
  202. // is not uniformly distributed for most values of i. A correct implementation
  203. // will need to essentially reimplement C++11 std::uniform_int_distribution,
  204. // which is beyond the scope of this example.
  205. }
  206. }
  207. } // end namespace anki