3 libs/numeric/odeint/test/const_step_time_iterator.cpp
6 This file tests the const step time iterator.
9 Copyright 2012-2013 Karsten Ahnert
10 Copyright 2012-2013 Mario Mulansky
12 Distributed under the Boost Software License, Version 1.0.
13 (See accompanying file LICENSE_1_0.txt or
14 copy at http://www.boost.org/LICENSE_1_0.txt)
18 #define BOOST_TEST_MODULE odeint_const_step_time_iterator
24 #include <boost/numeric/odeint/config.hpp>
25 #include <boost/array.hpp>
26 #include <boost/range/algorithm/copy.hpp>
27 #include <boost/range/algorithm/for_each.hpp>
28 #include <boost/mpl/vector.hpp>
30 #include <boost/test/unit_test.hpp>
31 #include <boost/test/floating_point_comparison.hpp>
33 #include <boost/numeric/odeint/iterator/const_step_time_iterator.hpp>
34 #include "dummy_steppers.hpp"
35 #include "dummy_odes.hpp"
36 #include "dummy_observers.hpp"
38 namespace mpl
= boost::mpl
;
39 using namespace boost::numeric::odeint
;
41 typedef dummy_stepper::state_type state_type
;
42 typedef dummy_stepper::value_type value_type
;
43 typedef dummy_stepper::time_type time_type
;
44 typedef std::vector
< std::pair
< state_type
, time_type
> > result_vector
;
46 BOOST_AUTO_TEST_SUITE( const_step_time_iterator_test
)
50 , dummy_dense_output_stepper
55 BOOST_AUTO_TEST_CASE_TEMPLATE( copy_stepper_iterator
, Stepper
, dummy_steppers
)
57 typedef const_step_time_iterator
< Stepper
, empty_system
, state_type
> iterator_type
;
58 state_type x
= {{ 1.0 }};
59 iterator_type iter1
= iterator_type( Stepper() , empty_system() , x
, 0.0 , 1.0 , 0.1 );
60 iterator_type iter2
= iter1
;
61 BOOST_CHECK_EQUAL( &( iter1
->first
) , &( iter2
->first
) );
62 BOOST_CHECK_EQUAL( &( iter1
->first
) , &x
);
63 BOOST_CHECK( iter1
.same( iter2
) );
66 BOOST_AUTO_TEST_CASE_TEMPLATE( assignment_stepper_iterator
, Stepper
, dummy_steppers
)
68 typedef const_step_time_iterator
< Stepper
, empty_system
, state_type
> iterator_type
;
69 state_type x1
= {{ 1.0 }} , x2
= {{ 2.0 }};
70 iterator_type iter1
= iterator_type( Stepper() , empty_system() , x1
, 0.0 , 1.0 , 0.1 );
71 iterator_type iter2
= iterator_type( Stepper() , empty_system() , x2
, 0.0 , 1.0 , 0.2 );
72 BOOST_CHECK_EQUAL( &( iter1
->first
) , &x1
);
73 BOOST_CHECK_EQUAL( &( iter2
->first
) , &x2
);
74 BOOST_CHECK( !iter1
.same( iter2
) );
76 BOOST_CHECK_EQUAL( &( iter1
->first
) , &x1
);
77 BOOST_CHECK_EQUAL( &( iter2
->first
) , &x1
);
78 BOOST_CHECK( iter1
.same( iter2
) );
81 BOOST_AUTO_TEST_CASE_TEMPLATE( stepper_iterator_factory
, Stepper
, dummy_steppers
)
85 state_type x
= {{ 1.0 }};
88 make_const_step_time_iterator_begin( stepper
, boost::ref( system
) , x
, 0.0 , 1.0 , 0.1 ) ,
89 make_const_step_time_iterator_end( stepper
, boost::ref( system
) , x
) ,
92 BOOST_CHECK_CLOSE( x
[0] , 3.5 , 1.0e-13 );
95 BOOST_AUTO_TEST_CASE_TEMPLATE( stepper_range
, Stepper
, dummy_steppers
)
99 state_type x
= {{ 1.0 }};
101 boost::for_each( make_const_step_time_range( stepper
, boost::ref( system
) , x
, 0.0 , 1.0 , 0.1 ) ,
104 BOOST_CHECK_CLOSE( x
[0] , 3.5 , 1.0e-13 );
107 BOOST_AUTO_TEST_CASE_TEMPLATE( stepper_iterator_with_reference_wrapper_factory
, Stepper
, dummy_steppers
)
111 state_type x
= {{ 1.0 }};
114 make_const_step_time_iterator_begin( boost::ref( stepper
) , boost::ref( system
) , x
, 0.0 , 1.0 , 0.1 ) ,
115 make_const_step_time_iterator_end( boost::ref( stepper
) , boost::ref( system
) , x
) ,
118 BOOST_CHECK_CLOSE( x
[0] , 3.5 , 1.0e-13 );
121 BOOST_AUTO_TEST_CASE_TEMPLATE( stepper_range_with_reference_wrapper
, Stepper
, dummy_steppers
)
125 state_type x
= {{ 1.0 }};
127 boost::for_each( make_const_step_time_range( boost::ref( stepper
) , boost::ref( system
) , x
, 0.0 , 1.0 , 0.1 ) ,
130 BOOST_CHECK_CLOSE( x
[0] , 3.5 , 1.0e-13 );
135 BOOST_AUTO_TEST_CASE_TEMPLATE( transitivity1
, Stepper
, dummy_steppers
)
137 typedef const_step_time_iterator
< Stepper
, empty_system
, state_type
> stepper_iterator
;
139 state_type x
= {{ 1.0 }};
140 stepper_iterator
first1( Stepper() , empty_system() , x
, 1.5 , 1.0 , 0.1 );
141 stepper_iterator
last1( Stepper() , empty_system() , x
);
142 stepper_iterator
last2( Stepper() , empty_system() , x
);
144 BOOST_CHECK( first1
== last1
);
145 BOOST_CHECK( first1
== last2
);
146 BOOST_CHECK( last1
== last2
);
148 first1
= stepper_iterator( Stepper() , empty_system() , x
, 2.0 , 2.0 , 0.1 );
149 last1
= stepper_iterator( Stepper() , empty_system() , x
);
150 BOOST_CHECK( first1
!= last1
);
151 BOOST_CHECK( ++first1
== last1
);
154 BOOST_AUTO_TEST_CASE_TEMPLATE( copy_algorithm
, Stepper
, dummy_steppers
)
156 typedef const_step_time_iterator
< Stepper
, empty_system
, state_type
> stepper_iterator
;
157 state_type x
= {{ 1.0 }};
159 stepper_iterator
first( Stepper() , empty_system() , x
, 0.0 , 0.35 , 0.1 );
160 stepper_iterator
last( Stepper() , empty_system() , x
);
162 std::copy( first
, last
, std::back_insert_iterator
< result_vector
>( res
) );
164 BOOST_CHECK_EQUAL( res
.size() , size_t( 4 ) );
165 BOOST_CHECK_CLOSE( res
[0].first
[0] , 1.0 , 1.0e-13 );
166 BOOST_CHECK_CLOSE( res
[0].second
, 0.0 , 1.0e-13 );
167 BOOST_CHECK_CLOSE( res
[1].first
[0] , 1.25 , 1.0e-13 );
168 BOOST_CHECK_CLOSE( res
[1].second
, 0.1 , 1.0e-13 );
169 BOOST_CHECK_CLOSE( res
[2].first
[0] , 1.5 , 1.0e-13 );
170 BOOST_CHECK_CLOSE( res
[2].second
, 0.2 , 1.0e-13 );
171 BOOST_CHECK_CLOSE( res
[3].first
[0] , 1.75 , 1.0e-13 );
172 BOOST_CHECK_CLOSE( res
[3].second
, 0.3 , 1.0e-13 );
173 BOOST_CHECK_CLOSE( x
[0] , 1.75 , 1.0e-13 );
176 BOOST_AUTO_TEST_CASE_TEMPLATE( copy_algorithm_with_factory
, Stepper
, dummy_steppers
)
178 state_type x
= {{ 1.0 }};
180 std::copy( make_const_step_time_iterator_begin( Stepper() , empty_system() , x
, 0.0 , 0.35 , 0.1 ) ,
181 make_const_step_time_iterator_end( Stepper() , empty_system() , x
) ,
182 std::back_insert_iterator
< result_vector
>( res
) );
184 BOOST_CHECK_EQUAL( res
.size() , size_t( 4 ) );
185 BOOST_CHECK_CLOSE( res
[0].first
[0] , 1.0 , 1.0e-13 );
186 BOOST_CHECK_CLOSE( res
[0].second
, 0.0 , 1.0e-13 );
187 BOOST_CHECK_CLOSE( res
[1].first
[0] , 1.25 , 1.0e-13 );
188 BOOST_CHECK_CLOSE( res
[1].second
, 0.1 , 1.0e-13 );
189 BOOST_CHECK_CLOSE( res
[2].first
[0] , 1.5 , 1.0e-13 );
190 BOOST_CHECK_CLOSE( res
[2].second
, 0.2 , 1.0e-13 );
191 BOOST_CHECK_CLOSE( res
[3].first
[0] , 1.75 , 1.0e-13 );
192 BOOST_CHECK_CLOSE( res
[3].second
, 0.3 , 1.0e-13 );
193 BOOST_CHECK_CLOSE( x
[0] , 1.75 , 1.0e-13 );
196 BOOST_AUTO_TEST_CASE_TEMPLATE( copy_algorithm_with_range_factory
, Stepper
, dummy_steppers
)
198 state_type x
= {{ 1.0 }};
200 boost::range::copy( make_const_step_time_range( Stepper() , empty_system() , x
, 0.0 , 0.35 , 0.1 ) ,
201 std::back_insert_iterator
< result_vector
>( res
) );
203 BOOST_CHECK_EQUAL( res
.size() , size_t( 4 ) );
204 BOOST_CHECK_CLOSE( res
[0].first
[0] , 1.0 , 1.0e-13 );
205 BOOST_CHECK_CLOSE( res
[0].second
, 0.0 , 1.0e-13 );
206 BOOST_CHECK_CLOSE( res
[1].first
[0] , 1.25 , 1.0e-13 );
207 BOOST_CHECK_CLOSE( res
[1].second
, 0.1 , 1.0e-13 );
208 BOOST_CHECK_CLOSE( res
[2].first
[0] , 1.5 , 1.0e-13 );
209 BOOST_CHECK_CLOSE( res
[2].second
, 0.2 , 1.0e-13 );
210 BOOST_CHECK_CLOSE( res
[3].first
[0] , 1.75 , 1.0e-13 );
211 BOOST_CHECK_CLOSE( res
[3].second
, 0.3 , 1.0e-13 );
212 BOOST_CHECK_CLOSE( x
[0] , 1.75 , 1.0e-13 );
218 BOOST_AUTO_TEST_SUITE_END()