]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/multi_index/test/test_copy_assignment.cpp
1 /* Boost.MultiIndex test for copying and assignment.
3 * Copyright 2003-2018 Joaquin M Lopez Munoz.
4 * Distributed under the Boost Software License, Version 1.0.
5 * (See accompanying file LICENSE_1_0.txt or copy at
6 * http://www.boost.org/LICENSE_1_0.txt)
8 * See http://www.boost.org/libs/multi_index for library home page.
11 #include "test_copy_assignment.hpp"
13 #include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
15 #include <boost/move/utility_core.hpp>
19 #include "pre_multi_index.hpp"
20 #include "employee.hpp"
21 #include "small_allocator.hpp"
22 #include <boost/detail/lightweight_test.hpp>
24 using namespace boost::multi_index
;
26 #if BOOST_WORKAROUND(__MWERKS__,<=0x3003)
27 /* The "ISO C++ Template Parser" option makes CW8.3 incorrectly fail at
28 * expressions of the form sizeof(x) where x is an array local to a
32 #pragma parse_func_templ off
35 typedef multi_index_container
<int> copyable_and_movable
;
39 copyable_and_movable c
;
42 template<typename Sequence
>
43 static void test_assign()
47 int a
[]={0,1,2,3,4,5};
48 std::size_t sa
=sizeof(a
)/sizeof(a
[0]);
50 s
.assign(&a
[0],&a
[sa
]);
51 BOOST_TEST(s
.size()==sa
&&std::equal(s
.begin(),s
.end(),&a
[0]));
53 s
.assign((const int*)(&a
[0]),(const int*)(&a
[sa
]));
54 BOOST_TEST(s
.size()==sa
&&std::equal(s
.begin(),s
.end(),&a
[0]));
56 #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
57 s
.assign({0,1,2,3,4,5});
59 s
.assign(&a
[0],&a
[sa
]);
62 BOOST_TEST(s
.size()==sa
&&std::equal(s
.begin(),s
.end(),&a
[0]));
64 s
.assign((std::size_t)18,37);
65 BOOST_TEST(s
.size()==18&&std::accumulate(s
.begin(),s
.end(),0)==666);
67 s
.assign((std::size_t)12,167);
68 BOOST_TEST(s
.size()==12&&std::accumulate(s
.begin(),s
.end(),0)==2004);
71 #if BOOST_WORKAROUND(__MWERKS__,<=0x3003)
72 #pragma parse_func_templ reset
75 template<typename Sequence
>
76 static void test_integral_assign()
78 /* Special cases described in 23.1.1/9: integral types must not
79 * be taken as iterators in assign(f,l) and insert(p,f,l).
85 BOOST_TEST(s
.size()==5&&std::accumulate(s
.begin(),s
.end(),0)==50);
87 BOOST_TEST(s
.size()==2&&std::accumulate(s
.begin(),s
.end(),0)==10);
90 s
.insert(s
.begin(),5,10);
91 BOOST_TEST(s
.size()==5&&std::accumulate(s
.begin(),s
.end(),0)==50);
92 s
.insert(s
.begin(),2u,5u);
93 BOOST_TEST(s
.size()==7&&std::accumulate(s
.begin(),s
.end(),0)==60);
96 employee_set
produce_employee_set()
99 es
.insert(employee(0,"Petr",60,2837));
100 es
.insert(employee(1,"Jiri",25,2143));
101 es
.insert(employee(2,"Radka",40,4875));
102 es
.insert(employee(3,"Milan",38,3474));
106 void test_copy_assignment()
109 employee_set
es2(es
);
111 employee_set::allocator_type al
=es
.get_allocator();
112 al
=get
<1>(es
).get_allocator();
113 al
=get
<2>(es
).get_allocator();
114 al
=get
<3>(es
).get_allocator();
115 al
=get
<4>(es
).get_allocator();
116 al
=get
<5>(es
).get_allocator();
118 BOOST_TEST(es2
.empty());
120 es2
.insert(employee(0,"Joe",31,1123));
121 es2
.insert(employee(1,"Robert",27,5601));
122 es2
.insert(employee(2,"John",40,7889));
123 es2
.insert(employee(2,"Aristotle",2388,3357)); /* clash */
124 es2
.insert(employee(3,"Albert",20,9012));
125 es2
.insert(employee(4,"John",57,1002));
126 es2
.insert(employee(0,"Andrew",60,2302)); /* clash */
128 employee_set
es3(es2
);
130 BOOST_TEST(es2
==es3
);
131 BOOST_TEST(get
<2>(es2
)==get
<2>(es3
));
132 BOOST_TEST(get
<3>(es2
)==get
<3>(es3
));
133 BOOST_TEST(get
<5>(es2
)==get
<5>(es3
));
135 employee_set es4
=employee_set(non_std_allocator
<employee
>());
136 employee_set_by_name
& i1
=get
<name
>(es4
);
139 BOOST_TEST(es4
==es2
);
141 employee_set es5
=employee_set(employee_set::ctor_args_list());
142 employee_set_by_age
& i2
=get
<age
>(es5
);
145 BOOST_TEST(i2
==get
<2>(es2
));
148 employee_set_as_inserted
& i3
=get
<as_inserted
>(es6
);
151 BOOST_TEST(i3
==get
<3>(es2
));
154 employee_set_randomly
& i5
=get
<randomly
>(es7
);
157 BOOST_TEST(i5
==get
<5>(es2
));
159 #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)&&\
160 !BOOST_WORKAROUND(BOOST_MSVC,==1800) /* MSVC 12.0 chokes on what follows */
161 employee_set
es8({{0,"Rose",40,4512},{1,"Mary",38,3345},{2,"Jo",25,7102}});
163 es9
={{0,"Rose",40,4512},{1,"Mary",38,3345},{2,"Jo",25,7102},
166 BOOST_TEST(es8
.size()==3);
167 BOOST_TEST(es9
==es8
);
170 get
<0>(es9
)={{0,"Rose",40,4512},{1,"Mary",38,3345},{2,"Jo",25,7102},
172 BOOST_TEST(es9
==es8
);
175 get
<0>(es9
)={{0,"Rose",40,4512},{2,"Jo",25,7102},{1,"Mary",38,3345}};
176 BOOST_TEST(es9
==es8
);
179 get
<1>(es9
)={{1,"Mary",38,3345},{0,"Rose",40,4512},{2,"Jo",25,7102},
181 BOOST_TEST(es9
==es8
);
184 get
<2>(es9
)={{2,"Jo",25,7102},{0,"Rose",40,4512},{1,"Mary",38,3345}};
185 BOOST_TEST(es9
==es8
);
188 get
<3>(es9
)={{0,"Rose",40,4512},{1,"Mary",38,3345},{1,"Mary",38,3345},
190 BOOST_TEST(es9
==es8
);
193 get
<4>(es9
)={{1,"Mary",38,3345},{2,"Jo",25,7102},{0,"Rose",40,4512}};
194 BOOST_TEST(es9
==es8
);
197 get
<5>(es9
)={{1,"Mary",38,3345},{2,"Jo",25,7102},{0,"Rose",40,4512},
199 BOOST_TEST(es9
==es8
);
202 employee_set
es10(produce_employee_set()),es11(produce_employee_set());
203 BOOST_TEST(es10
==es11
);
205 employee_set
es12(boost::move(es10
));
206 BOOST_TEST(es10
.empty());
207 BOOST_TEST(es11
==es12
);
209 es10
=boost::move(es12
);
210 BOOST_TEST(es12
.empty());
211 BOOST_TEST(es11
==es10
);
213 std::list
<employee
> l
;
214 l
.push_back(employee(3,"Anna",31,5388));
215 l
.push_back(employee(1,"Rachel",27,9012));
216 l
.push_back(employee(2,"Agatha",40,1520));
218 employee_set
es13(l
.begin(),l
.end());
222 BOOST_TEST(es13
.size()==l
.size()&&
223 std::equal(es13
.begin(),es13
.end(),l
.begin()));
225 test_assign
<multi_index_container
<int,indexed_by
<sequenced
<> > > >();
226 test_integral_assign
<
227 multi_index_container
<int,indexed_by
<sequenced
<> > > >();
229 test_assign
<multi_index_container
<int,indexed_by
<random_access
<> > > >();
230 test_integral_assign
<
231 multi_index_container
<int,indexed_by
<random_access
<> > > >();
233 /* Testcase for problem described at http://www.boost.org/doc/html/move/
234 * emulation_limitations.html#move.emulation_limitations.assignment_operator
237 holder
h((holder()));
240 #if !defined(BOOST_NO_CXX11_UNIFIED_INITIALIZATION_SYNTAX)
242 /* testcase for https://svn.boost.org/trac10/ticket/13518 */
244 multi_index_container
<int> x
={};
248 typedef small_allocator
<int> small_alloc
;
249 typedef multi_index_container
<
252 ordered_unique
<identity
<int> >,
253 ranked_unique
<identity
<int> >,
254 hashed_unique
<identity
<int> >,
260 typedef small_container::nth_index
<0>::type small_container_0
;
261 typedef small_container::nth_index
<1>::type small_container_1
;
262 typedef small_container::nth_index
<2>::type small_container_2
;
263 typedef small_container::nth_index
<3>::type small_container_3
;
264 typedef small_container::nth_index
<4>::type small_container_4
;
266 BOOST_STATIC_ASSERT((
267 boost::is_same
<small_container::size_type
,small_alloc::size_type
>::value
));
268 BOOST_STATIC_ASSERT((
270 small_container::difference_type
,small_alloc::difference_type
>::value
));
271 BOOST_STATIC_ASSERT((
272 boost::is_same
<small_container_0::size_type
,small_alloc::size_type
>::value
));
273 BOOST_STATIC_ASSERT((
275 small_container_0::difference_type
,small_alloc::difference_type
>::value
));
276 BOOST_STATIC_ASSERT((
277 boost::is_same
<small_container_1::size_type
,small_alloc::size_type
>::value
));
278 BOOST_STATIC_ASSERT((
280 small_container_1::difference_type
,small_alloc::difference_type
>::value
));
281 BOOST_STATIC_ASSERT((
282 boost::is_same
<small_container_2::size_type
,small_alloc::size_type
>::value
));
283 BOOST_STATIC_ASSERT((
285 small_container_2::difference_type
,small_alloc::difference_type
>::value
));
286 BOOST_STATIC_ASSERT((
287 boost::is_same
<small_container_3::size_type
,small_alloc::size_type
>::value
));
288 BOOST_STATIC_ASSERT((
290 small_container_3::difference_type
,small_alloc::difference_type
>::value
));
291 BOOST_STATIC_ASSERT((
292 boost::is_same
<small_container_4::size_type
,small_alloc::size_type
>::value
));
293 BOOST_STATIC_ASSERT((
295 small_container_4::difference_type
,small_alloc::difference_type
>::value
));
298 small_container_0
& sc0
=sc
.get
<0>();
299 small_container_1
& sc1
=sc
.get
<1>();
300 small_container_2
& sc2
=sc
.get
<2>();
301 small_container_3
& sc3
=sc
.get
<3>();
302 small_container_4
& sc4
=sc
.get
<4>();
303 small_alloc::size_type s
,
304 ms
=(small_alloc::size_type
)(-1);
309 BOOST_TEST(sc0
.max_size()<=ms
);
312 s
=sc0
.count(0,std::less
<int>());
315 BOOST_TEST(sc1
.max_size()<=ms
);
318 s
=sc1
.count(0,std::less
<int>());
320 s
=sc1
.rank(sc1
.begin());
322 s
=sc1
.find_rank(0,std::less
<int>());
323 s
=sc1
.lower_bound_rank(0);
324 s
=sc1
.lower_bound_rank(0,std::less
<int>());
325 s
=sc1
.upper_bound_rank(0);
326 s
=sc1
.upper_bound_rank(0,std::less
<int>());
327 s
=sc1
.equal_range_rank(0).first
;
328 s
=sc1
.equal_range_rank(0).second
;
329 s
=sc1
.range_rank(unbounded
,unbounded
).first
;
330 s
=sc1
.range_rank(unbounded
,unbounded
).second
;
333 BOOST_TEST(sc2
.max_size()<=ms
);
336 s
=sc2
.count(0,boost::hash
<int>(),std::equal_to
<int>());
337 s
=sc2
.bucket_count();
338 BOOST_TEST(sc2
.max_bucket_count()<=ms
);
339 BOOST_TEST(sc2
.bucket_size((small_alloc::size_type
)(0))<=ms
);
340 BOOST_TEST(sc2
.bucket_size(0)<=ms
);
349 BOOST_TEST(sc3
.max_size()<=ms
);
352 sc3
.assign((small_alloc::size_type
)(1),0);
353 sc3
.insert(sc3
.begin(),(small_alloc::size_type
)(0),0);
356 BOOST_TEST(sc4
.max_size()<=ms
);
357 BOOST_TEST(sc4
.capacity()<=ms
);
361 sc4
.assign((small_alloc::size_type
)(1),0);
364 sc4
.insert(sc4
.begin(),(small_alloc::size_type
)(0),0);