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)
6 // See http://www.boost.org/libs/test for the library home page.
9 ///Defines range generator
10 // ***************************************************************************
12 #ifndef BOOST_TEST_DATA_MONOMORPHIC_GENERATORS_XRANGE_HPP_112011GER
13 #define BOOST_TEST_DATA_MONOMORPHIC_GENERATORS_XRANGE_HPP_112011GER
16 #include <boost/test/data/config.hpp>
18 #include <boost/test/data/monomorphic/generators/keywords.hpp>
19 #include <boost/test/data/monomorphic/generate.hpp>
22 #include <boost/optional.hpp>
23 #include <boost/utility/enable_if.hpp>
24 #include <boost/type_traits/is_unsigned.hpp>
30 #include <boost/test/detail/suppress_warnings.hpp>
32 //____________________________________________________________________________//
37 namespace monomorphic {
39 // ************************************************************************** //
40 // ************** monomorphic::xrange_t ************** //
41 // ************************************************************************** //
44 /*!@brief Generator for the range sequences
46 * This class implements the generator concept (see @ref boost::unit_test::data::monomorphic::generated_by) for implementing
47 * a range like sequence of numbers.
49 template<typename SampleType, typename StepType=SampleType>
52 typedef SampleType sample;
54 xrange_t( SampleType const& begin_, StepType const& step_, data::size_t size_ )
62 // Generator interface
63 data::size_t capacity() const { return m_size; }
66 if( m_index == m_size )
69 SampleType res = m_curr;
91 //____________________________________________________________________________//
95 template<typename SampleType, typename StepType=SampleType>
97 static StepType abs( StepType s, boost::true_type* ) { return s; }
98 static StepType abs( StepType s, boost::false_type* ) { return std::abs(s); }
100 typedef xrange_t<SampleType, StepType> range_gen;
102 template<typename Params>
103 static generated_by<range_gen>
104 _( Params const& params )
106 SampleType begin_val = params.has( data::begin ) ? params[data::begin] : SampleType();
107 optional<SampleType> end_val = params.has( data::end ) ? params[data::end] : optional<SampleType>();
108 StepType step_val = params.has( data::step ) ? params[data::step] : 1;
110 BOOST_TEST_DS_ASSERT( step_val != 0, "Range step can't be zero" );
113 if( !end_val.is_initialized() )
114 size = BOOST_TEST_DS_INFINITE_SIZE;
116 BOOST_TEST_DS_ASSERT( (step_val < 0) ^ (begin_val < *end_val), "Invalid step direction" );
118 SampleType abs_distance = step_val < 0 ? begin_val - *end_val : *end_val-begin_val;
119 StepType abs_step = make_xrange::abs(step_val, (typename boost::is_unsigned<StepType>::type*)0 );
120 std::size_t s = static_cast<std::size_t>(abs_distance/abs_step);
122 if( static_cast<SampleType>(s*abs_step) < abs_distance )
128 return generated_by<range_gen>( range_gen( begin_val, step_val, size ) );
132 } // namespace ds_detail
133 } // namespace monomorphic
135 //____________________________________________________________________________//
137 //! Creates a range (sequence) dataset.
139 //! The following overloads are available:
141 //! auto d = xrange();
142 //! auto d = xrange(end_val);
143 //! auto d = xrange(end_val, param);
144 //! auto d = xrange(begin_val, end_val);
145 //! auto d = xrange(begin_val, end_val, step_val);
146 //! auto d = xrange(param);
149 //! - @c begin_val indicates the start of the sequence (default to 0).
150 //! - @c end_val is the end of the sequence. If ommited, the dataset has infinite size.\n
151 //! - @c step_val is the step between two consecutive elements of the sequence, and defaults to 1.\n
152 //! - @c param is the named parameters that describe the sequence. The following parameters are accepted:
153 //! - @c begin: same meaning @c begin_val
154 //! - @c end: same meaning as @c end_val
155 //! - @c step: same meaning as @c step_val
158 //! The returned value is an object that implements the dataset API.
160 //! @note the step size cannot be null, and it should be positive if @c begin_val < @c end_val, negative otherwise.
161 template<typename SampleType, typename Params>
162 inline monomorphic::generated_by<monomorphic::xrange_t<SampleType>>
163 xrange( Params const& params )
165 return monomorphic::ds_detail::make_xrange<SampleType>::_( params );
168 //____________________________________________________________________________//
170 /// @overload boost::unit_test::data::xrange()
171 template<typename SampleType>
172 inline monomorphic::generated_by<monomorphic::xrange_t<SampleType>>
173 xrange( SampleType const& end_val )
175 return monomorphic::ds_detail::make_xrange<SampleType>::_( data::end=end_val );
178 //____________________________________________________________________________//
180 /// @overload boost::unit_test::data::xrange()
181 template<typename SampleType, typename Params>
182 inline typename enable_if_c<nfp::is_named_param_pack<Params>::value,
183 monomorphic::generated_by<monomorphic::xrange_t<SampleType>>>::type
184 xrange( SampleType const& end_val, Params const& params )
186 return monomorphic::ds_detail::make_xrange<SampleType>::_(( params, data::end=end_val ));
189 //____________________________________________________________________________//
191 /// @overload boost::unit_test::data::xrange()
192 template<typename SampleType>
193 inline monomorphic::generated_by<monomorphic::xrange_t<SampleType>>
194 xrange( SampleType const& begin_val, SampleType const& end_val )
196 return monomorphic::ds_detail::make_xrange<SampleType>::_((
197 data::begin=begin_val,
198 data::end=end_val ));
201 //____________________________________________________________________________//
205 /// @overload boost::unit_test::data::xrange()
206 template<typename SampleType,typename StepType>
207 inline monomorphic::generated_by<monomorphic::xrange_t<SampleType>>
208 xrange( SampleType const& begin_val, SampleType const& end_val, StepType const& step_val )
210 return monomorphic::ds_detail::make_xrange<SampleType,StepType>::_((
211 data::begin=begin_val,
213 data::step=step_val ));
216 //____________________________________________________________________________//
219 } // namespace unit_test
222 #include <boost/test/detail/enable_warnings.hpp>
224 #endif // BOOST_TEST_DATA_MONOMORPHIC_GENERATORS_XRANGE_HPP_112011GER