1 /* Boost.MultiIndex test for copying and assignment.
3 * Copyright 2003-2013 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.hpp>
19 #include "pre_multi_index.hpp"
20 #include "employee.hpp"
21 #include <boost/detail/lightweight_test.hpp>
23 using namespace boost::multi_index;
25 #if BOOST_WORKAROUND(__MWERKS__,<=0x3003)
26 /* The "ISO C++ Template Parser" option makes CW8.3 incorrectly fail at
27 * expressions of the form sizeof(x) where x is an array local to a
31 #pragma parse_func_templ off
34 typedef multi_index_container<int> copyable_and_movable;
38 copyable_and_movable c;
41 template<typename Sequence>
42 static void test_assign()
46 int a[]={0,1,2,3,4,5};
47 std::size_t sa=sizeof(a)/sizeof(a[0]);
49 s.assign(&a[0],&a[sa]);
50 BOOST_TEST(s.size()==sa&&std::equal(s.begin(),s.end(),&a[0]));
52 s.assign((const int*)(&a[0]),(const int*)(&a[sa]));
53 BOOST_TEST(s.size()==sa&&std::equal(s.begin(),s.end(),&a[0]));
55 #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
56 s.assign({0,1,2,3,4,5});
58 s.assign(&a[0],&a[sa]);
61 BOOST_TEST(s.size()==sa&&std::equal(s.begin(),s.end(),&a[0]));
63 s.assign((std::size_t)18,37);
64 BOOST_TEST(s.size()==18&&std::accumulate(s.begin(),s.end(),0)==666);
66 s.assign((std::size_t)12,167);
67 BOOST_TEST(s.size()==12&&std::accumulate(s.begin(),s.end(),0)==2004);
70 #if BOOST_WORKAROUND(__MWERKS__,<=0x3003)
71 #pragma parse_func_templ reset
74 template<typename Sequence>
75 static void test_integral_assign()
77 /* Special cases described in 23.1.1/9: integral types must not
78 * be taken as iterators in assign(f,l) and insert(p,f,l).
84 BOOST_TEST(s.size()==5&&std::accumulate(s.begin(),s.end(),0)==50);
86 BOOST_TEST(s.size()==2&&std::accumulate(s.begin(),s.end(),0)==10);
89 s.insert(s.begin(),5,10);
90 BOOST_TEST(s.size()==5&&std::accumulate(s.begin(),s.end(),0)==50);
91 s.insert(s.begin(),2u,5u);
92 BOOST_TEST(s.size()==7&&std::accumulate(s.begin(),s.end(),0)==60);
95 employee_set produce_employee_set()
98 es.insert(employee(0,"Petr",60,2837));
99 es.insert(employee(1,"Jiri",25,2143));
100 es.insert(employee(2,"Radka",40,4875));
101 es.insert(employee(3,"Milan",38,3474));
105 void test_copy_assignment()
108 employee_set es2(es);
110 employee_set::allocator_type al=es.get_allocator();
111 al=get<1>(es).get_allocator();
112 al=get<2>(es).get_allocator();
113 al=get<3>(es).get_allocator();
114 al=get<4>(es).get_allocator();
115 al=get<5>(es).get_allocator();
117 BOOST_TEST(es2.empty());
119 es2.insert(employee(0,"Joe",31,1123));
120 es2.insert(employee(1,"Robert",27,5601));
121 es2.insert(employee(2,"John",40,7889));
122 es2.insert(employee(2,"Aristotle",2388,3357)); /* clash */
123 es2.insert(employee(3,"Albert",20,9012));
124 es2.insert(employee(4,"John",57,1002));
125 es2.insert(employee(0,"Andrew",60,2302)); /* clash */
127 employee_set es3(es2);
129 BOOST_TEST(es2==es3);
130 BOOST_TEST(get<2>(es2)==get<2>(es3));
131 BOOST_TEST(get<3>(es2)==get<3>(es3));
132 BOOST_TEST(get<5>(es2)==get<5>(es3));
134 employee_set es4=employee_set(non_std_allocator<employee>());
135 employee_set_by_name& i1=get<name>(es4);
138 BOOST_TEST(es4==es2);
140 employee_set es5=employee_set(employee_set::ctor_args_list());
141 employee_set_by_age& i2=get<age>(es5);
144 BOOST_TEST(i2==get<2>(es2));
147 employee_set_as_inserted& i3=get<as_inserted>(es6);
150 BOOST_TEST(i3==get<3>(es2));
153 employee_set_randomly& i5=get<randomly>(es7);
156 BOOST_TEST(i5==get<5>(es2));
158 #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
159 employee_set es8({{0,"Rose",40,4512},{1,"Mary",38,3345},{2,"Jo",25,7102}});
161 es9={{0,"Rose",40,4512},{1,"Mary",38,3345},{2,"Jo",25,7102},
164 BOOST_TEST(es8.size()==3);
165 BOOST_TEST(es9==es8);
168 get<0>(es9)={{0,"Rose",40,4512},{1,"Mary",38,3345},{2,"Jo",25,7102},
170 BOOST_TEST(es9==es8);
173 get<0>(es9)={{0,"Rose",40,4512},{2,"Jo",25,7102},{1,"Mary",38,3345}};
174 BOOST_TEST(es9==es8);
177 get<1>(es9)={{1,"Mary",38,3345},{0,"Rose",40,4512},{2,"Jo",25,7102},
179 BOOST_TEST(es9==es8);
182 get<2>(es9)={{2,"Jo",25,7102},{0,"Rose",40,4512},{1,"Mary",38,3345}};
183 BOOST_TEST(es9==es8);
186 get<3>(es9)={{0,"Rose",40,4512},{1,"Mary",38,3345},{1,"Mary",38,3345},
188 BOOST_TEST(es9==es8);
191 get<4>(es9)={{1,"Mary",38,3345},{2,"Jo",25,7102},{0,"Rose",40,4512}};
192 BOOST_TEST(es9==es8);
195 get<5>(es9)={{1,"Mary",38,3345},{2,"Jo",25,7102},{0,"Rose",40,4512},
197 BOOST_TEST(es9==es8);
200 employee_set es10(produce_employee_set()),es11(produce_employee_set());
201 BOOST_TEST(es10==es11);
203 employee_set es12(boost::move(es10));
204 BOOST_TEST(es10.empty());
205 BOOST_TEST(es11==es12);
207 es10=boost::move(es12);
208 BOOST_TEST(es12.empty());
209 BOOST_TEST(es11==es10);
211 std::list<employee> l;
212 l.push_back(employee(3,"Anna",31,5388));
213 l.push_back(employee(1,"Rachel",27,9012));
214 l.push_back(employee(2,"Agatha",40,1520));
216 employee_set es13(l.begin(),l.end());
220 BOOST_TEST(es13.size()==l.size()&&
221 std::equal(es13.begin(),es13.end(),l.begin()));
223 test_assign<multi_index_container<int,indexed_by<sequenced<> > > >();
224 test_integral_assign<
225 multi_index_container<int,indexed_by<sequenced<> > > >();
227 test_assign<multi_index_container<int,indexed_by<random_access<> > > >();
228 test_integral_assign<
229 multi_index_container<int,indexed_by<random_access<> > > >();
231 /* Testcase for problem described at http://www.boost.org/doc/html/move/
232 * emulation_limitations.html#move.emulation_limitations.assignment_operator
235 holder h((holder()));