| 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 | |
| 11 | #include <boost/test/test_tools.hpp> |
| 12 | #include <boost/test/unit_test.hpp> |
| 13 | |
| 14 | #include <boost/assign.hpp> |
| 15 | #include <algorithm> |
| 16 | #include <functional> |
| 17 | #include <list> |
| 18 | #include <numeric> |
| 19 | #include <deque> |
| 20 | #include <vector> |
| 21 | |
| 22 | namespace boost |
| 23 | { |
| 24 | namespace |
| 25 | { |
| 26 | template<class Container, class Iterator> |
| 27 | void test_rotate_impl(Container& cont, Iterator where_it) |
| 28 | { |
| 29 | Container reference(cont); |
| 30 | Container test(cont); |
| 31 | |
| 32 | Iterator reference_where_it = reference.begin(); |
| 33 | std::advance(reference_where_it, |
| 34 | std::distance(cont.begin(), where_it)); |
| 35 | |
| 36 | std::rotate(reference.begin(), reference_where_it, reference.end()); |
| 37 | |
| 38 | Iterator test_where_it = test.begin(); |
| 39 | std::advance(test_where_it, |
| 40 | std::distance(cont.begin(), where_it)); |
| 41 | |
| 42 | boost::rotate(test, test_where_it); |
| 43 | |
| 44 | BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(), |
| 45 | test.begin(), test.end() ); |
| 46 | |
| 47 | test = cont; |
| 48 | test_where_it = test.begin(); |
| 49 | std::advance(test_where_it, |
| 50 | std::distance(cont.begin(), where_it)); |
| 51 | |
| 52 | boost::rotate(boost::make_iterator_range(test), test_where_it); |
| 53 | |
| 54 | BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(), |
| 55 | test.begin(), test.end() ); |
| 56 | } |
| 57 | |
| 58 | template<class Container> |
| 59 | void test_rotate_impl(Container& cont) |
| 60 | { |
| 61 | typedef BOOST_DEDUCED_TYPENAME range_iterator<Container>::type iterator_t; |
| 62 | |
| 63 | iterator_t last = cont.end(); |
| 64 | for (iterator_t it = cont.begin(); it != last; ++it) |
| 65 | { |
| 66 | test_rotate_impl(cont, it); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | template<class Container> |
| 71 | void test_rotate_impl() |
| 72 | { |
| 73 | using namespace boost::assign; |
| 74 | |
| 75 | Container cont; |
| 76 | test_rotate_impl(cont); |
| 77 | |
| 78 | cont.clear(); |
| 79 | cont += 1; |
| 80 | test_rotate_impl(cont); |
| 81 | |
| 82 | cont.clear(); |
| 83 | cont += 1,2,3,4,5,6,7,8,9; |
| 84 | test_rotate_impl(cont); |
| 85 | } |
| 86 | |
| 87 | void test_rotate() |
| 88 | { |
| 89 | test_rotate_impl< std::vector<int> >(); |
| 90 | test_rotate_impl< std::list<int> >(); |
| 91 | test_rotate_impl< std::deque<int> >(); |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | |
| 97 | boost::unit_test::test_suite* |
| 98 | init_unit_test_suite(int argc, char* argv[]) |
| 99 | { |
| 100 | boost::unit_test::test_suite* test |
| 101 | = BOOST_TEST_SUITE( "RangeTestSuite.algorithm.rotate" ); |
| 102 | |
| 103 | test->add( BOOST_TEST_CASE( &boost::test_rotate ) ); |
| 104 | |
| 105 | return test; |
| 106 | } |
| 107 | |