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