1 | /* |
2 | [auto_generated] |
3 | libs/numeric/odeint/test/times_time_iterator.cpp |
4 | |
5 | [begin_description] |
6 | This file tests the times 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_times_time_iterator |
19 | |
20 | #include <iterator> |
21 | #include <algorithm> |
22 | #include <vector> |
23 | #include <iostream> |
24 | #include <array> |
25 | |
26 | #include <boost/numeric/odeint/config.hpp> |
27 | #include <array> |
28 | #include <boost/range/algorithm/for_each.hpp> |
29 | #include <boost/range/algorithm/copy.hpp> |
30 | #include <boost/mpl/vector.hpp> |
31 | |
32 | #include <boost/test/unit_test.hpp> |
33 | #include <boost/test/tools/floating_point_comparison.hpp> |
34 | |
35 | #include <boost/numeric/odeint/iterator/times_time_iterator.hpp> |
36 | #include "dummy_steppers.hpp" |
37 | #include "dummy_odes.hpp" |
38 | #include "dummy_observers.hpp" |
39 | |
40 | namespace mpl = boost::mpl; |
41 | using namespace boost::numeric::odeint; |
42 | |
43 | typedef dummy_stepper::state_type state_type; |
44 | typedef dummy_stepper::value_type value_type; |
45 | typedef dummy_stepper::time_type time_type; |
46 | |
47 | BOOST_AUTO_TEST_SUITE( times_time_iterator_test ) |
48 | |
49 | typedef mpl::vector< |
50 | dummy_stepper |
51 | , dummy_dense_output_stepper |
52 | > dummy_steppers; |
53 | |
54 | std::array<double,4> times = { 0.0 , 0.1, 0.2, 0.3 }; |
55 | typedef std::array<double,4>::iterator time_iterator_type; |
56 | typedef std::vector< std::pair< state_type , time_type > > result_vector; |
57 | |
58 | BOOST_AUTO_TEST_CASE_TEMPLATE( copy_stepper_iterator , Stepper , dummy_steppers ) |
59 | { |
60 | typedef times_time_iterator< Stepper , empty_system , state_type , time_iterator_type > iterator_type; |
61 | state_type x = {._M_elems: { 1.0 }}; |
62 | iterator_type iter1 = iterator_type( Stepper() , empty_system() , x , times.begin() , times.end() , 0.1 ); |
63 | iterator_type iter2 = iter1; |
64 | BOOST_CHECK_EQUAL( &(iter1->first) , &(iter2->first) ); |
65 | BOOST_CHECK_EQUAL( &(iter1->first) , &x ); |
66 | BOOST_CHECK( iter1.same( iter2 ) ); |
67 | } |
68 | |
69 | |
70 | BOOST_AUTO_TEST_CASE_TEMPLATE( assignment_stepper_iterator , Stepper , dummy_steppers ) |
71 | { |
72 | std::cout << "assignment" << std::endl; |
73 | typedef times_time_iterator< Stepper , empty_system , state_type , time_iterator_type> iterator_type; |
74 | state_type x1 = {._M_elems: { 1.0 }} , x2 = {._M_elems: { 2.0 }}; |
75 | iterator_type iter1 = iterator_type( Stepper() , empty_system() , x1 , times.begin() , times.end() , 0.1 ); |
76 | iterator_type iter2 = iterator_type( Stepper() , empty_system() , x2 , times.begin() , times.end() , 0.2 ); |
77 | BOOST_CHECK_EQUAL( &(iter1->first) , &x1 ); |
78 | BOOST_CHECK_EQUAL( &(iter2->first) , &x2 ); |
79 | BOOST_CHECK( !iter1.same( iter2 ) ); |
80 | iter2 = iter1; |
81 | BOOST_CHECK_EQUAL( &(iter1->first) , &x1 ); |
82 | BOOST_CHECK_EQUAL( &(iter2->first) , &x1 ); |
83 | BOOST_CHECK( iter1.same( iter2 ) ); |
84 | } |
85 | |
86 | |
87 | |
88 | BOOST_AUTO_TEST_CASE_TEMPLATE( stepper_iterator_factory , Stepper , dummy_steppers ) |
89 | { |
90 | std::cout << "factory" << std::endl; |
91 | Stepper stepper; |
92 | empty_system system; |
93 | state_type x = {._M_elems: { 1.0 }}; |
94 | |
95 | std::for_each( |
96 | make_times_time_iterator_begin( stepper , boost::ref( t&: system ) , x , times.begin(), times.end() , 0.1 ) , |
97 | make_times_time_iterator_end<time_iterator_type>( stepper , boost::ref( t&: system ) , x ) , |
98 | dummy_observer() ); |
99 | |
100 | // dummy_steppers just add 0.25 at each step, the above for_each leads to 3 do_step calls so x should be 1.75 |
101 | BOOST_CHECK_CLOSE( x[0] , 1.75 , 1.0e-13 ); |
102 | } |
103 | |
104 | BOOST_AUTO_TEST_CASE_TEMPLATE( stepper_range , Stepper , dummy_steppers ) |
105 | { |
106 | std::cout << "range" << std::endl; |
107 | Stepper stepper; |
108 | empty_system system; |
109 | state_type x = {._M_elems: { 1.0 }}; |
110 | |
111 | boost::for_each( make_times_time_range( stepper , boost::ref( t&: system ) , x , times.begin() , times.end() , 0.1 ) , |
112 | dummy_observer() ); |
113 | |
114 | BOOST_CHECK_CLOSE( x[0] , 1.75 , 1.0e-13 ); |
115 | } |
116 | |
117 | BOOST_AUTO_TEST_CASE_TEMPLATE( stepper_iterator_with_reference_wrapper_factory , Stepper , dummy_steppers ) |
118 | { |
119 | Stepper stepper; |
120 | empty_system system; |
121 | state_type x = {._M_elems: { 1.0 }}; |
122 | |
123 | std::for_each( |
124 | make_times_time_iterator_begin( boost::ref( stepper ) , boost::ref( t&: system ) , x , times.begin() , times.end() , 0.1 ) , |
125 | make_times_time_iterator_end<time_iterator_type>( boost::ref( stepper ) , boost::ref( t&: system ) , x ) , |
126 | dummy_observer() ); |
127 | |
128 | BOOST_CHECK_CLOSE( x[0] , 1.75 , 1.0e-13 ); |
129 | } |
130 | |
131 | BOOST_AUTO_TEST_CASE_TEMPLATE( stepper_range_with_reference_wrapper , Stepper , dummy_steppers ) |
132 | { |
133 | Stepper stepper; |
134 | empty_system system; |
135 | state_type x = {._M_elems: { 1.0 }}; |
136 | |
137 | boost::for_each( make_times_time_range( boost::ref( stepper ) , boost::ref( t&: system ) , x , times.begin() , times.end(), 0.1 ) , |
138 | dummy_observer() ); |
139 | |
140 | BOOST_CHECK_CLOSE( x[0] , 1.75 , 1.0e-13 ); |
141 | } |
142 | |
143 | /* |
144 | BOOST_AUTO_TEST_CASE_TEMPLATE( transitivity1 , Stepper , dummy_steppers ) |
145 | { |
146 | typedef times_time_iterator< Stepper , empty_system , state_type , time_iterator_type > stepper_iterator; |
147 | std::cout << "transitivity1" << std::endl; |
148 | state_type x = {{ 1.0 }}; |
149 | stepper_iterator first1( Stepper() , empty_system() , x , times.end() , times.end() , 0.1 ); |
150 | stepper_iterator last1( Stepper() , empty_system() , x ); |
151 | stepper_iterator last2( Stepper() , empty_system() , x ); |
152 | |
153 | BOOST_CHECK( first1 == last1 ); |
154 | BOOST_CHECK( first1 == last2 ); |
155 | BOOST_CHECK( last1 == last2 ); |
156 | |
157 | first1 = stepper_iterator( Stepper() , empty_system() , x , times.end()-1 , times.end() , 0.1 ); |
158 | last1 = stepper_iterator( Stepper() , empty_system() , x ); |
159 | BOOST_CHECK( first1 != last1 ); |
160 | BOOST_CHECK( ++first1 == last1 ); |
161 | } |
162 | */ |
163 | |
164 | BOOST_AUTO_TEST_CASE_TEMPLATE( copy_algorithm , Stepper , dummy_steppers ) |
165 | { |
166 | typedef times_time_iterator< Stepper , empty_system , state_type , time_iterator_type> stepper_iterator; |
167 | state_type x = {._M_elems: { 1.0 }}; |
168 | result_vector res; |
169 | stepper_iterator first( Stepper() , empty_system() , x , times.begin() , times.end() , 0.1 ); |
170 | stepper_iterator last( Stepper() , empty_system() , x ); |
171 | |
172 | std::copy( first , last , std::back_insert_iterator< result_vector >( res ) ); |
173 | |
174 | BOOST_CHECK_EQUAL( res.size() , size_t( 4 ) ); |
175 | BOOST_CHECK_CLOSE( res[0].first[0] , 1.0 , 1.0e-13 ); |
176 | BOOST_CHECK_CLOSE( res[1].first[0] , 1.25 , 1.0e-13 ); |
177 | BOOST_CHECK_CLOSE( res[2].first[0] , 1.5 , 1.0e-13 ); |
178 | BOOST_CHECK_CLOSE( res[3].first[0] , 1.75 , 1.0e-13 ); |
179 | |
180 | for( size_t n=0 ; n<4 ; ++n ) |
181 | BOOST_CHECK_CLOSE( res[n].second , times[n] , 1.0e-13 ); |
182 | |
183 | BOOST_CHECK_CLOSE( x[0] , 1.75 , 1.0e-13 ); // the iterator should not iterate over the end |
184 | } |
185 | |
186 | BOOST_AUTO_TEST_CASE_TEMPLATE( copy_algorithm_negative_time_step , Stepper , dummy_steppers ) |
187 | { |
188 | typedef times_time_iterator< Stepper , empty_system , state_type , time_iterator_type > stepper_iterator; |
189 | state_type x = {._M_elems: { 1.0 }}; |
190 | result_vector res; |
191 | std::array<double,4> neg_times = {._M_elems: { 0.0 , -0.1, -0.2, -0.3 }}; |
192 | stepper_iterator first( Stepper() , empty_system() , x , neg_times.begin() , neg_times.end() , -0.1 ); |
193 | stepper_iterator last( Stepper() , empty_system() , x ); |
194 | |
195 | std::copy( first , last , std::back_insert_iterator< result_vector >( res ) ); |
196 | |
197 | BOOST_CHECK_EQUAL( res.size() , size_t( 4 ) ); |
198 | BOOST_CHECK_CLOSE( res[0].first[0] , 1.0 , 1.0e-13 ); |
199 | BOOST_CHECK_CLOSE( res[1].first[0] , 1.25 , 1.0e-13 ); |
200 | BOOST_CHECK_CLOSE( res[2].first[0] , 1.5 , 1.0e-13 ); |
201 | BOOST_CHECK_CLOSE( res[3].first[0] , 1.75 , 1.0e-13 ); |
202 | |
203 | for( size_t n=0 ; n<4 ; ++n ) |
204 | BOOST_CHECK_CLOSE( res[n].second , neg_times[n] , 1.0e-13 ); |
205 | |
206 | BOOST_CHECK_CLOSE( x[0] , 1.75 , 1.0e-13 ); |
207 | } |
208 | |
209 | |
210 | BOOST_AUTO_TEST_CASE_TEMPLATE( copy_algorithm_with_factory , Stepper , dummy_steppers ) |
211 | { |
212 | state_type x = {._M_elems: { 1.0 }}; |
213 | result_vector res; |
214 | std::copy( make_times_time_iterator_begin( Stepper() , empty_system() , x , times.begin() , times.end() , 0.1 ) , |
215 | make_times_time_iterator_end<time_iterator_type>( Stepper() , empty_system() , x ) , |
216 | std::back_insert_iterator< result_vector >( res ) ); |
217 | |
218 | BOOST_CHECK_EQUAL( res.size() , size_t( 4 ) ); |
219 | BOOST_CHECK_CLOSE( res[0].first[0] , 1.0 , 1.0e-13 ); |
220 | BOOST_CHECK_CLOSE( res[1].first[0] , 1.25 , 1.0e-13 ); |
221 | BOOST_CHECK_CLOSE( res[2].first[0] , 1.5 , 1.0e-13 ); |
222 | BOOST_CHECK_CLOSE( res[3].first[0] , 1.75 , 1.0e-13 ); |
223 | |
224 | for( size_t n=0 ; n<4 ; ++n ) |
225 | BOOST_CHECK_CLOSE( res[n].second , times[n] , 1.0e-13 ); |
226 | |
227 | BOOST_CHECK_CLOSE( x[0] , 1.75 , 1.0e-13 ); |
228 | } |
229 | |
230 | BOOST_AUTO_TEST_CASE_TEMPLATE( copy_algorithm_with_range_factory , Stepper , dummy_steppers ) |
231 | { |
232 | state_type x = {._M_elems: { 1.0 }}; |
233 | result_vector res; |
234 | boost::range::copy( make_times_time_range( Stepper() , empty_system() , x , times.begin() , times.end() , 0.1 ) , |
235 | std::back_insert_iterator< result_vector >( res ) ); |
236 | |
237 | BOOST_CHECK_EQUAL( res.size() , size_t( 4 ) ); |
238 | BOOST_CHECK_CLOSE( res[0].first[0] , 1.0 , 1.0e-13 ); |
239 | BOOST_CHECK_CLOSE( res[1].first[0] , 1.25 , 1.0e-13 ); |
240 | BOOST_CHECK_CLOSE( res[2].first[0] , 1.5 , 1.0e-13 ); |
241 | BOOST_CHECK_CLOSE( res[3].first[0] , 1.75 , 1.0e-13 ); |
242 | |
243 | for( size_t n=0 ; n<4 ; ++n ) |
244 | BOOST_CHECK_CLOSE( res[n].second , times[n] , 1.0e-13 ); |
245 | |
246 | BOOST_CHECK_CLOSE( x[0] , 1.75 , 1.0e-13 ); |
247 | } |
248 | |
249 | |
250 | BOOST_AUTO_TEST_SUITE_END() |
251 | |