times_iterator.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. [auto_generated]
  3. libs/numeric/odeint/test/times_iterator.cpp
  4. [begin_description]
  5. This file tests the n-step iterator.
  6. [end_description]
  7. Copyright 2009-2013 Karsten Ahnert
  8. Copyright 2009-2013 Mario Mulansky
  9. Distributed under the Boost Software License, Version 1.0.
  10. (See accompanying file LICENSE_1_0.txt or
  11. copy at http://www.boost.org/LICENSE_1_0.txt)
  12. */
  13. #define BOOST_TEST_MODULE odeint_times_iterator
  14. #include <iterator>
  15. #include <algorithm>
  16. #include <vector>
  17. #include <iostream>
  18. #include <boost/numeric/odeint/config.hpp>
  19. #include <boost/array.hpp>
  20. #include <boost/range/algorithm/for_each.hpp>
  21. #include <boost/range/algorithm/copy.hpp>
  22. #include <boost/mpl/vector.hpp>
  23. #include <boost/test/unit_test.hpp>
  24. #include <boost/test/floating_point_comparison.hpp>
  25. #include <boost/numeric/odeint/iterator/times_iterator.hpp>
  26. #include "dummy_steppers.hpp"
  27. #include "dummy_odes.hpp"
  28. #include "dummy_observers.hpp"
  29. namespace mpl = boost::mpl;
  30. using namespace boost::numeric::odeint;
  31. typedef dummy_stepper::state_type state_type;
  32. typedef dummy_stepper::value_type value_type;
  33. BOOST_AUTO_TEST_SUITE( times_iterator_test )
  34. typedef mpl::vector<
  35. dummy_stepper
  36. , dummy_dense_output_stepper
  37. > dummy_steppers;
  38. boost::array<double,4> times = {{ 0.0 , 0.1, 0.2, 0.3 }};
  39. typedef boost::array<double,4>::iterator time_iterator_type;
  40. BOOST_AUTO_TEST_CASE_TEMPLATE( copy_stepper_iterator , Stepper , dummy_steppers )
  41. {
  42. typedef times_iterator< Stepper , empty_system , state_type , time_iterator_type > iterator_type;
  43. state_type x = {{ 1.0 }};
  44. iterator_type iter1 = iterator_type( Stepper() , empty_system() , x , times.begin() , times.end() , 0.1 );
  45. iterator_type iter2 = iter1;
  46. BOOST_CHECK_EQUAL( &(*iter1) , &(*iter2) );
  47. BOOST_CHECK_EQUAL( &(*iter1) , &x );
  48. BOOST_CHECK( iter1.same( iter2 ) );
  49. }
  50. BOOST_AUTO_TEST_CASE_TEMPLATE( assignment_stepper_iterator , Stepper , dummy_steppers )
  51. {
  52. std::cout << "assignment" << std::endl;
  53. typedef times_iterator< Stepper , empty_system , state_type , time_iterator_type> iterator_type;
  54. state_type x1 = {{ 1.0 }} , x2 = {{ 2.0 }};
  55. iterator_type iter1 = iterator_type( Stepper() , empty_system() , x1 , times.begin() , times.end() , 0.1 );
  56. iterator_type iter2 = iterator_type( Stepper() , empty_system() , x2 , times.begin() , times.end() , 0.2 );
  57. BOOST_CHECK_EQUAL( &(*iter1) , &x1 );
  58. BOOST_CHECK_EQUAL( &(*iter2) , &x2 );
  59. BOOST_CHECK( !iter1.same( iter2 ) );
  60. iter2 = iter1;
  61. BOOST_CHECK_EQUAL( &(*iter1) , &x1 );
  62. BOOST_CHECK_EQUAL( &(*iter2) , &x1 );
  63. BOOST_CHECK( iter1.same( iter2 ) );
  64. }
  65. BOOST_AUTO_TEST_CASE_TEMPLATE( stepper_iterator_factory , Stepper , dummy_steppers )
  66. {
  67. std::cout << "factory" << std::endl;
  68. Stepper stepper;
  69. empty_system system;
  70. state_type x = {{ 1.0 }};
  71. std::for_each(
  72. make_times_iterator_begin( stepper , boost::ref( system ) , x , times.begin(), times.end() , 0.1 ) ,
  73. make_times_iterator_end<time_iterator_type>( stepper , boost::ref( system ) , x ) ,
  74. dummy_observer() );
  75. // 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
  76. BOOST_CHECK_CLOSE( x[0] , 1.75 , 1.0e-13 );
  77. }
  78. BOOST_AUTO_TEST_CASE_TEMPLATE( stepper_range , Stepper , dummy_steppers )
  79. {
  80. std::cout << "range" << std::endl;
  81. Stepper stepper;
  82. empty_system system;
  83. state_type x = {{ 1.0 }};
  84. boost::for_each( make_times_range( stepper , boost::ref( system ) , x , times.begin() , times.end() , 0.1 ) ,
  85. dummy_observer() );
  86. BOOST_CHECK_CLOSE( x[0] , 1.75 , 1.0e-13 );
  87. }
  88. BOOST_AUTO_TEST_CASE_TEMPLATE( stepper_iterator_with_reference_wrapper_factory , Stepper , dummy_steppers )
  89. {
  90. Stepper stepper;
  91. empty_system system;
  92. state_type x = {{ 1.0 }};
  93. std::for_each(
  94. make_times_iterator_begin( boost::ref( stepper ) , boost::ref( system ) , x , times.begin() , times.end() , 0.1 ) ,
  95. make_times_iterator_end<time_iterator_type>( boost::ref( stepper ) , boost::ref( system ) , x ) ,
  96. dummy_observer() );
  97. BOOST_CHECK_CLOSE( x[0] , 1.75 , 1.0e-13 );
  98. }
  99. BOOST_AUTO_TEST_CASE_TEMPLATE( stepper_range_with_reference_wrapper , Stepper , dummy_steppers )
  100. {
  101. Stepper stepper;
  102. empty_system system;
  103. state_type x = {{ 1.0 }};
  104. boost::for_each( make_times_range( boost::ref( stepper ) , boost::ref( system ) , x , times.begin() , times.end(), 0.1 ) ,
  105. dummy_observer() );
  106. BOOST_CHECK_CLOSE( x[0] , 1.75 , 1.0e-13 );
  107. }
  108. BOOST_AUTO_TEST_CASE_TEMPLATE( transitivity1 , Stepper , dummy_steppers )
  109. {
  110. typedef times_iterator< Stepper , empty_system , state_type , time_iterator_type > stepper_iterator;
  111. std::cout << "transitivity1" << std::endl;
  112. state_type x = {{ 1.0 }};
  113. stepper_iterator first1( Stepper() , empty_system() , x , times.end() , times.end() , 0.1 );
  114. stepper_iterator last1( Stepper() , empty_system() , x );
  115. stepper_iterator last2( Stepper() , empty_system() , x );
  116. BOOST_CHECK( first1 == last1 );
  117. BOOST_CHECK( first1 == last2 );
  118. BOOST_CHECK( last1 == last2 );
  119. first1 = stepper_iterator( Stepper() , empty_system() , x , times.end()-1 , times.end() , 0.1 );
  120. last1 = stepper_iterator( Stepper() , empty_system() , x );
  121. BOOST_CHECK( first1 != last1 );
  122. BOOST_CHECK( ++first1 == last1 );
  123. }
  124. BOOST_AUTO_TEST_CASE_TEMPLATE( copy_algorithm , Stepper , dummy_steppers )
  125. {
  126. typedef times_iterator< Stepper , empty_system , state_type , time_iterator_type> stepper_iterator;
  127. state_type x = {{ 1.0 }};
  128. std::vector< state_type > res;
  129. stepper_iterator first( Stepper() , empty_system() , x , times.begin() , times.end() , 0.1 );
  130. stepper_iterator last( Stepper() , empty_system() , x );
  131. std::copy( first , last , std::back_insert_iterator< std::vector< state_type > >( res ) );
  132. BOOST_CHECK_EQUAL( res.size() , size_t( 4 ) );
  133. BOOST_CHECK_CLOSE( res[0][0] , 1.0 , 1.0e-13 );
  134. BOOST_CHECK_CLOSE( res[1][0] , 1.25 , 1.0e-13 );
  135. BOOST_CHECK_CLOSE( res[2][0] , 1.5 , 1.0e-13 );
  136. BOOST_CHECK_CLOSE( res[3][0] , 1.75 , 1.0e-13 );
  137. BOOST_CHECK_CLOSE( x[0] , 1.75 , 1.0e-13 ); // the iterator should not iterate over the end
  138. }
  139. BOOST_AUTO_TEST_CASE_TEMPLATE( copy_algorithm_negative_time_step , Stepper , dummy_steppers )
  140. {
  141. typedef times_iterator< Stepper , empty_system , state_type , time_iterator_type > stepper_iterator;
  142. state_type x = {{ 1.0 }};
  143. std::vector< state_type > res;
  144. boost::array<double,4> neg_times = {{ 0.0 , -0.1, -0.2, -0.3 }};
  145. stepper_iterator first( Stepper() , empty_system() , x , neg_times.begin() , neg_times.end() , -0.1 );
  146. stepper_iterator last( Stepper() , empty_system() , x );
  147. std::copy( first , last , std::back_insert_iterator< std::vector< state_type > >( res ) );
  148. BOOST_CHECK_EQUAL( res.size() , size_t( 4 ) );
  149. BOOST_CHECK_CLOSE( res[0][0] , 1.0 , 1.0e-13 );
  150. BOOST_CHECK_CLOSE( res[1][0] , 1.25 , 1.0e-13 );
  151. BOOST_CHECK_CLOSE( res[2][0] , 1.5 , 1.0e-13 );
  152. BOOST_CHECK_CLOSE( res[3][0] , 1.75 , 1.0e-13 );
  153. BOOST_CHECK_CLOSE( x[0] , 1.75 , 1.0e-13 );
  154. }
  155. BOOST_AUTO_TEST_CASE_TEMPLATE( copy_algorithm_with_factory , Stepper , dummy_steppers )
  156. {
  157. state_type x = {{ 1.0 }};
  158. std::vector< state_type > res;
  159. std::copy( make_times_iterator_begin( Stepper() , empty_system() , x , times.begin() , times.end() , 0.1 ) ,
  160. make_times_iterator_end<time_iterator_type>( Stepper() , empty_system() , x ) ,
  161. std::back_insert_iterator< std::vector< state_type > >( res ) );
  162. BOOST_CHECK_EQUAL( res.size() , size_t( 4 ) );
  163. BOOST_CHECK_CLOSE( res[0][0] , 1.0 , 1.0e-13 );
  164. BOOST_CHECK_CLOSE( res[1][0] , 1.25 , 1.0e-13 );
  165. BOOST_CHECK_CLOSE( res[2][0] , 1.5 , 1.0e-13 );
  166. BOOST_CHECK_CLOSE( res[3][0] , 1.75 , 1.0e-13 );
  167. BOOST_CHECK_CLOSE( x[0] , 1.75 , 1.0e-13 );
  168. }
  169. BOOST_AUTO_TEST_CASE_TEMPLATE( copy_algorithm_with_range_factory , Stepper , dummy_steppers )
  170. {
  171. state_type x = {{ 1.0 }};
  172. std::vector< state_type > res;
  173. boost::range::copy( make_times_range( Stepper() , empty_system() , x , times.begin() , times.end() , 0.1 ) ,
  174. std::back_insert_iterator< std::vector< state_type > >( res ) );
  175. BOOST_CHECK_EQUAL( res.size() , size_t( 4 ) );
  176. BOOST_CHECK_CLOSE( res[0][0] , 1.0 , 1.0e-13 );
  177. BOOST_CHECK_CLOSE( res[1][0] , 1.25 , 1.0e-13 );
  178. BOOST_CHECK_CLOSE( res[2][0] , 1.5 , 1.0e-13 );
  179. BOOST_CHECK_CLOSE( res[3][0] , 1.75 , 1.0e-13 );
  180. BOOST_CHECK_CLOSE( x[0] , 1.75 , 1.0e-13 );
  181. }
  182. BOOST_AUTO_TEST_SUITE_END()