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)
5 
6 //  See http://www.boost.org/libs/test for the library home page.
7 //
8 //!@file
9 //!@brief generators and helper macros for parameterized tests
10 // ***************************************************************************
11 
12 #ifndef BOOST_TEST_PARAMETERIZED_TEST_HPP_021102GER
13 #define BOOST_TEST_PARAMETERIZED_TEST_HPP_021102GER
14 
15 // Boost.Test
16 #include <boost/test/unit_test_suite.hpp>
17 #include <boost/test/utils/string_cast.hpp>
18 
19 // Boost
20 #include <boost/type_traits/remove_reference.hpp>
21 #include <boost/type_traits/remove_const.hpp>
22 
23 #include <boost/bind/bind.hpp>
24 #include <boost/function/function1.hpp>
25 
26 #include <boost/test/detail/suppress_warnings.hpp>
27 
28 //____________________________________________________________________________//
29 
30 #define BOOST_PARAM_TEST_CASE( function, begin, end )                      \
31 boost::unit_test::make_test_case( function,                                \
32                                   BOOST_TEST_STRINGIZE( function ),        \
33                                   __FILE__, __LINE__,                      \
34                                   (begin), (end) )                         \
35 /**/
36 
37 #define BOOST_PARAM_CLASS_TEST_CASE( function, tc_instance, begin, end )   \
38 boost::unit_test::make_test_case( function,                                \
39                                   BOOST_TEST_STRINGIZE( function ),        \
40                                   __FILE__, __LINE__,                      \
41                                   (tc_instance),                           \
42                                   (begin), (end) )                         \
43 /**/
44 
45 namespace boost {
46 namespace unit_test {
47 
48 namespace ut_detail {
49 
50 // ************************************************************************** //
51 // **************           param_test_case_generator          ************** //
52 // ************************************************************************** //
53 
54 template<typename ParamType, typename ParamIter>
55 class param_test_case_generator : public test_unit_generator {
56 public:
param_test_case_generator(boost::function<void (ParamType)> const & test_func,const_string tc_name,const_string tc_file,std::size_t tc_line,ParamIter par_begin,ParamIter par_end)57     param_test_case_generator( boost::function<void (ParamType)> const& test_func,
58                                const_string                             tc_name,
59                                const_string                             tc_file,
60                                std::size_t                              tc_line,
61                                ParamIter                                par_begin,
62                                ParamIter                                par_end )
63     : m_test_func( test_func )
64     , m_tc_name( ut_detail::normalize_test_case_name( tc_name ) )
65     , m_tc_file( tc_file )
66     , m_tc_line( tc_line )
67     , m_par_begin( par_begin )
68     , m_par_end( par_end )
69     , m_index( 0 )
70     {}
71 
next() const72     virtual test_unit* next() const
73     {
74         if( m_par_begin == m_par_end )
75             return (test_unit*)0;
76 
77         test_unit* res = new test_case( m_tc_name + "_" + utils::string_cast(m_index), m_tc_file, m_tc_line, boost::bind( m_test_func, *m_par_begin ) );
78 
79         ++m_par_begin;
80         ++m_index;
81 
82         return res;
83     }
84 
85 private:
86     // Data members
87     boost::function<void (ParamType)>    m_test_func;
88     std::string             m_tc_name;
89     const_string            m_tc_file;
90     std::size_t             m_tc_line;
91     mutable ParamIter       m_par_begin;
92     ParamIter               m_par_end;
93     mutable std::size_t     m_index;
94 };
95 
96 //____________________________________________________________________________//
97 
98 template<typename UserTestCase,typename ParamType>
99 struct user_param_tc_method_invoker {
100     typedef void (UserTestCase::*test_method)( ParamType );
101 
102     // Constructor
user_param_tc_method_invokerboost::unit_test::ut_detail::user_param_tc_method_invoker103     user_param_tc_method_invoker( shared_ptr<UserTestCase> inst, test_method test_method )
104     : m_inst( inst ), m_test_method( test_method ) {}
105 
operator ()boost::unit_test::ut_detail::user_param_tc_method_invoker106     void operator()( ParamType p ) { ((*m_inst).*m_test_method)( p ); }
107 
108     // Data members
109     shared_ptr<UserTestCase> m_inst;
110     test_method              m_test_method;
111 };
112 
113 //____________________________________________________________________________//
114 
115 } // namespace ut_detail
116 
117 template<typename ParamType, typename ParamIter>
118 inline ut_detail::param_test_case_generator<ParamType,ParamIter>
make_test_case(boost::function<void (ParamType)> const & test_func,const_string tc_name,const_string tc_file,std::size_t tc_line,ParamIter par_begin,ParamIter par_end)119 make_test_case( boost::function<void (ParamType)> const& test_func,
120                 const_string                             tc_name,
121                 const_string                             tc_file,
122                 std::size_t                              tc_line,
123                 ParamIter                                par_begin,
124                 ParamIter                                par_end )
125 {
126     return ut_detail::param_test_case_generator<ParamType,ParamIter>( test_func, tc_name, tc_file, tc_line, par_begin, par_end );
127 }
128 
129 //____________________________________________________________________________//
130 
131 template<typename ParamType, typename ParamIter>
132 inline ut_detail::param_test_case_generator<
133     BOOST_DEDUCED_TYPENAME remove_const<BOOST_DEDUCED_TYPENAME remove_reference<ParamType>::type>::type,ParamIter>
make_test_case(void (* test_func)(ParamType),const_string tc_name,const_string tc_file,std::size_t tc_line,ParamIter par_begin,ParamIter par_end)134 make_test_case( void (*test_func)( ParamType ),
135                 const_string  tc_name,
136                 const_string  tc_file,
137                 std::size_t   tc_line,
138                 ParamIter     par_begin,
139                 ParamIter     par_end )
140 {
141     typedef BOOST_DEDUCED_TYPENAME remove_const<BOOST_DEDUCED_TYPENAME remove_reference<ParamType>::type>::type param_value_type;
142     return ut_detail::param_test_case_generator<param_value_type,ParamIter>( test_func, tc_name, tc_file, tc_line, par_begin, par_end );
143 }
144 
145 //____________________________________________________________________________//
146 
147 template<typename UserTestCase,typename ParamType, typename ParamIter>
148 inline ut_detail::param_test_case_generator<
149     BOOST_DEDUCED_TYPENAME remove_const<BOOST_DEDUCED_TYPENAME remove_reference<ParamType>::type>::type,ParamIter>
make_test_case(void (UserTestCase::* test_method)(ParamType),const_string tc_name,const_string tc_file,std::size_t tc_line,boost::shared_ptr<UserTestCase> const & user_test_case,ParamIter par_begin,ParamIter par_end)150 make_test_case( void (UserTestCase::*test_method )( ParamType ),
151                 const_string                           tc_name,
152                 const_string                           tc_file,
153                 std::size_t                            tc_line,
154                 boost::shared_ptr<UserTestCase> const& user_test_case,
155                 ParamIter                              par_begin,
156                 ParamIter                              par_end )
157 {
158     typedef BOOST_DEDUCED_TYPENAME remove_const<BOOST_DEDUCED_TYPENAME remove_reference<ParamType>::type>::type param_value_type;
159     return ut_detail::param_test_case_generator<param_value_type,ParamIter>(
160                ut_detail::user_param_tc_method_invoker<UserTestCase,ParamType>( user_test_case, test_method ),
161                tc_name,
162                tc_file,
163                tc_line,
164                par_begin,
165                par_end );
166 }
167 
168 //____________________________________________________________________________//
169 
170 } // unit_test
171 } // namespace boost
172 
173 #include <boost/test/detail/enable_warnings.hpp>
174 
175 #endif // BOOST_TEST_PARAMETERIZED_TEST_HPP_021102GER
176 
177