3 libs/numeric/odeint/test/const_step_iterator.cpp
6 This file tests the const step iterator.
9 Copyright 2012-2013 Karsten Ahnert
10 Copyright 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_iterator
24 #include <boost/numeric/odeint/config.hpp>
25 #include <boost/array.hpp>
26 #include <boost/range/algorithm/for_each.hpp>
27 #include <boost/range/algorithm/copy.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_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;
45 BOOST_AUTO_TEST_SUITE( const_step_iterator_test )
49 , dummy_dense_output_stepper
53 BOOST_AUTO_TEST_CASE_TEMPLATE( copy_stepper_iterator , Stepper , dummy_steppers )
55 typedef const_step_iterator< Stepper , empty_system , state_type > iterator_type;
56 state_type x = {{ 1.0 }};
57 iterator_type iter1 = iterator_type( Stepper() , empty_system() , x , 0.0 , 0.999 , 0.1 );
58 iterator_type iter2 = iter1;
59 BOOST_CHECK_EQUAL( &(*iter1) , &(*iter2) );
60 BOOST_CHECK_EQUAL( &(*iter1) , &x );
61 BOOST_CHECK( iter1.same( iter2 ) );
65 BOOST_AUTO_TEST_CASE_TEMPLATE( assignment_stepper_iterator , Stepper , dummy_steppers )
67 typedef const_step_iterator< Stepper , empty_system , state_type > iterator_type;
68 state_type x1 = {{ 1.0 }} , x2 = {{ 2.0 }};
69 iterator_type iter1 = iterator_type( Stepper() , empty_system() , x1 , 0.0 , 1.0 , 0.1 );
70 iterator_type iter2 = iterator_type( Stepper() , empty_system() , x2 , 0.0 , 1.0 , 0.2 );
71 BOOST_CHECK_EQUAL( &(*iter1) , &x1 );
72 BOOST_CHECK_EQUAL( &(*iter2) , &x2 );
73 BOOST_CHECK( !iter1.same( iter2 ) );
75 BOOST_CHECK_EQUAL( &(*iter1) , &x1 );
76 BOOST_CHECK_EQUAL( &(*iter2) , &x1 );
77 BOOST_CHECK( iter1.same( iter2 ) );
82 BOOST_AUTO_TEST_CASE_TEMPLATE( stepper_iterator_factory , Stepper , dummy_steppers )
86 state_type x = {{ 1.0 }};
89 make_const_step_iterator_begin( stepper , boost::ref( system ) , x , 0.0 , 1.0 , 0.1 ) ,
90 make_const_step_iterator_end( stepper , boost::ref( system ) , x ) ,
93 // dummy_steppers just add 0.25 at each step, the above for_each leads to 10 do_step calls so x should be 3.5
94 BOOST_CHECK_CLOSE( x[0] , 3.5 , 1.0e-13 );
97 BOOST_AUTO_TEST_CASE_TEMPLATE( stepper_range , Stepper , dummy_steppers )
101 state_type x = {{ 1.0 }};
103 boost::for_each( make_const_step_range( stepper , boost::ref( system ) , x , 0.0 , 1.0 , 0.1 ) ,
106 BOOST_CHECK_CLOSE( x[0] , 3.5 , 1.0e-13 );
109 BOOST_AUTO_TEST_CASE_TEMPLATE( stepper_iterator_with_reference_wrapper_factory , Stepper , dummy_steppers )
113 state_type x = {{ 1.0 }};
116 make_const_step_iterator_begin( boost::ref( stepper ) , boost::ref( system ) , x , 0.0 , 1.0 , 0.1 ) ,
117 make_const_step_iterator_end( boost::ref( stepper ) , boost::ref( system ) , x ) ,
120 BOOST_CHECK_CLOSE( x[0] , 3.5 , 1.0e-13 );
123 BOOST_AUTO_TEST_CASE_TEMPLATE( stepper_range_with_reference_wrapper , Stepper , dummy_steppers )
127 state_type x = {{ 1.0 }};
129 boost::for_each( make_const_step_range( boost::ref( stepper ) , boost::ref( system ) , x , 0.0 , 1.0 , 0.1 ) ,
132 BOOST_CHECK_CLOSE( x[0] , 3.5 , 1.0e-13 );
137 BOOST_AUTO_TEST_CASE_TEMPLATE( transitivity1 , Stepper , dummy_steppers )
139 typedef const_step_iterator< Stepper , empty_system , state_type > stepper_iterator;
141 state_type x = {{ 1.0 }};
142 stepper_iterator first1( Stepper() , empty_system() , x , 2.5 , 2.0 , 0.1 );
143 stepper_iterator last1( Stepper() , empty_system() , x );
144 stepper_iterator last2( Stepper() , empty_system() , x );
146 BOOST_CHECK( first1 == last1 );
147 BOOST_CHECK( first1 == last2 );
148 BOOST_CHECK( last1 == last2 );
150 first1 = stepper_iterator( Stepper() , empty_system() , x , 2.0 , 2.0 , 0.1 );
151 last1 = stepper_iterator( Stepper() , empty_system() , x );
152 BOOST_CHECK( first1 != last1 );
153 BOOST_CHECK( ++first1 == last1 );
157 BOOST_AUTO_TEST_CASE_TEMPLATE( copy_algorithm , Stepper , dummy_steppers )
159 typedef const_step_iterator< Stepper , empty_system , state_type > stepper_iterator;
160 state_type x = {{ 1.0 }};
161 std::vector< state_type > res;
162 stepper_iterator first( Stepper() , empty_system() , x , 0.0 , 0.35 , 0.1 );
163 stepper_iterator last( Stepper() , empty_system() , x );
165 std::copy( first , last , std::back_insert_iterator< std::vector< state_type > >( res ) );
167 BOOST_CHECK_EQUAL( res.size() , size_t( 4 ) );
168 BOOST_CHECK_CLOSE( res[0][0] , 1.0 , 1.0e-14 );
169 BOOST_CHECK_CLOSE( res[1][0] , 1.25 , 1.0e-14 );
170 BOOST_CHECK_CLOSE( res[2][0] , 1.5 , 1.0e-14 );
171 BOOST_CHECK_CLOSE( res[3][0] , 1.75 , 1.0e-14 );
173 BOOST_CHECK_CLOSE( x[0] , 1.75 , 1.0e-14 ); // the iterator should not iterate over the end
176 BOOST_AUTO_TEST_CASE_TEMPLATE( copy_algorithm_negative_time_step , Stepper , dummy_steppers )
178 typedef const_step_iterator< Stepper , empty_system , state_type > stepper_iterator;
179 state_type x = {{ 1.0 }};
180 std::vector< state_type > res;
181 stepper_iterator first( Stepper() , empty_system() , x , 0.3 , -0.05 , -0.1 );
182 stepper_iterator last( Stepper() , empty_system() , x );
184 std::copy( first , last , std::back_insert_iterator< std::vector< state_type > >( res ) );
186 BOOST_CHECK_EQUAL( res.size() , size_t( 4 ) );
187 BOOST_CHECK_CLOSE( res[0][0] , 1.0 , 1.0e-14 );
188 BOOST_CHECK_CLOSE( res[1][0] , 1.25 , 1.0e-14 );
189 BOOST_CHECK_CLOSE( res[2][0] , 1.5 , 1.0e-14 );
190 BOOST_CHECK_CLOSE( res[3][0] , 1.75 , 1.0e-14 );
192 BOOST_CHECK_CLOSE( x[0] , 1.75 , 1.0e-14 );
196 BOOST_AUTO_TEST_CASE_TEMPLATE( copy_algorithm_with_factory , Stepper , dummy_steppers )
198 state_type x = {{ 1.0 }};
199 std::vector< state_type > res;
200 std::copy( make_const_step_iterator_begin( Stepper() , empty_system() , x , 0.0 , 0.35 , 0.1 ) ,
201 make_const_step_iterator_end( Stepper() , empty_system() , x ) ,
202 std::back_insert_iterator< std::vector< state_type > >( res ) );
204 BOOST_CHECK_EQUAL( res.size() , size_t( 4 ) );
205 BOOST_CHECK_CLOSE( res[0][0] , 1.0 , 1.0e-14 );
206 BOOST_CHECK_CLOSE( res[1][0] , 1.25 , 1.0e-14 );
207 BOOST_CHECK_CLOSE( res[2][0] , 1.5 , 1.0e-14 );
208 BOOST_CHECK_CLOSE( res[3][0] , 1.75 , 1.0e-14 );
210 BOOST_CHECK_CLOSE( x[0] , 1.75 , 1.0e-14 );
213 BOOST_AUTO_TEST_CASE_TEMPLATE( copy_algorithm_with_range_factory , Stepper , dummy_steppers )
215 state_type x = {{ 1.0 }};
216 std::vector< state_type > res;
217 boost::range::copy( make_const_step_range( Stepper() , empty_system() , x , 0.0 , 0.35 , 0.1 ) ,
218 std::back_insert_iterator< std::vector< state_type > >( res ) );
220 BOOST_CHECK_EQUAL( res.size() , size_t( 4 ) );
221 BOOST_CHECK_CLOSE( res[0][0] , 1.0 , 1.0e-14 );
222 BOOST_CHECK_CLOSE( res[1][0] , 1.25 , 1.0e-14 );
223 BOOST_CHECK_CLOSE( res[2][0] , 1.5 , 1.0e-14 );
224 BOOST_CHECK_CLOSE( res[3][0] , 1.75 , 1.0e-14 );
226 BOOST_CHECK_CLOSE( x[0] , 1.75 , 1.0e-14 );
232 BOOST_AUTO_TEST_SUITE_END()