]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/range/test/algorithm_test/equal_range.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / range / test / algorithm_test / equal_range.cpp
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)
5 //
6 //
7 // For more information, see http://www.boost.org/libs/range/
8 //
9 #include <boost/range/algorithm/equal_range.hpp>
10
11 #include <boost/test/test_tools.hpp>
12 #include <boost/test/unit_test.hpp>
13
14 #include <boost/assign.hpp>
15 #include <boost/bind.hpp>
16 #include <algorithm>
17 #include <functional>
18 #include <list>
19 #include <numeric>
20 #include <deque>
21 #include <vector>
22
23 namespace boost
24 {
25 namespace
26 {
27 template<class Container, class Pair>
28 void check_result(
29 const Container& reference,
30 Pair reference_pair,
31 const Container& test,
32 Pair test_pair
33 )
34 {
35 typedef BOOST_DEDUCED_TYPENAME range_iterator<const Container>::type
36 const_iterator_t;
37
38 BOOST_CHECK_EQUAL_COLLECTIONS(
39 reference.begin(), reference.end(),
40 test.begin(), test.end()
41 );
42
43 BOOST_CHECK_EQUAL(
44 std::distance<const_iterator_t>(reference.begin(), reference_pair.first),
45 std::distance<const_iterator_t>(test.begin(), test_pair.first)
46 );
47
48 BOOST_CHECK_EQUAL(
49 std::distance<const_iterator_t>(reference.begin(), reference_pair.second),
50 std::distance<const_iterator_t>(test.begin(), test_pair.second)
51 );
52
53 BOOST_CHECK_EQUAL_COLLECTIONS(
54 reference_pair.first, reference_pair.second,
55 test_pair.first, test_pair.second
56 );
57 }
58
59 template<class Container>
60 void test(Container& cont)
61 {
62 Container reference(cont);
63 Container test(cont);
64
65 typedef BOOST_DEDUCED_TYPENAME range_iterator<Container>::type iterator_t;
66 typedef std::pair<iterator_t, iterator_t> pair_t;
67
68 pair_t reference_result
69 = std::equal_range(reference.begin(), reference.end(), 5);
70
71 pair_t test_result = boost::equal_range(test, 5);
72
73 check_result(reference, reference_result, test, test_result);
74
75 pair_t test_result2 = boost::equal_range(boost::make_iterator_range(test), 5);
76
77 check_result(reference, reference_result, test, test_result2);
78 }
79
80 template<class Container, class BinaryPredicate>
81 void sort_container(Container& cont, BinaryPredicate pred)
82 {
83 typedef BOOST_DEDUCED_TYPENAME Container::value_type value_t;
84
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());
88 }
89
90 template<class Container, class BinaryPredicate>
91 void test_pred(Container& cont, BinaryPredicate pred)
92 {
93 typedef BOOST_DEDUCED_TYPENAME boost::remove_const<Container>::type container_t;
94
95 container_t reference_temp(cont);
96 container_t test_temp(cont);
97
98 sort_container(reference_temp, pred);
99 sort_container(test_temp, pred);
100
101 Container reference(reference_temp);
102 Container test(test_temp);
103
104 typedef BOOST_DEDUCED_TYPENAME range_iterator<Container>::type iterator_t;
105 typedef std::pair<iterator_t, iterator_t> pair_t;
106
107 pair_t reference_result
108 = std::equal_range(reference.begin(), reference.end(), 5,
109 BinaryPredicate());
110
111 pair_t test_result = boost::equal_range(test, 5, BinaryPredicate());
112
113 check_result(reference, reference_result, test, test_result);
114
115 pair_t test_result2 = boost::equal_range(boost::make_iterator_range(test), 5, BinaryPredicate());
116
117 check_result(reference, reference_result, test, test_result2);
118 }
119
120 template<class Container>
121 void test_driver(const Container& cont)
122 {
123 Container mutable_cont(cont);
124 test(mutable_cont);
125
126 test(cont);
127 }
128
129 template<class Container, class BinaryPredicate>
130 void test_pred_driver(const Container& cont, BinaryPredicate pred)
131 {
132 Container mutable_cont(cont);
133 test_pred(mutable_cont, pred);
134
135 test_pred(cont, pred);
136 }
137
138 template<class Container>
139 void test_equal_range_impl()
140 {
141 using namespace boost::assign;
142
143 Container cont;
144
145 test_driver(cont);
146 test_pred_driver(cont, std::less<int>());
147 test_pred_driver(cont, std::greater<int>());
148
149 cont.clear();
150 cont += 1;
151 test_driver(cont);
152 test_pred_driver(cont, std::less<int>());
153 test_pred_driver(cont, std::greater<int>());
154
155 cont.clear();
156 cont += 1,2,3,4,5,6,7,8,9;
157 test_driver(cont);
158 test_pred_driver(cont, std::less<int>());
159 test_pred_driver(cont, std::greater<int>());
160 }
161
162 void test_equal_range()
163 {
164 test_equal_range_impl< std::vector<int> >();
165 test_equal_range_impl< std::list<int> >();
166 test_equal_range_impl< std::deque<int> >();
167 }
168 }
169 }
170
171
172 boost::unit_test::test_suite*
173 init_unit_test_suite(int argc, char* argv[])
174 {
175 boost::unit_test::test_suite* test
176 = BOOST_TEST_SUITE( "RangeTestSuite.algorithm.equal_range" );
177
178 test->add( BOOST_TEST_CASE( &boost::test_equal_range ) );
179
180 return test;
181 }