parameterized_test.hpp 7.0 KB

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