File: test_copy_assignment.cpp

package info (click to toggle)
boost1.90 1.90.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 593,120 kB
  • sloc: cpp: 4,190,908; xml: 196,648; python: 34,618; ansic: 23,145; asm: 5,468; sh: 3,774; makefile: 1,161; perl: 1,020; sql: 728; ruby: 676; yacc: 478; java: 77; lisp: 24; csh: 6
file content (365 lines) | stat: -rw-r--r-- 10,641 bytes parent folder | download | duplicates (15)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/* Boost.MultiIndex test for copying and assignment.
 *
 * Copyright 2003-2018 Joaquin M Lopez Munoz.
 * Distributed under the Boost Software License, Version 1.0.
 * (See accompanying file LICENSE_1_0.txt or copy at
 * http://www.boost.org/LICENSE_1_0.txt)
 *
 * See http://www.boost.org/libs/multi_index for library home page.
 */

#include "test_copy_assignment.hpp"

#include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
#include <algorithm>
#include <boost/move/utility_core.hpp>
#include <list>
#include <numeric>
#include <vector>
#include "pre_multi_index.hpp"
#include "employee.hpp"
#include "small_allocator.hpp"
#include <boost/detail/lightweight_test.hpp>

using namespace boost::multi_index;

#if BOOST_WORKAROUND(__MWERKS__,<=0x3003)
/* The "ISO C++ Template Parser" option makes CW8.3 incorrectly fail at
 * expressions of the form sizeof(x) where x is an array local to a
 * template function.
 */

#pragma parse_func_templ off
#endif

typedef multi_index_container<int> copyable_and_movable;

struct holder
{
  copyable_and_movable c;
};

template<typename Sequence>
static void test_assign()
{
  Sequence s;

  int a[]={0,1,2,3,4,5};
  std::size_t sa=sizeof(a)/sizeof(a[0]);

  s.assign(&a[0],&a[sa]);
  BOOST_TEST(s.size()==sa&&std::equal(s.begin(),s.end(),&a[0]));

  s.assign((const int*)(&a[0]),(const int*)(&a[sa]));
  BOOST_TEST(s.size()==sa&&std::equal(s.begin(),s.end(),&a[0]));

#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
  s.assign({0,1,2,3,4,5});
#else
  s.assign(&a[0],&a[sa]);
#endif

  BOOST_TEST(s.size()==sa&&std::equal(s.begin(),s.end(),&a[0]));

  s.assign((std::size_t)18,37);
  BOOST_TEST(s.size()==18&&std::accumulate(s.begin(),s.end(),0)==666);

  s.assign((std::size_t)12,167);
  BOOST_TEST(s.size()==12&&std::accumulate(s.begin(),s.end(),0)==2004);
}

#if BOOST_WORKAROUND(__MWERKS__,<=0x3003)
#pragma parse_func_templ reset
#endif

template<typename Sequence>
static void test_integral_assign()
{
  /* Special cases described in 23.1.1/9: integral types must not
   * be taken as iterators in assign(f,l) and insert(p,f,l).
   */

  Sequence s;

  s.assign(5,10);
  BOOST_TEST(s.size()==5&&std::accumulate(s.begin(),s.end(),0)==50);
  s.assign(2u,5u);
  BOOST_TEST(s.size()==2&&std::accumulate(s.begin(),s.end(),0)==10);

  s.clear();
  s.insert(s.begin(),5,10);
  BOOST_TEST(s.size()==5&&std::accumulate(s.begin(),s.end(),0)==50);
  s.insert(s.begin(),2u,5u);
  BOOST_TEST(s.size()==7&&std::accumulate(s.begin(),s.end(),0)==60);
}

employee_set produce_employee_set()
{
  employee_set es;
  es.insert(employee(0,"Petr",60,2837));
  es.insert(employee(1,"Jiri",25,2143));
  es.insert(employee(2,"Radka",40,4875));
  es.insert(employee(3,"Milan",38,3474));
  return es;
}

