1 // (C) Copyright Gennadiy Rozental 2001.
2 // Distributed under the Boost Software License, Version 1.0.
3 // (See accompanying file LICENSE_1_0.txt or copy at
4 // http://www.boost.org/LICENSE_1_0.txt)
6 // See http://www.boost.org/libs/test for the library home page.
9 //!@brief generators and helper macros for parameterized tests
10 // ***************************************************************************
12 #ifndef BOOST_TEST_PARAMETERIZED_TEST_HPP_021102GER
13 #define BOOST_TEST_PARAMETERIZED_TEST_HPP_021102GER
16 #include <boost/test/unit_test_suite.hpp>
19 #include <boost/type_traits/remove_reference.hpp>
20 #include <boost/type_traits/remove_const.hpp>
22 #include <boost/bind.hpp>
23 #include <boost/function/function1.hpp>
25 #include <boost/test/detail/suppress_warnings.hpp>
27 //____________________________________________________________________________//
29 #define BOOST_PARAM_TEST_CASE( function, begin, end ) \
30 boost::unit_test::make_test_case( function, \
31 BOOST_TEST_STRINGIZE( function ), \
36 #define BOOST_PARAM_CLASS_TEST_CASE( function, tc_instance, begin, end ) \
37 boost::unit_test::make_test_case( function, \
38 BOOST_TEST_STRINGIZE( function ), \
49 // ************************************************************************** //
50 // ************** param_test_case_generator ************** //
51 // ************************************************************************** //
53 template<typename ParamType, typename ParamIter>
54 class param_test_case_generator : public test_unit_generator {
56 param_test_case_generator( boost::function<void (ParamType)> const& test_func,
62 : m_test_func( test_func )
63 , m_tc_name( ut_detail::normalize_test_case_name( tc_name ) )
64 , m_tc_file( tc_file )
65 , m_tc_line( tc_line )
66 , m_par_begin( par_begin )
67 , m_par_end( par_end )
70 virtual test_unit* next() const
72 if( m_par_begin == m_par_end )
75 test_unit* res = new test_case( m_tc_name, m_tc_file, m_tc_line, boost::bind( m_test_func, *m_par_begin ) );
84 boost::function<void (ParamType)> m_test_func;
85 std::string m_tc_name;
86 const_string m_tc_file;
87 std::size_t m_tc_line;
88 mutable ParamIter m_par_begin;
92 //____________________________________________________________________________//
94 template<typename UserTestCase,typename ParamType>
95 struct user_param_tc_method_invoker {
96 typedef void (UserTestCase::*test_method)( ParamType );
99 user_param_tc_method_invoker( shared_ptr<UserTestCase> inst, test_method test_method )
100 : m_inst( inst ), m_test_method( test_method ) {}
102 void operator()( ParamType p ) { ((*m_inst).*m_test_method)( p ); }
105 shared_ptr<UserTestCase> m_inst;
106 test_method m_test_method;
109 //____________________________________________________________________________//
111 } // namespace ut_detail
113 template<typename ParamType, typename ParamIter>
114 inline ut_detail::param_test_case_generator<ParamType,ParamIter>
115 make_test_case( boost::function<void (ParamType)> const& test_func,
116 const_string tc_name,
117 const_string tc_file,
122 return ut_detail::param_test_case_generator<ParamType,ParamIter>( test_func, tc_name, tc_file, tc_line, par_begin, par_end );
125 //____________________________________________________________________________//
127 template<typename ParamType, typename ParamIter>
128 inline ut_detail::param_test_case_generator<
129 BOOST_DEDUCED_TYPENAME remove_const<BOOST_DEDUCED_TYPENAME remove_reference<ParamType>::type>::type,ParamIter>
130 make_test_case( void (*test_func)( ParamType ),
131 const_string tc_name,
132 const_string tc_file,
137 typedef BOOST_DEDUCED_TYPENAME remove_const<BOOST_DEDUCED_TYPENAME remove_reference<ParamType>::type>::type param_value_type;
138 return ut_detail::param_test_case_generator<param_value_type,ParamIter>( test_func, tc_name, tc_file, tc_line, par_begin, par_end );
141 //____________________________________________________________________________//
143 template<typename UserTestCase,typename ParamType, typename ParamIter>
144 inline ut_detail::param_test_case_generator<
145 BOOST_DEDUCED_TYPENAME remove_const<BOOST_DEDUCED_TYPENAME remove_reference<ParamType>::type>::type,ParamIter>
146 make_test_case( void (UserTestCase::*test_method )( ParamType ),
147 const_string tc_name,
148 const_string tc_file,
150 boost::shared_ptr<UserTestCase> const& user_test_case,
154 typedef BOOST_DEDUCED_TYPENAME remove_const<BOOST_DEDUCED_TYPENAME remove_reference<ParamType>::type>::type param_value_type;
155 return ut_detail::param_test_case_generator<param_value_type,ParamIter>(
156 ut_detail::user_param_tc_method_invoker<UserTestCase,ParamType>( user_test_case, test_method ),
164 //____________________________________________________________________________//
169 #include <boost/test/detail/enable_warnings.hpp>
171 #endif // BOOST_TEST_PARAMETERIZED_TEST_HPP_021102GER