]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/intrusive/test/generic_multiset_test.hpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / boost / libs / intrusive / test / generic_multiset_test.hpp
1 /////////////////////////////////////////////////////////////////////////////
2 //
3 // (C) Copyright Olaf Krzikalla 2004-2006.
4 // (C) Copyright Ion Gaztanaga 2006-2013.
5 //
6 // Distributed under the Boost Software License, Version 1.0.
7 // (See accompanying file LICENSE_1_0.txt or copy at
8 // http://www.boost.org/LICENSE_1_0.txt)
9 //
10 // See http://www.boost.org/libs/intrusive for documentation.
11 //
12 /////////////////////////////////////////////////////////////////////////////
13 #include <boost/intrusive/detail/config_begin.hpp>
14
15 #include <boost/container/vector.hpp>
16 #include "common_functors.hpp"
17 #include <boost/core/lightweight_test.hpp>
18 #include <boost/intrusive/options.hpp>
19 #include <boost/intrusive/detail/iterator.hpp>
20 #include "test_macros.hpp"
21 #include "test_container.hpp"
22 #include "generic_assoc_test.hpp"
23 #include <typeinfo>
24
25 namespace boost{
26 namespace intrusive{
27 namespace test{
28
29 template<class ContainerDefiner>
30 struct test_generic_multiset
31 {
32 typedef typename ContainerDefiner::value_cont_type value_cont_type;
33
34 static void test_all();
35 static void test_sort(value_cont_type&);
36 static void test_insert(value_cont_type&);
37 static void test_swap(value_cont_type&);
38 static void test_merge(value_cont_type&);
39 static void test_find(value_cont_type&);
40 static void test_impl();
41 };
42
43 template<class ContainerDefiner>
44 void test_generic_multiset<ContainerDefiner>::test_all ()
45 {
46 static const int random_init[6] = { 3, 2, 4, 1, 5, 2 };
47 value_cont_type values (6);
48 for (std::size_t i = 0u; i < 6u; ++i)
49 (&values[i])->value_ = random_init[i];
50 typedef typename ContainerDefiner::template container
51 <>::type multiset_type;
52 {
53 multiset_type testset(values.begin(), values.end());
54 test::test_container(testset);
55 testset.clear();
56 testset.insert(values.begin(), values.end());
57 test::test_common_unordered_and_associative_container(testset, values);
58 testset.clear();
59 testset.insert(values.begin(), values.end());
60 test::test_associative_container(testset, values);
61 testset.clear();
62 testset.insert(values.begin(), values.end());
63 test::test_non_unique_container(testset, values);
64 }
65 test_sort(values);
66 test_insert(values);
67 test_swap(values);
68 test_merge(values);
69 test_find(values);
70 test_impl();
71 test_generic_assoc<ContainerDefiner>::test_all(values);
72 }
73
74 //test case due to an error in tree implementation:
75 template<class ContainerDefiner>
76 void test_generic_multiset<ContainerDefiner>::test_impl()
77 {
78 value_cont_type values (5);
79 for (std::size_t i = 0u; i < 5u; ++i)
80 (&values[i])->value_ = (int)i;
81 typedef typename ContainerDefiner::template container
82 <>::type multiset_type;
83
84 multiset_type testset;
85 for (std::size_t i = 0; i < 5u; ++i)
86 testset.insert (values[i]);
87
88 testset.erase (testset.iterator_to (values[0]));
89 testset.erase (testset.iterator_to (values[1]));
90 testset.insert (values[1]);
91
92 testset.erase (testset.iterator_to (values[2]));
93 testset.erase (testset.iterator_to (values[3]));
94 }
95
96 //test: constructor, iterator, clear, reverse_iterator, front, back, size:
97 template<class ContainerDefiner>
98 void test_generic_multiset<ContainerDefiner>::test_sort(value_cont_type& values)
99 {
100 typedef typename ContainerDefiner::template container
101 <>::type multiset_type;
102
103 multiset_type testset1 (values.begin(), values.end());
104 { int init_values [] = { 1, 2, 2, 3, 4, 5 };
105 TEST_INTRUSIVE_SEQUENCE( init_values, testset1.begin() ); }
106
107 testset1.clear();
108 BOOST_TEST (testset1.empty());
109
110 typedef typename ContainerDefiner::template container
111 <compare<even_odd>
112 >::type multiset_type2;
113
114 multiset_type2 testset2 (values.begin(), values.begin() + 6);
115 { int init_values [] = { 5, 3, 1, 4, 2, 2 };
116 TEST_INTRUSIVE_SEQUENCE( init_values, testset2.rbegin() ); }
117
118 BOOST_TEST (testset2.begin()->value_ == 2);
119 BOOST_TEST (testset2.rbegin()->value_ == 5);
120 }
121
122 //test: insert, const_iterator, const_reverse_iterator, erase, iterator_to:
123 template<class ContainerDefiner>
124 void test_generic_multiset<ContainerDefiner>::test_insert(value_cont_type& values)
125 {
126 typedef typename ContainerDefiner::template container
127 <>::type multiset_type;
128
129 multiset_type testset;
130 testset.insert(values.begin() + 2, values.begin() + 5);
131 testset.check();
132 { int init_values [] = { 1, 4, 5 };
133 TEST_INTRUSIVE_SEQUENCE( init_values, testset.begin() ); }
134
135 typename multiset_type::iterator i = testset.begin();
136 BOOST_TEST (i->value_ == 1);
137
138 i = testset.insert (i, values[0]);
139 testset.check();
140 BOOST_TEST (&*i == &values[0]);
141
142 { int init_values [] = { 5, 4, 3, 1 };
143 TEST_INTRUSIVE_SEQUENCE( init_values, testset.rbegin() ); }
144
145 i = testset.iterator_to (values[2]);
146 BOOST_TEST (&*i == &values[2]);
147
148 i = multiset_type::s_iterator_to (values[2]);
149 BOOST_TEST (&*i == &values[2]);
150
151 testset.erase(i);
152 testset.check();
153
154 { int init_values [] = { 1, 3, 5 };
155 TEST_INTRUSIVE_SEQUENCE( init_values, testset.begin() ); }
156 }
157
158 //test: insert (seq-version), swap, erase (seq-version), size:
159 template<class ContainerDefiner>
160 void test_generic_multiset<ContainerDefiner>::test_swap(value_cont_type& values)
161 {
162 typedef typename ContainerDefiner::template container
163 <>::type multiset_type;
164 multiset_type testset1 (values.begin(), values.begin() + 2);
165 multiset_type testset2;
166 testset2.insert (values.begin() + 2, values.begin() + 6);
167 testset1.swap (testset2);
168
169 { int init_values [] = { 1, 2, 4, 5 };
170 TEST_INTRUSIVE_SEQUENCE( init_values, testset1.begin() ); }
171 { int init_values [] = { 2, 3 };
172 TEST_INTRUSIVE_SEQUENCE( init_values, testset2.begin() ); }
173
174 testset1.erase (testset1.iterator_to(values[5]), testset1.end());
175 BOOST_TEST (testset1.size() == 1);
176 BOOST_TEST (&*testset1.begin() == &values[3]);
177 }
178
179 template<class ContainerDefiner>
180 void test_generic_multiset<ContainerDefiner>::test_merge(value_cont_type& values)
181 {
182 typedef typename ContainerDefiner::template container
183 <>::type multiset_type;
184 typedef typename multiset_type::key_of_value key_of_value;
185 typedef typename multiset_type::key_type key_type;
186
187 typedef typename ContainerDefiner::template container
188 < compare< std::greater<key_type> > >::type multiset_greater_type;
189
190 //original vector: 3, 2, 4, 1, 5, 2
191 //2,3
192 multiset_type testset1 (values.begin(), values.begin() + 2);
193 //5, 4, 2, 1
194 multiset_greater_type testset2;
195 testset2.insert (values.begin() + 2, values.begin() + 6);
196
197 testset2.merge(testset1);
198 testset1.check();
199 testset2.check();
200
201 BOOST_TEST (testset1.empty());
202 BOOST_TEST (testset2.size() == 6);
203 { int init_values [] = { 5, 4, 3, 2, 2, 1 };
204 TEST_INTRUSIVE_SEQUENCE( init_values, testset2.begin() ); }
205
206 value_cont_type cmp_val_cont(1);
207 typename value_cont_type::reference cmp_val = cmp_val_cont.front();
208 (&cmp_val)->value_ = 2;
209
210 BOOST_TEST (*testset2.find(key_of_value()(cmp_val)) == values[5]);
211 BOOST_TEST (*testset2.find(2, any_greater()) == values[5]);
212 BOOST_TEST (&*(++testset2.find(key_of_value()(cmp_val))) == &values[1]);
213
214 testset1.merge(testset2);
215 testset1.check();
216 testset2.check();
217
218 BOOST_TEST (testset1.size() == 6);
219 { int init_values [] = { 1, 2, 2, 3, 4, 5 };
220 TEST_INTRUSIVE_SEQUENCE( init_values, testset1.begin() ); }
221 BOOST_TEST (*testset1.find(key_of_value()(cmp_val)) == values[5]);
222 BOOST_TEST (*testset1.find(2, any_less()) == values[5]);
223 BOOST_TEST (&*(++testset1.find(key_of_value()(cmp_val))) == &values[1]);
224 BOOST_TEST (testset2.empty());
225 }
226
227 //test: find, equal_range (lower_bound, upper_bound):
228 template<class ContainerDefiner>
229 void test_generic_multiset<ContainerDefiner>::test_find(value_cont_type& values)
230 {
231 typedef typename ContainerDefiner::template container
232 <>::type multiset_type;
233 typedef typename multiset_type::key_of_value key_of_value;
234 multiset_type testset (values.begin(), values.end());
235 typedef typename multiset_type::iterator iterator;
236
237 {
238 value_cont_type cmp_val_cont(1);
239 typename value_cont_type::reference cmp_val = cmp_val_cont.front();
240 (&cmp_val)->value_ = 2;
241 iterator i = testset.find (key_of_value()(cmp_val));
242 BOOST_TEST (i == testset.find (2, any_less()));
243 BOOST_TEST (i->value_ == 2);
244 BOOST_TEST ((++i)->value_ == 2);
245 std::pair<iterator,iterator> range = testset.equal_range (key_of_value()(cmp_val));
246 BOOST_TEST(range == testset.equal_range (2, any_less()));
247
248 BOOST_TEST (range.first->value_ == 2);
249 BOOST_TEST (range.second->value_ == 3);
250 BOOST_TEST (boost::intrusive::iterator_distance (range.first, range.second) == 2);
251
252 (&cmp_val)->value_ = 7;
253 BOOST_TEST (testset.find(key_of_value()(cmp_val)) == testset.end());
254 BOOST_TEST (testset.find (7, any_less()) == testset.end());
255 }
256 { //1, 2, 2, 3, 4, 5
257
258 std::pair<iterator,iterator> range;
259 value_cont_type cmp_val_cont(2);
260 typename value_cont_type::reference cmp_val_lower = cmp_val_cont.front();
261 typename value_cont_type::reference cmp_val_upper = cmp_val_cont.back();
262 {
263 (&cmp_val_lower)->value_ = 1;
264 (&cmp_val_upper)->value_ = 2;
265 //left-closed, right-closed
266 range = testset.bounded_range (key_of_value()(cmp_val_lower), key_of_value()(cmp_val_upper), true, true);
267 BOOST_TEST (range == testset.bounded_range (1, 2, any_less(), true, true));
268 BOOST_TEST (range.first->value_ == 1);
269 BOOST_TEST (range.second->value_ == 3);
270 BOOST_TEST (boost::intrusive::iterator_distance (range.first, range.second) == 3);
271 }
272 const multiset_type &const_testset = testset;
273 typedef typename multiset_type::const_iterator const_iterator;
274 std::pair<const_iterator, const_iterator> const_range;
275 {
276 (&cmp_val_lower)->value_ = 1;
277 (&cmp_val_upper)->value_ = 2;
278 const_range = const_testset.bounded_range (key_of_value()(cmp_val_lower), key_of_value()(cmp_val_upper), true, false);
279 BOOST_TEST (const_range == const_testset.bounded_range (1, 2, any_less(), true, false));
280 BOOST_TEST (const_range.first->value_ == 1);
281 BOOST_TEST (const_range.second->value_ == 2);
282 BOOST_TEST (boost::intrusive::iterator_distance (const_range.first, const_range.second) == 1);
283
284 (&cmp_val_lower)->value_ = 1;
285 (&cmp_val_upper)->value_ = 3;
286 range = testset.bounded_range (key_of_value()(cmp_val_lower), key_of_value()(cmp_val_upper), true, false);
287 BOOST_TEST (range == testset.bounded_range (1, 3, any_less(), true, false));
288 BOOST_TEST (range.first->value_ == 1);
289 BOOST_TEST (range.second->value_ == 3);
290 BOOST_TEST (boost::intrusive::iterator_distance (range.first, range.second) == 3);
291 }
292 {
293 (&cmp_val_lower)->value_ = 1;
294 (&cmp_val_upper)->value_ = 2;
295 const_range = const_testset.bounded_range (key_of_value()(cmp_val_lower), key_of_value()(cmp_val_upper), false, true);
296 BOOST_TEST (const_range == const_testset.bounded_range (1, 2, any_less(), false, true));
297 BOOST_TEST (const_range.first->value_ == 2);
298 BOOST_TEST (const_range.second->value_ == 3);
299 BOOST_TEST (boost::intrusive::iterator_distance (const_range.first, const_range.second) == 2);
300 }
301 {
302 (&cmp_val_lower)->value_ = 1;
303 (&cmp_val_upper)->value_ = 2;
304 range = testset.bounded_range (key_of_value()(cmp_val_lower), key_of_value()(cmp_val_upper), false, false);
305 BOOST_TEST (range == testset.bounded_range (1, 2, any_less(), false, false));
306 BOOST_TEST (range.first->value_ == 2);
307 BOOST_TEST (range.second->value_ == 2);
308 BOOST_TEST (boost::intrusive::iterator_distance (range.first, range.second) == 0);
309 }
310 {
311 (&cmp_val_lower)->value_ = 5;
312 (&cmp_val_upper)->value_ = 6;
313 const_range = const_testset.bounded_range (key_of_value()(cmp_val_lower), key_of_value()(cmp_val_upper), true, false);
314 BOOST_TEST (const_range == const_testset.bounded_range (5, 6, any_less(), true, false));
315 BOOST_TEST (const_range.first->value_ == 5);
316 BOOST_TEST (const_range.second == const_testset.end());
317 BOOST_TEST (boost::intrusive::iterator_distance (const_range.first, const_range.second) == 1);
318 }
319 }
320 }
321
322 }}} //namespace boost::intrusive::test
323
324 #include <boost/intrusive/detail/config_end.hpp>