File: small_vector_test.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 (556 lines) | stat: -rw-r--r-- 16,291 bytes parent folder | download | duplicates (3)
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556

//////////////////////////////////////////////////////////////////////////////
//
// (C) Copyright Ion Gaztanaga 2004-2013. 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/container for documentation.
//
//////////////////////////////////////////////////////////////////////////////
#include <boost/container/small_vector.hpp>
#include "vector_test.hpp"
#include "movable_int.hpp"
#include "propagate_allocator_test.hpp"
#include "default_init_test.hpp"
#include "../../intrusive/test/iterator_test.hpp"

#include <boost/container/allocator_traits.hpp>

#include <iostream>

struct boost_container_small_vector;

namespace boost { namespace container {   namespace test {

template<>
struct alloc_propagate_base<boost_container_small_vector>
{
   template <class T, class Allocator>
   struct apply
   {
      typedef boost::container::small_vector<T, 10, Allocator> type;
   };
};

}}}   //namespace boost::container::test


bool test_small_vector_shrink_to_fit()
{
   boost::container::small_vector<int, 5> sm5;
   boost::container::vector<int> v;
   sm5.push_back(1);
   sm5.push_back(2);
   sm5.push_back(3);
   sm5.push_back(4);
   sm5.push_back(5);

   v.push_back(1);
   v.push_back(2);
   v.push_back(3);
   v.push_back(4);
   v.push_back(5);

   if (!sm5.is_small())
      return false;
   if (!boost::container::algo_equal(sm5.begin(), sm5.end(), v.begin(), v.end()))
      return false;

   //Shrinking a when internal storage is used is a no-op
   sm5.shrink_to_fit();

   if (!sm5.is_small())
      return false;
   if (!boost::container::algo_equal(sm5.begin(), sm5.end(), v.begin(), v.end()))
      return false;

   //If dynamic memory is used, shrink_to_fit will move elements to the internal storage
   sm5.push_back(6);
   v.push_back(6);
   if (sm5.is_small())
      return false;
   sm5.pop_back();
   v.pop_back();
   if (sm5.is_small())
      return false;

   sm5.shrink_to_fit();
   if (!sm5.is_small())
      return false;
   if (!boost::container::algo_equal(sm5.begin(), sm5.end(), v.begin(), v.end()))
      return false;

   //If dynamic memory is used, and size is zero the dynamic storage is deallocated
   sm5.push_back(6);
   v.push_back(6);
   if (sm5.is_small())
      return false;
   sm5.clear();
   v.clear();
   if (sm5.is_small())
      return false;

   sm5.shrink_to_fit();
   if (!sm5.is_small())
      return false;
   if (!boost::container::algo_equal(sm5.begin(), sm5.end(), v.begin(), v.end()))
      return false;

   return true;
}

