times_time_iterator.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. [auto_generated]
  3. libs/numeric/odeint/test/times_time_iterator.cpp
  4. [begin_description]
  5. This file tests the times 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_time_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_time_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. typedef dummy_stepper::time_type time_type;
  34. BOOST_AUTO_TEST_SUITE( times_time_iterator_test )
  35. typedef mpl::vector<
  36. dummy_stepper
  37. , dummy_dense_output_stepper
  38. > dummy_steppers;
  39. boost::array<double,4> times = {{ 0.0 , 0.1, 0.2, 0.3 }};
  40. typedef boost::array<double,4>::iterator time_iterator_type;
  41. typedef std::vector< std::pair< state_type , time_type > > result_vector;
  42. BOOST_AUTO_TEST_CASE_TEMPLATE( copy_stepper_iterator , Stepper , dummy_steppers )
  43. {
  44. typedef times_time_iterator< Stepper , empty_system , state_type , time_iterator_type > iterator_type;
  45. state_type x = {{ 1.0 }};
  46. iterator_type iter1 = iterator_type( Stepper() , empty_system() , x , times.begin() , times.end() , 0.1 );
  47. iterator_type iter2 = iter1;
  48. BOOST_CHECK_EQUAL( &(iter1->first) , &(iter2->first) );
  49. BOOST_CHECK_EQUAL( &(iter1->first) , &x );
  50. BOOST_CHECK( iter1.same( iter2 ) );
  51. }
  52. BOOST_AUTO_TEST_CASE_TEMPLATE( assignment_stepper_iterator , Stepper , dummy_steppers )
  53. {
  54. std::cout << "assignment" << std::endl;
  55. typedef times_time_iterator< Stepper , empty_system , state_type , time_iterator_type> iterator_type;
  56. state_type x1 = {{ 1.0 }} , x2 = {{ 2.0 }};
  57. iterator_type iter1 = iterator_type( Stepper() , empty_system() , x1 , times.begin() , times.end() , 0.1 );
  58. iterator_type iter2 = iterator_type( Stepper() , empty_system() , x2 , times.begin() , times.end() , 0.2 );
  59. BOOST_CHECK_EQUAL( &(iter1->first) , &x1 );
  60. BOOST_CHECK_EQUAL( &(iter2->first) , &x2 );
  61. BOOST_CHECK( !iter1.same( iter2 ) );
  62. iter2 = iter1;
  63. BOOST_CHECK_EQUAL( &(iter1->first) , &x1 );
  64. BOOST_CHECK_EQUAL( &(iter2->first) , &x1 );
  65. BOOST_CHECK( iter1.same( iter2 ) );
  66. }
  67. BOOST_AUTO_TEST_CASE_TEMPLATE( stepper_iterator_factory , Stepper , dummy_steppers )
  68. {
  69. std::cout << "factory" << std::endl;
  70. Stepper stepper;
  71. empty_system system;
  72. state_type x = {{ 1.0 }};
  73. std::for_each(
  74. make_times_time_iterator_begin( stepper , boost::ref( system ) , x , times.begin(), times.end() , 0.1 ) ,
  75. make_times_time_iterator_end<time_iterator_type>( stepper , boost::ref( system ) , x ) ,
  76. dummy_observer() );
  77. // 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
  78. BOOST_CHECK_CLOSE( x[0] , 1.75 , 1.0e-13 );
  79. }
  80. BOOST_AUTO_TEST_CASE_TEMPLATE( stepper_range , Stepper , dummy_steppers )
  81. {
  82. std::cout << "range" << std::endl;
  83. Stepper stepper;
  84. empty_system system;
  85. state_type x = {{ 1.0 }};
  86. boost::for_each( make_times_time_range( stepper , boost::ref( system ) , x , times.begin() , times.end() , 0.1 ) ,
  87. dummy_observer() );
  88. BOOST_CHECK_CLOSE( x[0] , 1.75 , 1.0e-13 );
  89. }
  90. BOOST_AUTO_TEST_CASE_TEMPLATE( stepper_iterator_with_reference_wrapper_factory , Stepper , dummy_steppers )
  91. {
  92. Stepper stepper;
  93. empty_system system;
  94. state_type x = {{ 1.0 }};
  95. std::for_each(
  96. make_times_time_iterator_begin( boost::ref( stepper ) , boost::ref( system ) , x , times.begin() , times.end() , 0.1 ) ,
  97. make_times_time_iterator_end<time_iterator_type>( boost::ref( stepper ) , boost::ref( system ) , x ) ,
  98. dummy_observer() );
  99. BOOST_CHECK_CLOSE( x[0] , 1.75 , 1.0e-13 );
  100. }
  101. BOOST_AUTO_TEST_CASE_TEMPLATE( stepper_range_with_reference_wrapper , Stepper , dummy_steppers )
  102. {
  103. Stepper stepper;
  104. empty_system system;
  105. state_type x = {{ 1.0 }};
  106. boost::for_each( make_times_time_range( boost::ref( stepper ) , boost::ref( system ) , x , times.begin() , times.end(), 0.1 ) ,
  107. dummy_observer() );
  108. BOOST_CHECK_CLOSE( x[0] , 1.75 , 1.0e-13 );
  109. }
  110. BOOST_AUTO_TEST_CASE_TEMPLATE( transitivity1 , Stepper , dummy_steppers )
  111. {
  112. typedef times_time_iterator< Stepper , empty_system , state_type , time_iterator_type > stepper_iterator;
  113. std::cout << "transitivity1" << std::endl;
  114. state_type x = {{ 1.0 }};
  115. stepper_iterator first1( Stepper() , empty_system() , x , times.end() , times.end() , 0.1 );
  116. stepper_iterator last1( Stepper() , empty_system() , x );
  117. stepper_iterator last2( Stepper() , empty_system() , x );
  118. BOOST_CHECK( first1 == last1 );
  119. BOOST_CHECK( first1 == last2 );
  120. BOOST_CHECK( last1 == last2 );
  121. first1 = stepper_iterator( Stepper() , empty_system() , x , times.end()-1 , times.end() , 0.1 );
  122. last1 = stepper_iterator( Stepper() , empty_system() , x );
  123. BOOST_CHECK( first1 != last1 );
  124. BOOST_CHECK( ++first1 == last1 );
  125. }
  126. BOOST_AUTO_TEST_CASE_TEMPLATE( copy_algorithm , Stepper , dummy_steppers )
  127. {
  128. typedef times_time_iterator< Stepper , empty_system , state_type , time_iterator_type> stepper_iterator;
  129. state_type x = {{ 1.0 }};
  130. result_vector res;
  131. stepper_iterator first( Stepper() , empty_system() , x , times.begin() , times.end() , 0.1 );
  132. stepper_iterator last( Stepper() , empty_system() , x );
  133. std::copy( first , last , std::back_insert_iterator< result_vector >( res ) );
  134. BOOST_CHECK_EQUAL( res.size() , size_t( 4 ) );
  135. BOOST_CHECK_CLOSE( res[0].first[0] , 1.0 , 1.0e-13 );
  136. BOOST_CHECK_CLOSE( res[1].first[0] , 1.25 , 1.0e-13 );
  137. BOOST_CHECK_CLOSE( res[2].first[0] , 1.5 , 1.0e-13 );
  138. BOOST_CHECK_CLOSE( res[3].first[0] , 1.75 , 1.0e-13 );
  139. for( size_t n=0 ; n<4 ; ++n )
  140. BOOST_CHECK_CLOSE( res[n].second , times[n] , 1.0e-13 );
  141. BOOST_CHECK_CLOSE( x[0] , 1.75 , 1.0e-13 ); // the iterator should not iterate over the end
  142. }
  143. BOOST_AUTO_TEST_CASE_TEMPLATE( copy_algorithm_negative_time_step , Stepper , dummy_steppers )
  144. {
  145. typedef times_time_iterator< Stepper , empty_system , state_type , time_iterator_type > stepper_iterator;
  146. state_type x = {{ 1.0 }};
  147. result_vector res;
  148. boost::array<double,4> neg_times = {{ 0.0 , -0.1, -0.2, -0.3 }};
  149. stepper_iterator first( Stepper() , empty_system() , x , neg_times.begin() , neg_times.end() , -0.1 );
  150. stepper_iterator last( Stepper() , empty_system() , x );
  151. std::copy( first , last , std::back_insert_iterator< result_vector >( res ) );
  152. BOOST_CHECK_EQUAL( res.size() , size_t( 4 ) );
  153. BOOST_CHECK_CLOSE( res[0].first[0] , 1.0 , 1.0e-13 );
  154. BOOST_CHECK_CLOSE( res[1].first[0] , 1.25 , 1.0e-13 );
  155. BOOST_CHECK_CLOSE( res[2].first[0] , 1.5 , 1.0e-13 );
  156. BOOST_CHECK_CLOSE( res[3].first[0] , 1.75 , 1.0e-13 );
  157. for( size_t n=0 ; n<4 ; ++n )
  158. BOOST_CHECK_CLOSE( res[n].second , neg_times[n] , 1.0e-13 );
  159. BOOST_CHECK_CLOSE( x[0] , 1.75 , 1.0e-13 );
  160. }
  161. BOOST_AUTO_TEST_CASE_TEMPLATE( copy_algorithm_with_factory , Stepper , dummy_steppers )
  162. {
  163. state_type x = {{ 1.0 }};
  164. result_vector res;
  165. std::copy( make_times_time_iterator_begin( Stepper() , empty_system() , x , times.begin() , times.end() , 0.1 ) ,
  166. make_times_time_iterator_end<time_iterator_type>( Stepper() , empty_system() , x ) ,
  167. std::back_insert_iterator< result_vector >( res ) );
  168. BOOST_CHECK_EQUAL( res.size() , size_t( 4 ) );
  169. BOOST_CHECK_CLOSE( res[0].first[0] , 1.0 , 1.0e-13 );
  170. BOOST_CHECK_CLOSE( res[1].first[0] , 1.25 , 1.0e-13 );
  171. BOOST_CHECK_CLOSE( res[2].first[0] , 1.5 , 1.0e-13 );
  172. BOOST_CHECK_CLOSE( res[3].first[0] , 1.75 , 1.0e-13 );
  173. for( size_t n=0 ; n<4 ; ++n )
  174. BOOST_CHECK_CLOSE( res[n].second , times[n] , 1.0e-13 );
  175. BOOST_CHECK_CLOSE( x[0] , 1.75 , 1.0e-13 );
  176. }
  177. BOOST_AUTO_TEST_CASE_TEMPLATE( copy_algorithm_with_range_factory , Stepper , dummy_steppers )
  178. {
  179. state_type x = {{ 1.0 }};
  180. result_vector res;
  181. boost::range::copy( make_times_time_range( Stepper() , empty_system() , x , times.begin() , times.end() , 0.1 ) ,
  182. std::back_insert_iterator< result_vector >( res ) );
  183. BOOST_CHECK_EQUAL( res.size() , size_t( 4 ) );
  184. BOOST_CHECK_CLOSE( res[0].first[0] , 1.0 , 1.0e-13 );
  185. BOOST_CHECK_CLOSE( res[1].first[0] , 1.25 , 1.0e-13 );
  186. BOOST_CHECK_CLOSE( res[2].first[0] , 1.5 , 1.0e-13 );
  187. BOOST_CHECK_CLOSE( res[3].first[0] , 1.75 , 1.0e-13 );
  188. for( size_t n=0 ; n<4 ; ++n )
  189. BOOST_CHECK_CLOSE( res[n].second , times[n] , 1.0e-13 );
  190. BOOST_CHECK_CLOSE( x[0] , 1.75 , 1.0e-13 );
  191. }
  192. BOOST_AUTO_TEST_SUITE_END()