Brian Silverman | 7c33ab2 | 2018-08-04 17:14:51 -0700 | [diff] [blame^] | 1 | /* |
| 2 | [auto_generated] |
| 3 | libs/numeric/odeint/test/const_step_iterator.cpp |
| 4 | |
| 5 | [begin_description] |
| 6 | This file tests the const step iterator. |
| 7 | [end_description] |
| 8 | |
| 9 | Copyright 2012-2013 Karsten Ahnert |
| 10 | Copyright 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_const_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/const_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( const_step_iterator_test ) |
| 46 | |
| 47 | typedef mpl::vector< |
| 48 | dummy_stepper |
| 49 | , dummy_dense_output_stepper |
| 50 | > dummy_steppers; |
| 51 | |
| 52 | |
| 53 | BOOST_AUTO_TEST_CASE_TEMPLATE( copy_stepper_iterator , Stepper , dummy_steppers ) |
| 54 | { |
| 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 ) ); |
| 62 | } |
| 63 | |
| 64 | |
| 65 | BOOST_AUTO_TEST_CASE_TEMPLATE( assignment_stepper_iterator , Stepper , dummy_steppers ) |
| 66 | { |
| 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 ) ); |
| 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 | |
| 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_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 ) , |
| 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 | |
| 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_const_step_range( stepper , boost::ref( system ) , x , 0.0 , 1.0 , 0.1 ) , |
| 104 | dummy_observer() ); |
| 105 | |
| 106 | BOOST_CHECK_CLOSE( x[0] , 3.5 , 1.0e-13 ); |
| 107 | } |
| 108 | |
| 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_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 ) , |
| 118 | dummy_observer() ); |
| 119 | |
| 120 | BOOST_CHECK_CLOSE( x[0] , 3.5 , 1.0e-13 ); |
| 121 | } |
| 122 | |
| 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_const_step_range( boost::ref( stepper ) , boost::ref( system ) , x , 0.0 , 1.0 , 0.1 ) , |
| 130 | dummy_observer() ); |
| 131 | |
| 132 | BOOST_CHECK_CLOSE( x[0] , 3.5 , 1.0e-13 ); |
| 133 | } |
| 134 | |
| 135 | |
| 136 | |
| 137 | BOOST_AUTO_TEST_CASE_TEMPLATE( transitivity1 , Stepper , dummy_steppers ) |
| 138 | { |
| 139 | typedef const_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 , 2.0 , 0.1 ); |
| 143 | stepper_iterator last1( Stepper() , empty_system() , x ); |
| 144 | stepper_iterator last2( Stepper() , empty_system() , x ); |
| 145 | |
| 146 | BOOST_CHECK( first1 == last1 ); |
| 147 | BOOST_CHECK( first1 == last2 ); |
| 148 | BOOST_CHECK( last1 == last2 ); |
| 149 | |
| 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 ); |
| 154 | } |
| 155 | |
| 156 | |
| 157 | BOOST_AUTO_TEST_CASE_TEMPLATE( copy_algorithm , Stepper , dummy_steppers ) |
| 158 | { |
| 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 ); |
| 164 | |
| 165 | std::copy( first , last , std::back_insert_iterator< std::vector< state_type > >( res ) ); |
| 166 | |
| 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 ); |
| 172 | |
| 173 | BOOST_CHECK_CLOSE( x[0] , 1.75 , 1.0e-14 ); // the iterator should not iterate over the end |
| 174 | } |
| 175 | |
| 176 | BOOST_AUTO_TEST_CASE_TEMPLATE( copy_algorithm_negative_time_step , Stepper , dummy_steppers ) |
| 177 | { |
| 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 ); |
| 183 | |
| 184 | std::copy( first , last , std::back_insert_iterator< std::vector< state_type > >( res ) ); |
| 185 | |
| 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 ); |
| 191 | |
| 192 | BOOST_CHECK_CLOSE( x[0] , 1.75 , 1.0e-14 ); |
| 193 | } |
| 194 | |
| 195 | |
| 196 | BOOST_AUTO_TEST_CASE_TEMPLATE( copy_algorithm_with_factory , Stepper , dummy_steppers ) |
| 197 | { |
| 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 ) ); |
| 203 | |
| 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 ); |
| 209 | |
| 210 | BOOST_CHECK_CLOSE( x[0] , 1.75 , 1.0e-14 ); |
| 211 | } |
| 212 | |
| 213 | BOOST_AUTO_TEST_CASE_TEMPLATE( copy_algorithm_with_range_factory , Stepper , dummy_steppers ) |
| 214 | { |
| 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 ) ); |
| 219 | |
| 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 ); |
| 225 | |
| 226 | BOOST_CHECK_CLOSE( x[0] , 1.75 , 1.0e-14 ); |
| 227 | } |
| 228 | |
| 229 | |
| 230 | |
| 231 | |
| 232 | BOOST_AUTO_TEST_SUITE_END() |