Framework.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. // Copyright (C) 2009-2015, Panagiotis Christopoulos Charitos.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #ifndef ANKI_TESTS_FRAMEWORK_FRAMEWORK_H
  6. #define ANKI_TESTS_FRAMEWORK_FRAMEWORK_H
  7. #include "anki/util/Singleton.h"
  8. #include <stdexcept>
  9. #include <vector>
  10. #include <string>
  11. #include <sstream>
  12. namespace anki {
  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. Foo##suiteName_##name_() { \
  67. getTesterSingleton().addTest(#name_, #suiteName_, \
  68. 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. if((x) != (y)) { \
  77. std::stringstream ss; \
  78. ss << "FAILURE: " << #x << " != " << #y << " (" \
  79. << file_ << ":" << line_ << ")"; \
  80. fprintf(stderr, "%s\n", ss.str().c_str()); \
  81. abort(); \
  82. } \
  83. } while(0);
  84. /// Intermediate macro
  85. #define ANKI_TEST_EXPECT_NEQ_IMPL(file_, line_, func_, x, y) \
  86. do { \
  87. if((x) == (y)) { \
  88. std::stringstream ss; \
  89. ss << "FAILURE: " << #x << " == " << #y << " (" \
  90. << file_ << ":" << line_ << ")"; \
  91. fprintf(stderr, "%s\n", ss.str().c_str()); \
  92. abort(); \
  93. } \
  94. } while(0);
  95. /// Intermediate macro
  96. #define ANKI_TEST_EXPECT_GT_IMPL(file_, line_, func_, x, y) \
  97. do { \
  98. if((x) <= (y)) { \
  99. std::stringstream ss; \
  100. ss << "FAILURE: " << #x << " > " << #y << " (" \
  101. << file_ << ":" << line_ << ")"; \
  102. fprintf(stderr, "%s\n", ss.str().c_str()); \
  103. abort(); \
  104. } \
  105. } while(0);
  106. /// Intermediate macro
  107. #define ANKI_TEST_EXPECT_GEQ_IMPL(file_, line_, func_, x, y) \
  108. do { \
  109. if((x) < (y)) { \
  110. std::stringstream ss; \
  111. ss << "FAILURE: " << #x << " >= " << #y << " (" \
  112. << file_ << ":" << line_ << ")"; \
  113. fprintf(stderr, "%s\n", ss.str().c_str()); \
  114. abort(); \
  115. } \
  116. } while(0);
  117. /// Intermediate macro
  118. #define ANKI_TEST_EXPECT_LT_IMPL(file_, line_, func_, x, y) \
  119. do { \
  120. if((x) >= (y)) { \
  121. std::stringstream ss; \
  122. ss << "FAILURE: " << #x << " < " << #y << " (" \
  123. << file_ << ":" << line_ << ")"; \
  124. fprintf(stderr, "%s\n", ss.str().c_str()); \
  125. abort(); \
  126. } \
  127. } while(0);
  128. /// Intermediate macro
  129. #define ANKI_TEST_EXPECT_LEQ_IMPL(file_, line_, func_, x, y) \
  130. do { \
  131. if((x) > (y)) { \
  132. std::stringstream ss; \
  133. ss << "FAILURE: " << #x << " <= " << #y << " (" \
  134. << file_ << ":" << line_ << ")"; \
  135. fprintf(stderr, "%s\n", ss.str().c_str()); \
  136. abort(); \
  137. } \
  138. } while(0);
  139. /// Intermediate macro
  140. #define ANKI_TEST_EXPECT_NEAR_IMPL(file_, line_, func_, x, y, epsilon_) \
  141. do { \
  142. if(abs((x) - (y)) > (epsilon_)) { \
  143. std::stringstream ss; \
  144. ss << "FAILURE: " << #x << " != " << #y << " (" \
  145. << file_ << ":" << line_ << ")"; \
  146. fprintf(stderr, "%s\n", ss.str().c_str()); \
  147. abort(); \
  148. } \
  149. } while(0);
  150. /// Macro to compare equal
  151. #define ANKI_TEST_EXPECT_EQ(x_, y_) \
  152. ANKI_TEST_EXPECT_EQ_IMPL(__FILE__, __LINE__, __func__, x_, y_)
  153. /// Macro to compare equal
  154. #define ANKI_TEST_EXPECT_NEQ(x_, y_) \
  155. ANKI_TEST_EXPECT_NEQ_IMPL(__FILE__, __LINE__, __func__, x_, y_)
  156. /// Macro to compare greater than
  157. #define ANKI_TEST_EXPECT_GT(x_, y_) \
  158. ANKI_TEST_EXPECT_GT_IMPL(__FILE__, __LINE__, __func__, x_, y_)
  159. /// Macro to compare greater than or equal
  160. #define ANKI_TEST_EXPECT_GEQ(x_, y_) \
  161. ANKI_TEST_EXPECT_GEQ_IMPL(__FILE__, __LINE__, __func__, x_, y_)
  162. /// Macro to compare less than
  163. #define ANKI_TEST_EXPECT_LT(x_, y_) \
  164. ANKI_TEST_EXPECT_LT_IMPL(__FILE__, __LINE__, __func__, x_, y_)
  165. /// Macro to compare less than or equal
  166. #define ANKI_TEST_EXPECT_LEQ(x_, y_) \
  167. ANKI_TEST_EXPECT_LEQ_IMPL(__FILE__, __LINE__, __func__, x_, y_)
  168. /// Compare floats with epsilon
  169. #define ANKI_TEST_EXPECT_NEAR(x_, y_, e_) \
  170. ANKI_TEST_EXPECT_NEAR_IMPL(__FILE__, __LINE__, __func__, x_, y_, e_)
  171. /// Check error code.
  172. #define ANKI_TEST_EXPECT_NO_ERR(x_) \
  173. ANKI_TEST_EXPECT_EQ_IMPL(__FILE__, __LINE__, __func__, x_, ErrorCode::NONE)
  174. } // end namespace anki
  175. #endif