Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 

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