rotate.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Copyright Neil Groves 2009. Use, modification and
  2. // distribution is subject to the Boost Software License, Version
  3. // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. //
  6. //
  7. // For more information, see http://www.boost.org/libs/range/
  8. //
  9. #include <boost/range/algorithm/rotate.hpp>
  10. #include <boost/test/test_tools.hpp>
  11. #include <boost/test/unit_test.hpp>
  12. #include <boost/assign.hpp>
  13. #include <boost/bind.hpp>
  14. #include <algorithm>
  15. #include <functional>
  16. #include <list>
  17. #include <numeric>
  18. #include <deque>
  19. #include <vector>
  20. namespace boost
  21. {
  22. namespace
  23. {
  24. template<class Container, class Iterator>
  25. void test_rotate_impl(Container& cont, Iterator where_it)
  26. {
  27. Container reference(cont);
  28. Container test(cont);
  29. Iterator reference_where_it = reference.begin();
  30. std::advance(reference_where_it,
  31. std::distance(cont.begin(), where_it));
  32. std::rotate(reference.begin(), reference_where_it, reference.end());
  33. Iterator test_where_it = test.begin();
  34. std::advance(test_where_it,
  35. std::distance(cont.begin(), where_it));
  36. boost::rotate(test, test_where_it);
  37. BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(),
  38. test.begin(), test.end() );
  39. test = cont;
  40. test_where_it = test.begin();
  41. std::advance(test_where_it,
  42. std::distance(cont.begin(), where_it));
  43. boost::rotate(boost::make_iterator_range(test), test_where_it);
  44. BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(),
  45. test.begin(), test.end() );
  46. }
  47. template<class Container>
  48. void test_rotate_impl(Container& cont)
  49. {
  50. typedef BOOST_DEDUCED_TYPENAME range_iterator<Container>::type iterator_t;
  51. iterator_t last = cont.end();
  52. for (iterator_t it = cont.begin(); it != last; ++it)
  53. {
  54. test_rotate_impl(cont, it);
  55. }
  56. }
  57. template<class Container>
  58. void test_rotate_impl()
  59. {
  60. using namespace boost::assign;
  61. Container cont;
  62. test_rotate_impl(cont);
  63. cont.clear();
  64. cont += 1;
  65. test_rotate_impl(cont);
  66. cont.clear();
  67. cont += 1,2,3,4,5,6,7,8,9;
  68. test_rotate_impl(cont);
  69. }
  70. void test_rotate()
  71. {
  72. test_rotate_impl< std::vector<int> >();
  73. test_rotate_impl< std::list<int> >();
  74. test_rotate_impl< std::deque<int> >();
  75. }
  76. }
  77. }
  78. boost::unit_test::test_suite*
  79. init_unit_test_suite(int argc, char* argv[])
  80. {
  81. boost::unit_test::test_suite* test
  82. = BOOST_TEST_SUITE( "RangeTestSuite.algorithm.rotate" );
  83. test->add( BOOST_TEST_CASE( &boost::test_rotate ) );
  84. return test;
  85. }