diff --git a/test/test.hpp b/test/test.hpp new file mode 100644 index 0000000..f0fbb79 --- /dev/null +++ b/test/test.hpp @@ -0,0 +1,105 @@ +// +// Copyright Antony Polukhin, 2014. +// +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// Unfortunately Boost.Test does not satisfies the requirements of Boost.TypeIndex library: +// * Boost.Test fails to work with RTTI off +// * release version of Boost.Test is not updated for a long time, so new fixes do not make their way to release + +#ifndef BOOST_TEST_LIGHTWEIGHT_EMULATION +#define BOOST_TEST_LIGHTWEIGHT_EMULATION + +#include +#include +#include + +namespace boost { namespace test_x2 { + + +class tester { + typedef void (*test_function_t)(); + typedef std::pair pair_t; + +private: + std::vector tests_; + int error_count_; + +public: + tester() : error_count_(0) {} + + void on_fail() { + ++ error_count_; + } + + void add_test(const char* name, test_function_t func) { + tests_.push_back(pair_t(name, func)); + } + + int run() { + std::cout << "Running " << tests_.size() << " tests\n"; + + for (std::size_t i = 0; i < tests_.size(); ++i) { + std::cout << "Running '" << tests_[i].first << "' test case\n"; + (*tests_[i].second)(); // calling function + } + + return error_count_; + } +}; + + +extern tester g_tester; + +}} // namespace boost::test_x2 + +#define BOOST_AUTO_TEST_CASE(name) \ + void name (); \ + struct name ## _starter { name ## _starter () { \ + boost::test_x2::g_tester.add_test(#name, & name); \ + }} name ## _starter_variable; \ + void name () \ + /**/ + + +#define BOOST_CHECK(pred) \ + do{ \ + if(!(pred)){ \ + std::cout << __FILE__ << ":" << __LINE__ << ": Error in " << BOOST_STRINGIZE(pred) << std::endl; \ + boost::test_x2::g_tester.on_fail(); \ + } \ + }while(0) + +#define BOOST_CHECK_TRIPLE_IMPL(x, oper, y) \ + do{ \ + if(!(x oper y)){ \ + std::cout << __FILE__ << ":" << __LINE__ << ": Error in " \ + << BOOST_STRINGIZE(x) << BOOST_STRINGIZE(oper) << BOOST_STRINGIZE(y) \ + << " where lhs = " << x << " and rhs = " << y << std::endl; \ + boost::test_x2::g_tester.on_fail(); \ + } \ + }while(0) + +#define BOOST_CHECK_EQUAL(x, y) BOOST_CHECK_TRIPLE_IMPL(x, ==, y) +#define BOOST_CHECK_NE(x, y) BOOST_CHECK_TRIPLE_IMPL(x, !=, y) +#define BOOST_CHECK_LE(x, y) BOOST_CHECK_TRIPLE_IMPL(x, <=, y) +#define BOOST_CHECK_GE(x, y) BOOST_CHECK_TRIPLE_IMPL(x, >=, y) + +#endif // BOOST_TEST_LIGHTWEIGHT_EMULATION + + +#ifdef BOOST_TEST_MODULE + +namespace boost { namespace test_x2 { + tester g_tester; +}} // namespace boost::test_x2 + +int main() { + return boost::test_x2::g_tester.run(); +} + +#endif + + diff --git a/test/testing_crossmodule.cpp b/test/testing_crossmodule.cpp index 859d678..8a96f2c 100644 --- a/test/testing_crossmodule.cpp +++ b/test/testing_crossmodule.cpp @@ -5,19 +5,17 @@ // accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) -#include +#define BOOST_TEST_MODULE testing_crossmodule_module +#include "test.hpp" #include #include "test_lib.hpp" -#define BOOST_CHECK_EQUAL(x, y) BOOST_CHECK(x == y) -#define BOOST_CHECK_NE(x, y) BOOST_CHECK(x != y) - namespace user_defined_namespace { class user_defined{}; } -void comparing_types_between_modules() +BOOST_AUTO_TEST_CASE(comparing_types_between_modules) { boost::typeindex::type_index t_const_int = boost::typeindex::type_id_with_cvr(); boost::typeindex::type_index t_int = boost::typeindex::type_id(); @@ -47,10 +45,3 @@ void comparing_types_between_modules() test_lib::accept_typeindex(t_int); } - -int test_main(int , char* []) { - comparing_types_between_modules(); - - return 0; -} - diff --git a/test/testing_crossmodule_anonymous.cpp b/test/testing_crossmodule_anonymous.cpp index 825af61..deee42c 100644 --- a/test/testing_crossmodule_anonymous.cpp +++ b/test/testing_crossmodule_anonymous.cpp @@ -5,18 +5,17 @@ // accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) -#include "boost/test/minimal.hpp" +#define BOOST_TEST_MODULE testing_crossmodule_anon_module +#include "test.hpp" #include #include "test_lib_anonymous.hpp" -#define BOOST_CHECK_NE(x, y) BOOST_CHECK(x != y) - namespace { class user_defined{}; } -void comparing_anonymous_types_between_modules() +BOOST_AUTO_TEST_CASE(comparing_anonymous_types_between_modules) { boost::typeindex::type_index t_const_userdef = boost::typeindex::type_id_with_cvr(); boost::typeindex::type_index t_userdef = boost::typeindex::type_id(); @@ -27,9 +26,3 @@ void comparing_anonymous_types_between_modules() BOOST_CHECK_NE(t_userdef, test_lib::get_const_anonymous_user_defined_class()); } -int test_main(int , char* []) { - comparing_anonymous_types_between_modules(); - - return 0; -} - diff --git a/test/type_index_test.cpp b/test/type_index_test.cpp index 8f2771a..919f395 100644 --- a/test/type_index_test.cpp +++ b/test/type_index_test.cpp @@ -5,19 +5,14 @@ // accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) -#include +#define BOOST_TEST_MODULE type_index_test_module +#include "test.hpp" #include #include #include -#define BOOST_CHECK_EQUAL(x, y) BOOST_CHECK(x == y) -#define BOOST_CHECK_NE(x, y) BOOST_CHECK(x != y) -#define BOOST_CHECK_LE(x, y) BOOST_CHECK(x <= y) -#define BOOST_CHECK_GE(x, y) BOOST_CHECK(x >= y) - - namespace my_namespace1 { class my_class{}; } @@ -28,19 +23,19 @@ namespace my_namespace2 { } -void names_matches_type_id() +BOOST_AUTO_TEST_CASE(names_matches_type_id) { using namespace boost::typeindex; BOOST_CHECK_EQUAL(type_id().pretty_name(), "int"); BOOST_CHECK_EQUAL(type_id().pretty_name(), "double"); - BOOST_CHECK_EQUAL(type_id().name(), type_id().name()); + BOOST_CHECK_NE(type_id().name(), type_id().name()); BOOST_CHECK_NE(type_id().name(), type_id().name()); BOOST_CHECK_NE(type_id().name(), type_id().name()); BOOST_CHECK_EQUAL(type_id().name(), type_id().name()); } -void default_construction() +BOOST_AUTO_TEST_CASE(default_construction) { using namespace boost::typeindex; type_index ti1, ti2; @@ -52,7 +47,7 @@ void default_construction() } -void copy_construction() +BOOST_AUTO_TEST_CASE(copy_construction) { using namespace boost::typeindex; type_index ti1, ti2 = type_id(); @@ -64,7 +59,7 @@ void copy_construction() BOOST_CHECK_EQUAL(ti3, ti1); } -void comparators_type_id() +BOOST_AUTO_TEST_CASE(comparators_type_id) { using namespace boost::typeindex; type_index t_int = type_id(); @@ -83,7 +78,7 @@ void comparators_type_id() BOOST_CHECK(t_double > t_int || t_int > t_double); } -void hash_code_type_id() +BOOST_AUTO_TEST_CASE(hash_code_type_id) { using namespace boost::typeindex; std::size_t t_int1 = type_id().hash_code(); @@ -97,8 +92,6 @@ void hash_code_type_id() BOOST_CHECK_LE(t_double1, t_double2); } - - template static void test_with_modofiers() { using namespace boost::typeindex; @@ -139,7 +132,7 @@ static void test_with_modofiers() { BOOST_CHECK_NE(t2.hash_code(), type_id_with_cvr().hash_code()); } -void type_id_storing_modifiers() +BOOST_AUTO_TEST_CASE(type_id_storing_modifiers) { test_with_modofiers(); test_with_modofiers(); @@ -204,7 +197,7 @@ static void test_storing_nonstoring_modifiers_templ() { BOOST_CHECK_EQUAL(t2.pretty_name(), t1.pretty_name()); } -void type_id_storing_modifiers_vs_nonstoring() +BOOST_AUTO_TEST_CASE(type_id_storing_modifiers_vs_nonstoring) { test_storing_nonstoring_modifiers_templ(); test_storing_nonstoring_modifiers_templ(); @@ -216,7 +209,7 @@ void type_id_storing_modifiers_vs_nonstoring() BOOST_CHECK(t1.pretty_name() == "const int" || t1.pretty_name() == "int const"); } -void type_index_stream_operator_via_lexical_cast_testing() +BOOST_AUTO_TEST_CASE(type_index_stream_operator_via_lexical_cast_testing) { using namespace boost::typeindex; @@ -227,7 +220,7 @@ void type_index_stream_operator_via_lexical_cast_testing() BOOST_CHECK_EQUAL(s_double2, "double"); } -void type_index_stripping_cvr_test() +BOOST_AUTO_TEST_CASE(type_index_stripping_cvr_test) { using namespace boost::typeindex; @@ -250,7 +243,7 @@ void type_index_stripping_cvr_test() } -void type_index_user_defined_class_test() +BOOST_AUTO_TEST_CASE(type_index_user_defined_class_test) { using namespace boost::typeindex; @@ -271,9 +264,6 @@ void type_index_user_defined_class_test() } - - - struct A { public: BOOST_TYPE_INDEX_REGISTER_CLASS @@ -288,7 +278,7 @@ struct C: public B { BOOST_TYPE_INDEX_REGISTER_CLASS }; -void comparators_type_id_runtime() +BOOST_AUTO_TEST_CASE(comparators_type_id_runtime) { C c1; B b1; @@ -335,7 +325,7 @@ void comparators_type_id_runtime() #ifndef BOOST_NO_RTTI -void comparators_type_id_vs_type_info() +BOOST_AUTO_TEST_CASE(comparators_type_id_vs_type_info) { using namespace boost::typeindex; type_index t_int = type_id(); @@ -385,23 +375,3 @@ void comparators_type_id_vs_type_info() #endif // BOOST_NO_RTTI -int test_main(int , char* []) { - names_matches_type_id(); - default_construction(); - copy_construction(); - comparators_type_id(); - hash_code_type_id(); - - type_id_storing_modifiers(); - type_id_storing_modifiers_vs_nonstoring(); - type_index_stream_operator_via_lexical_cast_testing(); - type_index_stripping_cvr_test(); - type_index_user_defined_class_test(); - - comparators_type_id_runtime(); -#ifndef BOOST_NO_RTTI - comparators_type_id_vs_type_info(); -#endif - return 0; -} -