// (C) Copyright Gennadiy Rozental 2001. // 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) // See http://www.boost.org/libs/test for the library home page. // //!@file //!@brief test case family based on data generator // *************************************************************************** #ifndef ACTIV_BOOST_TEST_DATA_TEST_CASE_HPP_102211GER #define ACTIV_BOOST_TEST_DATA_TEST_CASE_HPP_102211GER // Boost.Test #include #include #include #include // Boost #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #if defined(ACTIV_BOOST_NO_CXX11_VARIADIC_TEMPLATES) \ && !defined(ACTIV_BOOST_TEST_DATASET_MAX_ARITY) # define ACTIV_BOOST_TEST_DATASET_MAX_ARITY 10 #endif //____________________________________________________________________________// namespace Boost { namespace unit_test { namespace data { namespace ds_detail { // ************************************************************************** // // ************** seed ************** // // ************************************************************************** // struct seed { template typename data::result_of::make::type operator->*( DataSet&& ds ) const { return data::make( std::forward( ds ) ); } }; #if !defined(ACTIV_BOOST_NO_CXX11_VARIADIC_TEMPLATES) && \ !defined(ACTIV_BOOST_NO_CXX11_RVALUE_REFERENCES) && \ !defined(ACTIV_BOOST_NO_CXX11_DECLTYPE) && \ !defined(ACTIV_BOOST_NO_CXX11_TRAILING_RESULT_TYPES) && \ !defined(ACTIV_BOOST_NO_CXX11_SMART_PTR) #define ACTIV_BOOST_TEST_DATASET_VARIADIC template struct parameter_holder { std::shared_ptr value; parameter_holder(T && value_) : value(std::make_shared(std::move(value_))) {} operator T const&() const { return *value; } }; template parameter_holder::type> boost_bind_rvalue_holder_helper_impl(T&& value, Boost::false_type /* is copy constructible */) { return parameter_holder::type>(std::forward(value)); } template T&& boost_bind_rvalue_holder_helper_impl(T&& value, Boost::true_type /* is copy constructible */) { return std::forward(value); } template auto boost_bind_rvalue_holder_helper(T&& value) -> decltype(boost_bind_rvalue_holder_helper_impl( std::forward(value), typename Boost::is_copy_constructible::type>::type())) { // need to use Boost::is_copy_constructible because std::is_copy_constructible is broken on MSVC12 return boost_bind_rvalue_holder_helper_impl( std::forward(value), typename Boost::is_copy_constructible::type>::type()); } #endif // ************************************************************************** // // ************** test_case_gen ************** // // ************************************************************************** // template class test_case_gen : public test_unit_generator { public: // Constructor #ifndef ACTIV_BOOST_NO_CXX11_RVALUE_REFERENCES test_case_gen( const_string tc_name, const_string tc_file, std::size_t tc_line, DataSet&& ds ) : m_dataset( std::forward( ds ) ) , m_generated( false ) , m_tc_name( ut_detail::normalize_test_case_name( tc_name ) ) , m_tc_file( tc_file ) , m_tc_line( tc_line ) , m_tc_index( 0 ) {} test_case_gen( test_case_gen&& gen ) : m_dataset( std::move( gen.m_dataset ) ) , m_generated( gen.m_generated ) , m_tc_name( gen.m_tc_name ) , m_tc_file( gen.m_tc_file ) , m_tc_line( gen.m_tc_line ) , m_tc_index( gen.m_tc_index ) , m_test_cases( std::move(gen.m_test_cases) ) {} #else test_case_gen( const_string tc_name, const_string tc_file, std::size_t tc_line, DataSet const& ds ) : m_dataset( ds ) , m_generated( false ) , m_tc_name( ut_detail::normalize_test_case_name( tc_name ) ) , m_tc_file( tc_file ) , m_tc_line( tc_line ) , m_tc_index( 0 ) {} #endif public: virtual test_unit* next() const { if(!m_generated) { data::for_each_sample( m_dataset, *this ); m_generated = true; } if( m_test_cases.empty() ) return 0; test_unit* res = m_test_cases.front(); m_test_cases.pop_front(); return res; } #if !defined(ACTIV_BOOST_TEST_DATASET_VARIADIC) // see ACTIV_BOOST_TEST_DATASET_MAX_ARITY to increase the default supported arity // there is also a limit on Boost::bind #define TC_MAKE(z,arity,_) \ template \ void operator()( ACTIV_BOOST_PP_ENUM_BINARY_PARAMS(arity, Arg, const& arg) ) const \ { \ m_test_cases.push_back( new test_case( genTestCaseName(), m_tc_file, m_tc_line, \ Boost::bind( &TestCase::template test_method,\ ACTIV_BOOST_PP_ENUM_PARAMS(arity, arg) ) ) ); \ } \ ACTIV_BOOST_PP_REPEAT_FROM_TO(1, ACTIV_BOOST_TEST_DATASET_MAX_ARITY, TC_MAKE, _) #else template void operator()(Arg&& ... arg) const { m_test_cases.push_back( new test_case( genTestCaseName(), m_tc_file, m_tc_line, std::bind( &TestCase::template test_method, boost_bind_rvalue_holder_helper(std::forward(arg))...))); } #endif private: std::string genTestCaseName() const { return "_" + utils::string_cast(m_tc_index++); } // Data members DataSet m_dataset; mutable bool m_generated; std::string m_tc_name; const_string m_tc_file; std::size_t m_tc_line; mutable std::size_t m_tc_index; mutable std::list m_test_cases; }; //____________________________________________________________________________// #ifndef ACTIV_BOOST_NO_CXX11_RVALUE_REFERENCES template Boost::shared_ptr //test_case_gen make_test_case_gen( const_string tc_name, const_string tc_file, std::size_t tc_line, DataSet&& ds ) { return Boost::shared_ptr(new test_case_gen( tc_name, tc_file, tc_line, std::forward(ds) )); } #else template test_case_gen make_test_case_gen( const_string tc_name, const_string tc_file, std::size_t tc_line, DataSet const& ds ) { return test_case_gen( tc_name, tc_file, tc_line, ds ); } #endif //____________________________________________________________________________// } // namespace ds_detail // ************************************************************************** // // ************** ACTIV_BOOST_DATA_TEST_CASE ************** // // ************************************************************************** // #define ACTIV_BOOST_DATA_TEST_CASE_PARAM(r, _, i, param) (ACTIV_BOOST_PP_CAT(Arg, i) const& param) #define ACTIV_BOOST_DATA_TEST_CONTEXT(r, _, param) << ACTIV_BOOST_STRINGIZE(param) << " = " << Boost::test_tools::tt_detail::print_helper(param) << "; " #define ACTIV_BOOST_DATA_TEST_CASE_PARAMS( params ) \ ACTIV_BOOST_PP_SEQ_ENUM( \ ACTIV_BOOST_PP_SEQ_FOR_EACH_I(ACTIV_BOOST_DATA_TEST_CASE_PARAM, _, params)) \ /**/ #define ACTIV_BOOST_DATA_TEST_CASE_IMPL(arity, F, test_name, dataset, params) \ struct ACTIV_BOOST_PP_CAT(test_name, case) : public F { \ template \ static void test_method( ACTIV_BOOST_DATA_TEST_CASE_PARAMS( params ) ) \ { \ ACTIV_BOOST_TEST_CONTEXT( "" \ ACTIV_BOOST_PP_SEQ_FOR_EACH(ACTIV_BOOST_DATA_TEST_CONTEXT, _, params)) \ { \ ACTIV_BOOST_TEST_CHECKPOINT('"' << #test_name << "\" fixture ctor");\ ACTIV_BOOST_PP_CAT(test_name, case) t; \ ACTIV_BOOST_TEST_CHECKPOINT('"' \ << #test_name << "\" fixture setup"); \ Boost::unit_test::setup_conditional(t); \ ACTIV_BOOST_TEST_CHECKPOINT('"' << #test_name << "\" test entry"); \ t._impl(ACTIV_BOOST_PP_SEQ_ENUM(params)); \ ACTIV_BOOST_TEST_CHECKPOINT('"' \ << #test_name << "\" fixture teardown"); \ Boost::unit_test::teardown_conditional(t); \ ACTIV_BOOST_TEST_CHECKPOINT('"' << #test_name << "\" fixture dtor");\ } \ } \ private: \ template \ void _impl(ACTIV_BOOST_DATA_TEST_CASE_PARAMS( params )); \ }; \ \ ACTIV_BOOST_AUTO_TEST_SUITE( test_name, \ *Boost::unit_test::decorator::stack_decorator()) \ \ ACTIV_BOOST_AUTO_TU_REGISTRAR( ACTIV_BOOST_PP_CAT(test_name, case) )( \ Boost::unit_test::data::ds_detail::make_test_case_gen< \ ACTIV_BOOST_PP_CAT(test_name, case)>( \ ACTIV_BOOST_STRINGIZE( test_name ), \ __FILE__, __LINE__, \ Boost::unit_test::data::ds_detail::seed{} ->* dataset ), \ Boost::unit_test::decorator::collector_t::instance() ); \ \ ACTIV_BOOST_AUTO_TEST_SUITE_END() \ \ template \ void ACTIV_BOOST_PP_CAT(test_name, case)::_impl( \ ACTIV_BOOST_DATA_TEST_CASE_PARAMS( params ) ) \ /**/ #define ACTIV_BOOST_DATA_TEST_CASE_WITH_PARAMS( F, test_name, dataset, ... ) \ ACTIV_BOOST_DATA_TEST_CASE_IMPL( ACTIV_BOOST_PP_VARIADIC_SIZE(__VA_ARGS__), \ F, test_name, dataset, \ ACTIV_BOOST_PP_VARIADIC_TO_SEQ(__VA_ARGS__) ) \ /**/ #define ACTIV_BOOST_DATA_TEST_CASE_NO_PARAMS( F, test_name, dataset ) \ ACTIV_BOOST_DATA_TEST_CASE_WITH_PARAMS( F, test_name, dataset, sample ) \ /**/ #if ACTIV_BOOST_PP_VARIADICS_MSVC #define ACTIV_BOOST_DATA_TEST_CASE( ... ) \ ACTIV_BOOST_PP_CAT( \ ACTIV_BOOST_PP_IIF(ACTIV_BOOST_PP_EQUAL(ACTIV_BOOST_PP_VARIADIC_SIZE(__VA_ARGS__),2), \ ACTIV_BOOST_DATA_TEST_CASE_NO_PARAMS, \ ACTIV_BOOST_DATA_TEST_CASE_WITH_PARAMS) ( \ ACTIV_BOOST_AUTO_TEST_CASE_FIXTURE, __VA_ARGS__), ) \ /**/ #define ACTIV_BOOST_DATA_TEST_CASE_F( F, ... ) \ ACTIV_BOOST_PP_CAT( \ ACTIV_BOOST_PP_IIF(ACTIV_BOOST_PP_EQUAL(ACTIV_BOOST_PP_VARIADIC_SIZE(__VA_ARGS__),2), \ ACTIV_BOOST_DATA_TEST_CASE_NO_PARAMS, \ ACTIV_BOOST_DATA_TEST_CASE_WITH_PARAMS) ( \ F, __VA_ARGS__), ) \ /**/ #else #define ACTIV_BOOST_DATA_TEST_CASE( ... ) \ ACTIV_BOOST_PP_IIF(ACTIV_BOOST_PP_EQUAL(ACTIV_BOOST_PP_VARIADIC_SIZE(__VA_ARGS__),2), \ ACTIV_BOOST_DATA_TEST_CASE_NO_PARAMS, \ ACTIV_BOOST_DATA_TEST_CASE_WITH_PARAMS) ( \ ACTIV_BOOST_AUTO_TEST_CASE_FIXTURE, __VA_ARGS__) \ /**/ #define ACTIV_BOOST_DATA_TEST_CASE_F( F, ... ) \ ACTIV_BOOST_PP_IIF(ACTIV_BOOST_PP_EQUAL(ACTIV_BOOST_PP_VARIADIC_SIZE(__VA_ARGS__),2), \ ACTIV_BOOST_DATA_TEST_CASE_NO_PARAMS, \ ACTIV_BOOST_DATA_TEST_CASE_WITH_PARAMS) ( \ F, __VA_ARGS__) \ /**/ #endif } // namespace data } // namespace unit_test } // namespace Boost #include #endif // ACTIV_BOOST_TEST_DATA_TEST_CASE_HPP_102211GER