bool test_small_vector_base_test()
{
   typedef boost::container::small_vector_base<int> smb_t;
   {
      typedef boost::container::small_vector<int, 5> sm5_t;
      BOOST_CONTAINER_STATIC_ASSERT(sm5_t::static_capacity == 5);
      sm5_t sm5;
      smb_t &smb = sm5;
      smb.push_back(1);
      sm5_t sm5_copy(sm5);
      sm5_copy.push_back(1);
      if (!boost::container::test::CheckEqualContainers(sm5, smb))
         return false;
   }
   {
      typedef boost::container::small_vector<int, 7> sm7_t;
      BOOST_CONTAINER_STATIC_ASSERT(sm7_t::static_capacity == 7);
      sm7_t sm7;
      smb_t &smb = sm7;
      smb.push_back(2);
      sm7_t sm7_copy(sm7);
      sm7_copy.push_back(2);
      if (!boost::container::test::CheckEqualContainers(sm7, smb))
         return false;
   }
   {
      typedef boost::container::small_vector<int, 5> sm5_t;
      sm5_t sm5;
      smb_t &smb = sm5;
      smb.push_back(1);
      sm5_t sm5_copy(smb);
      if (!boost::container::test::CheckEqualContainers(sm5, sm5_copy))
         return false;
      smb.push_back(2);
      if(smb.size() != 2){
         return false;
      }
      sm5_copy = smb;
      if (!boost::container::test::CheckEqualContainers(sm5, sm5_copy))
         return false;
      sm5_t sm5_move(boost::move(smb));
      smb.clear();
      if (!boost::container::test::CheckEqualContainers(sm5_move, sm5_copy))
         return false;
      smb = sm5_copy;
      sm5_move = boost::move(smb);
      smb.clear();
      if (!boost::container::test::CheckEqualContainers(sm5_move, sm5_copy))
         return false;
   }
   {
      typedef boost::container::small_vector<int, 5> sm5_t;
      typedef boost::container::small_vector<int, 7> sm7_t;

      {
         //Both in static memory
         sm5_t sm5;
         sm5.push_back(1);

         sm7_t sm7;
         sm7.push_back(2);

         sm5_t sm5_copy(sm5);
         sm7_t sm7_copy(sm7);
         smb_t& smb5 = sm5_copy;
         smb_t& smb7 = sm7_copy;
         if (!sm5.is_small() || !sm7.is_small())
            return false;

         smb5.swap(smb7);
         if (!boost::container::test::CheckEqualContainers(sm5_copy, sm7))
            return false;
         if (!boost::container::test::CheckEqualContainers(sm7_copy, sm5))
            return false;

         if (!sm5.is_small() || !sm7.is_small())
            return false;

         smb5.swap(smb7);
         if (!boost::container::test::CheckEqualContainers(sm7_copy, sm7))
            return false;
         if (!boost::container::test::CheckEqualContainers(sm5_copy, sm5))
            return false;

         if (!sm5.is_small() || !sm7.is_small())
            return false;
      }
      {
         //Both in dynamic memory
         sm5_t sm5;
         for(std::size_t i = 0, max = sm5.capacity()+1; i != max; ++i){
            sm5.push_back(int(i));
         }

         sm7_t sm7;
         for(std::size_t i = 0, max = sm7.capacity()+1; i != max; ++i){
            sm7.push_back(int(i));
         }

         sm5_t sm5_copy(sm5);
         sm7_t sm7_copy(sm7);
         smb_t& smb5 = sm5_copy;
         smb_t& smb7 = sm7_copy;

         if (smb5.is_small() || smb7.is_small())
            return false;

         smb5.swap(smb7);
         if (!boost::container::test::CheckEqualContainers(sm5_copy, sm7))
            return false;
         if (!boost::container::test::CheckEqualContainers(sm7_copy, sm5))
            return false;

         if (smb5.is_small() || smb7.is_small())
            return false;

         smb5.swap(smb7);
         if (!boost::container::test::CheckEqualContainers(sm7_copy, sm7))
            return false;
         if (!boost::container::test::CheckEqualContainers(sm5_copy, sm5))
            return false;

         if (smb5.is_small() || smb7.is_small())
            return false;
      }
      {
         //sm7 in dynamic memory
         sm5_t sm5;
         for(std::size_t i = 0, max = sm5.capacity()-1; i != max; ++i){
            sm5.push_back(int(i));
         }

         sm7_t sm7;
         for(std::size_t i = 0, max = sm7.capacity()+1; i != max; ++i){
            sm7.push_back(int(i));
         }

         sm5_t sm5_copy(sm5);
         sm7_t sm7_copy(sm7);
         smb_t& smb5 = sm5_copy;
         smb_t& smb7 = sm7_copy;

         if (!smb5.is_small() || smb7.is_small())
            return false;

         //As small_vector_base is capacity-erased, will make an element-wise swap,
         //remaining smb7 elements won't fit in smb5's internal buffer so both will be dynamic
         smb5.swap(smb7);
         if (!boost::container::test::CheckEqualContainers(sm5_copy, sm7))
            return false;
         if (!boost::container::test::CheckEqualContainers(sm7_copy, sm5))
            return false;

         if (smb5.is_small() || smb7.is_small())
            return false;

         //Swap them again (both dynamic)
         smb5.swap(smb7);
         if (!boost::container::test::CheckEqualContainers(sm7_copy, sm7))
            return false;
         if (!boost::container::test::CheckEqualContainers(sm5_copy, sm5))
            return false;

         if (smb5.is_small() || smb7.is_small())
            return false;

         //Try again with one dynamic, but the reverse option
         //shrink to fit should free the dynamic buffer after clear
         //and both should be small again using the internal buffer
         sm5_copy.clear();
         sm5_copy.shrink_to_fit();
         sm7_copy.clear();
         sm7_copy.shrink_to_fit();
      
         if (!smb5.is_small() || !smb7.is_small())
            return false;

         sm5_copy = sm5;
         sm7_copy = sm7;

         if (!smb5.is_small() || smb7.is_small())
            return false;
      }
   }

   return true;
}

