]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | /* Boost.MultiIndex test for copying and assignment. |
2 | * | |
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) | |
7 | * | |
8 | * See http://www.boost.org/libs/multi_index for library home page. | |
9 | */ | |
10 | ||
11 | #include "test_copy_assignment.hpp" | |
12 | ||
13 | #include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */ | |
14 | #include <algorithm> | |
15 | #include <boost/move/utility.hpp> | |
16 | #include <list> | |
17 | #include <numeric> | |
18 | #include <vector> | |
19 | #include "pre_multi_index.hpp" | |
20 | #include "employee.hpp" | |
21 | #include <boost/detail/lightweight_test.hpp> | |
22 | ||
23 | using namespace boost::multi_index; | |
24 | ||
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 | |
28 | * template function. | |
29 | */ | |
30 | ||
31 | #pragma parse_func_templ off | |
32 | #endif | |
33 | ||
34 | typedef multi_index_container<int> copyable_and_movable; | |
35 | ||
36 | struct holder | |
37 | { | |
38 | copyable_and_movable c; | |
39 | }; | |
40 | ||
41 | template<typename Sequence> | |
42 | static void test_assign() | |
43 | { | |
44 | Sequence s; | |
45 | ||
46 | int a[]={0,1,2,3,4,5}; | |
47 | std::size_t sa=sizeof(a)/sizeof(a[0]); | |
48 | ||
49 | s.assign(&a[0],&a[sa]); | |
50 | BOOST_TEST(s.size()==sa&&std::equal(s.begin(),s.end(),&a[0])); | |
51 | ||
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])); | |
54 | ||
55 | #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) | |
56 | s.assign({0,1,2,3,4,5}); | |
57 | #else | |
58 | s.assign(&a[0],&a[sa]); | |
59 | #endif | |
60 | ||
61 | BOOST_TEST(s.size()==sa&&std::equal(s.begin(),s.end(),&a[0])); | |
62 | ||
63 | s.assign((std::size_t)18,37); | |
64 | BOOST_TEST(s.size()==18&&std::accumulate(s.begin(),s.end(),0)==666); | |
65 | ||
66 | s.assign((std::size_t)12,167); | |
67 | BOOST_TEST(s.size()==12&&std::accumulate(s.begin(),s.end(),0)==2004); | |
68 | } | |
69 | ||
70 | #if BOOST_WORKAROUND(__MWERKS__,<=0x3003) | |
71 | #pragma parse_func_templ reset | |
72 | #endif | |
73 | ||
74 | template<typename Sequence> | |
75 | static void test_integral_assign() | |
76 | { | |
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). | |
79 | */ | |
80 | ||
81 | Sequence s; | |
82 | ||
83 | s.assign(5,10); | |
84 | BOOST_TEST(s.size()==5&&std::accumulate(s.begin(),s.end(),0)==50); | |
85 | s.assign(2u,5u); | |
86 | BOOST_TEST(s.size()==2&&std::accumulate(s.begin(),s.end(),0)==10); | |
87 | ||
88 | s.clear(); | |
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); | |
93 | } | |
94 | ||
95 | employee_set produce_employee_set() | |
96 | { | |
97 | employee_set es; | |
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)); | |
102 | return es; | |
103 | } | |
104 | ||
105 | void test_copy_assignment() | |
106 | { | |
107 | employee_set es; | |
108 | employee_set es2(es); | |
109 | ||
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(); | |
116 | ||
117 | BOOST_TEST(es2.empty()); | |
118 | ||
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 */ | |
126 | ||
127 | employee_set es3(es2); | |
128 | ||
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)); | |
133 | ||
134 | employee_set es4=employee_set(non_std_allocator<employee>()); | |
135 | employee_set_by_name& i1=get<name>(es4); | |
136 | i1=get<1>(es2); | |
137 | ||
138 | BOOST_TEST(es4==es2); | |
139 | ||
140 | employee_set es5=employee_set(employee_set::ctor_args_list()); | |
141 | employee_set_by_age& i2=get<age>(es5); | |
142 | i2=get<2>(es2); | |
143 | ||
144 | BOOST_TEST(i2==get<2>(es2)); | |
145 | ||
146 | employee_set es6; | |
147 | employee_set_as_inserted& i3=get<as_inserted>(es6); | |
148 | i3=get<3>(es2); | |
149 | ||
150 | BOOST_TEST(i3==get<3>(es2)); | |
151 | ||
152 | employee_set es7; | |
153 | employee_set_randomly& i5=get<randomly>(es7); | |
154 | i5=get<5>(es2); | |
155 | ||
156 | BOOST_TEST(i5==get<5>(es2)); | |
157 | ||
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}}); | |
160 | employee_set es9; | |
161 | es9={{0,"Rose",40,4512},{1,"Mary",38,3345},{2,"Jo",25,7102}, | |
162 | {0,"Rose",40,4512}}; | |
163 | ||
164 | BOOST_TEST(es8.size()==3); | |
165 | BOOST_TEST(es9==es8); | |
166 | ||
167 | es9.clear(); | |
168 | get<0>(es9)={{0,"Rose",40,4512},{1,"Mary",38,3345},{2,"Jo",25,7102}, | |
169 | {0,"Rose",40,4512}}; | |
170 | BOOST_TEST(es9==es8); | |
171 | ||
172 | es9.clear(); | |
173 | get<0>(es9)={{0,"Rose",40,4512},{2,"Jo",25,7102},{1,"Mary",38,3345}}; | |
174 | BOOST_TEST(es9==es8); | |
175 | ||
176 | es9.clear(); | |
177 | get<1>(es9)={{1,"Mary",38,3345},{0,"Rose",40,4512},{2,"Jo",25,7102}, | |
178 | {0,"Rose",40,4512}}; | |
179 | BOOST_TEST(es9==es8); | |
180 | ||
181 | es9.clear(); | |
182 | get<2>(es9)={{2,"Jo",25,7102},{0,"Rose",40,4512},{1,"Mary",38,3345}}; | |
183 | BOOST_TEST(es9==es8); | |
184 | ||
185 | es9.clear(); | |
186 | get<3>(es9)={{0,"Rose",40,4512},{1,"Mary",38,3345},{1,"Mary",38,3345}, | |
187 | {2,"Jo",25,7102}}; | |
188 | BOOST_TEST(es9==es8); | |
189 | ||
190 | es9.clear(); | |
191 | get<4>(es9)={{1,"Mary",38,3345},{2,"Jo",25,7102},{0,"Rose",40,4512}}; | |
192 | BOOST_TEST(es9==es8); | |
193 | ||
194 | es9.clear(); | |
195 | get<5>(es9)={{1,"Mary",38,3345},{2,"Jo",25,7102},{0,"Rose",40,4512}, | |
196 | {2,"Jo",25,7102}}; | |
197 | BOOST_TEST(es9==es8); | |
198 | #endif | |
199 | ||
200 | employee_set es10(produce_employee_set()),es11(produce_employee_set()); | |
201 | BOOST_TEST(es10==es11); | |
202 | ||
203 | employee_set es12(boost::move(es10)); | |
204 | BOOST_TEST(es10.empty()); | |
205 | BOOST_TEST(es11==es12); | |
206 | ||
207 | es10=boost::move(es12); | |
208 | BOOST_TEST(es12.empty()); | |
209 | BOOST_TEST(es11==es10); | |
210 | ||
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)); | |
215 | ||
216 | employee_set es13(l.begin(),l.end()); | |
217 | ||
218 | l.sort(); | |
219 | ||
220 | BOOST_TEST(es13.size()==l.size()&& | |
221 | std::equal(es13.begin(),es13.end(),l.begin())); | |
222 | ||
223 | test_assign<multi_index_container<int,indexed_by<sequenced<> > > >(); | |
224 | test_integral_assign< | |
225 | multi_index_container<int,indexed_by<sequenced<> > > >(); | |
226 | ||
227 | test_assign<multi_index_container<int,indexed_by<random_access<> > > >(); | |
228 | test_integral_assign< | |
229 | multi_index_container<int,indexed_by<random_access<> > > >(); | |
230 | ||
231 | /* Testcase for problem described at http://www.boost.org/doc/html/move/ | |
232 | * emulation_limitations.html#move.emulation_limitations.assignment_operator | |
233 | */ | |
234 | ||
235 | holder h((holder())); | |
236 | h=holder(); | |
237 | } |