Framework.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #ifndef ANKI_TESTS_FRAMEWORK_FRAMEWORK_H
  2. #define ANKI_TESTS_FRAMEWORK_FRAMEWORK_H
  3. #include "anki/util/Vector.h"
  4. #include "anki/util/Singleton.h"
  5. #include <stdexcept>
  6. #include <string>
  7. #include <iostream>
  8. #include <sstream>
  9. #include <cmath>
  10. namespace anki {
  11. // Forward
  12. class TestSuite;
  13. class Test;
  14. class Tester;
  15. /// The actual test
  16. typedef void (*TestCallback)(Test&);
  17. /// Test suite
  18. struct TestSuite
  19. {
  20. std::string name;
  21. PtrVector<Test> tests;
  22. };
  23. /// Test
  24. struct Test
  25. {
  26. std::string name;
  27. TestSuite* suite = nullptr;
  28. TestCallback callback = nullptr;
  29. void run();
  30. };
  31. /// A container of test suites
  32. struct Tester
  33. {
  34. PtrVector<TestSuite> suites;
  35. std::string programName;
  36. void addTest(const char* name, const char* suite, TestCallback callback);
  37. int run(int argc, char** argv);
  38. int listTests();
  39. };
  40. /// Singleton so we can do the ANKI_TEST trick
  41. extern Tester& getTesterSingleton();
  42. /// Delete the instance to make valgrind a bit happy
  43. extern void deleteTesterSingleton();
  44. //==============================================================================
  45. // Macros
  46. /// Create a new test and add it. It does a trick to add the test by using a
  47. /// static function
  48. #define ANKI_TEST(suiteName_, name_) \
  49. using namespace anki; \
  50. void test_##suiteName_##name_(Test&); \
  51. \
  52. struct Foo##suiteName_##name_ { \
  53. Foo##suiteName_##name_() { \
  54. getTesterSingleton().addTest(#name_, #suiteName_, \
  55. test_##suiteName_##name_); \
  56. } \
  57. }; \
  58. static Foo##suiteName_##name_ yada##suiteName_##name_; \
  59. void test_##suiteName_##name_(Test&)
  60. /// Intermediate macro
  61. #define ANKI_TEST_EXPECT_EQ_IMPL(file_, line_, func_, x, y) \
  62. do { \
  63. if((x) != (y)) { \
  64. std::stringstream ss; \
  65. ss << #x << " != " << #y << " (" \
  66. << file_ << ":" << line_ << ")"; \
  67. throw std::runtime_error(ss.str()); \
  68. } \
  69. } while(0);
  70. /// Intermediate macro
  71. #define ANKI_TEST_EXPECT_NEQ_IMPL(file_, line_, func_, x, y) \
  72. do { \
  73. if((x) == (y)) { \
  74. std::stringstream ss; \
  75. ss << #x << " == " << #y << " (" \
  76. << file_ << ":" << line_ << ")"; \
  77. throw std::runtime_error(ss.str()); \
  78. } \
  79. } while(0);
  80. /// Intermediate macro
  81. #define ANKI_TEST_EXPECT_NEAR_IMPL(file_, line_, func_, x, y, epsilon_) \
  82. do { \
  83. if(fabs((x) - (y)) > (epsilon_)) { \
  84. std::stringstream ss; \
  85. ss << #x << " != " << #y << " (" \
  86. << file_ << ":" << line_ << ")"; \
  87. throw std::runtime_error(ss.str()); \
  88. } \
  89. } while(0);
  90. /// Macro to compare equal
  91. #define ANKI_TEST_EXPECT_EQ(x_, y_) \
  92. ANKI_TEST_EXPECT_EQ_IMPL(__FILE__, __LINE__, __func__, x_, y_)
  93. /// Macro to compare equal
  94. #define ANKI_TEST_EXPECT_NEQ(x_, y_) \
  95. ANKI_TEST_EXPECT_NEQ_IMPL(__FILE__, __LINE__, __func__, x_, y_)
  96. /// XXX
  97. #define ANKI_TEST_EXPECT_NEAR(x_, y_, e_) \
  98. ANKI_TEST_EXPECT_NEAR_IMPL(__FILE__, __LINE__, __func__, x_, y_, e_)
  99. } // end namespace anki
  100. #endif