Framework.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. // Copyright (C) 2009-2023, 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/Window.h>
  11. #include <AnKi/Gr.h>
  12. #include <AnKi/Resource.h>
  13. #include <AnKi/Physics.h>
  14. #include <stdexcept>
  15. #include <vector>
  16. #include <string>
  17. #include <sstream>
  18. namespace anki {
  19. // Forward
  20. class TestSuite;
  21. class Test;
  22. class Tester;
  23. #define ANKI_TEST_LOGI(...) ANKI_LOG("TEST", kNormal, __VA_ARGS__)
  24. #define ANKI_TEST_LOGE(...) ANKI_LOG("TEST", kError, __VA_ARGS__)
  25. #define ANKI_TEST_LOGW(...) ANKI_LOG("TEST", kWarning, __VA_ARGS__)
  26. #define ANKI_TEST_LOGF(...) ANKI_LOG("TEST", kFatal, __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. ANKI_DEBUG_BREAK(); \
  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. ANKI_DEBUG_BREAK(); \
  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. ANKI_DEBUG_BREAK(); \
  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. ANKI_DEBUG_BREAK(); \
  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. ANKI_DEBUG_BREAK(); \
  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. ANKI_DEBUG_BREAK(); \
  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. ANKI_DEBUG_BREAK(); \
  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::kNone)
  183. /// Check error code.
  184. #define ANKI_TEST_EXPECT_ANY_ERR(x_) ANKI_TEST_EXPECT_NEQ_IMPL(__FILE__, __LINE__, __func__, x_, Error::kNone)
  185. /// Check error code.
  186. #define ANKI_TEST_EXPECT_ERR(x_, y_) ANKI_TEST_EXPECT_EQ_IMPL(__FILE__, __LINE__, __func__, x_, y_)
  187. NativeWindow* createWindow();
  188. GrManager* createGrManager(NativeWindow* win);
  189. ResourceManager* createResourceManager(GrManager* gr);
  190. /// Stolen from https://en.cppreference.com/w/cpp/algorithm/random_shuffle because std::random_suffle got deprecated
  191. template<class TRandomIt>
  192. static void randomShuffle(TRandomIt first, TRandomIt last)
  193. {
  194. typename std::iterator_traits<TRandomIt>::difference_type i, n;
  195. n = last - first;
  196. for(i = n - 1; i > 0; --i)
  197. {
  198. using std::swap;
  199. swap(first[i], first[std::rand() % (i + 1)]);
  200. // rand() % (i+1) isn't actually correct, because the generated number
  201. // is not uniformly distributed for most values of i. A correct implementation
  202. // will need to essentially reimplement C++11 std::uniform_int_distribution,
  203. // which is beyond the scope of this example.
  204. }
  205. }
  206. } // end namespace anki