• 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 monomorphic dataset n+m dimentional *. Samples in this
10 /// dataset is grid of elements in DataSet1 and DataSet2. There will be total
11 /// |DataSet1| * |DataSet2| samples
12 // ***************************************************************************
13 
14 #ifndef BOOST_TEST_DATA_MONOMORPHIC_GRID_HPP_101512GER
15 #define BOOST_TEST_DATA_MONOMORPHIC_GRID_HPP_101512GER
16 
17 // Boost.Test
18 #include <boost/test/data/config.hpp>
19 
20 #if !defined(BOOST_TEST_NO_GRID_COMPOSITION_AVAILABLE) || defined(BOOST_TEST_DOXYGEN_DOC__)
21 
22 #include <boost/test/data/monomorphic/fwd.hpp>
23 #include <boost/test/data/monomorphic/sample_merge.hpp>
24 
25 #include <boost/core/enable_if.hpp>
26 #include <boost/mpl/identity.hpp>
27 
28 #include <boost/test/detail/suppress_warnings.hpp>
29 
30 //____________________________________________________________________________//
31 
32 namespace boost {
33 namespace unit_test {
34 namespace data {
35 namespace monomorphic {
36 
37 // ************************************************************************** //
38 // **************                       grid                    ************** //
39 // ************************************************************************** //
40 
41 
42 //! Implements the dataset resulting from a cartesian product/grid operation on datasets.
43 //!
44 //! The arity of the resulting dataset is the sum of the arity of its operands.
45 template<typename DataSet1, typename DataSet2>
46 class grid {
47     typedef typename boost::decay<DataSet1>::type   dataset1_decay;
48     typedef typename boost::decay<DataSet2>::type   dataset2_decay;
49 
50     typedef typename dataset1_decay::iterator       dataset1_iter;
51     typedef typename dataset2_decay::iterator       dataset2_iter;
52 
53 public:
54 
55     struct iterator {
56         // Constructor
iteratorboost::unit_test::data::monomorphic::grid::iterator57         explicit    iterator( dataset1_iter iter1, DataSet2 const& ds2 )
58         : m_iter1( std::move( iter1 ) )
59         , m_iter2( std::move( ds2.begin() ) )
60         , m_ds2( &ds2 )
61         , m_ds2_pos( 0 )
62         {}
63 
64         using iterator_sample = decltype(
65             sample_merge( *std::declval<dataset1_iter>(),
66                           *std::declval<dataset2_iter>()) );
67 
68         // forward iterator interface
operator *boost::unit_test::data::monomorphic::grid::iterator69         auto            operator*() const -> iterator_sample {
70             return sample_merge( *m_iter1, *m_iter2 );
71         }
operator ++boost::unit_test::data::monomorphic::grid::iterator72         void            operator++()
73         {
74             ++m_ds2_pos;
75             if( m_ds2_pos != m_ds2->size() )
76                 ++m_iter2;
77             else {
78                 m_ds2_pos = 0;
79                 ++m_iter1;
80                 m_iter2 = std::move( m_ds2->begin() );
81             }
82         }
83 
84     private:
85         // Data members
86         dataset1_iter   m_iter1;
87         dataset2_iter   m_iter2;
88         dataset2_decay const* m_ds2;
89         data::size_t    m_ds2_pos;
90     };
91 
92 public:
93     enum { arity = boost::decay<DataSet1>::type::arity + boost::decay<DataSet2>::type::arity };
94 
95     //! Constructor
grid(DataSet1 && ds1,DataSet2 && ds2)96     grid( DataSet1&& ds1, DataSet2&& ds2 )
97     : m_ds1( std::forward<DataSet1>( ds1 ) )
98     , m_ds2( std::forward<DataSet2>( ds2 ) )
99     {}
100 
101     //! Move constructor
grid(grid && j)102     grid( grid&& j )
103     : m_ds1( std::forward<DataSet1>( j.m_ds1 ) )
104     , m_ds2( std::forward<DataSet2>( j.m_ds2 ) )
105     {}
106 
107     // dataset interface
size() const108     data::size_t    size() const    {
109       BOOST_TEST_DS_ASSERT( !m_ds1.size().is_inf() && !m_ds2.size().is_inf(), "Grid axes can't have infinite size" );
110       return m_ds1.size() * m_ds2.size();
111     }
begin() const112     iterator        begin() const   { return iterator( m_ds1.begin(), m_ds2 ); }
113 
114 private:
115     // Data members
116     DataSet1             m_ds1;
117     DataSet2             m_ds2;
118 };
119 
120 //____________________________________________________________________________//
121 
122 // A grid dataset is a dataset
123 template<typename DataSet1, typename DataSet2>
124 struct is_dataset<grid<DataSet1,DataSet2>> : mpl::true_ {};
125 
126 //____________________________________________________________________________//
127 
128 namespace result_of {
129 
130 /// Result type of the grid operation on dataset.
131 template<typename DS1Gen, typename DS2Gen>
132 struct grid {
133     typedef monomorphic::grid<typename DS1Gen::type,typename DS2Gen::type> type;
134 };
135 
136 } // namespace result_of
137 
138 //____________________________________________________________________________//
139 
140 //! Grid operation
141 template<typename DataSet1, typename DataSet2>
142 inline typename boost::lazy_enable_if_c<is_dataset<DataSet1>::value && is_dataset<DataSet2>::value,
143                                         result_of::grid<mpl::identity<DataSet1>,mpl::identity<DataSet2>>
144 >::type
operator *(DataSet1 && ds1,DataSet2 && ds2)145 operator*( DataSet1&& ds1, DataSet2&& ds2 )
146 {
147     return grid<DataSet1,DataSet2>( std::forward<DataSet1>( ds1 ),  std::forward<DataSet2>( ds2 ) );
148 }
149 
150 //____________________________________________________________________________//
151 
152 //! @overload boost::unit_test::data::operator*
153 template<typename DataSet1, typename DataSet2>
154 inline typename boost::lazy_enable_if_c<is_dataset<DataSet1>::value && !is_dataset<DataSet2>::value,
155                                         result_of::grid<mpl::identity<DataSet1>,data::result_of::make<DataSet2>>
156 >::type
operator *(DataSet1 && ds1,DataSet2 && ds2)157 operator*( DataSet1&& ds1, DataSet2&& ds2 )
158 {
159     return std::forward<DataSet1>(ds1) * data::make(std::forward<DataSet2>(ds2));
160 }
161 
162 //____________________________________________________________________________//
163 
164 //! @overload boost::unit_test::data::operator*
165 template<typename DataSet1, typename DataSet2>
166 inline typename boost::lazy_enable_if_c<!is_dataset<DataSet1>::value && is_dataset<DataSet2>::value,
167                                         result_of::grid<data::result_of::make<DataSet1>,mpl::identity<DataSet2>>
168 >::type
operator *(DataSet1 && ds1,DataSet2 && ds2)169 operator*( DataSet1&& ds1, DataSet2&& ds2 )
170 {
171     return data::make(std::forward<DataSet1>(ds1)) * std::forward<DataSet2>(ds2);
172 }
173 
174 } // namespace monomorphic
175 
176 } // namespace data
177 } // namespace unit_test
178 } // namespace boost
179 
180 #include <boost/test/detail/enable_warnings.hpp>
181 
182 #endif // BOOST_TEST_NO_GRID_COMPOSITION_AVAILABLE
183 
184 #endif // BOOST_TEST_DATA_MONOMORPHIC_GRID_HPP_101512GER
185 
186