You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

440 lines
14 KiB

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