parameterized_test.hpp 6.9 KB

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