Framework.h 12 KB

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