Framework.h 10.0 KB

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