adaptive_iterator.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /*
  2. [auto_generated]
  3. libs/numeric/odeint/test/adaptive_iterator.cpp
  4. [begin_description]
  5. This file tests the adaptive iterators.
  6. [end_description]
  7. Copyright 2012-2013 Karsten Ahnert
  8. Copyright 2012-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_adaptive_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/adaptive_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( adaptive_iterator_test )
  33. typedef mpl::vector<
  34. dummy_controlled_stepper
  35. , dummy_dense_output_stepper
  36. > dummy_steppers;
  37. BOOST_AUTO_TEST_CASE( copy_controlled_stepper_iterator )
  38. {
  39. typedef adaptive_iterator< dummy_controlled_stepper , empty_system , state_type > iterator_type;
  40. state_type x = {{ 1.0 }};
  41. iterator_type iter1( dummy_controlled_stepper() , empty_system() , x );
  42. iterator_type iter2( iter1 );
  43. BOOST_CHECK_EQUAL( &( *iter1 ) , &x );
  44. BOOST_CHECK_EQUAL( &( *iter2 ) , &x );
  45. BOOST_CHECK_EQUAL( &( *iter1 ) , &( *iter2 ) );
  46. BOOST_CHECK( iter1.same( iter2 ) );
  47. ++iter1;
  48. ++iter2;
  49. BOOST_CHECK_EQUAL( &( *iter1 ) , &x );
  50. BOOST_CHECK_EQUAL( &( *iter2 ) , &x );
  51. BOOST_CHECK_EQUAL( &( *iter1 ) , &( *iter2 ) );
  52. BOOST_CHECK( iter1.same( iter2 ) );
  53. }
  54. BOOST_AUTO_TEST_CASE( copy_dense_output_stepper_iterator )
  55. {
  56. typedef adaptive_iterator< dummy_dense_output_stepper , empty_system , state_type > iterator_type;
  57. state_type x = {{ 1.0 }};
  58. // fix by mario: do not dereference iterators at the end - made iter1 start iterator
  59. iterator_type iter1( dummy_dense_output_stepper() , empty_system() , x , 0.0 , 1.0 , 0.1 );
  60. iterator_type iter2( iter1 );
  61. // fix by mario: iterator dereference now always gives internal state also for dense output, consistent with other iterator implementations
  62. // changed: iterators with dense output stepper do not have an internal state now to avoid a copy
  63. BOOST_CHECK_NE( & (*iter1) , & (*iter2) );
  64. BOOST_CHECK( iter1.same( iter2 ) );
  65. ++iter1;
  66. ++iter2;
  67. BOOST_CHECK_NE( & (*iter1) , & (*iter2) );
  68. BOOST_CHECK( iter1.same( iter2 ) );
  69. }
  70. BOOST_AUTO_TEST_CASE( copy_dense_output_stepper_iterator_with_reference_wrapper )
  71. {
  72. // bad use case, the same stepper is iterated twice
  73. typedef adaptive_iterator< boost::reference_wrapper< dummy_dense_output_stepper > , empty_system , state_type > iterator_type;
  74. state_type x = {{ 1.0 }};
  75. dummy_dense_output_stepper stepper;
  76. iterator_type iter1( boost::ref( stepper ) , empty_system() , x , 0.0 , 0.9 , 0.1 );
  77. iterator_type iter2( iter1 );
  78. BOOST_CHECK_EQUAL( & (*iter1) , & (*iter2) );
  79. BOOST_CHECK( iter1.same( iter2 ) );
  80. ++iter1;
  81. ++iter2;
  82. BOOST_CHECK_EQUAL( & (*iter1) , & (*iter2) );
  83. BOOST_CHECK( !iter1.same( iter2 ) ); // they point to the same stepper, there the times will be different
  84. }
  85. BOOST_AUTO_TEST_CASE( assignment_controlled_stepper_iterator )
  86. {
  87. typedef adaptive_iterator< dummy_controlled_stepper , empty_system , state_type > iterator_type;
  88. state_type x1 = {{ 1.0 }} , x2 = {{ 2.0 }};
  89. iterator_type iter1 = iterator_type( dummy_controlled_stepper() , empty_system() , x1 , 0.0 , 1.0 , 0.1 );
  90. iterator_type iter2 = iterator_type( dummy_controlled_stepper() , empty_system() , x2 , 0.0 , 1.0 , 0.2 );
  91. BOOST_CHECK_EQUAL( &(*iter1) , &x1 );
  92. BOOST_CHECK_EQUAL( &(*iter2) , &x2 );
  93. // the iterators are indeed the same as this only checks the time values
  94. BOOST_CHECK( !iter1.same( iter2 ) );
  95. iter2 = iter1;
  96. BOOST_CHECK_EQUAL( &(*iter1) , &x1 );
  97. BOOST_CHECK_EQUAL( &(*iter2) , &x1 );
  98. BOOST_CHECK( iter1.same( iter2 ) );
  99. }
  100. BOOST_AUTO_TEST_CASE( assignment_dense_output_stepper_iterator )
  101. {
  102. typedef adaptive_iterator< dummy_dense_output_stepper , empty_system , state_type > iterator_type;
  103. state_type x1 = {{ 1.0 }};
  104. iterator_type iter1 = iterator_type( dummy_dense_output_stepper() , empty_system() , x1 , 0.0 , 1.0 , 0.1 );
  105. iterator_type iter2 = iterator_type( dummy_dense_output_stepper() , empty_system() , x1 , 0.0 , 1.0 , 0.2 );
  106. BOOST_CHECK_NE( & (*iter1) , & (*iter2) );
  107. BOOST_CHECK( !iter1.same( iter2 ) );
  108. iter2 = iter1;
  109. // fix by mario: iterator dereference now always gives internal state also for dense output, consistent with other iterator implementations
  110. // changed: iterators with dense output stepper do not have an internal state now to avoid a copy
  111. BOOST_CHECK_NE( & (*iter1) , & (*iter2) );
  112. BOOST_CHECK( iter1.same( iter2 ) );
  113. }
  114. BOOST_AUTO_TEST_CASE( assignment_dense_output_stepper_iterator_with_reference_wrapper )
  115. {
  116. typedef adaptive_iterator< boost::reference_wrapper< dummy_dense_output_stepper > , empty_system , state_type > iterator_type;
  117. state_type x1 = {{ 1.0 }};
  118. dummy_dense_output_stepper stepper;
  119. iterator_type iter1 = iterator_type( boost::ref( stepper ) , empty_system() , x1 , 0.0 , 1.0 , 0.1 );
  120. iterator_type iter2 = iterator_type( boost::ref( stepper ) , empty_system() , x1 , 0.0 , 1.0 , 0.2 );
  121. BOOST_CHECK_EQUAL( & (*iter1) , & (*iter2) );
  122. BOOST_CHECK( !iter1.same( iter2 ) );
  123. iter2 = iter1;
  124. BOOST_CHECK_EQUAL( & (*iter1) , & (*iter2) );
  125. BOOST_CHECK( iter1.same( iter2 ) );
  126. }
  127. BOOST_AUTO_TEST_CASE( controlled_stepper_iterator_factory )
  128. {
  129. dummy_controlled_stepper stepper;
  130. empty_system system;
  131. state_type x = {{ 1.0 }};
  132. std::for_each(
  133. make_adaptive_iterator_begin( stepper , boost::ref( system ) , x , 0.0 , 1.0 , 0.1 ) ,
  134. make_adaptive_iterator_end( stepper , boost::ref( system ) , x ) ,
  135. dummy_observer() );
  136. BOOST_CHECK_CLOSE( x[0] , 3.5 , 1.0e-14 );
  137. }
  138. // just test if it compiles
  139. BOOST_AUTO_TEST_CASE( dense_output_stepper_iterator_factory )
  140. {
  141. dummy_dense_output_stepper stepper;
  142. empty_system system;
  143. state_type x = {{ 1.0 }};
  144. std::for_each(
  145. make_adaptive_iterator_begin( stepper , boost::ref( system ) , x , 0.0 , 1.0 , 0.1 ) ,
  146. make_adaptive_iterator_end( stepper , boost::ref( system ) , x ) ,
  147. dummy_observer() );
  148. }
  149. BOOST_AUTO_TEST_CASE( controlled_stepper_range )
  150. {
  151. dummy_controlled_stepper stepper;
  152. empty_system system;
  153. state_type x = {{ 1.0 }};
  154. boost::for_each( make_adaptive_range( stepper , boost::ref( system ) , x , 0.0 , 1.0 , 0.1 ) ,
  155. dummy_observer() );
  156. BOOST_CHECK_CLOSE( x[0] , 3.5 , 1.0e-14 );
  157. }
  158. // just test if it compiles
  159. BOOST_AUTO_TEST_CASE( dense_output_stepper_range )
  160. {
  161. dummy_dense_output_stepper stepper;
  162. empty_system system;
  163. state_type x = {{ 1.0 }};
  164. boost::for_each( make_adaptive_range( stepper , boost::ref( system ) , x , 0.0 , 1.0 , 0.1 ) ,
  165. dummy_observer() );
  166. }
  167. BOOST_AUTO_TEST_CASE( controlled_stepper_iterator_with_reference_wrapper_factory )
  168. {
  169. dummy_controlled_stepper stepper;
  170. empty_system system;
  171. state_type x = {{ 1.0 }};
  172. std::for_each(
  173. make_adaptive_iterator_begin( boost::ref( stepper ) , boost::ref( system ) , x , 0.0 , 1.0 , 0.1 ) ,
  174. make_adaptive_iterator_end( boost::ref( stepper ) , boost::ref( system ) , x ) ,
  175. dummy_observer() );
  176. BOOST_CHECK_CLOSE( x[0] , 3.5 , 1.0e-14 );
  177. }
  178. // just test if it compiles
  179. BOOST_AUTO_TEST_CASE( dense_output_stepper_iterator_with_reference_wrapper_factory )
  180. {
  181. dummy_dense_output_stepper stepper;
  182. empty_system system;
  183. state_type x = {{ 1.0 }};
  184. std::for_each(
  185. make_adaptive_iterator_begin( boost::ref( stepper ) , boost::ref( system ) , x , 0.0 , 1.0 , 0.1 ) ,
  186. make_adaptive_iterator_end( boost::ref( stepper ) , boost::ref( system ) , x ) ,
  187. dummy_observer() );
  188. }
  189. BOOST_AUTO_TEST_CASE( controlled_stepper_range_with_reference_wrapper )
  190. {
  191. dummy_controlled_stepper stepper;
  192. empty_system system;
  193. state_type x = {{ 1.0 }};
  194. boost::for_each( make_adaptive_range( boost::ref( stepper ) , boost::ref( system ) , x , 0.0 , 1.0 , 0.1 ) ,
  195. dummy_observer() );
  196. BOOST_CHECK_CLOSE( x[0] , 3.5 , 1.0e-14 );
  197. }
  198. // just test if it compiles
  199. BOOST_AUTO_TEST_CASE( dense_output_stepper_range_with_reference_wrapper )
  200. {
  201. dummy_dense_output_stepper stepper;
  202. empty_system system;
  203. state_type x = {{ 1.0 }};
  204. boost::for_each( make_adaptive_range( boost::ref( stepper ) , boost::ref( system ) , x , 0.0 , 1.0 , 0.1 ) ,
  205. dummy_observer() );
  206. }
  207. BOOST_AUTO_TEST_CASE_TEMPLATE( transitivity1 , Stepper , dummy_steppers )
  208. {
  209. typedef adaptive_iterator< Stepper , empty_system , state_type > stepper_iterator;
  210. state_type x = {{ 1.0 }};
  211. stepper_iterator first1( Stepper() , empty_system() , x , 2.5 , 2.0 , 0.1 );
  212. stepper_iterator last1( Stepper() , empty_system() , x );
  213. stepper_iterator last2( Stepper() , empty_system() , x );
  214. BOOST_CHECK( first1 == last1 );
  215. BOOST_CHECK( first1 == last2 );
  216. BOOST_CHECK( last1 == last2 );
  217. }
  218. BOOST_AUTO_TEST_CASE_TEMPLATE( copy_algorithm , Stepper , dummy_steppers )
  219. {
  220. typedef adaptive_iterator< Stepper , empty_system , state_type > stepper_iterator;
  221. state_type x = {{ 1.0 }};
  222. std::vector< state_type > res;
  223. stepper_iterator first( Stepper() , empty_system() , x , 0.0 , 0.35 , 0.1 );
  224. stepper_iterator last( Stepper() , empty_system() , x );
  225. std::copy( first , last , std::back_insert_iterator< std::vector< state_type > >( res ) );
  226. BOOST_CHECK_EQUAL( res.size() , size_t( 5 ) );
  227. BOOST_CHECK_CLOSE( res[0][0] , 1.0 , 1.0e-14 );
  228. BOOST_CHECK_CLOSE( res[1][0] , 1.25 , 1.0e-14 );
  229. BOOST_CHECK_CLOSE( res[2][0] , 1.5 , 1.0e-14 );
  230. BOOST_CHECK_CLOSE( res[3][0] , 1.75 , 1.0e-14 );
  231. BOOST_CHECK_CLOSE( res[4][0] , 2.0 , 1.0e-14 );
  232. }
  233. BOOST_AUTO_TEST_CASE_TEMPLATE( copy_algorithm_with_factory , Stepper , dummy_steppers )
  234. {
  235. state_type x = {{ 1.0 }};
  236. std::vector< state_type > res;
  237. std::copy( make_adaptive_iterator_begin( Stepper() , empty_system() , x , 0.0 , 0.35 , 0.1 ) ,
  238. make_adaptive_iterator_end( Stepper() , empty_system() , x ) ,
  239. std::back_insert_iterator< std::vector< state_type > >( res ) );
  240. BOOST_CHECK_EQUAL( res.size() , size_t( 5 ) );
  241. BOOST_CHECK_CLOSE( res[0][0] , 1.0 , 1.0e-14 );
  242. BOOST_CHECK_CLOSE( res[1][0] , 1.25 , 1.0e-14 );
  243. BOOST_CHECK_CLOSE( res[2][0] , 1.5 , 1.0e-14 );
  244. BOOST_CHECK_CLOSE( res[3][0] , 1.75 , 1.0e-14 );
  245. BOOST_CHECK_CLOSE( res[4][0] , 2.0 , 1.0e-14 );
  246. }
  247. BOOST_AUTO_TEST_CASE_TEMPLATE( copy_algorithm_with_range_factory , Stepper , dummy_steppers )
  248. {
  249. state_type x = {{ 1.0 }};
  250. std::vector< state_type > res;
  251. boost::range::copy( make_adaptive_range( Stepper() , empty_system() , x , 0.0 , 0.35 , 0.1 ) ,
  252. std::back_insert_iterator< std::vector< state_type > >( res ) );
  253. BOOST_CHECK_EQUAL( res.size() , size_t( 5 ) );
  254. BOOST_CHECK_CLOSE( res[0][0] , 1.0 , 1.0e-14 );
  255. BOOST_CHECK_CLOSE( res[1][0] , 1.25 , 1.0e-14 );
  256. BOOST_CHECK_CLOSE( res[2][0] , 1.5 , 1.0e-14 );
  257. BOOST_CHECK_CLOSE( res[3][0] , 1.75 , 1.0e-14 );
  258. BOOST_CHECK_CLOSE( res[4][0] , 2.0 , 1.0e-14 );
  259. }
  260. BOOST_AUTO_TEST_SUITE_END()