Imported Upstream version 1.57.0
[platform/upstream/boost.git] / libs / numeric / odeint / test / times_time_iterator.cpp
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
25 #include <boost/numeric/odeint/config.hpp>
26 #include <boost/array.hpp>
27 #include <boost/range/algorithm/for_each.hpp>
28 #include <boost/range/algorithm/copy.hpp>
29 #include <boost/mpl/vector.hpp>
30
31 #include <boost/test/unit_test.hpp>
32 #include <boost/test/floating_point_comparison.hpp>
33
34 #include <boost/numeric/odeint/iterator/times_time_iterator.hpp>
35 #include "dummy_steppers.hpp"
36 #include "dummy_odes.hpp"
37 #include "dummy_observers.hpp"
38
39 namespace mpl = boost::mpl;
40 using namespace boost::numeric::odeint;
41
42 typedef dummy_stepper::state_type state_type;
43 typedef dummy_stepper::value_type value_type;
44 typedef dummy_stepper::time_type time_type;
45
46 BOOST_AUTO_TEST_SUITE( times_time_iterator_test )
47
48 typedef mpl::vector<
49     dummy_stepper
50     , dummy_dense_output_stepper
51     > dummy_steppers;
52
53 boost::array<double,4> times = {{ 0.0 , 0.1, 0.2, 0.3 }};
54 typedef boost::array<double,4>::iterator time_iterator_type;
55 typedef std::vector< std::pair< state_type , time_type > > result_vector;
56
57 BOOST_AUTO_TEST_CASE_TEMPLATE( copy_stepper_iterator , Stepper , dummy_steppers )
58 {
59     typedef times_time_iterator< Stepper , empty_system , state_type , time_iterator_type > iterator_type;
60     state_type x = {{ 1.0 }};
61     iterator_type iter1 = iterator_type( Stepper() , empty_system() , x , times.begin() , times.end() , 0.1 );
62     iterator_type iter2 = iter1;
63     BOOST_CHECK_EQUAL( &(iter1->first) , &(iter2->first) );
64     BOOST_CHECK_EQUAL( &(iter1->first) , &x );
65     BOOST_CHECK( iter1.same( iter2 ) );
66 }
67
68
69 BOOST_AUTO_TEST_CASE_TEMPLATE( assignment_stepper_iterator , Stepper , dummy_steppers )
70 {
71     std::cout << "assignment" << std::endl;
72     typedef times_time_iterator< Stepper , empty_system , state_type , time_iterator_type> iterator_type;
73     state_type x1 = {{ 1.0 }} , x2 = {{ 2.0 }};
74     iterator_type iter1 = iterator_type( Stepper() , empty_system() , x1 , times.begin() , times.end() , 0.1 );
75     iterator_type iter2 = iterator_type( Stepper() , empty_system() , x2 , times.begin() , times.end() , 0.2 );
76     BOOST_CHECK_EQUAL( &(iter1->first) , &x1 );
77     BOOST_CHECK_EQUAL( &(iter2->first) , &x2 );
78     BOOST_CHECK( !iter1.same( iter2 ) );
79     iter2 = iter1;
80     BOOST_CHECK_EQUAL( &(iter1->first) , &x1 );
81     BOOST_CHECK_EQUAL( &(iter2->first) , &x1 );
82     BOOST_CHECK( iter1.same( iter2 ) );
83 }
84
85
86
87 BOOST_AUTO_TEST_CASE_TEMPLATE( stepper_iterator_factory , Stepper , dummy_steppers )
88 {
89     std::cout << "factory" << std::endl;
90     Stepper stepper;
91     empty_system system;
92     state_type x = {{ 1.0 }};
93
94     std::for_each(
95         make_times_time_iterator_begin( stepper , boost::ref( system ) , x , times.begin(), times.end() , 0.1 ) ,
96         make_times_time_iterator_end<time_iterator_type>( stepper , boost::ref( system ) , x ) ,
97         dummy_observer() );
98
99     // 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
100     BOOST_CHECK_CLOSE( x[0] , 1.75 , 1.0e-13 );
101 }
102
103 BOOST_AUTO_TEST_CASE_TEMPLATE( stepper_range , Stepper , dummy_steppers )
104 {
105     std::cout << "range" << std::endl;
106     Stepper stepper;
107     empty_system system;
108     state_type x = {{ 1.0 }};
109
110     boost::for_each( make_times_time_range( stepper , boost::ref( system ) , x , times.begin() , times.end() , 0.1 ) ,
111                      dummy_observer() );
112
113     BOOST_CHECK_CLOSE( x[0] , 1.75 , 1.0e-13 );
114 }
115
116 BOOST_AUTO_TEST_CASE_TEMPLATE( stepper_iterator_with_reference_wrapper_factory , Stepper , dummy_steppers )
117 {
118     Stepper stepper;
119     empty_system system;
120     state_type x = {{ 1.0 }};
121
122     std::for_each(
123         make_times_time_iterator_begin( boost::ref( stepper ) , boost::ref( system ) , x , times.begin() , times.end() , 0.1 ) ,
124         make_times_time_iterator_end<time_iterator_type>( boost::ref( stepper ) , boost::ref( system ) , x ) ,
125         dummy_observer() );
126
127     BOOST_CHECK_CLOSE( x[0] , 1.75 , 1.0e-13 );
128 }
129
130 BOOST_AUTO_TEST_CASE_TEMPLATE( stepper_range_with_reference_wrapper , Stepper , dummy_steppers )
131 {
132     Stepper stepper;
133     empty_system system;
134     state_type x = {{ 1.0 }};
135
136     boost::for_each( make_times_time_range( boost::ref( stepper ) , boost::ref( system ) , x , times.begin() , times.end(), 0.1 ) ,
137                      dummy_observer() );
138
139     BOOST_CHECK_CLOSE( x[0] , 1.75 , 1.0e-13 );
140 }
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 = {{ 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 = {{ 1.0 }};
190     result_vector res;
191     boost::array<double,4> neg_times = {{ 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 = {{ 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 = {{ 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()