]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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.hpp> | |
12 | #include <boost/range/detail/range_return.hpp> | |
13 | ||
14 | #include <boost/test/test_tools.hpp> | |
15 | #include <boost/test/unit_test.hpp> | |
16 | ||
17 | #include <boost/assign.hpp> | |
18 | #include <boost/bind.hpp> | |
19 | #include <boost/config.hpp> | |
20 | #include "../test_driver/range_overload_test_driver.hpp" | |
21 | #include <algorithm> | |
22 | #include <functional> | |
23 | #include <list> | |
24 | #include <numeric> | |
25 | #include <deque> | |
26 | #include <vector> | |
27 | ||
28 | namespace boost_range_test_algorithm_unique | |
29 | { | |
30 | // test the 'unique' algorithm without a predicate | |
31 | class unique_test_policy | |
32 | { | |
33 | public: | |
34 | template< class Container > | |
35 | BOOST_DEDUCED_TYPENAME boost::range_iterator<Container>::type | |
36 | test_iter(Container& cont) | |
37 | { | |
38 | // There isn't an iterator return version of boost::unique, so just | |
39 | // perform the standard algorithm | |
40 | return std::unique(cont.begin(), cont.end()); | |
41 | } | |
42 | ||
43 | template< boost::range_return_value return_type > | |
44 | struct test_range | |
45 | { | |
46 | template< class Container, class Policy > | |
47 | BOOST_DEDUCED_TYPENAME boost::range_return<Container,return_type>::type | |
48 | operator()(Policy&, Container& cont) | |
49 | { | |
50 | typedef BOOST_DEDUCED_TYPENAME boost::range_return<Container,return_type>::type result_t; | |
51 | ||
52 | Container cont2(cont); | |
53 | ||
54 | result_t result = boost::unique<return_type>(cont); | |
55 | ||
56 | boost::unique<return_type>(boost::make_iterator_range(cont2)); | |
57 | ||
58 | BOOST_CHECK_EQUAL_COLLECTIONS( cont.begin(), cont.end(), | |
59 | cont2.begin(), cont2.end() ); | |
60 | ||
61 | return result; | |
62 | } | |
63 | }; | |
64 | ||
65 | template<typename Container> | |
66 | struct test_range_overload | |
67 | { | |
68 | BOOST_STATIC_CONSTANT( | |
69 | ::boost::range_return_value, | |
70 | result_type = ::boost::return_begin_found); | |
71 | ||
72 | template<typename Policy> | |
73 | BOOST_DEDUCED_TYPENAME boost::range_return< | |
74 | Container, result_type | |
75 | >::type | |
76 | operator()(Policy& policy, Container& cont) | |
77 | { | |
78 | typedef BOOST_DEDUCED_TYPENAME boost::range_return< | |
79 | Container,result_type>::type result_t; | |
80 | ||
81 | Container cont2(cont); | |
82 | ||
83 | result_t result = boost::unique(cont); | |
84 | ||
85 | boost::unique(boost::make_iterator_range(cont2)); | |
86 | ||
87 | BOOST_CHECK_EQUAL_COLLECTIONS( | |
88 | cont.begin(), cont.end(), | |
89 | cont2.begin(), cont2.end()); | |
90 | ||
91 | return result; | |
92 | } | |
93 | }; | |
94 | ||
95 | template< class Container > | |
96 | BOOST_DEDUCED_TYPENAME boost::range_iterator<Container>::type | |
97 | reference(Container& cont) | |
98 | { | |
99 | return std::unique(cont.begin(), cont.end()); | |
100 | } | |
101 | }; | |
102 | ||
103 | // test the 'unique' algorithm with a predicate | |
104 | template<class Pred> | |
105 | class unique_pred_test_policy | |
106 | { | |
107 | public: | |
108 | template< class Container > | |
109 | BOOST_DEDUCED_TYPENAME boost::range_iterator<Container>::type | |
110 | test_iter(Container& cont) | |
111 | { | |
112 | // There isn't an iterator return version of boost::unique, so just | |
113 | // perform the standard algorithm | |
114 | return std::unique(cont.begin(), cont.end(), Pred()); | |
115 | } | |
116 | ||
117 | Pred pred() const { return Pred(); } | |
118 | ||
119 | template< boost::range_return_value return_type > | |
120 | struct test_range | |
121 | { | |
122 | template< class Container, class Policy > | |
123 | BOOST_DEDUCED_TYPENAME boost::range_return<Container,return_type>::type | |
124 | operator()(Policy& policy, Container& cont) | |
125 | { | |
126 | typedef BOOST_DEDUCED_TYPENAME boost::range_return<Container,return_type>::type result_t; | |
127 | ||
128 | Container cont2(cont); | |
129 | ||
130 | result_t result = boost::unique<return_type>(cont, policy.pred()); | |
131 | ||
132 | boost::unique<return_type>(boost::make_iterator_range(cont2), policy.pred()); | |
133 | ||
134 | BOOST_CHECK_EQUAL_COLLECTIONS( cont.begin(), cont.end(), | |
135 | cont2.begin(), cont2.end() ); | |
136 | ||
137 | return result; | |
138 | } | |
139 | }; | |
140 | ||
141 | template<typename Container> | |
142 | struct test_range_overload | |
143 | { | |
144 | BOOST_STATIC_CONSTANT( | |
145 | ::boost::range_return_value, | |
146 | result_type = ::boost::return_begin_found); | |
147 | ||
148 | template<typename Policy> | |
149 | BOOST_DEDUCED_TYPENAME boost::range_return<Container,result_type>::type | |
150 | operator()(Policy& policy, Container& cont) | |
151 | { | |
152 | typedef BOOST_DEDUCED_TYPENAME boost::range_return< | |
153 | Container,result_type>::type result_t; | |
154 | ||
155 | Container cont2(cont); | |
156 | ||
157 | result_t result = boost::unique(cont, policy.pred()); | |
158 | ||
159 | boost::unique(boost::make_iterator_range(cont2), policy.pred()); | |
160 | ||
161 | BOOST_CHECK_EQUAL_COLLECTIONS( | |
162 | cont.begin(), cont.end(), | |
163 | cont2.begin(), cont2.end()); | |
164 | ||
165 | return result; | |
166 | } | |
167 | }; | |
168 | ||
169 | template< class Container > | |
170 | BOOST_DEDUCED_TYPENAME boost::range_iterator<Container>::type | |
171 | reference(Container& cont) | |
172 | { | |
173 | return std::unique(cont.begin(), cont.end(), Pred()); | |
174 | } | |
175 | }; | |
176 | ||
177 | template<class Container, class TestPolicy, class Pred> | |
178 | void test_unique_impl(TestPolicy policy, Pred pred) | |
179 | { | |
180 | using namespace boost::assign; | |
181 | ||
182 | typedef BOOST_DEDUCED_TYPENAME Container::value_type value_t; | |
183 | ||
184 | boost::range_test::range_overload_test_driver test_driver; | |
185 | ||
186 | Container cont; | |
187 | ||
188 | test_driver(cont, policy); | |
189 | ||
190 | cont.clear(); | |
191 | cont += 1; | |
192 | ||
193 | std::vector<value_t> temp(cont.begin(), cont.end()); | |
194 | std::sort(temp.begin(), temp.end(), pred); | |
195 | cont.assign(temp.begin(), temp.end()); | |
196 | ||
197 | test_driver(cont, policy); | |
198 | ||
199 | cont.clear(); | |
200 | cont += 1,2,2,2,2,3,4,5,6,7,8,9; | |
201 | ||
202 | temp.assign(cont.begin(), cont.end()); | |
203 | std::sort(temp.begin(), temp.end(), pred); | |
204 | cont.assign(temp.begin(), temp.end()); | |
205 | ||
206 | test_driver(cont, policy); | |
207 | } | |
208 | ||
209 | template<typename T> | |
210 | struct equal_div_2 | |
211 | { | |
212 | typedef bool result_type; | |
213 | typedef const T& first_argument_type; | |
214 | typedef const T& second_argument_type; | |
215 | ||
216 | bool operator()(const T& left, const T& right) const | |
217 | { | |
218 | return left / 2 == right / 2; | |
219 | } | |
220 | }; | |
221 | ||
222 | template<class Container> | |
223 | void test_unique_impl() | |
224 | { | |
225 | test_unique_impl<Container>( | |
226 | unique_test_policy(), | |
227 | std::less<int>() | |
228 | ); | |
229 | ||
230 | test_unique_impl<Container>( | |
231 | unique_pred_test_policy<std::equal_to<int> >(), | |
232 | std::less<int>() | |
233 | ); | |
234 | ||
235 | test_unique_impl<Container>( | |
236 | unique_pred_test_policy<std::equal_to<int> >(), | |
237 | std::greater<int>() | |
238 | ); | |
239 | ||
240 | test_unique_impl<Container>( | |
241 | unique_pred_test_policy<equal_div_2<int> >(), | |
242 | std::less<int>() | |
243 | ); | |
244 | } | |
245 | ||
246 | void test_unique() | |
247 | { | |
248 | test_unique_impl< std::vector<int> >(); | |
249 | test_unique_impl< std::list<int> >(); | |
250 | test_unique_impl< std::deque<int> >(); | |
251 | } | |
252 | } | |
253 | ||
254 | boost::unit_test::test_suite* | |
255 | init_unit_test_suite(int argc, char* argv[]) | |
256 | { | |
257 | boost::unit_test::test_suite* test | |
258 | = BOOST_TEST_SUITE( "RangeTestSuite.algorithm.unique" ); | |
259 | ||
260 | test->add( BOOST_TEST_CASE( &boost_range_test_algorithm_unique::test_unique ) ); | |
261 | ||
262 | return test; | |
263 | } |