• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Boost.Range library
2 //
3 //  Copyright Neil Groves 2009. Use, modification and
4 //  distribution is subject to the Boost Software License, Version
5 //  1.0. (See accompanying file LICENSE_1_0.txt or copy at
6 //  http://www.boost.org/LICENSE_1_0.txt)
7 //
8 //
9 // For more information, see http://www.boost.org/libs/range/
10 //
11 #include <boost/range/algorithm/unique_copy.hpp>
12 
13 #include <boost/test/test_tools.hpp>
14 #include <boost/test/unit_test.hpp>
15 
16 #include <boost/assign.hpp>
17 #include <algorithm>
18 #include <functional>
19 #include <list>
20 #include <numeric>
21 #include <deque>
22 #include <vector>
23 
24 namespace
25 {
26     template<class OutputIterator, class Value>
test_append(OutputIterator target,Value value)27     void test_append(OutputIterator target, Value value)
28     {
29         *target++ = value;
30     }
31 
32     template<class Container>
test_unique_copy_impl(Container & c)33     void test_unique_copy_impl(Container& c)
34     {
35         typedef BOOST_DEDUCED_TYPENAME boost::range_value<Container>::type value_type;
36         std::vector<value_type> reference;
37         std::vector<value_type> test;
38 
39         test_append(
40             std::unique_copy(c.begin(), c.end(), std::back_inserter(reference)),
41             value_type()
42             );
43 
44         test_append(
45             boost::unique_copy(c, std::back_inserter(test)),
46             value_type()
47             );
48 
49         BOOST_CHECK_EQUAL_COLLECTIONS(reference.begin(), reference.end(),
50                                       test.begin(), test.end());
51 
52         test.clear();
53 
54         test_append(
55             boost::unique_copy(boost::make_iterator_range(c),
56                                std::back_inserter(test)),
57             value_type()
58             );
59 
60         BOOST_CHECK_EQUAL_COLLECTIONS(reference.begin(), reference.end(),
61                                       test.begin(), test.end());
62     }
63 
64     template<class Container, class Pred>
test_unique_copy_impl(Container & c,Pred pred)65     void test_unique_copy_impl(Container& c, Pred pred)
66     {
67         typedef BOOST_DEDUCED_TYPENAME boost::range_value<Container>::type value_type;
68         std::vector<value_type> reference;
69         std::vector<value_type> test;
70 
71         test_append(
72             std::unique_copy(c.begin(), c.end(), std::back_inserter(reference), pred),
73             value_type()
74             );
75 
76         test_append(
77             boost::unique_copy(c, std::back_inserter(test), pred),
78             value_type()
79             );
80 
81         BOOST_CHECK_EQUAL_COLLECTIONS(reference.begin(), reference.end(),
82                                       test.begin(), test.end());
83 
84         test.clear();
85 
86         test_append(
87             boost::unique_copy(boost::make_iterator_range(c),
88                                std::back_inserter(test), pred),
89             value_type()
90             );
91 
92         BOOST_CHECK_EQUAL_COLLECTIONS(reference.begin(), reference.end(),
93                                       test.begin(), test.end());
94     }
95 
96     template<class Container, class Pred>
test_unique_copy_driver(Pred pred)97     void test_unique_copy_driver(Pred pred)
98     {
99         using namespace boost::assign;
100 
101         typedef BOOST_DEDUCED_TYPENAME Container::value_type value_t;
102 
103         Container cont;
104 
105         test_unique_copy_impl(cont);
106         test_unique_copy_impl(cont, pred);
107 
108         cont.clear();
109         cont += 1;
110 
111         std::vector<value_t> temp(cont.begin(), cont.end());
112         std::sort(temp.begin(), temp.end());
113         cont.assign(temp.begin(), temp.end());
114         test_unique_copy_impl(cont);
115 
116         std::sort(temp.begin(), temp.end(), pred);
117         cont.assign(temp.begin(), temp.end());
118         test_unique_copy_impl(cont, pred);
119 
120         cont.clear();
121         cont += 1,2,2,2,2,3,4,5,6,7,8,9;
122 
123         temp.assign(cont.begin(), cont.end());
124         std::sort(temp.begin(), temp.end());
125         cont.assign(temp.begin(), temp.end());
126         test_unique_copy_impl(cont);
127 
128         std::sort(temp.begin(), temp.end(), pred);
129         cont.assign(temp.begin(), temp.end());
130         test_unique_copy_impl(cont, pred);
131     }
132 
133     template<class Container>
test_unique_copy_impl()134     void test_unique_copy_impl()
135     {
136         test_unique_copy_driver<Container>(std::less<int>());
137         test_unique_copy_driver<Container>(std::greater<int>());
138     }
139 
test_unique_copy()140     void test_unique_copy()
141     {
142         test_unique_copy_impl< std::vector<int> >();
143         test_unique_copy_impl< std::list<int> >();
144         test_unique_copy_impl< std::deque<int> >();
145     }
146 }
147 
148 boost::unit_test::test_suite*
init_unit_test_suite(int argc,char * argv[])149 init_unit_test_suite(int argc, char* argv[])
150 {
151     boost::unit_test::test_suite* test
152         = BOOST_TEST_SUITE( "RangeTestSuite.algorithm.unique_copy" );
153 
154     test->add( BOOST_TEST_CASE( &test_unique_copy ) );
155 
156     return test;
157 }
158