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)
7 // For more information, see http://www.boost.org/libs/range/
9 #include <boost/range/algorithm/equal_range.hpp>
11 #include <boost/test/test_tools.hpp>
12 #include <boost/test/unit_test.hpp>
14 #include <boost/assign.hpp>
26 template<class Container
, class Pair
>
28 const Container
& reference
,
30 const Container
& test
,
34 typedef BOOST_DEDUCED_TYPENAME range_iterator
<const Container
>::type
37 BOOST_CHECK_EQUAL_COLLECTIONS(
38 reference
.begin(), reference
.end(),
39 test
.begin(), test
.end()
43 std::distance
<const_iterator_t
>(reference
.begin(), reference_pair
.first
),
44 std::distance
<const_iterator_t
>(test
.begin(), test_pair
.first
)
48 std::distance
<const_iterator_t
>(reference
.begin(), reference_pair
.second
),
49 std::distance
<const_iterator_t
>(test
.begin(), test_pair
.second
)
52 BOOST_CHECK_EQUAL_COLLECTIONS(
53 reference_pair
.first
, reference_pair
.second
,
54 test_pair
.first
, test_pair
.second
58 template<class Container
>
59 void test(Container
& cont
)
61 Container
reference(cont
);
64 typedef BOOST_DEDUCED_TYPENAME range_iterator
<Container
>::type iterator_t
;
65 typedef std::pair
<iterator_t
, iterator_t
> pair_t
;
67 pair_t reference_result
68 = std::equal_range(reference
.begin(), reference
.end(), 5);
70 pair_t test_result
= boost::equal_range(test
, 5);
72 check_result(reference
, reference_result
, test
, test_result
);
74 pair_t test_result2
= boost::equal_range(boost::make_iterator_range(test
), 5);
76 check_result(reference
, reference_result
, test
, test_result2
);
79 template<class Container
, class BinaryPredicate
>
80 void sort_container(Container
& cont
, BinaryPredicate pred
)
82 typedef BOOST_DEDUCED_TYPENAME
Container::value_type value_t
;
84 std::vector
<value_t
> temp(cont
.begin(), cont
.end());
85 std::sort(temp
.begin(), temp
.end(), pred
);
86 cont
.assign(temp
.begin(), temp
.end());
89 template<class Container
, class BinaryPredicate
>
90 void test_pred(Container
& cont
, BinaryPredicate pred
)
92 typedef BOOST_DEDUCED_TYPENAME
boost::remove_const
<Container
>::type container_t
;
94 container_t
reference_temp(cont
);
95 container_t
test_temp(cont
);
97 sort_container(reference_temp
, pred
);
98 sort_container(test_temp
, pred
);
100 Container
reference(reference_temp
);
101 Container
test(test_temp
);
103 typedef BOOST_DEDUCED_TYPENAME range_iterator
<Container
>::type iterator_t
;
104 typedef std::pair
<iterator_t
, iterator_t
> pair_t
;
106 pair_t reference_result
107 = std::equal_range(reference
.begin(), reference
.end(), 5,
110 pair_t test_result
= boost::equal_range(test
, 5, BinaryPredicate());
112 check_result(reference
, reference_result
, test
, test_result
);
114 pair_t test_result2
= boost::equal_range(boost::make_iterator_range(test
), 5, BinaryPredicate());
116 check_result(reference
, reference_result
, test
, test_result2
);
119 template<class Container
>
120 void test_driver(const Container
& cont
)
122 Container
mutable_cont(cont
);
128 template<class Container
, class BinaryPredicate
>
129 void test_pred_driver(const Container
& cont
, BinaryPredicate pred
)
131 Container
mutable_cont(cont
);
132 test_pred(mutable_cont
, pred
);
134 test_pred(cont
, pred
);
137 template<class Container
>
138 void test_equal_range_impl()
140 using namespace boost::assign
;
145 test_pred_driver(cont
, std::less
<int>());
146 test_pred_driver(cont
, std::greater
<int>());
151 test_pred_driver(cont
, std::less
<int>());
152 test_pred_driver(cont
, std::greater
<int>());
155 cont
+= 1,2,3,4,5,6,7,8,9;
157 test_pred_driver(cont
, std::less
<int>());
158 test_pred_driver(cont
, std::greater
<int>());
161 void test_equal_range()
163 test_equal_range_impl
< std::vector
<int> >();
164 test_equal_range_impl
< std::list
<int> >();
165 test_equal_range_impl
< std::deque
<int> >();
171 boost::unit_test::test_suite
*
172 init_unit_test_suite(int argc
, char* argv
[])
174 boost::unit_test::test_suite
* test
175 = BOOST_TEST_SUITE( "RangeTestSuite.algorithm.equal_range" );
177 test
->add( BOOST_TEST_CASE( &boost::test_equal_range
) );