void test_copy_assignment()
{
  employee_set es;
  employee_set es2(es);

  employee_set::allocator_type al=es.get_allocator();
  al=get<1>(es).get_allocator();
  al=get<2>(es).get_allocator();
  al=get<3>(es).get_allocator();
  al=get<4>(es).get_allocator();
  al=get<5>(es).get_allocator();

  BOOST_TEST(es2.empty());

  es2.insert(employee(0,"Joe",31,1123));
  es2.insert(employee(1,"Robert",27,5601));
  es2.insert(employee(2,"John",40,7889));
  es2.insert(employee(2,"Aristotle",2388,3357)); /* clash */
  es2.insert(employee(3,"Albert",20,9012));
  es2.insert(employee(4,"John",57,1002));
  es2.insert(employee(0,"Andrew",60,2302));      /* clash */

  employee_set es3(es2);

  BOOST_TEST(es2==es3);
  BOOST_TEST(get<2>(es2)==get<2>(es3));
  BOOST_TEST(get<3>(es2)==get<3>(es3));
  BOOST_TEST(get<5>(es2)==get<5>(es3));

  employee_set es4=employee_set(non_std_allocator<employee>());
  employee_set_by_name& i1=get<name>(es4);
  i1=get<1>(es2);

  BOOST_TEST(es4==es2);

  employee_set es5=employee_set(employee_set::ctor_args_list());
  employee_set_by_age& i2=get<age>(es5);
  i2=get<2>(es2);

  BOOST_TEST(i2==get<2>(es2));

  employee_set es6;
  employee_set_as_inserted& i3=get<as_inserted>(es6);
  i3=get<3>(es2);

  BOOST_TEST(i3==get<3>(es2));

  employee_set es7;
  employee_set_randomly& i5=get<randomly>(es7);
  i5=get<5>(es2);

  BOOST_TEST(i5==get<5>(es2));

#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)&&\
    !BOOST_WORKAROUND(BOOST_MSVC,==1800) /* MSVC 12.0 chokes on what follows */
  employee_set es8({{0,"Rose",40,4512},{1,"Mary",38,3345},{2,"Jo",25,7102}});
  employee_set es9;
  es9={{0,"Rose",40,4512},{1,"Mary",38,3345},{2,"Jo",25,7102},
       {0,"Rose",40,4512}};

  BOOST_TEST(es8.size()==3);
  BOOST_TEST(es9==es8);

  es9.clear();
  get<0>(es9)={{0,"Rose",40,4512},{1,"Mary",38,3345},{2,"Jo",25,7102},
               {0,"Rose",40,4512}};
  BOOST_TEST(es9==es8);

  es9.clear();
  get<0>(es9)={{0,"Rose",40,4512},{2,"Jo",25,7102},{1,"Mary",38,3345}};
  BOOST_TEST(es9==es8);

  es9.clear();
  get<1>(es9)={{1,"Mary",38,3345},{0,"Rose",40,4512},{2,"Jo",25,7102},
               {0,"Rose",40,4512}};
  BOOST_TEST(es9==es8);

  es9.clear();
  get<2>(es9)={{2,"Jo",25,7102},{0,"Rose",40,4512},{1,"Mary",38,3345}};
  BOOST_TEST(es9==es8);

  es9.clear();
  get<3>(es9)={{0,"Rose",40,4512},{1,"Mary",38,3345},{1,"Mary",38,3345},
               {2,"Jo",25,7102}};
  BOOST_TEST(es9==es8);

  es9.clear();
  get<4>(es9)={{1,"Mary",38,3345},{2,"Jo",25,7102},{0,"Rose",40,4512}};
  BOOST_TEST(es9==es8);

  es9.clear();
  get<5>(es9)={{1,"Mary",38,3345},{2,"Jo",25,7102},{0,"Rose",40,4512},
               {2,"Jo",25,7102}};
  BOOST_TEST(es9==es8);
#endif

  employee_set es10(produce_employee_set()),es11(produce_employee_set());
  BOOST_TEST(es10==es11);

  employee_set es12(boost::move(es10));
  BOOST_TEST(es10.empty());
  BOOST_TEST(es11==es12);

  es10=boost::move(es12);
  BOOST_TEST(es12.empty());
  BOOST_TEST(es11==es10);

  std::list<employee> l;
  l.push_back(employee(3,"Anna",31,5388));
  l.push_back(employee(1,"Rachel",27,9012));
  l.push_back(employee(2,"Agatha",40,1520));

  employee_set es13(l.begin(),l.end());

  l.sort();

  BOOST_TEST(es13.size()==l.size()&&
              std::equal(es13.begin(),es13.end(),l.begin()));

  test_assign<multi_index_container<int,indexed_by<sequenced<> > > >();
  test_integral_assign<
    multi_index_container<int,indexed_by<sequenced<> > > >();

  test_assign<multi_index_container<int,indexed_by<random_access<> > > >();
  test_integral_assign<
    multi_index_container<int,indexed_by<random_access<> > > >();

  /* Testcase for problem described at  http://www.boost.org/doc/html/move/
   * emulation_limitations.html#move.emulation_limitations.assignment_operator
   */

  holder h((holder()));
  h=holder();

#if !defined(BOOST_NO_CXX11_UNIFIED_INITIALIZATION_SYNTAX)
{
  /* testcase for https://svn.boost.org/trac10/ticket/13518 */

  multi_index_container<int> x={};
}
#endif

  typedef small_allocator<int> small_alloc;
  typedef multi_index_container<
    int,
    indexed_by<
      ordered_unique<identity<int> >,
      ranked_unique<identity<int> >,
      hashed_unique<identity<int> >,
      sequenced<>,
      random_access<>
    >,
    small_alloc
  > small_container;
  typedef small_container::nth_index<0>::type small_container_0;
  typedef small_container::nth_index<1>::type small_container_1;
  typedef small_container::nth_index<2>::type small_container_2;
  typedef small_container::nth_index<3>::type small_container_3;
  typedef small_container::nth_index<4>::type small_container_4;

  BOOST_STATIC_ASSERT((
    boost::is_same<small_container::size_type,small_alloc::size_type>::value));
  BOOST_STATIC_ASSERT((
    boost::is_same<
      small_container::difference_type,small_alloc::difference_type>::value));
  BOOST_STATIC_ASSERT((
    boost::is_same<small_container_0::size_type,small_alloc::size_type>::value));
  BOOST_STATIC_ASSERT((
    boost::is_same<
      small_container_0::difference_type,small_alloc::difference_type>::value));
  BOOST_STATIC_ASSERT((
    boost::is_same<small_container_1::size_type,small_alloc::size_type>::value));
  BOOST_STATIC_ASSERT((
    boost::is_same<
      small_container_1::difference_type,small_alloc::difference_type>::value));
  BOOST_STATIC_ASSERT((
    boost::is_same<small_container_2::size_type,small_alloc::size_type>::value));
  BOOST_STATIC_ASSERT((
    boost::is_same<
      small_container_2::difference_type,small_alloc::difference_type>::value));
  BOOST_STATIC_ASSERT((
    boost::is_same<small_container_3::size_type,small_alloc::size_type>::value));
  BOOST_STATIC_ASSERT((
    boost::is_same<
      small_container_3::difference_type,small_alloc::difference_type>::value));
  BOOST_STATIC_ASSERT((
    boost::is_same<small_container_4::size_type,small_alloc::size_type>::value));
  BOOST_STATIC_ASSERT((
    boost::is_same<
      small_container_4::difference_type,small_alloc::difference_type>::value));

  small_container        sc;
  small_container_0&     sc0=sc.get<0>();
  small_container_1&     sc1=sc.get<1>();
  small_container_2&     sc2=sc.get<2>();
  small_container_3&     sc3=sc.get<3>();
  small_container_4&     sc4=sc.get<4>();
  small_alloc::size_type s,
                         ms=(small_alloc::size_type)(-1);

  sc.insert(0);

  s=sc0.size();
  BOOST_TEST(sc0.max_size()<=ms);
  s=sc0.erase(1);
  s=sc0.count(0);
  s=sc0.count(0,std::less<int>());

  s=sc1.size();
  BOOST_TEST(sc1.max_size()<=ms);
  s=sc1.erase(1);
  s=sc1.count(0);
  s=sc1.count(0,std::less<int>());
  (void)sc1.nth(s);
  s=sc1.rank(sc1.begin());
  s=sc1.find_rank(0);
  s=sc1.find_rank(0,std::less<int>());
  s=sc1.lower_bound_rank(0);
  s=sc1.lower_bound_rank(0,std::less<int>());
  s=sc1.upper_bound_rank(0);
  s=sc1.upper_bound_rank(0,std::less<int>());
  s=sc1.equal_range_rank(0).first;
  s=sc1.equal_range_rank(0).second;
  s=sc1.range_rank(unbounded,unbounded).first;
  s=sc1.range_rank(unbounded,unbounded).second;

  s=sc2.size();
  BOOST_TEST(sc2.max_size()<=ms);
  s=sc2.erase(1);
  s=sc2.count(0);
  s=sc2.count(0,boost::hash<int>(),std::equal_to<int>());
  s=sc2.bucket_count();
  BOOST_TEST(sc2.max_bucket_count()<=ms);
  BOOST_TEST(sc2.bucket_size((small_alloc::size_type)(0))<=ms);
  BOOST_TEST(sc2.bucket_size(0)<=ms);
  (void)sc2.begin(0);
  (void)sc2.end(0);
  (void)sc2.cbegin(0);
  (void)sc2.cend(0);
  sc2.rehash(2);
  sc2.reserve(2);

  s=sc3.size();
  BOOST_TEST(sc3.max_size()<=ms);
  sc3.resize(0);
  sc3.resize(0,0);
  sc3.assign((small_alloc::size_type)(1),0);
  sc3.insert(sc3.begin(),(small_alloc::size_type)(0),0);

  s=sc4.size();
  BOOST_TEST(sc4.max_size()<=ms);
  BOOST_TEST(sc4.capacity()<=ms);
  sc4.reserve(0);
  sc4.resize(0);
  sc4.resize(0,0);
  sc4.assign((small_alloc::size_type)(1),0);
  (void)sc4[0];
  (void)sc4.at(0);
  sc4.insert(sc4.begin(),(small_alloc::size_type)(0),0);
}