1 /*
2 [auto_generated]
3 libs/numeric/odeint/test/n_step_iterator.cpp
4
5 [begin_description]
6 This file tests the n-step iterator.
7 [end_description]
8
9 Copyright 2009-2013 Karsten Ahnert
10 Copyright 2009-2013 Mario Mulansky
11
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)
15 */
16
17
18 #define BOOST_TEST_MODULE odeint_n_step_iterator
19
20 #include <iterator>
21 #include <algorithm>
22 #include <vector>
23
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>
29
30 #include <boost/test/unit_test.hpp>
31 #include <boost/test/floating_point_comparison.hpp>
32
33 #include <boost/numeric/odeint/iterator/n_step_iterator.hpp>
34 #include "dummy_steppers.hpp"
35 #include "dummy_odes.hpp"
36 #include "dummy_observers.hpp"
37
38 namespace mpl = boost::mpl;
39 using namespace boost::numeric::odeint;
40
41 typedef dummy_stepper::state_type state_type;
42 typedef dummy_stepper::value_type value_type;
43
44
45 BOOST_AUTO_TEST_SUITE( n_step_iterator_test )
46
47 typedef mpl::vector<
48 dummy_stepper
49 , dummy_dense_output_stepper
50 > dummy_steppers;
51
52
BOOST_AUTO_TEST_CASE_TEMPLATE(copy_stepper_iterator,Stepper,dummy_steppers)53 BOOST_AUTO_TEST_CASE_TEMPLATE( copy_stepper_iterator , Stepper , dummy_steppers )
54 {
55 typedef n_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.1 , 10 );
58 iterator_type iter2 = iter1;
59 BOOST_CHECK_EQUAL( &(*iter1) , &(*iter2) );
60 BOOST_CHECK_EQUAL( &(*iter1) , &x );
61 BOOST_CHECK( iter1.same( iter2 ) );
62 }
63
64
BOOST_AUTO_TEST_CASE_TEMPLATE(assignment_stepper_iterator,Stepper,dummy_steppers)65 BOOST_AUTO_TEST_CASE_TEMPLATE( assignment_stepper_iterator , Stepper , dummy_steppers )
66 {
67 typedef n_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 , 0.1 , 10 );
70 iterator_type iter2 = iterator_type( Stepper() , empty_system() , x2 , 0.0 , 0.2 , 10 );
71 BOOST_CHECK_EQUAL( &(*iter1) , &x1 );
72 BOOST_CHECK_EQUAL( &(*iter2) , &x2 );
73 BOOST_CHECK( !iter1.same( iter2 ) );
74 iter2 = iter1;
75 BOOST_CHECK_EQUAL( &(*iter1) , &x1 );
76 BOOST_CHECK_EQUAL( &(*iter2) , &x1 );
77 BOOST_CHECK( iter1.same( iter2 ) );
78 }
79
80
81
BOOST_AUTO_TEST_CASE_TEMPLATE(stepper_iterator_factory,Stepper,dummy_steppers)82 BOOST_AUTO_TEST_CASE_TEMPLATE( stepper_iterator_factory , Stepper , dummy_steppers )
83 {
84 Stepper stepper;
85 empty_system system;
86 state_type x = {{ 1.0 }};
87
88 std::for_each(
89 make_n_step_iterator_begin( stepper , boost::ref( system ) , x , 0.0 , 0.1 , 10 ) ,
90 make_n_step_iterator_end( stepper , boost::ref( system ) , x ) ,
91 dummy_observer() );
92
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 );
95 }
96
BOOST_AUTO_TEST_CASE_TEMPLATE(stepper_range,Stepper,dummy_steppers)97 BOOST_AUTO_TEST_CASE_TEMPLATE( stepper_range , Stepper , dummy_steppers )
98 {
99 Stepper stepper;
100 empty_system system;
101 state_type x = {{ 1.0 }};
102
103 boost::for_each( make_n_step_range( stepper , boost::ref( system ) , x , 0.0 , 0.1 , 10 ) ,
104 dummy_observer() );
105
106 BOOST_CHECK_CLOSE( x[0] , 3.5 , 1.0e-13 );
107 }
108
BOOST_AUTO_TEST_CASE_TEMPLATE(stepper_iterator_with_reference_wrapper_factory,Stepper,dummy_steppers)109 BOOST_AUTO_TEST_CASE_TEMPLATE( stepper_iterator_with_reference_wrapper_factory , Stepper , dummy_steppers )
110 {
111 Stepper stepper;
112 empty_system system;
113 state_type x = {{ 1.0 }};
114
115 std::for_each(
116 make_n_step_iterator_begin( boost::ref( stepper ) , boost::ref( system ) , x , 0.0 , 0.1 , 10 ) ,
117 make_n_step_iterator_end( boost::ref( stepper ) , boost::ref( system ) , x ) ,
118 dummy_observer() );
119
120 BOOST_CHECK_CLOSE( x[0] , 3.5 , 1.0e-13 );
121 }
122
BOOST_AUTO_TEST_CASE_TEMPLATE(stepper_range_with_reference_wrapper,Stepper,dummy_steppers)123 BOOST_AUTO_TEST_CASE_TEMPLATE( stepper_range_with_reference_wrapper , Stepper , dummy_steppers )
124 {
125 Stepper stepper;
126 empty_system system;
127 state_type x = {{ 1.0 }};
128
129 boost::for_each( make_n_step_range( boost::ref( stepper ) , boost::ref( system ) , x , 0.0 , 0.1 , 10 ) ,
130 dummy_observer() );
131
132 BOOST_CHECK_CLOSE( x[0] , 3.5 , 1.0e-13 );
133 }
134
135
136
BOOST_AUTO_TEST_CASE_TEMPLATE(transitivity1,Stepper,dummy_steppers)137 BOOST_AUTO_TEST_CASE_TEMPLATE( transitivity1 , Stepper , dummy_steppers )
138 {
139 typedef n_step_iterator< Stepper , empty_system , state_type > stepper_iterator;
140
141 state_type x = {{ 1.0 }};
142 stepper_iterator first1( Stepper() , empty_system() , x , 2.5 , 0.1 , 0 );
143 stepper_iterator last1( Stepper() , empty_system() , x );
144 stepper_iterator last2( Stepper() , empty_system() , x );
145
146 BOOST_CHECK( last1 == last2 );
147 BOOST_CHECK( first1 != last1 );
148 BOOST_CHECK( ++first1 == last1 );
149 }
150
151
BOOST_AUTO_TEST_CASE_TEMPLATE(copy_algorithm,Stepper,dummy_steppers)152 BOOST_AUTO_TEST_CASE_TEMPLATE( copy_algorithm , Stepper , dummy_steppers )
153 {
154 typedef n_step_iterator< Stepper , empty_system , state_type > stepper_iterator;
155 state_type x = {{ 1.0 }};
156 std::vector< state_type > res;
157 stepper_iterator first( Stepper() , empty_system() , x , 0.0 , 0.1 , 3 );
158 stepper_iterator last( Stepper() , empty_system() , x );
159
160 std::copy( first , last , std::back_insert_iterator< std::vector< state_type > >( res ) );
161
162 BOOST_CHECK_EQUAL( res.size() , size_t( 4 ) );
163 BOOST_CHECK_CLOSE( res[0][0] , 1.0 , 1.0e-14 );
164 BOOST_CHECK_CLOSE( res[1][0] , 1.25 , 1.0e-14 );
165 BOOST_CHECK_CLOSE( res[2][0] , 1.5 , 1.0e-14 );
166 BOOST_CHECK_CLOSE( res[3][0] , 1.75 , 1.0e-14 );
167
168 BOOST_CHECK_CLOSE( x[0] , 1.75 , 1.0e-14 ); // the iterator should not iterate over the end
169 }
170
BOOST_AUTO_TEST_CASE_TEMPLATE(copy_algorithm_negative_time_step,Stepper,dummy_steppers)171 BOOST_AUTO_TEST_CASE_TEMPLATE( copy_algorithm_negative_time_step , Stepper , dummy_steppers )
172 {
173 typedef n_step_iterator< Stepper , empty_system , state_type > stepper_iterator;
174 state_type x = {{ 1.0 }};
175 std::vector< state_type > res;
176 stepper_iterator first( Stepper() , empty_system() , x , 0.3 , -0.1 , 3 );
177 stepper_iterator last( Stepper() , empty_system() , x );
178
179 std::copy( first , last , std::back_insert_iterator< std::vector< state_type > >( res ) );
180
181 BOOST_CHECK_EQUAL( res.size() , size_t( 4 ) );
182 BOOST_CHECK_CLOSE( res[0][0] , 1.0 , 1.0e-14 );
183 BOOST_CHECK_CLOSE( res[1][0] , 1.25 , 1.0e-14 );
184 BOOST_CHECK_CLOSE( res[2][0] , 1.5 , 1.0e-14 );
185 BOOST_CHECK_CLOSE( res[3][0] , 1.75 , 1.0e-14 );
186
187 BOOST_CHECK_CLOSE( x[0] , 1.75 , 1.0e-14 );
188 }
189
190
BOOST_AUTO_TEST_CASE_TEMPLATE(copy_algorithm_with_factory,Stepper,dummy_steppers)191 BOOST_AUTO_TEST_CASE_TEMPLATE( copy_algorithm_with_factory , Stepper , dummy_steppers )
192 {
193 state_type x = {{ 1.0 }};
194 std::vector< state_type > res;
195 std::copy( make_n_step_iterator_begin( Stepper() , empty_system() , x , 0.0 , 0.1 , 3 ) ,
196 make_n_step_iterator_end( Stepper() , empty_system() , x ) ,
197 std::back_insert_iterator< std::vector< state_type > >( res ) );
198
199 BOOST_CHECK_EQUAL( res.size() , size_t( 4 ) );
200 BOOST_CHECK_CLOSE( res[0][0] , 1.0 , 1.0e-14 );
201 BOOST_CHECK_CLOSE( res[1][0] , 1.25 , 1.0e-14 );
202 BOOST_CHECK_CLOSE( res[2][0] , 1.5 , 1.0e-14 );
203 BOOST_CHECK_CLOSE( res[3][0] , 1.75 , 1.0e-14 );
204
205 BOOST_CHECK_CLOSE( x[0] , 1.75 , 1.0e-14 );
206 }
207
BOOST_AUTO_TEST_CASE_TEMPLATE(copy_algorithm_with_range_factory,Stepper,dummy_steppers)208 BOOST_AUTO_TEST_CASE_TEMPLATE( copy_algorithm_with_range_factory , Stepper , dummy_steppers )
209 {
210 state_type x = {{ 1.0 }};
211 std::vector< state_type > res;
212 boost::range::copy( make_n_step_range( Stepper() , empty_system() , x , 0.0 , 0.1 , 3 ) ,
213 std::back_insert_iterator< std::vector< state_type > >( res ) );
214
215 BOOST_CHECK_EQUAL( res.size() , size_t( 4 ) );
216 BOOST_CHECK_CLOSE( res[0][0] , 1.0 , 1.0e-14 );
217 BOOST_CHECK_CLOSE( res[1][0] , 1.25 , 1.0e-14 );
218 BOOST_CHECK_CLOSE( res[2][0] , 1.5 , 1.0e-14 );
219 BOOST_CHECK_CLOSE( res[3][0] , 1.75 , 1.0e-14 );
220
221 BOOST_CHECK_CLOSE( x[0] , 1.75 , 1.0e-14 );
222 }
223
224
225 BOOST_AUTO_TEST_SUITE_END()
226