25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

453 lines
14 KiB

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