const_step_iterator.cpp 7.8 KB

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