rotate_copy.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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_copy.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
  21. {
  22. template<class OutputIterator, class Value>
  23. void test_append(OutputIterator target, Value value)
  24. {
  25. *target++ = value;
  26. }
  27. template<class Container, class Iterator>
  28. void test_rotate_copy_impl(Container& cont, Iterator where_it)
  29. {
  30. typedef BOOST_DEDUCED_TYPENAME boost::range_value<Container>::type
  31. value_type;
  32. std::vector<value_type> reference;
  33. std::vector<value_type> test;
  34. typedef BOOST_DEDUCED_TYPENAME boost::range_iterator<Container>::type
  35. iterator_t BOOST_RANGE_UNUSED;
  36. test_append(
  37. std::rotate_copy(cont.begin(), where_it, cont.end(),
  38. std::back_inserter(reference)),
  39. value_type()
  40. );
  41. test_append(
  42. boost::rotate_copy(cont, where_it, std::back_inserter(test)),
  43. value_type()
  44. );
  45. BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(),
  46. test.begin(), test.end() );
  47. test.clear();
  48. test_append(
  49. boost::rotate_copy(boost::make_iterator_range(cont), where_it,
  50. std::back_inserter(test)),
  51. value_type()
  52. );
  53. BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(),
  54. test.begin(), test.end() );
  55. }
  56. template<class Container>
  57. void test_rotate_copy_impl(Container& cont)
  58. {
  59. typedef BOOST_DEDUCED_TYPENAME boost::range_iterator<Container>::type iterator_t;
  60. iterator_t last = cont.end();
  61. for (iterator_t it = cont.begin(); it != last; ++it)
  62. {
  63. test_rotate_copy_impl(cont, it);
  64. }
  65. }
  66. template<class Container>
  67. void test_rotate_copy_impl()
  68. {
  69. using namespace boost::assign;
  70. Container cont;
  71. test_rotate_copy_impl(cont);
  72. cont.clear();
  73. cont += 1;
  74. test_rotate_copy_impl(cont);
  75. cont.clear();
  76. cont += 1,2,3,4,5,6,7,8,9;
  77. test_rotate_copy_impl(cont);
  78. }
  79. void test_rotate_copy()
  80. {
  81. test_rotate_copy_impl< std::vector<int> >();
  82. test_rotate_copy_impl< std::list<int> >();
  83. test_rotate_copy_impl< std::deque<int> >();
  84. }
  85. }
  86. boost::unit_test::test_suite*
  87. init_unit_test_suite(int argc, char* argv[])
  88. {
  89. boost::unit_test::test_suite* test
  90. = BOOST_TEST_SUITE( "RangeTestSuite.algorithm.rotate_copy" );
  91. test->add( BOOST_TEST_CASE( &test_rotate_copy ) );
  92. return test;
  93. }