Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

430 строки
13 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-2011 Sam Hocevar <sam@hocevar.net>
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the Do What The Fuck You Want To
  7. // Public License, Version 2, as published by Sam Hocevar. See
  8. // http://www.wtfpl.net/ for more details.
  9. //
  10. //
  11. // The Unit test framework
  12. // -----------------------
  13. //
  14. #if !defined __LOL_UNIT_H__
  15. #define __LOL_UNIT_H__
  16. #include <iostream>
  17. #include <iomanip>
  18. #include <sstream>
  19. #include <cstdio>
  20. namespace lol
  21. {
  22. /*
  23. * This is the base class for all fixtures. It keeps track of all
  24. * fixtures registered through the LOLUNIT_FIXTURE macro and puts them
  25. * in a linked list.
  26. */
  27. class FixtureBase
  28. {
  29. friend class TextTestRunner;
  30. public:
  31. virtual void SetUp(void) {};
  32. virtual void TearDown(void) {};
  33. protected:
  34. FixtureBase() : m_next(NULL), m_testcases(0), m_failcases(0) {}
  35. virtual ~FixtureBase() {}
  36. static void AddFixture(FixtureBase *fixture)
  37. {
  38. /* Protect against several instances of the same Fixture subclass */
  39. if (fixture->MarkFixture())
  40. return;
  41. FixtureListHelper(fixture);
  42. }
  43. static FixtureBase *FixtureList() { return FixtureListHelper(NULL); }
  44. virtual void RunFixture() = 0;
  45. virtual bool MarkFixture() = 0;
  46. /* Prevent compiler complaints about unreachable code */
  47. static inline bool True() { return true; }
  48. FixtureBase *m_next;
  49. int m_testcases, m_failcases;
  50. int m_asserts, m_failure;
  51. char const *m_fixturename, *m_currentname;
  52. std::stringstream m_errorlog, m_context;
  53. private:
  54. /* The FixtureBase class keeps track of all instanciated children
  55. * fixtures through this method. */
  56. static FixtureBase *FixtureListHelper(FixtureBase *set)
  57. {
  58. static FixtureBase *head = NULL, *tail = NULL;
  59. if (set)
  60. {
  61. if (!head) head = set;
  62. if (tail) tail->m_next = set;
  63. tail = set;
  64. }
  65. return head;
  66. }
  67. };
  68. /*
  69. * This template specialises FixtureBase and provides registration of
  70. * test cases in a linked list through the LOLUNIT_TEST macro.
  71. */
  72. template<class T> class Fixture : protected FixtureBase
  73. {
  74. public:
  75. typedef T FixtureClass;
  76. struct TestCase
  77. {
  78. void (FixtureClass::* m_fun)();
  79. char const *m_testname;
  80. TestCase *m_next;
  81. };
  82. Fixture<T>()
  83. {
  84. AddFixture(this);
  85. }
  86. /* Run all test cases in this fixture. */
  87. virtual void RunFixture()
  88. {
  89. m_errorlog.str("");
  90. m_testcases = 0;
  91. m_failcases = 0;
  92. for (TestCase *c = TestCaseList(); c; c = c->m_next)
  93. {
  94. m_testcases++;
  95. m_asserts = 0;
  96. m_failure = false;
  97. m_currentname = c->m_testname;
  98. m_context.str("");
  99. std::cout << '.';
  100. (static_cast<FixtureClass *>(this)->*c->m_fun)();
  101. if (m_failure) std::cout << 'F';
  102. }
  103. }
  104. /* Mark the current fixture type as already registered and return whether
  105. * it was seen before. */
  106. virtual bool MarkFixture()
  107. {
  108. static bool seen = false;
  109. if (seen)
  110. {
  111. SealFixture();
  112. return true;
  113. }
  114. seen = true;
  115. return false;
  116. }
  117. /* Manage Fixture sealing. Once SealFixture() has been called, we
  118. * will no longer accept TestCase registrations. */
  119. static void SealFixture() { SealFixtureHelper(true); }
  120. static bool IsFixtureSealed() { return SealFixtureHelper(false); }
  121. /* Each Fixture class specialisation keeps track of its instanciated
  122. * test cases. */
  123. static void AddTestCase(TestCase *that, char const *name,
  124. void (FixtureClass::*fun)())
  125. {
  126. if (IsFixtureSealed())
  127. return;
  128. that->m_fun = fun;
  129. that->m_testname = name;
  130. that->m_next = NULL;
  131. TestCaseListHelper(that);
  132. }
  133. static TestCase *TestCaseList() { return TestCaseListHelper(NULL); }
  134. private:
  135. static bool SealFixtureHelper(bool set)
  136. {
  137. static bool sealed = false;
  138. if (set) sealed = true;
  139. return sealed;
  140. }
  141. static TestCase *TestCaseListHelper(TestCase *set)
  142. {
  143. static TestCase *head = NULL, *tail = NULL;
  144. if (set)
  145. {
  146. if (!head) head = set;
  147. if (tail) tail->m_next = set;
  148. tail = set;
  149. }
  150. return head;
  151. }
  152. };
  153. /*
  154. * This simple class runs all automatically registered tests and reports
  155. * on error and success in the standard output.
  156. */
  157. class TextTestRunner
  158. {
  159. public:
  160. bool Run()
  161. {
  162. bool ret = true;
  163. std::stringstream errors("");
  164. int failcases = 0, testcases = 0;
  165. for (FixtureBase *f = FixtureBase::FixtureList(); f; f = f->m_next)
  166. {
  167. f->SetUp();
  168. f->RunFixture();
  169. f->TearDown();
  170. errors << f->m_errorlog.str();
  171. testcases += f->m_testcases;
  172. failcases += f->m_failcases;
  173. }
  174. std::cout << std::endl;
  175. std::cout << std::endl << std::endl;
  176. if (failcases)
  177. {
  178. std::cout << "!!!FAILURES!!!" << std::endl;
  179. std::cout << "Test Results:" << std::endl;
  180. std::cout << "Run: " << testcases
  181. << " Failures: " << failcases
  182. << " Errors: 0" << std::endl; /* TODO: handle errors */
  183. std::cout << errors.str();
  184. ret = false;
  185. }
  186. else
  187. {
  188. std::cout << "OK (" << testcases << " tests)" << std::endl;
  189. }
  190. std::cout << std::endl << std::endl;
  191. return ret;
  192. }
  193. };
  194. #define LOLUNIT_ASSERT_GENERIC(msg, cond) \
  195. do { \
  196. m_asserts++; \
  197. if (True() && !(cond)) \
  198. { \
  199. m_errorlog << std::endl << std::endl; \
  200. m_errorlog << ++m_failcases << ") test: " \
  201. << lol_unit_helper_name(this) << "::" << m_currentname \
  202. << " (F) line: " << __LINE__ << " " \
  203. << __FILE__ << std::endl; \
  204. m_errorlog << "assertion failed" << std::endl; \
  205. m_errorlog << "- Expression: " << #cond << std::endl; \
  206. m_errorlog << msg; \
  207. m_failure = true; \
  208. return; \
  209. } \
  210. } while (!True())
  211. #define LOLUNIT_ASSERT_OP(op, modifier, opdesc, msg, a, b) \
  212. do { \
  213. m_asserts++; \
  214. if (True() && !modifier((a) op (b))) \
  215. { \
  216. m_errorlog << std::endl << std::endl; \
  217. m_errorlog << ++m_failcases << ") test: " \
  218. << lol_unit_helper_name(this) << "::" << m_currentname \
  219. << " (F) line: " << __LINE__ << " " \
  220. << __FILE__ << std::endl; \
  221. m_errorlog << opdesc << " assertion failed" << std::endl; \
  222. m_errorlog << "- Expected: " << #a << " = " << (a) << std::endl; \
  223. m_errorlog << "- Actual : " << #b << " = " << (b) << std::endl; \
  224. m_errorlog << msg; \
  225. m_errorlog << m_context.str(); \
  226. m_failure = true; \
  227. return; \
  228. } \
  229. } while (!True())
  230. #define LOLUNIT_MSG(msg) \
  231. "- " << msg << std::endl
  232. #define LOLUNIT_ASSERT_DOUBLES_EQUAL_GENERIC(msg, a, b, t) \
  233. do { \
  234. m_asserts++; \
  235. using std::fabs; \
  236. if (True() && fabs((a) - (b)) > fabs((t))) \
  237. { \
  238. m_errorlog << std::endl << std::endl; \
  239. m_errorlog << ++m_failcases << ") test: " \
  240. << lol_unit_helper_name(this) << "::" << m_currentname \
  241. << " (F) line: " << __LINE__ << " " \
  242. << __FILE__ << std::endl; \
  243. m_errorlog << "double equality assertion failed" << std::endl; \
  244. std::streamsize old_prec = m_errorlog.precision(); \
  245. m_errorlog << std::setprecision(16); \
  246. m_errorlog << "- Expected: " << #a << " = " << (a) << std::endl; \
  247. m_errorlog << "- Actual : " << #b << " = " << (b) << std::endl; \
  248. m_errorlog << "- Delta : " << (t) << std::endl; \
  249. m_errorlog << std::setprecision(old_prec); \
  250. m_errorlog << msg; \
  251. m_errorlog << m_context.str(); \
  252. m_failure = true; \
  253. return; \
  254. } \
  255. } while (!True())
  256. /*
  257. * Public helper macros
  258. */
  259. #define LOLUNIT_FIXTURE(N) \
  260. class N; \
  261. /* This pattern allows us to statically create a Fixture instance \
  262. * before its exact implementation was defined. */ \
  263. template<typename T> struct lol_unit_helper_fixture_##N \
  264. { \
  265. lol_unit_helper_fixture_##N() { p = new T(); } \
  266. ~lol_unit_helper_fixture_##N() { delete p; } \
  267. T *p; \
  268. }; \
  269. lol_unit_helper_fixture_##N<N> lol_unit_helper_fixture_##N##_instance; \
  270. /* Allow to retrieve the class name without using RTTI and without \
  271. * knowing the type of "this". */ \
  272. static inline char const *lol_unit_helper_name(N *p) \
  273. { \
  274. (void)p; \
  275. return #N; \
  276. } \
  277. /* Now the user can define the implementation */ \
  278. class N : public lol::Fixture<N>
  279. #define LOLUNIT_TEST(N) \
  280. /* For each test in the fixture, we create an object that will \
  281. * automatically register the test method in a list global to the \
  282. * specialised fixture. */ \
  283. class lol_unit_helper_test_##N : public TestCase \
  284. { \
  285. public: \
  286. lol_unit_helper_test_##N() \
  287. { \
  288. AddTestCase(this, #N, \
  289. (void (FixtureClass::*)()) &FixtureClass::N); \
  290. } \
  291. }; \
  292. lol_unit_helper_test_##N lol_unit_helper_test_instance_##N; \
  293. void N()
  294. /*
  295. * Provide context for error messages
  296. */
  297. #define LOLUNIT_SET_CONTEXT(n) \
  298. do { \
  299. m_context.str(""); \
  300. m_context << "- Context : " << #n << " = " << (n) << std::endl; \
  301. } while (!True())
  302. #define LOLUNIT_UNSET_CONTEXT(n) \
  303. m_context.str("")
  304. /*
  305. * Public assert macros
  306. */
  307. #define LOLUNIT_FAIL(msg) \
  308. do { \
  309. m_asserts++; \
  310. m_errorlog << std::endl << std::endl; \
  311. m_errorlog << ++m_failcases << ") test: " \
  312. << lol_unit_helper_name(this) << "::" << m_currentname \
  313. << " (F) line: " << __LINE__ << " " \
  314. << __FILE__ << std::endl; \
  315. m_errorlog << "forced failure" << std::endl; \
  316. m_errorlog << LOLUNIT_MSG(msg); \
  317. m_errorlog << m_context.str(); \
  318. m_failure = true; \
  319. return; \
  320. } while (!True())
  321. #define LOLUNIT_ASSERT(cond) \
  322. LOLUNIT_ASSERT_GENERIC("", cond)
  323. #define LOLUNIT_ASSERT_MESSAGE(m, cond) \
  324. LOLUNIT_ASSERT_GENERIC(LOLUNIT_MSG(m), cond)
  325. #define LOLUNIT_ASSERT_EQUAL(a, b) \
  326. LOLUNIT_ASSERT_OP(==, (bool), "equality", "", a, b)
  327. #define LOLUNIT_ASSERT_EQUAL_MESSAGE(m, a, b) \
  328. LOLUNIT_ASSERT_OP(==, (bool), "equality", LOLUNIT_MSG(m), a, b)
  329. #define LOLUNIT_ASSERT_DIFFERENT(a, b) \
  330. LOLUNIT_ASSERT_OP(!=, (bool), "inequality", "", a, b)
  331. #define LOLUNIT_ASSERT_DIFFERENT_MESSAGE(m, a, b) \
  332. LOLUNIT_ASSERT_OP(!=, (bool), "inequality", LOLUNIT_MSG(m), a, b)
  333. #define LOLUNIT_ASSERT_LESS(a, b) \
  334. LOLUNIT_ASSERT_OP(<, (bool), "less than", "", a, b)
  335. #define LOLUNIT_ASSERT_LESS_MESSAGE(m, a, b) \
  336. LOLUNIT_ASSERT_OP(<, (bool), "less than", LOLUNIT_MSG(m), a, b)
  337. #define LOLUNIT_ASSERT_LEQUAL(a, b) \
  338. LOLUNIT_ASSERT_OP(<=, (bool), "less than or equal", "", a, b)
  339. #define LOLUNIT_ASSERT_LEQUAL_MESSAGE(m, a, b) \
  340. LOLUNIT_ASSERT_OP(<=, (bool), "less than or equal", LOLUNIT_MSG(m), a, b)
  341. #define LOLUNIT_ASSERT_GREATER(a, b) \
  342. LOLUNIT_ASSERT_OP(>, (bool), "greater than", "", a, b)
  343. #define LOLUNIT_ASSERT_GREATER_MESSAGE(m, a, b) \
  344. LOLUNIT_ASSERT_OP(>, (bool), "greater than", LOLUNIT_MSG(m), a, b)
  345. #define LOLUNIT_ASSERT_GEQUAL(a, b) \
  346. LOLUNIT_ASSERT_OP(>=, (bool), "greater than or equal", "", a, b)
  347. #define LOLUNIT_ASSERT_GEQUAL_MESSAGE(m, a, b) \
  348. LOLUNIT_ASSERT_OP(>=, (bool), "greater than or equal", LOLUNIT_MSG(m), a, b)
  349. #define LOLUNIT_ASSERT_NOT_EQUAL(a, b) \
  350. LOLUNIT_ASSERT_OP(==, !, "not equality", "", a, b)
  351. #define LOLUNIT_ASSERT_NOT_EQUAL_MESSAGE(m, a, b) \
  352. LOLUNIT_ASSERT_OP(==, !, "not equality", LOLUNIT_MSG(m), a, b)
  353. #define LOLUNIT_ASSERT_NOT_DIFFERENT(a, b) \
  354. LOLUNIT_ASSERT_OP(!=, !, "not inequality", "", a, b)
  355. #define LOLUNIT_ASSERT_NOT_DIFFERENT_MESSAGE(m, a, b) \
  356. LOLUNIT_ASSERT_OP(!=, !, "not inequality", LOLUNIT_MSG(m), a, b)
  357. #define LOLUNIT_ASSERT_NOT_LESS(a, b) \
  358. LOLUNIT_ASSERT_OP(<, !, "not less than", "", a, b)
  359. #define LOLUNIT_ASSERT_NOT_LESS_MESSAGE(m, a, b) \
  360. LOLUNIT_ASSERT_OP(<, !, "not less than", LOLUNIT_MSG(m), a, b)
  361. #define LOLUNIT_ASSERT_NOT_LEQUAL(a, b) \
  362. LOLUNIT_ASSERT_OP(<=, !, "not less than or equal", "", a, b)
  363. #define LOLUNIT_ASSERT_NOT_LEQUAL_MESSAGE(m, a, b) \
  364. LOLUNIT_ASSERT_OP(<=, !, "not less than or equal", LOLUNIT_MSG(m), a, b)
  365. #define LOLUNIT_ASSERT_NOT_GREATER(a, b) \
  366. LOLUNIT_ASSERT_OP(>, !, "not greater than", "", a, b)
  367. #define LOLUNIT_ASSERT_NOT_GREATER_MESSAGE(m, a, b) \
  368. LOLUNIT_ASSERT_OP(>, !, "not greater than", LOLUNIT_MSG(m), a, b)
  369. #define LOLUNIT_ASSERT_NOT_GEQUAL(a, b) \
  370. LOLUNIT_ASSERT_OP(>=, !, "not greater than or equal", "", a, b)
  371. #define LOLUNIT_ASSERT_NOT_GEQUAL_MESSAGE(m, a, b) \
  372. LOLUNIT_ASSERT_OP(>=, !, "not greater than or equal", LOLUNIT_MSG(m), a, b)
  373. #define LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, t) \
  374. LOLUNIT_ASSERT_DOUBLES_EQUAL_GENERIC("", a, b, t)
  375. #define LOLUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(msg, a, b, t) \
  376. LOLUNIT_ASSERT_DOUBLES_EQUAL_GENERIC(LOLUNIT_MSG(msg), a, b, t)
  377. } /* namespace lol */
  378. #endif // __LOL_UNIT_H__