for_each_sample.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. /// Defines for_each_sample algorithm
  9. // ***************************************************************************
  10. #ifndef BOOST_TEST_DATA_FOR_EACH_SAMPLE_HPP_102211GER
  11. #define BOOST_TEST_DATA_FOR_EACH_SAMPLE_HPP_102211GER
  12. // Boost.Test
  13. #include <boost/test/data/config.hpp>
  14. #include <boost/test/data/size.hpp>
  15. #include <boost/test/data/index_sequence.hpp>
  16. #include <boost/test/data/monomorphic/sample_merge.hpp>
  17. #include <boost/test/data/monomorphic/fwd.hpp>
  18. // STL
  19. #include <tuple>
  20. #include <boost/test/detail/suppress_warnings.hpp>
  21. // needed for std::min
  22. #include <algorithm>
  23. //____________________________________________________________________________//
  24. namespace boost {
  25. namespace unit_test {
  26. namespace data {
  27. // ************************************************************************** //
  28. // ************** data::invoke_action ************** //
  29. // ************************************************************************** //
  30. template<typename Action, typename T>
  31. inline void
  32. invoke_action( Action const& action, T && arg, std::false_type /* is_tuple */ )
  33. {
  34. action( std::forward<T>(arg) );
  35. }
  36. //____________________________________________________________________________//
  37. template<typename Action, typename T, std::size_t ...I>
  38. inline void
  39. invoke_action_impl( Action const& action,
  40. T && args,
  41. index_sequence<I...> const& )
  42. {
  43. action( std::get<I>(std::forward<T>(args))... );
  44. }
  45. //____________________________________________________________________________//
  46. template<typename Action, typename T>
  47. inline void
  48. invoke_action( Action const& action, T&& args, std::true_type /* is_tuple */ )
  49. {
  50. invoke_action_impl( action,
  51. std::forward<T>(args),
  52. typename make_index_sequence< 0,
  53. std::tuple_size<typename std::decay<T>::type>::value
  54. >::type{} );
  55. }
  56. //____________________________________________________________________________//
  57. // ************************************************************************** //
  58. // ************** for_each_sample ************** //
  59. // ************************************************************************** //
  60. template<typename DataSet, typename Action>
  61. inline typename std::enable_if<monomorphic::is_dataset<DataSet>::value,void>::type
  62. for_each_sample( DataSet const & samples,
  63. Action const& act,
  64. data::size_t number_of_samples = BOOST_TEST_DS_INFINITE_SIZE )
  65. {
  66. data::size_t size = (std::min)( samples.size(), number_of_samples );
  67. BOOST_TEST_DS_ASSERT( !size.is_inf(), "Dataset has infinite size. Please specify the number of samples" );
  68. auto it = samples.begin();
  69. while( size-- > 0 ) {
  70. invoke_action( act,
  71. *it,
  72. typename monomorphic::ds_detail::is_tuple<decltype(*it)>::type());
  73. ++it;
  74. }
  75. }
  76. //____________________________________________________________________________//
  77. template<typename DataSet, typename Action>
  78. inline typename std::enable_if<!monomorphic::is_dataset<DataSet>::value,void>::type
  79. for_each_sample( DataSet && samples,
  80. Action const& act,
  81. data::size_t number_of_samples = BOOST_TEST_DS_INFINITE_SIZE )
  82. {
  83. data::for_each_sample( data::make( std::forward<DataSet>(samples) ),
  84. act,
  85. number_of_samples );
  86. }
  87. } // namespace data
  88. } // namespace unit_test
  89. } // namespace boost
  90. #include <boost/test/detail/enable_warnings.hpp>
  91. #endif // BOOST_TEST_DATA_FOR_EACH_SAMPLE_HPP_102211GER