123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866 |
- #ifndef RAPID_CXX_TEST_H
- #define RAPID_CXX_TEST_H
- # include <cstddef>
- # include <cstdlib>
- # include <cstdio>
- # include <cstring>
- # include <cassert>
- #include "test_macros.h"
- #if !defined(RAPID_CXX_TEST_NO_SYSTEM_HEADER) || !defined(__GNUC__)
- #pragma GCC system_header
- #endif
- # define RAPID_CXX_TEST_PP_CAT(x, y) RAPID_CXX_TEST_PP_CAT_2(x, y)
- # define RAPID_CXX_TEST_PP_CAT_2(x, y) x##y
- # define RAPID_CXX_TEST_PP_STR(...) RAPID_CXX_TEST_PP_STR_2(__VA_ARGS__)
- # define RAPID_CXX_TEST_PP_STR_2(...) #__VA_ARGS__
- # if defined(__GNUC__)
- # define TEST_FUNC_NAME() __PRETTY_FUNCTION__
- # define RAPID_CXX_TEST_UNUSED __attribute__((unused))
- # else
- # define TEST_FUNC_NAME() __func__
- # define RAPID_CXX_TEST_UNUSED
- # endif
- ////////////////////////////////////////////////////////////////////////////////
- // TEST_SUITE
- ////////////////////////////////////////////////////////////////////////////////
- # define TEST_SUITE(Name) \
- namespace Name \
- { \
- inline ::rapid_cxx_test::test_suite & get_test_suite() \
- { \
- static ::rapid_cxx_test::test_suite m_suite(#Name); \
- return m_suite; \
- } \
- \
- inline int unit_test_main(int, char**) \
- { \
- ::rapid_cxx_test::test_runner runner(get_test_suite()); \
- return runner.run(); \
- } \
- } \
- int main(int argc, char **argv) \
- { \
- return Name::unit_test_main(argc, argv); \
- } \
- namespace Name \
- { /* namespace closed in TEST_SUITE_END */
- #
- ////////////////////////////////////////////////////////////////////////////////
- // TEST_SUITE_END
- ////////////////////////////////////////////////////////////////////////////////
- # define TEST_SUITE_END() \
- } /* namespace opened in TEST_SUITE(...) */
- #
- ////////////////////////////////////////////////////////////////////////////////
- // TEST_CASE
- ////////////////////////////////////////////////////////////////////////////////
- # if !defined(__clang__)
- #
- # define TEST_CASE(Name) \
- void Name(); \
- static void RAPID_CXX_TEST_PP_CAT(Name, _invoker)() \
- { \
- Name(); \
- } \
- static ::rapid_cxx_test::registrar \
- RAPID_CXX_TEST_PP_CAT(rapid_cxx_test_registrar_, Name)( \
- get_test_suite() \
- , ::rapid_cxx_test::test_case(__FILE__, #Name, __LINE__, & RAPID_CXX_TEST_PP_CAT(Name, _invoker)) \
- ); \
- void Name()
- #
- # else /* __clang__ */
- #
- # define TEST_CASE(Name) \
- void Name(); \
- static void RAPID_CXX_TEST_PP_CAT(Name, _invoker)() \
- { \
- Name(); \
- } \
- _Pragma("clang diagnostic push") \
- _Pragma("clang diagnostic ignored \"-Wglobal-constructors\"") \
- static ::rapid_cxx_test::registrar \
- RAPID_CXX_TEST_PP_CAT(rapid_cxx_test_registrar_, Name)( \
- get_test_suite() \
- , ::rapid_cxx_test::test_case(__FILE__, #Name, __LINE__, & RAPID_CXX_TEST_PP_CAT(Name, _invoker)) \
- ); \
- _Pragma("clang diagnostic pop") \
- void Name()
- #
- # endif /* !defined(__clang__) */
- # define TEST_SET_CHECKPOINT() ::rapid_cxx_test::set_checkpoint(__FILE__, TEST_FUNC_NAME(), __LINE__)
- #define RAPID_CXX_TEST_OUTCOME()
- ////////////////////////////////////////////////////////////////////////////////
- // TEST_UNSUPPORTED
- ////////////////////////////////////////////////////////////////////////////////
- # define TEST_UNSUPPORTED() \
- do { \
- TEST_SET_CHECKPOINT(); \
- ::rapid_cxx_test::test_outcome m_f( \
- ::rapid_cxx_test::failure_type::unsupported, __FILE__, TEST_FUNC_NAME(), __LINE__ \
- , "", "" \
- ); \
- ::rapid_cxx_test::get_reporter().report(m_f); \
- return; \
- } while (false)
- #
- ////////////////////////////////////////////////////////////////////////////////
- // BASIC ASSERTIONS
- ////////////////////////////////////////////////////////////////////////////////
- # define TEST_WARN(...) \
- do { \
- TEST_SET_CHECKPOINT(); \
- ::rapid_cxx_test::test_outcome m_f( \
- ::rapid_cxx_test::failure_type::none, __FILE__, TEST_FUNC_NAME(), __LINE__ \
- , "TEST_WARN(" #__VA_ARGS__ ")", "" \
- ); \
- if (not (__VA_ARGS__)) { \
- m_f.type = ::rapid_cxx_test::failure_type::warn; \
- } \
- ::rapid_cxx_test::get_reporter().report(m_f); \
- } while (false)
- #
- # define TEST_CHECK(...) \
- do { \
- TEST_SET_CHECKPOINT(); \
- ::rapid_cxx_test::test_outcome m_f( \
- ::rapid_cxx_test::failure_type::none, __FILE__, TEST_FUNC_NAME(), __LINE__ \
- , "TEST_CHECK(" #__VA_ARGS__ ")", "" \
- ); \
- if (not (__VA_ARGS__)) { \
- m_f.type = ::rapid_cxx_test::failure_type::check; \
- } \
- ::rapid_cxx_test::get_reporter().report(m_f); \
- } while (false)
- #
- # define TEST_REQUIRE(...) \
- do { \
- TEST_SET_CHECKPOINT(); \
- ::rapid_cxx_test::test_outcome m_f( \
- ::rapid_cxx_test::failure_type::none, __FILE__, TEST_FUNC_NAME(), __LINE__ \
- , "TEST_REQUIRE(" #__VA_ARGS__ ")", "" \
- ); \
- if (not (__VA_ARGS__)) { \
- m_f.type = ::rapid_cxx_test::failure_type::require; \
- } \
- ::rapid_cxx_test::get_reporter().report(m_f); \
- if (m_f.type != ::rapid_cxx_test::failure_type::none) { \
- return; \
- } \
- } while (false)
- #
- # define TEST_ASSERT(...) \
- do { \
- TEST_SET_CHECKPOINT(); \
- ::rapid_cxx_test::test_outcome m_f( \
- ::rapid_cxx_test::failure_type::none, __FILE__, TEST_FUNC_NAME(), __LINE__ \
- , "TEST_ASSERT(" #__VA_ARGS__ ")", "" \
- ); \
- if (not (__VA_ARGS__)) { \
- m_f.type = ::rapid_cxx_test::failure_type::assert; \
- } \
- ::rapid_cxx_test::get_reporter().report(m_f); \
- if (m_f.type != ::rapid_cxx_test::failure_type::none) { \
- std::abort(); \
- } \
- } while (false)
- #
- ////////////////////////////////////////////////////////////////////////////////
- // TEST_CHECK_NO_THROW / TEST_CHECK_THROW
- ////////////////////////////////////////////////////////////////////////////////
- #ifndef TEST_HAS_NO_EXCEPTIONS
- # define TEST_CHECK_NO_THROW(...) \
- do { \
- TEST_SET_CHECKPOINT(); \
- ::rapid_cxx_test::test_outcome m_f( \
- ::rapid_cxx_test::failure_type::none, __FILE__, TEST_FUNC_NAME(), __LINE__ \
- , "TEST_CHECK_NO_THROW(" #__VA_ARGS__ ")", "" \
- ); \
- try { \
- (static_cast<void>(__VA_ARGS__)); \
- } catch (...) { \
- m_f.type = ::rapid_cxx_test::failure_type::check; \
- } \
- ::rapid_cxx_test::get_reporter().report(m_f); \
- } while (false)
- #
- # define TEST_CHECK_THROW(Except, ...) \
- do { \
- TEST_SET_CHECKPOINT(); \
- ::rapid_cxx_test::test_outcome m_f( \
- ::rapid_cxx_test::failure_type::none, __FILE__, TEST_FUNC_NAME(), __LINE__ \
- , "TEST_CHECK_THROW(" #Except "," #__VA_ARGS__ ")", "" \
- ); \
- try { \
- (static_cast<void>(__VA_ARGS__)); \
- m_f.type = ::rapid_cxx_test::failure_type::check; \
- } catch (Except const &) {} \
- ::rapid_cxx_test::get_reporter().report(m_f); \
- } while (false)
- #
- #define TEST_CHECK_THROW_RESULT(Except, Checker, ...) \
- do { \
- TEST_SET_CHECKPOINT(); \
- ::rapid_cxx_test::test_outcome m_f(::rapid_cxx_test::failure_type::none, \
- __FILE__, TEST_FUNC_NAME(), __LINE__, \
- "TEST_CHECK_THROW_RESULT(" #Except \
- "," #Checker "," #__VA_ARGS__ ")", \
- ""); \
- try { \
- (static_cast<void>(__VA_ARGS__)); \
- m_f.type = ::rapid_cxx_test::failure_type::check; \
- } catch (Except const& Caught) { \
- Checker(Caught); \
- } \
- ::rapid_cxx_test::get_reporter().report(m_f); \
- } while (false)
- #
- #else // TEST_HAS_NO_EXCEPTIONS
- # define TEST_CHECK_NO_THROW(...) \
- do { \
- TEST_SET_CHECKPOINT(); \
- ::rapid_cxx_test::test_outcome m_f( \
- ::rapid_cxx_test::failure_type::none, __FILE__, TEST_FUNC_NAME(), __LINE__ \
- , "TEST_CHECK_NO_THROW(" #__VA_ARGS__ ")", "" \
- ); \
- (static_cast<void>(__VA_ARGS__)); \
- ::rapid_cxx_test::get_reporter().report(m_f); \
- } while (false)
- #
- #define TEST_CHECK_THROW(Except, ...) ((void)0)
- #define TEST_CHECK_THROW_RESULT(Except, Checker, ...) ((void)0)
- #endif // TEST_HAS_NO_EXCEPTIONS
- ////////////////////////////////////////////////////////////////////////////////
- // TEST_REQUIRE_NO_THROW / TEST_REQUIRE_THROWs
- ////////////////////////////////////////////////////////////////////////////////
- #ifndef TEST_HAS_NO_EXCEPTIONS
- # define TEST_REQUIRE_NO_THROW(...) \
- do { \
- TEST_SET_CHECKPOINT(); \
- ::rapid_cxx_test::test_outcome m_f( \
- ::rapid_cxx_test::failure_type::none, __FILE__, TEST_FUNC_NAME(), __LINE__ \
- , "TEST_REQUIRE_NO_THROW(" #__VA_ARGS__ ")", "" \
- ); \
- try { \
- (static_cast<void>(__VA_ARGS__)); \
- } catch (...) { \
- m_f.type = ::rapid_cxx_test::failure_type::require; \
- } \
- ::rapid_cxx_test::get_reporter().report(m_f); \
- if (m_f.type != ::rapid_cxx_test::failure_type::none) { \
- return; \
- } \
- } while (false)
- #
- # define TEST_REQUIRE_THROW(Except, ...) \
- do { \
- TEST_SET_CHECKPOINT(); \
- ::rapid_cxx_test::test_outcome m_f( \
- ::rapid_cxx_test::failure_type::none, __FILE__, TEST_FUNC_NAME(), __LINE__ \
- , "TEST_REQUIRE_THROW(" #Except "," #__VA_ARGS__ ")", "" \
- ); \
- try { \
- (static_cast<void>(__VA_ARGS__)); \
- m_f.type = ::rapid_cxx_test::failure_type::require; \
- } catch (Except const &) {} \
- ::rapid_cxx_test::get_reporter().report(m_f); \
- if (m_f.type != ::rapid_cxx_test::failure_type::none) { \
- return; \
- } \
- } while (false)
- #
- #else // TEST_HAS_NO_EXCEPTIONS
- # define TEST_REQUIRE_NO_THROW(...) \
- do { \
- TEST_SET_CHECKPOINT(); \
- ::rapid_cxx_test::test_outcome m_f( \
- ::rapid_cxx_test::failure_type::none, __FILE__, TEST_FUNC_NAME(), __LINE__ \
- , "TEST_REQUIRE_NO_THROW(" #__VA_ARGS__ ")", "" \
- ); \
- (static_cast<void>(__VA_ARGS__)); \
- ::rapid_cxx_test::get_reporter().report(m_f); \
- } while (false)
- #
- #define TEST_REQUIRE_THROW(Except, ...) ((void)0)
- #endif // TEST_HAS_NO_EXCEPTIONS
- ////////////////////////////////////////////////////////////////////////////////
- // TEST_ASSERT_NO_THROW / TEST_ASSERT_THROW
- ////////////////////////////////////////////////////////////////////////////////
- #ifndef TEST_HAS_NO_EXCEPTIONS
- # define TEST_ASSERT_NO_THROW(...) \
- do { \
- TEST_SET_CHECKPOINT(); \
- ::rapid_cxx_test::test_outcome m_f( \
- ::rapid_cxx_test::failure_type::none, __FILE__, TEST_FUNC_NAME(), __LINE__ \
- , "TEST_ASSERT_NO_THROW(" #__VA_ARGS__ ")", "" \
- ); \
- try { \
- (static_cast<void>(__VA_ARGS__)); \
- } catch (...) { \
- m_f.type = ::rapid_cxx_test::failure_type::assert; \
- } \
- ::rapid_cxx_test::get_reporter().report(m_f); \
- if (m_f.type != ::rapid_cxx_test::failure_type::none) { \
- std::abort(); \
- } \
- } while (false)
- #
- # define TEST_ASSERT_THROW(Except, ...) \
- do { \
- TEST_SET_CHECKPOINT(); \
- ::rapid_cxx_test::test_outcome m_f( \
- ::rapid_cxx_test::failure_type::none, __FILE__, TEST_FUNC_NAME(), __LINE__ \
- , "TEST_ASSERT_THROW(" #Except "," #__VA_ARGS__ ")", "" \
- ); \
- try { \
- (static_cast<void>(__VA_ARGS__)); \
- m_f.type = ::rapid_cxx_test::failure_type::assert; \
- } catch (Except const &) {} \
- ::rapid_cxx_test::get_reporter().report(m_f); \
- if (m_f.type != ::rapid_cxx_test::failure_type::none) { \
- std::abort(); \
- } \
- } while (false)
- #
- #else // TEST_HAS_NO_EXCEPTIONS
- # define TEST_ASSERT_NO_THROW(...) \
- do { \
- TEST_SET_CHECKPOINT(); \
- ::rapid_cxx_test::test_outcome m_f( \
- ::rapid_cxx_test::failure_type::none, __FILE__, TEST_FUNC_NAME(), __LINE__ \
- , "TEST_ASSERT_NO_THROW(" #__VA_ARGS__ ")", "" \
- ); \
- (static_cast<void>(__VA_ARGS__)); \
- ::rapid_cxx_test::get_reporter().report(m_f); \
- } while (false)
- #
- #define TEST_ASSERT_THROW(Except, ...) ((void)0)
- #endif // TEST_HAS_NO_EXCEPTIONS
- ////////////////////////////////////////////////////////////////////////////////
- //
- ////////////////////////////////////////////////////////////////////////////////
- # define TEST_WARN_EQUAL_COLLECTIONS(...) \
- do { \
- TEST_SET_CHECKPOINT(); \
- ::rapid_cxx_test::test_outcome m_f( \
- ::rapid_cxx_test::failure_type::none, __FILE__, TEST_FUNC_NAME(), __LINE__ \
- , "TEST_WARN_EQUAL_COLLECTIONS(" #__VA_ARGS__ ")", "" \
- ); \
- if (not ::rapid_cxx_test::detail::check_equal_collections_impl(__VA_ARGS__)) { \
- m_f.type = ::rapid_cxx_test::failure_type::warn; \
- } \
- ::rapid_cxx_test::get_reporter().report(m_f); \
- } while (false)
- #
- # define TEST_CHECK_EQUAL_COLLECTIONS(...) \
- do { \
- TEST_SET_CHECKPOINT(); \
- ::rapid_cxx_test::test_outcome m_f( \
- ::rapid_cxx_test::failure_type::none, __FILE__, TEST_FUNC_NAME(), __LINE__ \
- , "TEST_CHECK_EQUAL_COLLECTIONS(" #__VA_ARGS__ ")", "" \
- ); \
- if (not ::rapid_cxx_test::detail::check_equal_collections_impl(__VA_ARGS__)) { \
- m_f.type = ::rapid_cxx_test::failure_type::check; \
- } \
- ::rapid_cxx_test::get_reporter().report(m_f); \
- } while (false)
- #
- # define TEST_REQUIRE_EQUAL_COLLECTIONS(...) \
- do { \
- TEST_SET_CHECKPOINT(); \
- ::rapid_cxx_test::test_outcome m_f( \
- ::rapid_cxx_test::failure_type::none, __FILE__, TEST_FUNC_NAME(), __LINE__ \
- , "TEST_REQUIRE_EQUAL_COLLECTIONS(" #__VA_ARGS__ ")", "" \
- ); \
- if (not ::rapid_cxx_test::detail::check_equal_collections_impl(__VA_ARGS__)) { \
- m_f.type = ::rapid_cxx_test::failure_type::require; \
- } \
- ::rapid_cxx_test::get_reporter().report(m_f); \
- if (m_f.type != ::rapid_cxx_test::failure_type::none) { \
- return; \
- } \
- } while (false)
- #
- # define TEST_ASSERT_EQUAL_COLLECTIONS(...) \
- do { \
- TEST_SET_CHECKPOINT(); \
- ::rapid_cxx_test::test_outcome m_f( \
- ::rapid_cxx_test::failure_type::none, __FILE__, TEST_FUNC_NAME(), __LINE__ \
- , "TEST_ASSERT_EQUAL_COLLECTIONS(" #__VA_ARGS__ ")", "" \
- ); \
- if (not ::rapid_cxx_test::detail::check_equal_collections_impl(__VA_ARGS__)) { \
- m_f.type = ::rapid_cxx_test::failure_type::assert; \
- } \
- ::rapid_cxx_test::get_reporter().report(m_f); \
- if (m_f.type != ::rapid_cxx_test::failure_type::none) { \
- ::std::abort(); \
- } \
- } while (false)
- #
- namespace rapid_cxx_test
- {
- typedef void (*invoker_t)();
- ////////////////////////////////////////////////////////////////////////////
- struct test_case
- {
- test_case()
- : file(""), func(""), line(0), invoke(NULL)
- {}
- test_case(const char* file1, const char* func1, std::size_t line1,
- invoker_t invoke1)
- : file(file1), func(func1), line(line1), invoke(invoke1)
- {}
- const char *file;
- const char *func;
- std::size_t line;
- invoker_t invoke;
- };
- ////////////////////////////////////////////////////////////////////////////
- struct failure_type
- {
- enum enum_type {
- none,
- unsupported,
- warn,
- check,
- require,
- assert,
- uncaught_exception
- };
- };
- typedef failure_type::enum_type failure_type_t;
- ////////////////////////////////////////////////////////////////////////////
- struct test_outcome
- {
- test_outcome()
- : type(failure_type::none),
- file(""), func(""), line(0),
- expression(""), message("")
- {}
- test_outcome(failure_type_t type1, const char* file1, const char* func1,
- std::size_t line1, const char* expression1,
- const char* message1)
- : type(type1), file(file1), func(func1), line(line1),
- expression(expression1), message(message1)
- {
- trim_func_string();
- }
- failure_type_t type;
- const char *file;
- const char *func;
- std::size_t line;
- const char *expression;
- const char *message;
- private:
- void trim_file_string() {
- const char* f_start = file;
- const char* prev_start = f_start;
- const char* last_start = f_start;
- char last;
- while ((last = *f_start) != '\0') {
- ++f_start;
- if (last == '/' && *f_start) {
- prev_start = last_start;
- last_start = f_start;
- }
- }
- file = prev_start;
- }
- void trim_func_string() {
- const char* void_loc = ::strstr(func, "void ");
- if (void_loc == func) {
- func += strlen("void ");
- }
- const char* namespace_loc = ::strstr(func, "::");
- if (namespace_loc) {
- func = namespace_loc + 2;
- }
- }
- };
- ////////////////////////////////////////////////////////////////////////////
- struct checkpoint
- {
- const char *file;
- const char *func;
- std::size_t line;
- };
- namespace detail
- {
- inline checkpoint & global_checkpoint()
- {
- static checkpoint cp = {"", "", 0};
- return cp;
- }
- }
- ////////////////////////////////////////////////////////////////////////////
- inline void set_checkpoint(const char* file, const char* func, std::size_t line)
- {
- checkpoint& cp = detail::global_checkpoint();
- cp.file = file;
- cp.func = func;
- cp.line = line;
- }
- ////////////////////////////////////////////////////////////////////////////
- inline checkpoint const & get_checkpoint()
- {
- return detail::global_checkpoint();
- }
- ////////////////////////////////////////////////////////////////////////////
- class test_suite
- {
- public:
- typedef test_case const* iterator;
- typedef iterator const_iterator;
- public:
- test_suite(const char *xname)
- : m_name(xname), m_tests(), m_size(0)
- {
- assert(xname);
- }
- public:
- const char *name() const { return m_name; }
- std::size_t size() const { return m_size; }
- test_case const & operator[](std::size_t i) const
- {
- assert(i < m_size);
- return m_tests[i];
- }
- const_iterator begin() const
- { return m_tests; }
- const_iterator end() const
- {
- return m_tests + m_size;
- }
- public:
- std::size_t register_test(test_case tc)
- {
- static std::size_t test_case_max = sizeof(m_tests) / sizeof(test_case);
- assert(m_size < test_case_max);
- m_tests[m_size] = tc;
- return m_size++;
- }
- private:
- test_suite(test_suite const &);
- test_suite & operator=(test_suite const &);
- private:
- const char* m_name;
- // Since fast compile times in a priority, we use simple containers
- // with hard limits.
- test_case m_tests[256];
- std::size_t m_size;
- };
- ////////////////////////////////////////////////////////////////////////////
- class registrar
- {
- public:
- registrar(test_suite & st, test_case tc)
- {
- st.register_test(tc);
- }
- };
- ////////////////////////////////////////////////////////////////////////////
- class test_reporter
- {
- public:
- test_reporter()
- : m_testcases(0), m_testcase_failures(0), m_unsupported(0),
- m_assertions(0), m_warning_failures(0), m_check_failures(0),
- m_require_failures(0), m_uncaught_exceptions(0), m_failure()
- {
- }
- void test_case_begin()
- {
- ++m_testcases;
- clear_failure();
- }
- void test_case_end()
- {
- if (m_failure.type != failure_type::none
- && m_failure.type != failure_type::unsupported) {
- ++m_testcase_failures;
- }
- }
- # if defined(__GNUC__)
- # pragma GCC diagnostic push
- # pragma GCC diagnostic ignored "-Wswitch-default"
- # endif
- // Each assertion and failure is reported through this function.
- void report(test_outcome o)
- {
- ++m_assertions;
- switch (o.type)
- {
- case failure_type::none:
- break;
- case failure_type::unsupported:
- ++m_unsupported;
- m_failure = o;
- break;
- case failure_type::warn:
- ++m_warning_failures;
- report_error(o);
- break;
- case failure_type::check:
- ++m_check_failures;
- report_error(o);
- m_failure = o;
- break;
- case failure_type::require:
- ++m_require_failures;
- report_error(o);
- m_failure = o;
- break;
- case failure_type::assert:
- report_error(o);
- break;
- case failure_type::uncaught_exception:
- ++m_uncaught_exceptions;
- std::fprintf(stderr
- , "Test case FAILED with uncaught exception:\n"
- " last checkpoint near %s::%lu %s\n\n"
- , o.file, o.line, o.func
- );
- m_failure = o;
- break;
- }
- }
- # if defined(__GNUC__)
- # pragma GCC diagnostic pop
- # endif
- test_outcome current_failure() const
- {
- return m_failure;
- }
- void clear_failure()
- {
- m_failure.type = failure_type::none;
- m_failure.file = "";
- m_failure.func = "";
- m_failure.line = 0;
- m_failure.expression = "";
- m_failure.message = "";
- }
- std::size_t test_case_count() const
- { return m_testcases; }
- std::size_t test_case_failure_count() const
- { return m_testcase_failures; }
- std::size_t unsupported_count() const
- { return m_unsupported; }
- std::size_t assertion_count() const
- { return m_assertions; }
- std::size_t warning_failure_count() const
- { return m_warning_failures; }
- std::size_t check_failure_count() const
- { return m_check_failures; }
- std::size_t require_failure_count() const
- { return m_require_failures; }
- std::size_t failure_count() const
- { return m_check_failures + m_require_failures + m_uncaught_exceptions; }
- // Print a summary of what was run and the outcome.
- void print_summary(const char* suitename) const
- {
- FILE* out = failure_count() ? stderr : stdout;
- std::size_t testcases_run = m_testcases - m_unsupported;
- std::fprintf(out, "Summary for testsuite %s:\n", suitename);
- std::fprintf(out, " %lu of %lu test cases passed.\n", testcases_run - m_testcase_failures, testcases_run);
- std::fprintf(out, " %lu of %lu assertions passed.\n", m_assertions - (m_warning_failures + m_check_failures + m_require_failures), m_assertions);
- std::fprintf(out, " %lu unsupported test case%s.\n", m_unsupported, (m_unsupported != 1 ? "s" : ""));
- }
- private:
- test_reporter(test_reporter const &);
- test_reporter const & operator=(test_reporter const &);
- void report_error(test_outcome o) const
- {
- std::fprintf(stderr, "In %s:%lu Assertion %s failed.\n in file: %s\n %s\n"
- , o.func, o.line, o.expression, o.file, o.message ? o.message : ""
- );
- }
- private:
- // counts of testcases, failed testcases, and unsupported testcases.
- std::size_t m_testcases;
- std::size_t m_testcase_failures;
- std::size_t m_unsupported;
- // counts of assertions and assertion failures.
- std::size_t m_assertions;
- std::size_t m_warning_failures;
- std::size_t m_check_failures;
- std::size_t m_require_failures;
- std::size_t m_uncaught_exceptions;
- // The last failure. This is cleared between testcases.
- test_outcome m_failure;
- };
- ////////////////////////////////////////////////////////////////////////////
- inline test_reporter & get_reporter()
- {
- static test_reporter o;
- return o;
- }
- ////////////////////////////////////////////////////////////////////////////
- class test_runner
- {
- public:
- test_runner(test_suite & ts)
- : m_ts(ts)
- {}
- public:
- int run()
- {
- // for each testcase
- for (test_suite::const_iterator b = m_ts.begin(), e = m_ts.end();
- b != e; ++b)
- {
- test_case const& tc = *b;
- set_checkpoint(tc.file, tc.func, tc.line);
- get_reporter().test_case_begin();
- #ifndef TEST_HAS_NO_EXCEPTIONS
- try {
- #endif
- tc.invoke();
- #ifndef TEST_HAS_NO_EXCEPTIONS
- } catch (...) {
- test_outcome o;
- o.type = failure_type::uncaught_exception;
- o.file = get_checkpoint().file;
- o.func = get_checkpoint().func;
- o.line = get_checkpoint().line;
- o.expression = "";
- o.message = "";
- get_reporter().report(o);
- }
- #endif
- get_reporter().test_case_end();
- }
- auto exit_code = get_reporter().failure_count() ? EXIT_FAILURE : EXIT_SUCCESS;
- if (exit_code == EXIT_FAILURE || get_reporter().unsupported_count())
- get_reporter().print_summary(m_ts.name());
- return exit_code;
- }
- private:
- test_runner(test_runner const &);
- test_runner operator=(test_runner const &);
- test_suite & m_ts;
- };
- namespace detail
- {
- template <class Iter1, class Iter2>
- bool check_equal_collections_impl(
- Iter1 start1, Iter1 const end1
- , Iter2 start2, Iter2 const end2
- )
- {
- while (start1 != end1 && start2 != end2) {
- if (*start1 != *start2) {
- return false;
- }
- ++start1; ++start2;
- }
- return (start1 == end1 && start2 == end2);
- }
- } // namespace detail
- } // namespace rapid_cxx_test
- # if defined(__GNUC__)
- # pragma GCC diagnostic pop
- # endif
- #endif /* RAPID_CXX_TEST_H */
|