| 1 | //  (C) Copyright Gennadiy Rozental 2005. | 
|---|
| 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        : $RCSfile: parameterized_test.hpp,v $ | 
|---|
| 9 | // | 
|---|
| 10 | //  Version     : $Revision: 1.4 $ | 
|---|
| 11 | // | 
|---|
| 12 | //  Description : generators and helper macros for parameterized tests | 
|---|
| 13 | // *************************************************************************** | 
|---|
| 14 |  | 
|---|
| 15 | #ifndef BOOST_TEST_PARAMETERIZED_TEST_HPP_021102GER | 
|---|
| 16 | #define BOOST_TEST_PARAMETERIZED_TEST_HPP_021102GER | 
|---|
| 17 |  | 
|---|
| 18 | // Boost.Test | 
|---|
| 19 | #include <boost/test/unit_test_suite.hpp> | 
|---|
| 20 | #include <boost/test/utils/callback.hpp> | 
|---|
| 21 |  | 
|---|
| 22 | // Boost | 
|---|
| 23 | #include <boost/type_traits/remove_reference.hpp> | 
|---|
| 24 | #include <boost/type_traits/remove_const.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 |                                   (begin), (end) )                         \ | 
|---|
| 34 | /**/ | 
|---|
| 35 |  | 
|---|
| 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 ),        \ | 
|---|
| 39 |                                   (tc_instance),                           \ | 
|---|
| 40 |                                   (begin), (end) )                         \ | 
|---|
| 41 | /**/ | 
|---|
| 42 |  | 
|---|
| 43 | namespace boost { | 
|---|
| 44 |  | 
|---|
| 45 | namespace unit_test { | 
|---|
| 46 |  | 
|---|
| 47 | // ************************************************************************** // | 
|---|
| 48 | // **************          test_func_with_bound_param          ************** // | 
|---|
| 49 | // ************************************************************************** // | 
|---|
| 50 |  | 
|---|
| 51 | namespace ut_detail { | 
|---|
| 52 |  | 
|---|
| 53 | template<typename ParamType> | 
|---|
| 54 | struct test_func_with_bound_param { | 
|---|
| 55 |     template<typename T> | 
|---|
| 56 |     test_func_with_bound_param( callback1<ParamType> test_func, T const& param ) | 
|---|
| 57 |     : m_test_func( test_func ) | 
|---|
| 58 |     , m_param( param ) | 
|---|
| 59 |     {} | 
|---|
| 60 |  | 
|---|
| 61 |     void operator()() { m_test_func( m_param ); } | 
|---|
| 62 |  | 
|---|
| 63 | private: | 
|---|
| 64 |     callback1<ParamType> m_test_func; | 
|---|
| 65 |     ParamType            m_param; | 
|---|
| 66 | }; | 
|---|
| 67 |  | 
|---|
| 68 | // ************************************************************************** // | 
|---|
| 69 | // **************           param_test_case_generator          ************** // | 
|---|
| 70 | // ************************************************************************** // | 
|---|
| 71 |  | 
|---|
| 72 | template<typename ParamType, typename ParamIter> | 
|---|
| 73 | class param_test_case_generator : public test_unit_generator { | 
|---|
| 74 | public: | 
|---|
| 75 |     param_test_case_generator( callback1<ParamType> const&  test_func, | 
|---|
| 76 |                                const_string                 tc_name,  | 
|---|
| 77 |                                ParamIter                    par_begin, | 
|---|
| 78 |                                ParamIter                    par_end ) | 
|---|
| 79 |     : m_test_func( test_func ) | 
|---|
| 80 |     , m_tc_name( ut_detail::normalize_test_case_name( tc_name ) ) | 
|---|
| 81 |     , m_par_begin( par_begin ) | 
|---|
| 82 |     , m_par_end( par_end ) | 
|---|
| 83 |     {} | 
|---|
| 84 |  | 
|---|
| 85 |     test_unit* next() const | 
|---|
| 86 |     { | 
|---|
| 87 |         if( m_par_begin == m_par_end ) | 
|---|
| 88 |             return (test_unit*)0; | 
|---|
| 89 |  | 
|---|
| 90 |         test_func_with_bound_param<ParamType> bound_test_func( m_test_func, *m_par_begin ); | 
|---|
| 91 | #if BOOST_WORKAROUND(BOOST_MSVC,<1300) | 
|---|
| 92 |         0; | 
|---|
| 93 | #endif | 
|---|
| 94 |         test_unit* res = new test_case( m_tc_name, bound_test_func ); | 
|---|
| 95 |  | 
|---|
| 96 |         ++m_par_begin; | 
|---|
| 97 |  | 
|---|
| 98 |         return res; | 
|---|
| 99 |     } | 
|---|
| 100 |  | 
|---|
| 101 | private: | 
|---|
| 102 |     // Data members | 
|---|
| 103 |     callback1<ParamType>    m_test_func; | 
|---|
| 104 |     std::string             m_tc_name; | 
|---|
| 105 |     mutable ParamIter       m_par_begin; | 
|---|
| 106 |     ParamIter               m_par_end; | 
|---|
| 107 | }; | 
|---|
| 108 |  | 
|---|
| 109 | //____________________________________________________________________________// | 
|---|
| 110 |  | 
|---|
| 111 | template<typename UserTestCase,typename ParamType> | 
|---|
| 112 | struct user_param_tc_method_invoker { | 
|---|
| 113 |     typedef void (UserTestCase::*test_method)( ParamType ); | 
|---|
| 114 |  | 
|---|
| 115 |     // Constructor | 
|---|
| 116 |     user_param_tc_method_invoker( shared_ptr<UserTestCase> inst, test_method tm ) | 
|---|
| 117 |     : m_inst( inst ), m_test_method( tm ) {} | 
|---|
| 118 |  | 
|---|
| 119 |     void operator()( ParamType p ) { ((*m_inst).*m_test_method)( p ); } | 
|---|
| 120 |  | 
|---|
| 121 |     // Data members | 
|---|
| 122 |     shared_ptr<UserTestCase> m_inst; | 
|---|
| 123 |     test_method              m_test_method; | 
|---|
| 124 | }; | 
|---|
| 125 |  | 
|---|
| 126 | //____________________________________________________________________________// | 
|---|
| 127 |  | 
|---|
| 128 | } // namespace ut_detail | 
|---|
| 129 |  | 
|---|
| 130 | template<typename ParamType, typename ParamIter> | 
|---|
| 131 | inline ut_detail::param_test_case_generator<ParamType,ParamIter> | 
|---|
| 132 | make_test_case( callback1<ParamType> const& test_func, | 
|---|
| 133 |                 const_string   tc_name,  | 
|---|
| 134 |                 ParamIter      par_begin, | 
|---|
| 135 |                 ParamIter      par_end ) | 
|---|
| 136 | { | 
|---|
| 137 |     return ut_detail::param_test_case_generator<ParamType,ParamIter>( test_func, tc_name, par_begin, par_end ); | 
|---|
| 138 | } | 
|---|
| 139 |  | 
|---|
| 140 | //____________________________________________________________________________// | 
|---|
| 141 |  | 
|---|
| 142 | template<typename ParamType, typename ParamIter> | 
|---|
| 143 | inline ut_detail::param_test_case_generator< | 
|---|
| 144 |     BOOST_DEDUCED_TYPENAME remove_const<BOOST_DEDUCED_TYPENAME remove_reference<ParamType>::type>::type,ParamIter> | 
|---|
| 145 | make_test_case( void (*test_func)( ParamType ), | 
|---|
| 146 |                 const_string  tc_name,  | 
|---|
| 147 |                 ParamIter     par_begin, | 
|---|
| 148 |                 ParamIter     par_end ) | 
|---|
| 149 | { | 
|---|
| 150 |     typedef BOOST_DEDUCED_TYPENAME remove_const<BOOST_DEDUCED_TYPENAME remove_reference<ParamType>::type>::type param_value_type; | 
|---|
| 151 |     return ut_detail::param_test_case_generator<param_value_type,ParamIter>( test_func, tc_name, par_begin, par_end ); | 
|---|
| 152 | } | 
|---|
| 153 |  | 
|---|
| 154 | //____________________________________________________________________________// | 
|---|
| 155 |  | 
|---|
| 156 | template<typename UserTestCase,typename ParamType, typename ParamIter> | 
|---|
| 157 | inline ut_detail::param_test_case_generator< | 
|---|
| 158 |     BOOST_DEDUCED_TYPENAME remove_const<BOOST_DEDUCED_TYPENAME remove_reference<ParamType>::type>::type,ParamIter> | 
|---|
| 159 | make_test_case( void (UserTestCase::*test_method )( ParamType ), | 
|---|
| 160 |                 const_string                           tc_name, | 
|---|
| 161 |                 boost::shared_ptr<UserTestCase> const& user_test_case, | 
|---|
| 162 |                 ParamIter                              par_begin, | 
|---|
| 163 |                 ParamIter                              par_end ) | 
|---|
| 164 | { | 
|---|
| 165 |     typedef BOOST_DEDUCED_TYPENAME remove_const<BOOST_DEDUCED_TYPENAME remove_reference<ParamType>::type>::type param_value_type; | 
|---|
| 166 |     return ut_detail::param_test_case_generator<param_value_type,ParamIter>(  | 
|---|
| 167 |                ut_detail::user_param_tc_method_invoker<UserTestCase,ParamType>( user_test_case, test_method ),  | 
|---|
| 168 |                tc_name, | 
|---|
| 169 |                par_begin, | 
|---|
| 170 |                par_end ); | 
|---|
| 171 | } | 
|---|
| 172 |  | 
|---|
| 173 | //____________________________________________________________________________// | 
|---|
| 174 |  | 
|---|
| 175 | } // unit_test | 
|---|
| 176 |  | 
|---|
| 177 | } // namespace boost | 
|---|
| 178 |  | 
|---|
| 179 | //____________________________________________________________________________// | 
|---|
| 180 |  | 
|---|
| 181 | #include <boost/test/detail/enable_warnings.hpp> | 
|---|
| 182 |  | 
|---|
| 183 | // *************************************************************************** | 
|---|
| 184 | //  Revision History : | 
|---|
| 185 | //   | 
|---|
| 186 | //  $Log: parameterized_test.hpp,v $ | 
|---|
| 187 | //  Revision 1.4  2005/05/02 06:00:10  rogeeff | 
|---|
| 188 | //  restore a parameterized user case method based testing | 
|---|
| 189 | // | 
|---|
| 190 | //  Revision 1.3  2005/03/21 15:32:31  rogeeff | 
|---|
| 191 | //  check reworked | 
|---|
| 192 | // | 
|---|
| 193 | //  Revision 1.2  2005/02/21 10:25:04  rogeeff | 
|---|
| 194 | //  remove const during ParamType deduction | 
|---|
| 195 | // | 
|---|
| 196 | //  Revision 1.1  2005/02/20 08:27:06  rogeeff | 
|---|
| 197 | //  This a major update for Boost.Test framework. See release docs for complete list of fixes/updates | 
|---|
| 198 | // | 
|---|
| 199 | // *************************************************************************** | 
|---|
| 200 |  | 
|---|
| 201 | #endif // BOOST_TEST_PARAMETERIZED_TEST_HPP_021102GER | 
|---|
| 202 |  | 
|---|