rapid-cxx-test.h 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866
  1. #ifndef RAPID_CXX_TEST_H
  2. #define RAPID_CXX_TEST_H
  3. # include <cstddef>
  4. # include <cstdlib>
  5. # include <cstdio>
  6. # include <cstring>
  7. # include <cassert>
  8. #include "test_macros.h"
  9. #if !defined(RAPID_CXX_TEST_NO_SYSTEM_HEADER) || !defined(__GNUC__)
  10. #pragma GCC system_header
  11. #endif
  12. # define RAPID_CXX_TEST_PP_CAT(x, y) RAPID_CXX_TEST_PP_CAT_2(x, y)
  13. # define RAPID_CXX_TEST_PP_CAT_2(x, y) x##y
  14. # define RAPID_CXX_TEST_PP_STR(...) RAPID_CXX_TEST_PP_STR_2(__VA_ARGS__)
  15. # define RAPID_CXX_TEST_PP_STR_2(...) #__VA_ARGS__
  16. # if defined(__GNUC__)
  17. # define TEST_FUNC_NAME() __PRETTY_FUNCTION__
  18. # define RAPID_CXX_TEST_UNUSED __attribute__((unused))
  19. # else
  20. # define TEST_FUNC_NAME() __func__
  21. # define RAPID_CXX_TEST_UNUSED
  22. # endif
  23. ////////////////////////////////////////////////////////////////////////////////
  24. // TEST_SUITE
  25. ////////////////////////////////////////////////////////////////////////////////
  26. # define TEST_SUITE(Name) \
  27. namespace Name \
  28. { \
  29. inline ::rapid_cxx_test::test_suite & get_test_suite() \
  30. { \
  31. static ::rapid_cxx_test::test_suite m_suite(#Name); \
  32. return m_suite; \
  33. } \
  34. \
  35. inline int unit_test_main(int, char**) \
  36. { \
  37. ::rapid_cxx_test::test_runner runner(get_test_suite()); \
  38. return runner.run(); \
  39. } \
  40. } \
  41. int main(int argc, char **argv) \
  42. { \
  43. return Name::unit_test_main(argc, argv); \
  44. } \
  45. namespace Name \
  46. { /* namespace closed in TEST_SUITE_END */
  47. #
  48. ////////////////////////////////////////////////////////////////////////////////
  49. // TEST_SUITE_END
  50. ////////////////////////////////////////////////////////////////////////////////
  51. # define TEST_SUITE_END() \
  52. } /* namespace opened in TEST_SUITE(...) */
  53. #
  54. ////////////////////////////////////////////////////////////////////////////////
  55. // TEST_CASE
  56. ////////////////////////////////////////////////////////////////////////////////
  57. # if !defined(__clang__)
  58. #
  59. # define TEST_CASE(Name) \
  60. void Name(); \
  61. static void RAPID_CXX_TEST_PP_CAT(Name, _invoker)() \
  62. { \
  63. Name(); \
  64. } \
  65. static ::rapid_cxx_test::registrar \
  66. RAPID_CXX_TEST_PP_CAT(rapid_cxx_test_registrar_, Name)( \
  67. get_test_suite() \
  68. , ::rapid_cxx_test::test_case(__FILE__, #Name, __LINE__, & RAPID_CXX_TEST_PP_CAT(Name, _invoker)) \
  69. ); \
  70. void Name()
  71. #
  72. # else /* __clang__ */
  73. #
  74. # define TEST_CASE(Name) \
  75. void Name(); \
  76. static void RAPID_CXX_TEST_PP_CAT(Name, _invoker)() \
  77. { \
  78. Name(); \
  79. } \
  80. _Pragma("clang diagnostic push") \
  81. _Pragma("clang diagnostic ignored \"-Wglobal-constructors\"") \
  82. static ::rapid_cxx_test::registrar \
  83. RAPID_CXX_TEST_PP_CAT(rapid_cxx_test_registrar_, Name)( \
  84. get_test_suite() \
  85. , ::rapid_cxx_test::test_case(__FILE__, #Name, __LINE__, & RAPID_CXX_TEST_PP_CAT(Name, _invoker)) \
  86. ); \
  87. _Pragma("clang diagnostic pop") \
  88. void Name()
  89. #
  90. # endif /* !defined(__clang__) */
  91. # define TEST_SET_CHECKPOINT() ::rapid_cxx_test::set_checkpoint(__FILE__, TEST_FUNC_NAME(), __LINE__)
  92. #define RAPID_CXX_TEST_OUTCOME()
  93. ////////////////////////////////////////////////////////////////////////////////
  94. // TEST_UNSUPPORTED
  95. ////////////////////////////////////////////////////////////////////////////////
  96. # define TEST_UNSUPPORTED() \
  97. do { \
  98. TEST_SET_CHECKPOINT(); \
  99. ::rapid_cxx_test::test_outcome m_f( \
  100. ::rapid_cxx_test::failure_type::unsupported, __FILE__, TEST_FUNC_NAME(), __LINE__ \
  101. , "", "" \
  102. ); \
  103. ::rapid_cxx_test::get_reporter().report(m_f); \
  104. return; \
  105. } while (false)
  106. #
  107. ////////////////////////////////////////////////////////////////////////////////
  108. // BASIC ASSERTIONS
  109. ////////////////////////////////////////////////////////////////////////////////
  110. # define TEST_WARN(...) \
  111. do { \
  112. TEST_SET_CHECKPOINT(); \
  113. ::rapid_cxx_test::test_outcome m_f( \
  114. ::rapid_cxx_test::failure_type::none, __FILE__, TEST_FUNC_NAME(), __LINE__ \
  115. , "TEST_WARN(" #__VA_ARGS__ ")", "" \
  116. ); \
  117. if (not (__VA_ARGS__)) { \
  118. m_f.type = ::rapid_cxx_test::failure_type::warn; \
  119. } \
  120. ::rapid_cxx_test::get_reporter().report(m_f); \
  121. } while (false)
  122. #
  123. # define TEST_CHECK(...) \
  124. do { \
  125. TEST_SET_CHECKPOINT(); \
  126. ::rapid_cxx_test::test_outcome m_f( \
  127. ::rapid_cxx_test::failure_type::none, __FILE__, TEST_FUNC_NAME(), __LINE__ \
  128. , "TEST_CHECK(" #__VA_ARGS__ ")", "" \
  129. ); \
  130. if (not (__VA_ARGS__)) { \
  131. m_f.type = ::rapid_cxx_test::failure_type::check; \
  132. } \
  133. ::rapid_cxx_test::get_reporter().report(m_f); \
  134. } while (false)
  135. #
  136. # define TEST_REQUIRE(...) \
  137. do { \
  138. TEST_SET_CHECKPOINT(); \
  139. ::rapid_cxx_test::test_outcome m_f( \
  140. ::rapid_cxx_test::failure_type::none, __FILE__, TEST_FUNC_NAME(), __LINE__ \
  141. , "TEST_REQUIRE(" #__VA_ARGS__ ")", "" \
  142. ); \
  143. if (not (__VA_ARGS__)) { \
  144. m_f.type = ::rapid_cxx_test::failure_type::require; \
  145. } \
  146. ::rapid_cxx_test::get_reporter().report(m_f); \
  147. if (m_f.type != ::rapid_cxx_test::failure_type::none) { \
  148. return; \
  149. } \
  150. } while (false)
  151. #
  152. # define TEST_ASSERT(...) \
  153. do { \
  154. TEST_SET_CHECKPOINT(); \
  155. ::rapid_cxx_test::test_outcome m_f( \
  156. ::rapid_cxx_test::failure_type::none, __FILE__, TEST_FUNC_NAME(), __LINE__ \
  157. , "TEST_ASSERT(" #__VA_ARGS__ ")", "" \
  158. ); \
  159. if (not (__VA_ARGS__)) { \
  160. m_f.type = ::rapid_cxx_test::failure_type::assert; \
  161. } \
  162. ::rapid_cxx_test::get_reporter().report(m_f); \
  163. if (m_f.type != ::rapid_cxx_test::failure_type::none) { \
  164. std::abort(); \
  165. } \
  166. } while (false)
  167. #
  168. ////////////////////////////////////////////////////////////////////////////////
  169. // TEST_CHECK_NO_THROW / TEST_CHECK_THROW
  170. ////////////////////////////////////////////////////////////////////////////////
  171. #ifndef TEST_HAS_NO_EXCEPTIONS
  172. # define TEST_CHECK_NO_THROW(...) \
  173. do { \
  174. TEST_SET_CHECKPOINT(); \
  175. ::rapid_cxx_test::test_outcome m_f( \
  176. ::rapid_cxx_test::failure_type::none, __FILE__, TEST_FUNC_NAME(), __LINE__ \
  177. , "TEST_CHECK_NO_THROW(" #__VA_ARGS__ ")", "" \
  178. ); \
  179. try { \
  180. (static_cast<void>(__VA_ARGS__)); \
  181. } catch (...) { \
  182. m_f.type = ::rapid_cxx_test::failure_type::check; \
  183. } \
  184. ::rapid_cxx_test::get_reporter().report(m_f); \
  185. } while (false)
  186. #
  187. # define TEST_CHECK_THROW(Except, ...) \
  188. do { \
  189. TEST_SET_CHECKPOINT(); \
  190. ::rapid_cxx_test::test_outcome m_f( \
  191. ::rapid_cxx_test::failure_type::none, __FILE__, TEST_FUNC_NAME(), __LINE__ \
  192. , "TEST_CHECK_THROW(" #Except "," #__VA_ARGS__ ")", "" \
  193. ); \
  194. try { \
  195. (static_cast<void>(__VA_ARGS__)); \
  196. m_f.type = ::rapid_cxx_test::failure_type::check; \
  197. } catch (Except const &) {} \
  198. ::rapid_cxx_test::get_reporter().report(m_f); \
  199. } while (false)
  200. #
  201. #define TEST_CHECK_THROW_RESULT(Except, Checker, ...) \
  202. do { \
  203. TEST_SET_CHECKPOINT(); \
  204. ::rapid_cxx_test::test_outcome m_f(::rapid_cxx_test::failure_type::none, \
  205. __FILE__, TEST_FUNC_NAME(), __LINE__, \
  206. "TEST_CHECK_THROW_RESULT(" #Except \
  207. "," #Checker "," #__VA_ARGS__ ")", \
  208. ""); \
  209. try { \
  210. (static_cast<void>(__VA_ARGS__)); \
  211. m_f.type = ::rapid_cxx_test::failure_type::check; \
  212. } catch (Except const& Caught) { \
  213. Checker(Caught); \
  214. } \
  215. ::rapid_cxx_test::get_reporter().report(m_f); \
  216. } while (false)
  217. #
  218. #else // TEST_HAS_NO_EXCEPTIONS
  219. # define TEST_CHECK_NO_THROW(...) \
  220. do { \
  221. TEST_SET_CHECKPOINT(); \
  222. ::rapid_cxx_test::test_outcome m_f( \
  223. ::rapid_cxx_test::failure_type::none, __FILE__, TEST_FUNC_NAME(), __LINE__ \
  224. , "TEST_CHECK_NO_THROW(" #__VA_ARGS__ ")", "" \
  225. ); \
  226. (static_cast<void>(__VA_ARGS__)); \
  227. ::rapid_cxx_test::get_reporter().report(m_f); \
  228. } while (false)
  229. #
  230. #define TEST_CHECK_THROW(Except, ...) ((void)0)
  231. #define TEST_CHECK_THROW_RESULT(Except, Checker, ...) ((void)0)
  232. #endif // TEST_HAS_NO_EXCEPTIONS
  233. ////////////////////////////////////////////////////////////////////////////////
  234. // TEST_REQUIRE_NO_THROW / TEST_REQUIRE_THROWs
  235. ////////////////////////////////////////////////////////////////////////////////
  236. #ifndef TEST_HAS_NO_EXCEPTIONS
  237. # define TEST_REQUIRE_NO_THROW(...) \
  238. do { \
  239. TEST_SET_CHECKPOINT(); \
  240. ::rapid_cxx_test::test_outcome m_f( \
  241. ::rapid_cxx_test::failure_type::none, __FILE__, TEST_FUNC_NAME(), __LINE__ \
  242. , "TEST_REQUIRE_NO_THROW(" #__VA_ARGS__ ")", "" \
  243. ); \
  244. try { \
  245. (static_cast<void>(__VA_ARGS__)); \
  246. } catch (...) { \
  247. m_f.type = ::rapid_cxx_test::failure_type::require; \
  248. } \
  249. ::rapid_cxx_test::get_reporter().report(m_f); \
  250. if (m_f.type != ::rapid_cxx_test::failure_type::none) { \
  251. return; \
  252. } \
  253. } while (false)
  254. #
  255. # define TEST_REQUIRE_THROW(Except, ...) \
  256. do { \
  257. TEST_SET_CHECKPOINT(); \
  258. ::rapid_cxx_test::test_outcome m_f( \
  259. ::rapid_cxx_test::failure_type::none, __FILE__, TEST_FUNC_NAME(), __LINE__ \
  260. , "TEST_REQUIRE_THROW(" #Except "," #__VA_ARGS__ ")", "" \
  261. ); \
  262. try { \
  263. (static_cast<void>(__VA_ARGS__)); \
  264. m_f.type = ::rapid_cxx_test::failure_type::require; \
  265. } catch (Except const &) {} \
  266. ::rapid_cxx_test::get_reporter().report(m_f); \
  267. if (m_f.type != ::rapid_cxx_test::failure_type::none) { \
  268. return; \
  269. } \
  270. } while (false)
  271. #
  272. #else // TEST_HAS_NO_EXCEPTIONS
  273. # define TEST_REQUIRE_NO_THROW(...) \
  274. do { \
  275. TEST_SET_CHECKPOINT(); \
  276. ::rapid_cxx_test::test_outcome m_f( \
  277. ::rapid_cxx_test::failure_type::none, __FILE__, TEST_FUNC_NAME(), __LINE__ \
  278. , "TEST_REQUIRE_NO_THROW(" #__VA_ARGS__ ")", "" \
  279. ); \
  280. (static_cast<void>(__VA_ARGS__)); \
  281. ::rapid_cxx_test::get_reporter().report(m_f); \
  282. } while (false)
  283. #
  284. #define TEST_REQUIRE_THROW(Except, ...) ((void)0)
  285. #endif // TEST_HAS_NO_EXCEPTIONS
  286. ////////////////////////////////////////////////////////////////////////////////
  287. // TEST_ASSERT_NO_THROW / TEST_ASSERT_THROW
  288. ////////////////////////////////////////////////////////////////////////////////
  289. #ifndef TEST_HAS_NO_EXCEPTIONS
  290. # define TEST_ASSERT_NO_THROW(...) \
  291. do { \
  292. TEST_SET_CHECKPOINT(); \
  293. ::rapid_cxx_test::test_outcome m_f( \
  294. ::rapid_cxx_test::failure_type::none, __FILE__, TEST_FUNC_NAME(), __LINE__ \
  295. , "TEST_ASSERT_NO_THROW(" #__VA_ARGS__ ")", "" \
  296. ); \
  297. try { \
  298. (static_cast<void>(__VA_ARGS__)); \
  299. } catch (...) { \
  300. m_f.type = ::rapid_cxx_test::failure_type::assert; \
  301. } \
  302. ::rapid_cxx_test::get_reporter().report(m_f); \
  303. if (m_f.type != ::rapid_cxx_test::failure_type::none) { \
  304. std::abort(); \
  305. } \
  306. } while (false)
  307. #
  308. # define TEST_ASSERT_THROW(Except, ...) \
  309. do { \
  310. TEST_SET_CHECKPOINT(); \
  311. ::rapid_cxx_test::test_outcome m_f( \
  312. ::rapid_cxx_test::failure_type::none, __FILE__, TEST_FUNC_NAME(), __LINE__ \
  313. , "TEST_ASSERT_THROW(" #Except "," #__VA_ARGS__ ")", "" \
  314. ); \
  315. try { \
  316. (static_cast<void>(__VA_ARGS__)); \
  317. m_f.type = ::rapid_cxx_test::failure_type::assert; \
  318. } catch (Except const &) {} \
  319. ::rapid_cxx_test::get_reporter().report(m_f); \
  320. if (m_f.type != ::rapid_cxx_test::failure_type::none) { \
  321. std::abort(); \
  322. } \
  323. } while (false)
  324. #
  325. #else // TEST_HAS_NO_EXCEPTIONS
  326. # define TEST_ASSERT_NO_THROW(...) \
  327. do { \
  328. TEST_SET_CHECKPOINT(); \
  329. ::rapid_cxx_test::test_outcome m_f( \
  330. ::rapid_cxx_test::failure_type::none, __FILE__, TEST_FUNC_NAME(), __LINE__ \
  331. , "TEST_ASSERT_NO_THROW(" #__VA_ARGS__ ")", "" \
  332. ); \
  333. (static_cast<void>(__VA_ARGS__)); \
  334. ::rapid_cxx_test::get_reporter().report(m_f); \
  335. } while (false)
  336. #
  337. #define TEST_ASSERT_THROW(Except, ...) ((void)0)
  338. #endif // TEST_HAS_NO_EXCEPTIONS
  339. ////////////////////////////////////////////////////////////////////////////////
  340. //
  341. ////////////////////////////////////////////////////////////////////////////////
  342. # define TEST_WARN_EQUAL_COLLECTIONS(...) \
  343. do { \
  344. TEST_SET_CHECKPOINT(); \
  345. ::rapid_cxx_test::test_outcome m_f( \
  346. ::rapid_cxx_test::failure_type::none, __FILE__, TEST_FUNC_NAME(), __LINE__ \
  347. , "TEST_WARN_EQUAL_COLLECTIONS(" #__VA_ARGS__ ")", "" \
  348. ); \
  349. if (not ::rapid_cxx_test::detail::check_equal_collections_impl(__VA_ARGS__)) { \
  350. m_f.type = ::rapid_cxx_test::failure_type::warn; \
  351. } \
  352. ::rapid_cxx_test::get_reporter().report(m_f); \
  353. } while (false)
  354. #
  355. # define TEST_CHECK_EQUAL_COLLECTIONS(...) \
  356. do { \
  357. TEST_SET_CHECKPOINT(); \
  358. ::rapid_cxx_test::test_outcome m_f( \
  359. ::rapid_cxx_test::failure_type::none, __FILE__, TEST_FUNC_NAME(), __LINE__ \
  360. , "TEST_CHECK_EQUAL_COLLECTIONS(" #__VA_ARGS__ ")", "" \
  361. ); \
  362. if (not ::rapid_cxx_test::detail::check_equal_collections_impl(__VA_ARGS__)) { \
  363. m_f.type = ::rapid_cxx_test::failure_type::check; \
  364. } \
  365. ::rapid_cxx_test::get_reporter().report(m_f); \
  366. } while (false)
  367. #
  368. # define TEST_REQUIRE_EQUAL_COLLECTIONS(...) \
  369. do { \
  370. TEST_SET_CHECKPOINT(); \
  371. ::rapid_cxx_test::test_outcome m_f( \
  372. ::rapid_cxx_test::failure_type::none, __FILE__, TEST_FUNC_NAME(), __LINE__ \
  373. , "TEST_REQUIRE_EQUAL_COLLECTIONS(" #__VA_ARGS__ ")", "" \
  374. ); \
  375. if (not ::rapid_cxx_test::detail::check_equal_collections_impl(__VA_ARGS__)) { \
  376. m_f.type = ::rapid_cxx_test::failure_type::require; \
  377. } \
  378. ::rapid_cxx_test::get_reporter().report(m_f); \
  379. if (m_f.type != ::rapid_cxx_test::failure_type::none) { \
  380. return; \
  381. } \
  382. } while (false)
  383. #
  384. # define TEST_ASSERT_EQUAL_COLLECTIONS(...) \
  385. do { \
  386. TEST_SET_CHECKPOINT(); \
  387. ::rapid_cxx_test::test_outcome m_f( \
  388. ::rapid_cxx_test::failure_type::none, __FILE__, TEST_FUNC_NAME(), __LINE__ \
  389. , "TEST_ASSERT_EQUAL_COLLECTIONS(" #__VA_ARGS__ ")", "" \
  390. ); \
  391. if (not ::rapid_cxx_test::detail::check_equal_collections_impl(__VA_ARGS__)) { \
  392. m_f.type = ::rapid_cxx_test::failure_type::assert; \
  393. } \
  394. ::rapid_cxx_test::get_reporter().report(m_f); \
  395. if (m_f.type != ::rapid_cxx_test::failure_type::none) { \
  396. ::std::abort(); \
  397. } \
  398. } while (false)
  399. #
  400. namespace rapid_cxx_test
  401. {
  402. typedef void (*invoker_t)();
  403. ////////////////////////////////////////////////////////////////////////////
  404. struct test_case
  405. {
  406. test_case()
  407. : file(""), func(""), line(0), invoke(NULL)
  408. {}
  409. test_case(const char* file1, const char* func1, std::size_t line1,
  410. invoker_t invoke1)
  411. : file(file1), func(func1), line(line1), invoke(invoke1)
  412. {}
  413. const char *file;
  414. const char *func;
  415. std::size_t line;
  416. invoker_t invoke;
  417. };
  418. ////////////////////////////////////////////////////////////////////////////
  419. struct failure_type
  420. {
  421. enum enum_type {
  422. none,
  423. unsupported,
  424. warn,
  425. check,
  426. require,
  427. assert,
  428. uncaught_exception
  429. };
  430. };
  431. typedef failure_type::enum_type failure_type_t;
  432. ////////////////////////////////////////////////////////////////////////////
  433. struct test_outcome
  434. {
  435. test_outcome()
  436. : type(failure_type::none),
  437. file(""), func(""), line(0),
  438. expression(""), message("")
  439. {}
  440. test_outcome(failure_type_t type1, const char* file1, const char* func1,
  441. std::size_t line1, const char* expression1,
  442. const char* message1)
  443. : type(type1), file(file1), func(func1), line(line1),
  444. expression(expression1), message(message1)
  445. {
  446. trim_func_string();
  447. }
  448. failure_type_t type;
  449. const char *file;
  450. const char *func;
  451. std::size_t line;
  452. const char *expression;
  453. const char *message;
  454. private:
  455. void trim_file_string() {
  456. const char* f_start = file;
  457. const char* prev_start = f_start;
  458. const char* last_start = f_start;
  459. char last;
  460. while ((last = *f_start) != '\0') {
  461. ++f_start;
  462. if (last == '/' && *f_start) {
  463. prev_start = last_start;
  464. last_start = f_start;
  465. }
  466. }
  467. file = prev_start;
  468. }
  469. void trim_func_string() {
  470. const char* void_loc = ::strstr(func, "void ");
  471. if (void_loc == func) {
  472. func += strlen("void ");
  473. }
  474. const char* namespace_loc = ::strstr(func, "::");
  475. if (namespace_loc) {
  476. func = namespace_loc + 2;
  477. }
  478. }
  479. };
  480. ////////////////////////////////////////////////////////////////////////////
  481. struct checkpoint
  482. {
  483. const char *file;
  484. const char *func;
  485. std::size_t line;
  486. };
  487. namespace detail
  488. {
  489. inline checkpoint & global_checkpoint()
  490. {
  491. static checkpoint cp = {"", "", 0};
  492. return cp;
  493. }
  494. }
  495. ////////////////////////////////////////////////////////////////////////////
  496. inline void set_checkpoint(const char* file, const char* func, std::size_t line)
  497. {
  498. checkpoint& cp = detail::global_checkpoint();
  499. cp.file = file;
  500. cp.func = func;
  501. cp.line = line;
  502. }
  503. ////////////////////////////////////////////////////////////////////////////
  504. inline checkpoint const & get_checkpoint()
  505. {
  506. return detail::global_checkpoint();
  507. }
  508. ////////////////////////////////////////////////////////////////////////////
  509. class test_suite
  510. {
  511. public:
  512. typedef test_case const* iterator;
  513. typedef iterator const_iterator;
  514. public:
  515. test_suite(const char *xname)
  516. : m_name(xname), m_tests(), m_size(0)
  517. {
  518. assert(xname);
  519. }
  520. public:
  521. const char *name() const { return m_name; }
  522. std::size_t size() const { return m_size; }
  523. test_case const & operator[](std::size_t i) const
  524. {
  525. assert(i < m_size);
  526. return m_tests[i];
  527. }
  528. const_iterator begin() const
  529. { return m_tests; }
  530. const_iterator end() const
  531. {
  532. return m_tests + m_size;
  533. }
  534. public:
  535. std::size_t register_test(test_case tc)
  536. {
  537. static std::size_t test_case_max = sizeof(m_tests) / sizeof(test_case);
  538. assert(m_size < test_case_max);
  539. m_tests[m_size] = tc;
  540. return m_size++;
  541. }
  542. private:
  543. test_suite(test_suite const &);
  544. test_suite & operator=(test_suite const &);
  545. private:
  546. const char* m_name;
  547. // Since fast compile times in a priority, we use simple containers
  548. // with hard limits.
  549. test_case m_tests[256];
  550. std::size_t m_size;
  551. };
  552. ////////////////////////////////////////////////////////////////////////////
  553. class registrar
  554. {
  555. public:
  556. registrar(test_suite & st, test_case tc)
  557. {
  558. st.register_test(tc);
  559. }
  560. };
  561. ////////////////////////////////////////////////////////////////////////////
  562. class test_reporter
  563. {
  564. public:
  565. test_reporter()
  566. : m_testcases(0), m_testcase_failures(0), m_unsupported(0),
  567. m_assertions(0), m_warning_failures(0), m_check_failures(0),
  568. m_require_failures(0), m_uncaught_exceptions(0), m_failure()
  569. {
  570. }
  571. void test_case_begin()
  572. {
  573. ++m_testcases;
  574. clear_failure();
  575. }
  576. void test_case_end()
  577. {
  578. if (m_failure.type != failure_type::none
  579. && m_failure.type != failure_type::unsupported) {
  580. ++m_testcase_failures;
  581. }
  582. }
  583. # if defined(__GNUC__)
  584. # pragma GCC diagnostic push
  585. # pragma GCC diagnostic ignored "-Wswitch-default"
  586. # endif
  587. // Each assertion and failure is reported through this function.
  588. void report(test_outcome o)
  589. {
  590. ++m_assertions;
  591. switch (o.type)
  592. {
  593. case failure_type::none:
  594. break;
  595. case failure_type::unsupported:
  596. ++m_unsupported;
  597. m_failure = o;
  598. break;
  599. case failure_type::warn:
  600. ++m_warning_failures;
  601. report_error(o);
  602. break;
  603. case failure_type::check:
  604. ++m_check_failures;
  605. report_error(o);
  606. m_failure = o;
  607. break;
  608. case failure_type::require:
  609. ++m_require_failures;
  610. report_error(o);
  611. m_failure = o;
  612. break;
  613. case failure_type::assert:
  614. report_error(o);
  615. break;
  616. case failure_type::uncaught_exception:
  617. ++m_uncaught_exceptions;
  618. std::fprintf(stderr
  619. , "Test case FAILED with uncaught exception:\n"
  620. " last checkpoint near %s::%lu %s\n\n"
  621. , o.file, o.line, o.func
  622. );
  623. m_failure = o;
  624. break;
  625. }
  626. }
  627. # if defined(__GNUC__)
  628. # pragma GCC diagnostic pop
  629. # endif
  630. test_outcome current_failure() const
  631. {
  632. return m_failure;
  633. }
  634. void clear_failure()
  635. {
  636. m_failure.type = failure_type::none;
  637. m_failure.file = "";
  638. m_failure.func = "";
  639. m_failure.line = 0;
  640. m_failure.expression = "";
  641. m_failure.message = "";
  642. }
  643. std::size_t test_case_count() const
  644. { return m_testcases; }
  645. std::size_t test_case_failure_count() const
  646. { return m_testcase_failures; }
  647. std::size_t unsupported_count() const
  648. { return m_unsupported; }
  649. std::size_t assertion_count() const
  650. { return m_assertions; }
  651. std::size_t warning_failure_count() const
  652. { return m_warning_failures; }
  653. std::size_t check_failure_count() const
  654. { return m_check_failures; }
  655. std::size_t require_failure_count() const
  656. { return m_require_failures; }
  657. std::size_t failure_count() const
  658. { return m_check_failures + m_require_failures + m_uncaught_exceptions; }
  659. // Print a summary of what was run and the outcome.
  660. void print_summary(const char* suitename) const
  661. {
  662. FILE* out = failure_count() ? stderr : stdout;
  663. std::size_t testcases_run = m_testcases - m_unsupported;
  664. std::fprintf(out, "Summary for testsuite %s:\n", suitename);
  665. std::fprintf(out, " %lu of %lu test cases passed.\n", testcases_run - m_testcase_failures, testcases_run);
  666. std::fprintf(out, " %lu of %lu assertions passed.\n", m_assertions - (m_warning_failures + m_check_failures + m_require_failures), m_assertions);
  667. std::fprintf(out, " %lu unsupported test case%s.\n", m_unsupported, (m_unsupported != 1 ? "s" : ""));
  668. }
  669. private:
  670. test_reporter(test_reporter const &);
  671. test_reporter const & operator=(test_reporter const &);
  672. void report_error(test_outcome o) const
  673. {
  674. std::fprintf(stderr, "In %s:%lu Assertion %s failed.\n in file: %s\n %s\n"
  675. , o.func, o.line, o.expression, o.file, o.message ? o.message : ""
  676. );
  677. }
  678. private:
  679. // counts of testcases, failed testcases, and unsupported testcases.
  680. std::size_t m_testcases;
  681. std::size_t m_testcase_failures;
  682. std::size_t m_unsupported;
  683. // counts of assertions and assertion failures.
  684. std::size_t m_assertions;
  685. std::size_t m_warning_failures;
  686. std::size_t m_check_failures;
  687. std::size_t m_require_failures;
  688. std::size_t m_uncaught_exceptions;
  689. // The last failure. This is cleared between testcases.
  690. test_outcome m_failure;
  691. };
  692. ////////////////////////////////////////////////////////////////////////////
  693. inline test_reporter & get_reporter()
  694. {
  695. static test_reporter o;
  696. return o;
  697. }
  698. ////////////////////////////////////////////////////////////////////////////
  699. class test_runner
  700. {
  701. public:
  702. test_runner(test_suite & ts)
  703. : m_ts(ts)
  704. {}
  705. public:
  706. int run()
  707. {
  708. // for each testcase
  709. for (test_suite::const_iterator b = m_ts.begin(), e = m_ts.end();
  710. b != e; ++b)
  711. {
  712. test_case const& tc = *b;
  713. set_checkpoint(tc.file, tc.func, tc.line);
  714. get_reporter().test_case_begin();
  715. #ifndef TEST_HAS_NO_EXCEPTIONS
  716. try {
  717. #endif
  718. tc.invoke();
  719. #ifndef TEST_HAS_NO_EXCEPTIONS
  720. } catch (...) {
  721. test_outcome o;
  722. o.type = failure_type::uncaught_exception;
  723. o.file = get_checkpoint().file;
  724. o.func = get_checkpoint().func;
  725. o.line = get_checkpoint().line;
  726. o.expression = "";
  727. o.message = "";
  728. get_reporter().report(o);
  729. }
  730. #endif
  731. get_reporter().test_case_end();
  732. }
  733. auto exit_code = get_reporter().failure_count() ? EXIT_FAILURE : EXIT_SUCCESS;
  734. if (exit_code == EXIT_FAILURE || get_reporter().unsupported_count())
  735. get_reporter().print_summary(m_ts.name());
  736. return exit_code;
  737. }
  738. private:
  739. test_runner(test_runner const &);
  740. test_runner operator=(test_runner const &);
  741. test_suite & m_ts;
  742. };
  743. namespace detail
  744. {
  745. template <class Iter1, class Iter2>
  746. bool check_equal_collections_impl(
  747. Iter1 start1, Iter1 const end1
  748. , Iter2 start2, Iter2 const end2
  749. )
  750. {
  751. while (start1 != end1 && start2 != end2) {
  752. if (*start1 != *start2) {
  753. return false;
  754. }
  755. ++start1; ++start2;
  756. }
  757. return (start1 == end1 && start2 == end2);
  758. }
  759. } // namespace detail
  760. } // namespace rapid_cxx_test
  761. # if defined(__GNUC__)
  762. # pragma GCC diagnostic pop
  763. # endif
  764. #endif /* RAPID_CXX_TEST_H */