//small vector has internal storage so some special swap cases must be tested
bool test_swap()
{
   typedef boost::container::small_vector<int, 10> vec;
   {  //v bigger than static capacity, w empty
      vec v;
      for(std::size_t i = 0, max = v.capacity()+1; i != max; ++i){
         v.push_back(int(i));
      }
      vec w;
      vec v_copy = v;
      vec w_copy = w;
      v.swap(w);
      if (w != v_copy || v != w_copy)
         return false;
   }
   {  //v smaller than static capacity, w empty
      vec v;
      for(std::size_t i = 0, max = v.capacity()-1; i != max; ++i){
         v.push_back(int(i));
      }
      vec w;
      vec v_copy = v;
      vec w_copy = w;
      v.swap(w);
      if (w != v_copy || v != w_copy)
         return false;
   }
   {  //v bigger than static capacity, w enough capacity for static
      vec v;
      for (std::size_t i = 0, max = v.capacity() + 1; i != max; ++i) {
         v.push_back(int(i));
      }
      vec w;
      for (std::size_t i = 0, max = w.capacity() / 2; i != max; ++i) {
         w.push_back(int(i));
      }
      vec v_copy = v;
      vec w_copy = w;
      v.swap(w);
      if (w != v_copy || v != w_copy)
         return false;
   }
   {  //v & w smaller than static capacity
      vec v;
      for(std::size_t i = 0, max = v.capacity()-1; i != max; ++i){
         v.push_back(int(i));
      }
      vec w;
      for(std::size_t i = 0, max = w.capacity()/2; i != max; ++i){
         w.push_back(int(i));
      }
      vec v_copy = v;
      vec w_copy = w;
      v.swap(w);
      if (w != v_copy || v != w_copy)
         return false;
   }
   {  //v & w bigger than static capacity
      vec v;
      for(std::size_t i = 0, max = v.capacity()+1; i != max; ++i){
         v.push_back(int(i));
      }
      vec w;
      for(std::size_t i = 0, max = w.capacity()*2; i != max; ++i){
         w.push_back(int(i));
      }
      vec v_copy = v;
      vec w_copy = w;
      v.swap(w);
      if (w != v_copy || v != w_copy)
         return false;
   }

   //Now test internal buffer/dynamic buffer swapping
   {
      typedef boost::container::small_vector<int, 5> sm5_t;

      {
         sm5_t sm5;
         for (std::size_t i = 0, max = sm5.capacity() - 1; i != max; ++i) {
            sm5.push_back(int(i));
         }

         sm5_t sm5_copy(sm5);
         {
            sm5_t sm5_dyn(sm5);
            sm5_dyn.resize(sm5_dyn.capacity() + 1u);
            sm5_dyn.resize(sm5.size());

            if (sm5_dyn != sm5 || sm5_dyn.is_small())
               return false;

            //Swap derived small vector one static one dynamic
            sm5_copy.swap(sm5_dyn);

            if (sm5_dyn != sm5)
               return false;

            //Dynamic buffer should be transferred, the old dynamic should be small now
            if (sm5_copy.is_small() || !sm5_dyn.is_small())
               return false;

            //Swap derived small vector one static one dynamic
            sm5_copy.swap(sm5_dyn);

            if (sm5_dyn != sm5)
               return false;

            //Dynamic buffer should be transferred, the old dynamic should be small now
            if (!sm5_copy.is_small() || sm5_dyn.is_small())
               return false;
         }
         {
            sm5_t sm5_int(sm5);
            if (sm5_int != sm5 || !sm5_int.is_small())
               return false;

            //Swap derived small vector one static one dynamic
            sm5_copy.swap(sm5_int);

            if (sm5_int != sm5)
               return false;

            //No dynamic memory should be present as small capacity is enough
            if (!sm5_copy.is_small() || !sm5_int.is_small())
               return false;

            //Swap derived small vector one static one dynamic
            sm5_copy.swap(sm5_int);

            if (sm5_int != sm5)
               return false;

            //No dynamic memory should be present as small capacity is enough
            if (!sm5_copy.is_small() || !sm5_int.is_small())
               return false;
         }
      }
   }
   return true;
}

