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)
9 // For more information, see http://www.boost.org/libs/range/
11 #include <boost/range/algorithm/unique.hpp>
12 #include <boost/range/detail/range_return.hpp>
14 #include <boost/test/test_tools.hpp>
15 #include <boost/test/unit_test.hpp>
17 #include <boost/assign.hpp>
18 #include <boost/config.hpp>
19 #include "../test_driver/range_overload_test_driver.hpp"
27 namespace boost_range_test_algorithm_unique
29 // test the 'unique' algorithm without a predicate
30 class unique_test_policy
33 template< class Container
>
34 BOOST_DEDUCED_TYPENAME
boost::range_iterator
<Container
>::type
35 test_iter(Container
& cont
)
37 // There isn't an iterator return version of boost::unique, so just
38 // perform the standard algorithm
39 return std::unique(cont
.begin(), cont
.end());
42 template< boost::range_return_value return_type
>
45 template< class Container
, class Policy
>
46 BOOST_DEDUCED_TYPENAME
boost::range_return
<Container
,return_type
>::type
47 operator()(Policy
&, Container
& cont
)
49 typedef BOOST_DEDUCED_TYPENAME
boost::range_return
<Container
,return_type
>::type result_t
;
51 Container
cont2(cont
);
53 result_t result
= boost::unique
<return_type
>(cont
);
55 boost::unique
<return_type
>(boost::make_iterator_range(cont2
));
57 BOOST_CHECK_EQUAL_COLLECTIONS( cont
.begin(), cont
.end(),
58 cont2
.begin(), cont2
.end() );
64 template<typename Container
>
65 struct test_range_overload
67 BOOST_STATIC_CONSTANT(
68 ::boost::range_return_value
,
69 result_type
= ::boost::return_begin_found
);
71 template<typename Policy
>
72 BOOST_DEDUCED_TYPENAME
boost::range_return
<
73 Container
, result_type
75 operator()(Policy
& policy
, Container
& cont
)
77 typedef BOOST_DEDUCED_TYPENAME
boost::range_return
<
78 Container
,result_type
>::type result_t
;
80 Container
cont2(cont
);
82 result_t result
= boost::unique(cont
);
84 boost::unique(boost::make_iterator_range(cont2
));
86 BOOST_CHECK_EQUAL_COLLECTIONS(
87 cont
.begin(), cont
.end(),
88 cont2
.begin(), cont2
.end());
94 template< class Container
>
95 BOOST_DEDUCED_TYPENAME
boost::range_iterator
<Container
>::type
96 reference(Container
& cont
)
98 return std::unique(cont
.begin(), cont
.end());
102 // test the 'unique' algorithm with a predicate
104 class unique_pred_test_policy
107 template< class Container
>
108 BOOST_DEDUCED_TYPENAME
boost::range_iterator
<Container
>::type
109 test_iter(Container
& cont
)
111 // There isn't an iterator return version of boost::unique, so just
112 // perform the standard algorithm
113 return std::unique(cont
.begin(), cont
.end(), Pred());
116 Pred
pred() const { return Pred(); }
118 template< boost::range_return_value return_type
>
121 template< class Container
, class Policy
>
122 BOOST_DEDUCED_TYPENAME
boost::range_return
<Container
,return_type
>::type
123 operator()(Policy
& policy
, Container
& cont
)
125 typedef BOOST_DEDUCED_TYPENAME
boost::range_return
<Container
,return_type
>::type result_t
;
127 Container
cont2(cont
);
129 result_t result
= boost::unique
<return_type
>(cont
, policy
.pred());
131 boost::unique
<return_type
>(boost::make_iterator_range(cont2
), policy
.pred());
133 BOOST_CHECK_EQUAL_COLLECTIONS( cont
.begin(), cont
.end(),
134 cont2
.begin(), cont2
.end() );
140 template<typename Container
>
141 struct test_range_overload
143 BOOST_STATIC_CONSTANT(
144 ::boost::range_return_value
,
145 result_type
= ::boost::return_begin_found
);
147 template<typename Policy
>
148 BOOST_DEDUCED_TYPENAME
boost::range_return
<Container
,result_type
>::type
149 operator()(Policy
& policy
, Container
& cont
)
151 typedef BOOST_DEDUCED_TYPENAME
boost::range_return
<
152 Container
,result_type
>::type result_t
;
154 Container
cont2(cont
);
156 result_t result
= boost::unique(cont
, policy
.pred());
158 boost::unique(boost::make_iterator_range(cont2
), policy
.pred());
160 BOOST_CHECK_EQUAL_COLLECTIONS(
161 cont
.begin(), cont
.end(),
162 cont2
.begin(), cont2
.end());
168 template< class Container
>
169 BOOST_DEDUCED_TYPENAME
boost::range_iterator
<Container
>::type
170 reference(Container
& cont
)
172 return std::unique(cont
.begin(), cont
.end(), Pred());
176 template<class Container
, class TestPolicy
, class Pred
>
177 void test_unique_impl(TestPolicy policy
, Pred pred
)
179 using namespace boost::assign
;
181 typedef BOOST_DEDUCED_TYPENAME
Container::value_type value_t
;
183 boost::range_test::range_overload_test_driver test_driver
;
187 test_driver(cont
, policy
);
192 std::vector
<value_t
> temp(cont
.begin(), cont
.end());
193 std::sort(temp
.begin(), temp
.end(), pred
);
194 cont
.assign(temp
.begin(), temp
.end());
196 test_driver(cont
, policy
);
199 cont
+= 1,2,2,2,2,3,4,5,6,7,8,9;
201 temp
.assign(cont
.begin(), cont
.end());
202 std::sort(temp
.begin(), temp
.end(), pred
);
203 cont
.assign(temp
.begin(), temp
.end());
205 test_driver(cont
, policy
);
211 typedef bool result_type
;
212 typedef const T
& first_argument_type
;
213 typedef const T
& second_argument_type
;
215 bool operator()(const T
& left
, const T
& right
) const
217 return left
/ 2 == right
/ 2;
221 template<class Container
>
222 void test_unique_impl()
224 test_unique_impl
<Container
>(
225 unique_test_policy(),
229 test_unique_impl
<Container
>(
230 unique_pred_test_policy
<std::equal_to
<int> >(),
234 test_unique_impl
<Container
>(
235 unique_pred_test_policy
<std::equal_to
<int> >(),
239 test_unique_impl
<Container
>(
240 unique_pred_test_policy
<equal_div_2
<int> >(),
247 test_unique_impl
< std::vector
<int> >();
248 test_unique_impl
< std::list
<int> >();
249 test_unique_impl
< std::deque
<int> >();
253 boost::unit_test::test_suite
*
254 init_unit_test_suite(int argc
, char* argv
[])
256 boost::unit_test::test_suite
* test
257 = BOOST_TEST_SUITE( "RangeTestSuite.algorithm.unique" );
259 test
->add( BOOST_TEST_CASE( &boost_range_test_algorithm_unique::test_unique
) );