Framework.h 12 KB

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