template<class VoidAllocator>
struct GetAllocatorCont
{
   template<class ValueType>
   struct apply
   {
      typedef boost::container::small_vector< ValueType, 10
         , typename boost::container::allocator_traits<VoidAllocator>
            ::template portable_rebind_alloc<ValueType>::type
      > type;
   };
};

template<class VoidAllocator>
int test_cont_variants()
{
   using namespace boost::container;
   typedef typename GetAllocatorCont<VoidAllocator>::template apply<int>::type MyCont;
   typedef typename GetAllocatorCont<VoidAllocator>::template apply<test::movable_int>::type MyMoveCont;
   typedef typename GetAllocatorCont<VoidAllocator>::template apply<test::movable_and_copyable_int>::type MyCopyMoveCont;
   typedef typename GetAllocatorCont<VoidAllocator>::template apply<test::copyable_int>::type MyCopyCont;
   typedef typename GetAllocatorCont<VoidAllocator>::template apply<test::moveconstruct_int>::type MyMoveConstructCont;

   if (test::vector_test<MyCont>())
      return 1;
   if (test::vector_test<MyMoveCont>())
      return 1;
   if (test::vector_test<MyCopyMoveCont>())
      return 1;
   if (test::vector_test<MyCopyCont>())
      return 1;
   if (test::vector_test<MyMoveConstructCont>())
      return 1;

   return 0;
}

int main()
{
   using namespace boost::container;

   if(!test_swap())
      return 1;

   if (test::vector_test< small_vector<int, 0> >())
      return 1;

   if (test::vector_test< small_vector<int, 2000> >())
      return 1;

   if (test_cont_variants< new_allocator<void> >())
      return 1;

   ////////////////////////////////////
   //    Default init test
   ////////////////////////////////////
   if(!test::default_init_test< small_vector<int, 5, test::default_init_allocator<int> > >()){
      std::cerr << "Default init test failed" << std::endl;
      return 1;
   }

   ////////////////////////////////////
   //    Emplace testing
   ////////////////////////////////////
   const test::EmplaceOptions Options = (test::EmplaceOptions)(test::EMPLACE_BACK | test::EMPLACE_BEFORE);
   if(!boost::container::test::test_emplace< small_vector<test::EmplaceInt, 5>, Options>()){
      return 1;
   }

   ////////////////////////////////////
   //    Allocator propagation testing
   ////////////////////////////////////
   if(!boost::container::test::test_propagate_allocator<boost_container_small_vector>()){
      return 1;
   }

   ////////////////////////////////////
   //    Initializer lists testing
   ////////////////////////////////////
   if(!boost::container::test::test_vector_methods_with_initializer_list_as_argument_for
      < boost::container::small_vector<int, 5> >()) {
      return 1;
   }

   ////////////////////////////////////
   //       Small vector base
   ////////////////////////////////////
   if (!test_small_vector_shrink_to_fit()){
      return 1;
   }

   ////////////////////////////////////
   //    Iterator testing
   ////////////////////////////////////
   {
      typedef boost::container::small_vector<int, 10> cont_int;
      for (std::size_t i = 10; i <= 10000; i *= 10) {
         cont_int a;
         for (int j = 0; j < (int)i; ++j)
            a.push_back((int)j);
         boost::intrusive::test::test_iterator_random< cont_int >(a);
         if (boost::report_errors() != 0) {
            return 1;
         }
      }
   }

   ////////////////////////////////////
   //    has_trivial_destructor_after_move testing
   ////////////////////////////////////
   // default allocator
   {
      typedef boost::container::small_vector<int, 0> cont;
      BOOST_CONTAINER_STATIC_ASSERT(!boost::has_trivial_destructor_after_move<cont>::value);
   }
   // std::allocator
   {
      typedef boost::container::small_vector<int, 0, std::allocator<int> > cont;
      BOOST_CONTAINER_STATIC_ASSERT(!boost::has_trivial_destructor_after_move<cont>::value);
   }

   return 0;
}