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>
15 #include <boost/bind.hpp>
27 template<class Container
, class Pair
>
29 const Container
& reference
,
31 const Container
& test
,
35 typedef BOOST_DEDUCED_TYPENAME range_iterator
<const Container
>::type
38 BOOST_CHECK_EQUAL_COLLECTIONS(
39 reference
.begin(), reference
.end(),
40 test
.begin(), test
.end()
44 std::distance
<const_iterator_t
>(reference
.begin(), reference_pair
.first
),
45 std::distance
<const_iterator_t
>(test
.begin(), test_pair
.first
)
49 std::distance
<const_iterator_t
>(reference
.begin(), reference_pair
.second
),
50 std::distance
<const_iterator_t
>(test
.begin(), test_pair
.second
)
53 BOOST_CHECK_EQUAL_COLLECTIONS(
54 reference_pair
.first
, reference_pair
.second
,
55 test_pair
.first
, test_pair
.second
59 template<class Container
>
60 void test(Container
& cont
)
62 Container
reference(cont
);
65 typedef BOOST_DEDUCED_TYPENAME range_iterator
<Container
>::type iterator_t
;
66 typedef std::pair
<iterator_t
, iterator_t
> pair_t
;
68 pair_t reference_result
69 = std::equal_range(reference
.begin(), reference
.end(), 5);
71 pair_t test_result
= boost::equal_range(test
, 5);
73 check_result(reference
, reference_result
, test
, test_result
);
75 pair_t test_result2
= boost::equal_range(boost::make_iterator_range(test
), 5);
77 check_result(reference
, reference_result
, test
, test_result2
);
80 template<class Container
, class BinaryPredicate
>
81 void sort_container(Container
& cont
, BinaryPredicate pred
)
83 typedef BOOST_DEDUCED_TYPENAME
Container::value_type value_t
;
85 std::vector
<value_t
> temp(cont
.begin(), cont
.end());
86 std::sort(temp
.begin(), temp
.end(), pred
);
87 cont
.assign(temp
.begin(), temp
.end());
90 template<class Container
, class BinaryPredicate
>
91 void test_pred(Container
& cont
, BinaryPredicate pred
)
93 typedef BOOST_DEDUCED_TYPENAME
boost::remove_const
<Container
>::type container_t
;
95 container_t
reference_temp(cont
);
96 container_t
test_temp(cont
);
98 sort_container(reference_temp
, pred
);
99 sort_container(test_temp
, pred
);
101 Container
reference(reference_temp
);
102 Container
test(test_temp
);
104 typedef BOOST_DEDUCED_TYPENAME range_iterator
<Container
>::type iterator_t
;
105 typedef std::pair
<iterator_t
, iterator_t
> pair_t
;
107 pair_t reference_result
108 = std::equal_range(reference
.begin(), reference
.end(), 5,
111 pair_t test_result
= boost::equal_range(test
, 5, BinaryPredicate());
113 check_result(reference
, reference_result
, test
, test_result
);
115 pair_t test_result2
= boost::equal_range(boost::make_iterator_range(test
), 5, BinaryPredicate());
117 check_result(reference
, reference_result
, test
, test_result2
);
120 template<class Container
>
121 void test_driver(const Container
& cont
)
123 Container
mutable_cont(cont
);
129 template<class Container
, class BinaryPredicate
>
130 void test_pred_driver(const Container
& cont
, BinaryPredicate pred
)
132 Container
mutable_cont(cont
);
133 test_pred(mutable_cont
, pred
);
135 test_pred(cont
, pred
);
138 template<class Container
>
139 void test_equal_range_impl()
141 using namespace boost::assign
;
146 test_pred_driver(cont
, std::less
<int>());
147 test_pred_driver(cont
, std::greater
<int>());
152 test_pred_driver(cont
, std::less
<int>());
153 test_pred_driver(cont
, std::greater
<int>());
156 cont
+= 1,2,3,4,5,6,7,8,9;
158 test_pred_driver(cont
, std::less
<int>());
159 test_pred_driver(cont
, std::greater
<int>());
162 void test_equal_range()
164 test_equal_range_impl
< std::vector
<int> >();
165 test_equal_range_impl
< std::list
<int> >();
166 test_equal_range_impl
< std::deque
<int> >();
172 boost::unit_test::test_suite
*
173 init_unit_test_suite(int argc
, char* argv
[])
175 boost::unit_test::test_suite
* test
176 = BOOST_TEST_SUITE( "RangeTestSuite.algorithm.equal_range" );
178 test
->add( BOOST_TEST_CASE( &boost::test_equal_range
) );