n_step_time_iterator.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. [auto_generated]
  3. libs/numeric/odeint/test/n_step_time_iterator.cpp
  4. [begin_description]
  5. This file tests the n-step time 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_n_step_time_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/copy.hpp>
  20. #include <boost/range/algorithm/for_each.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/n_step_time_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. typedef dummy_stepper::time_type time_type;
  33. typedef std::vector< std::pair< state_type , time_type > > result_vector;
  34. BOOST_AUTO_TEST_SUITE( n_step_time_iterator_test )
  35. typedef mpl::vector<
  36. dummy_stepper
  37. , dummy_dense_output_stepper
  38. > dummy_steppers;
  39. BOOST_AUTO_TEST_CASE_TEMPLATE( copy_stepper_iterator , Stepper , dummy_steppers )
  40. {
  41. typedef n_step_time_iterator< Stepper , empty_system , state_type > iterator_type;
  42. state_type x = {{ 1.0 }};
  43. iterator_type iter1 = iterator_type( Stepper() , empty_system() , x , 0.0 , 0.1 , 10 );
  44. iterator_type iter2 = iter1;
  45. BOOST_CHECK_EQUAL( &( iter1->first ) , &( iter2->first ) );
  46. BOOST_CHECK_EQUAL( &( iter1->first ) , &x );
  47. BOOST_CHECK( iter1.same( iter2 ) );
  48. }
  49. BOOST_AUTO_TEST_CASE_TEMPLATE( assignment_stepper_iterator , Stepper , dummy_steppers )
  50. {
  51. typedef n_step_time_iterator< Stepper , empty_system , state_type > iterator_type;
  52. state_type x1 = {{ 1.0 }} , x2 = {{ 2.0 }};
  53. iterator_type iter1 = iterator_type( Stepper() , empty_system() , x1 , 0.0 , 0.1 , 10 );
  54. iterator_type iter2 = iterator_type( Stepper() , empty_system() , x2 , 0.0 , 0.2 , 10 );
  55. BOOST_CHECK_EQUAL( &( iter1->first ) , &x1 );
  56. BOOST_CHECK_EQUAL( &( iter2->first ) , &x2 );
  57. BOOST_CHECK( !iter1.same( iter2 ) );
  58. iter2 = iter1;
  59. BOOST_CHECK_EQUAL( &( iter1->first ) , &x1 );
  60. BOOST_CHECK_EQUAL( &( iter2->first ) , &x1 );
  61. BOOST_CHECK( iter1.same( iter2 ) );
  62. }
  63. BOOST_AUTO_TEST_CASE_TEMPLATE( stepper_iterator_factory , Stepper , dummy_steppers )
  64. {
  65. Stepper stepper;
  66. empty_system system;
  67. state_type x = {{ 1.0 }};
  68. std::for_each(
  69. make_n_step_time_iterator_begin( stepper , boost::ref( system ) , x , 0.0 , 0.1 , 10 ) ,
  70. make_n_step_time_iterator_end( stepper , boost::ref( system ) , x ) ,
  71. dummy_observer() );
  72. BOOST_CHECK_CLOSE( x[0] , 3.5 , 1.0e-13 );
  73. }
  74. BOOST_AUTO_TEST_CASE_TEMPLATE( stepper_range , Stepper , dummy_steppers )
  75. {
  76. Stepper stepper;
  77. empty_system system;
  78. state_type x = {{ 1.0 }};
  79. boost::for_each( make_n_step_time_range( stepper , boost::ref( system ) , x , 0.0 , 0.1 , 10 ) ,
  80. dummy_observer() );
  81. BOOST_CHECK_CLOSE( x[0] , 3.5 , 1.0e-13 );
  82. }
  83. BOOST_AUTO_TEST_CASE_TEMPLATE( stepper_iterator_with_reference_wrapper_factory , Stepper , dummy_steppers )
  84. {
  85. Stepper stepper;
  86. empty_system system;
  87. state_type x = {{ 1.0 }};
  88. std::for_each(
  89. make_n_step_time_iterator_begin( boost::ref( stepper ) , boost::ref( system ) , x , 0.0 , 0.1 , 10 ) ,
  90. make_n_step_time_iterator_end( boost::ref( stepper ) , boost::ref( system ) , x ) ,
  91. dummy_observer() );
  92. BOOST_CHECK_CLOSE( x[0] , 3.5 , 1.0e-13 );
  93. }
  94. BOOST_AUTO_TEST_CASE_TEMPLATE( stepper_range_with_reference_wrapper , Stepper , dummy_steppers )
  95. {
  96. Stepper stepper;
  97. empty_system system;
  98. state_type x = {{ 1.0 }};
  99. boost::for_each( make_n_step_time_range( boost::ref( stepper ) , boost::ref( system ) , x , 0.0 , 0.1 , 10 ) ,
  100. dummy_observer() );
  101. BOOST_CHECK_CLOSE( x[0] , 3.5 , 1.0e-13 );
  102. }
  103. BOOST_AUTO_TEST_CASE_TEMPLATE( transitivity1 , Stepper , dummy_steppers )
  104. {
  105. typedef n_step_time_iterator< Stepper , empty_system , state_type > stepper_iterator;
  106. state_type x = {{ 1.0 }};
  107. stepper_iterator first1( Stepper() , empty_system() , x , 0.0 , 0.1 , 0 );
  108. stepper_iterator last1( Stepper() , empty_system() , x );
  109. stepper_iterator last2( Stepper() , empty_system() , x );
  110. BOOST_CHECK( last1 == last2 );
  111. BOOST_CHECK( first1 != last1 );
  112. BOOST_CHECK( ++first1 == last1 );
  113. }
  114. BOOST_AUTO_TEST_CASE_TEMPLATE( copy_algorithm , Stepper , dummy_steppers )
  115. {
  116. typedef n_step_time_iterator< Stepper , empty_system , state_type > stepper_iterator;
  117. state_type x = {{ 1.0 }};
  118. result_vector res;
  119. stepper_iterator first( Stepper() , empty_system() , x , 0.0 , 0.1 , 3 );
  120. stepper_iterator last( Stepper() , empty_system() , x );
  121. std::copy( first , last , std::back_insert_iterator< result_vector >( res ) );
  122. BOOST_CHECK_EQUAL( res.size() , size_t( 4 ) );
  123. BOOST_CHECK_CLOSE( res[0].first[0] , 1.0 , 1.0e-13 );
  124. BOOST_CHECK_CLOSE( res[0].second , 0.0 , 1.0e-13 );
  125. BOOST_CHECK_CLOSE( res[1].first[0] , 1.25 , 1.0e-13 );
  126. BOOST_CHECK_CLOSE( res[1].second , 0.1 , 1.0e-13 );
  127. BOOST_CHECK_CLOSE( res[2].first[0] , 1.5 , 1.0e-13 );
  128. BOOST_CHECK_CLOSE( res[2].second , 0.2 , 1.0e-13 );
  129. BOOST_CHECK_CLOSE( res[3].first[0] , 1.75 , 1.0e-13 );
  130. BOOST_CHECK_CLOSE( res[3].second , 0.3 , 1.0e-13 );
  131. BOOST_CHECK_CLOSE( x[0] , 1.75 , 1.0e-13 );
  132. }
  133. BOOST_AUTO_TEST_CASE_TEMPLATE( copy_algorithm_with_factory , Stepper , dummy_steppers )
  134. {
  135. state_type x = {{ 1.0 }};
  136. result_vector res;
  137. std::copy( make_n_step_time_iterator_begin( Stepper() , empty_system() , x , 0.0 , 0.1 , 3 ) ,
  138. make_n_step_time_iterator_end( Stepper() , empty_system() , x ) ,
  139. std::back_insert_iterator< result_vector >( res ) );
  140. BOOST_CHECK_EQUAL( res.size() , size_t( 4 ) );
  141. BOOST_CHECK_CLOSE( res[0].first[0] , 1.0 , 1.0e-13 );
  142. BOOST_CHECK_CLOSE( res[0].second , 0.0 , 1.0e-13 );
  143. BOOST_CHECK_CLOSE( res[1].first[0] , 1.25 , 1.0e-13 );
  144. BOOST_CHECK_CLOSE( res[1].second , 0.1 , 1.0e-13 );
  145. BOOST_CHECK_CLOSE( res[2].first[0] , 1.5 , 1.0e-13 );
  146. BOOST_CHECK_CLOSE( res[2].second , 0.2 , 1.0e-13 );
  147. BOOST_CHECK_CLOSE( res[3].first[0] , 1.75 , 1.0e-13 );
  148. BOOST_CHECK_CLOSE( res[3].second , 0.3 , 1.0e-13 );
  149. BOOST_CHECK_CLOSE( x[0] , 1.75 , 1.0e-13 );
  150. }
  151. BOOST_AUTO_TEST_CASE_TEMPLATE( copy_algorithm_with_range_factory , Stepper , dummy_steppers )
  152. {
  153. state_type x = {{ 1.0 }};
  154. result_vector res;
  155. boost::range::copy( make_n_step_time_range( Stepper() , empty_system() , x , 0.0 , 0.1 , 3 ) ,
  156. std::back_insert_iterator< result_vector >( res ) );
  157. BOOST_CHECK_EQUAL( res.size() , size_t( 4 ) );
  158. BOOST_CHECK_CLOSE( res[0].first[0] , 1.0 , 1.0e-13 );
  159. BOOST_CHECK_CLOSE( res[0].second , 0.0 , 1.0e-13 );
  160. BOOST_CHECK_CLOSE( res[1].first[0] , 1.25 , 1.0e-13 );
  161. BOOST_CHECK_CLOSE( res[1].second , 0.1 , 1.0e-13 );
  162. BOOST_CHECK_CLOSE( res[2].first[0] , 1.5 , 1.0e-13 );
  163. BOOST_CHECK_CLOSE( res[2].second , 0.2 , 1.0e-13 );
  164. BOOST_CHECK_CLOSE( res[3].first[0] , 1.75 , 1.0e-13 );
  165. BOOST_CHECK_CLOSE( res[3].second , 0.3 , 1.0e-13 );
  166. BOOST_CHECK_CLOSE( x[0] , 1.75 , 1.0e-13 );
  167. }
  168. BOOST_AUTO_TEST_SUITE_END()