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