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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
|
//////////////////////////////////////////////////////////////////////////////
//
// (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/detail/config_begin.hpp>
#include <set>
#include <boost/container/set.hpp>
#include <boost/container/adaptive_pool.hpp>
#include "print_container.hpp"
#include "movable_int.hpp"
#include "dummy_test_allocator.hpp"
#include "set_test.hpp"
#include "propagate_allocator_test.hpp"
#include "emplace_test.hpp"
#include "../../intrusive/test/iterator_test.hpp"
using namespace boost::container;
//Test recursive structures
class recursive_set
{
public:
recursive_set & operator=(const recursive_set &x)
{ id_ = x.id_; set_ = x.set_; return *this; }
int id_;
set<recursive_set> set_;
set<recursive_set>::iterator it_;
set<recursive_set>::const_iterator cit_;
set<recursive_set>::reverse_iterator rit_;
set<recursive_set>::const_reverse_iterator crit_;
friend bool operator< (const recursive_set &a, const recursive_set &b)
{ return a.id_ < b.id_; }
};
//Test recursive structures
class recursive_multiset
{
public:
recursive_multiset & operator=(const recursive_multiset &x)
{ id_ = x.id_; multiset_ = x.multiset_; return *this; }
int id_;
multiset<recursive_multiset> multiset_;
multiset<recursive_multiset>::iterator it_;
multiset<recursive_multiset>::const_iterator cit_;
multiset<recursive_multiset>::reverse_iterator rit_;
multiset<recursive_multiset>::const_reverse_iterator crit_;
friend bool operator< (const recursive_multiset &a, const recursive_multiset &b)
{ return a.id_ < b.id_; }
};
template<class C>
void test_move()
{
//Now test move semantics
C original;
original.emplace();
C move_ctor(boost::move(original));
C move_assign;
move_assign.emplace();
move_assign = boost::move(move_ctor);
move_assign.swap(original);
}
bool node_type_test()
{
using namespace boost::container;
{
typedef set<test::movable_int> set_type;
set_type src;
{
test::movable_int mv_1(1), mv_2(2), mv_3(3);
src.emplace(boost::move(mv_1));
src.emplace(boost::move(mv_2));
src.emplace(boost::move(mv_3));
}
if(src.size() != 3)
return false;
set_type dst;
{
test::movable_int mv_3(3);
dst.emplace(boost::move(mv_3));
}
if(dst.size() != 1)
return false;
const test::movable_int mv_1(1);
const test::movable_int mv_2(2);
const test::movable_int mv_3(3);
const test::movable_int mv_33(33);
set_type::insert_return_type r;
r = dst.insert(src.extract(mv_33)); // Key version, try to insert empty node
if(! (r.position == dst.end() && r.inserted == false && r.node.empty()) )
return false;
r = dst.insert(src.extract(src.find(mv_1))); // Iterator version, successful
if(! (r.position == dst.find(mv_1) && r.inserted == true && r.node.empty()) )
return false;
r = dst.insert(dst.begin(), src.extract(mv_2)); // Key type version, successful
if(! (r.position == dst.find(mv_2) && r.inserted == true && r.node.empty()) )
return false;
r = dst.insert(src.extract(mv_3)); // Key type version, unsuccessful
if(!src.empty())
return false;
if(dst.size() != 3)
return false;
if(! (r.position == dst.find(mv_3) && r.inserted == false && r.node.value() == mv_3) )
return false;
}
{
typedef multiset<test::movable_int> multiset_type;
multiset_type src;
{
test::movable_int mv_1(1), mv_2(2), mv_3(3), mv_3bis(3);
src.emplace(boost::move(mv_1));
src.emplace(boost::move(mv_2));
src.emplace(boost::move(mv_3));
src.emplace_hint(src.begin(), boost::move(mv_3bis));
}
if(src.size() != 4)
return false;
multiset_type dst;
{
test::movable_int mv_3(3);
dst.emplace(boost::move(mv_3));
}
if(dst.size() != 1)
return false;
const test::movable_int mv_1(1);
const test::movable_int mv_2(2);
const test::movable_int mv_3(3);
const test::movable_int mv_4(4);
multiset_type::iterator r;
multiset_type::node_type nt(src.extract(mv_3));
r = dst.insert(dst.begin(), boost::move(nt));
if(! (*r == mv_3 && dst.find(mv_3) == r && nt.empty()) )
return false;
nt = src.extract(src.find(mv_1));
r = dst.insert(boost::move(nt)); // Iterator version, successful
if(! (*r == mv_1 && nt.empty()) )
return false;
nt = src.extract(mv_2);
r = dst.insert(boost::move(nt)); // Key type version, successful
if(! (*r == mv_2 && nt.empty()) )
return false;
r = dst.insert(src.extract(mv_3)); // Key type version, successful
if(! (*r == mv_3 && r == --multiset_type::iterator(dst.upper_bound(mv_3)) && nt.empty()) )
return false;
r = dst.insert(src.extract(mv_4)); // Key type version, unsuccessful
if(! (r == dst.end()) )
return false;
if(!src.empty())
return false;
if(dst.size() != 5)
return false;
}
return true;
}
struct boost_container_set;
struct boost_container_multiset;
namespace boost {
namespace container {
namespace test {
template<>
struct alloc_propagate_base<boost_container_set>
{
template <class T, class Allocator>
struct apply
{
typedef boost::container::set<T, std::less<T>, Allocator> type;
};
};
template<>
struct alloc_propagate_base<boost_container_multiset>
{
template <class T, class Allocator>
struct apply
{
typedef boost::container::multiset<T, std::less<T>, Allocator> type;
};
};
bool constructor_template_auto_deduction_test()
{
#ifndef BOOST_CONTAINER_NO_CXX17_CTAD
using namespace boost::container;
const std::size_t NumElements = 100;
{
std::set<int> int_set;
for (std::size_t i = 0; i != NumElements; ++i) {
int_set.insert(static_cast<int>(i));
}
std::multiset<int> int_mset;
for (std::size_t i = 0; i != NumElements; ++i) {
int_mset.insert(static_cast<int>(i));
}
typedef std::less<int> comp_int_t;
typedef std::allocator<int> alloc_int_t;
//range
{
auto fset = set(int_set.begin(), int_set.end());
if (!CheckEqualContainers(int_set, fset))
return false;
auto fmset = multiset(int_mset.begin(), int_mset.end());
if (!CheckEqualContainers(int_mset, fmset))
return false;
}
//range+comp
{
auto fset = set(int_set.begin(), int_set.end(), comp_int_t());
if (!CheckEqualContainers(int_set, fset))
return false;
auto fmset = multiset(int_mset.begin(), int_mset.end(), comp_int_t());
if (!CheckEqualContainers(int_mset, fmset))
return false;
}
//range+comp+alloc
{
auto fset = set(int_set.begin(), int_set.end(), comp_int_t(), alloc_int_t());
if (!CheckEqualContainers(int_set, fset))
return false;
auto fmset = multiset(int_mset.begin(), int_mset.end(), comp_int_t(), alloc_int_t());
if (!CheckEqualContainers(int_mset, fmset))
return false;
}
//range+alloc
{
auto fset = set(int_set.begin(), int_set.end(), alloc_int_t());
if (!CheckEqualContainers(int_set, fset))
return false;
auto fmset = multiset(int_mset.begin(), int_mset.end(), alloc_int_t());
if (!CheckEqualContainers(int_mset, fmset))
return false;
}
//ordered_unique_range / ordered_range
//range
{
auto fset = set(ordered_unique_range, int_set.begin(), int_set.end());
if (!CheckEqualContainers(int_set, fset))
return false;
auto fmset = multiset(ordered_range, int_mset.begin(), int_mset.end());
if (!CheckEqualContainers(int_mset, fmset))
return false;
}
//range+comp
{
auto fset = set(ordered_unique_range, int_set.begin(), int_set.end(), comp_int_t());
if (!CheckEqualContainers(int_set, fset))
return false;
auto fmset = multiset(ordered_range, int_mset.begin(), int_mset.end(), comp_int_t());
if (!CheckEqualContainers(int_mset, fmset))
return false;
}
//range+comp+alloc
{
auto fset = set(ordered_unique_range, int_set.begin(), int_set.end(), comp_int_t(), alloc_int_t());
if (!CheckEqualContainers(int_set, fset))
return false;
auto fmset = multiset(ordered_range, int_mset.begin(), int_mset.end(), comp_int_t(), alloc_int_t());
if (!CheckEqualContainers(int_mset, fmset))
return false;
}
//range+alloc
{
auto fset = set(ordered_unique_range, int_set.begin(), int_set.end(), alloc_int_t());
if (!CheckEqualContainers(int_set, fset))
return false;
auto fmset = multiset(ordered_range, int_mset.begin(), int_mset.end(), alloc_int_t());
if (!CheckEqualContainers(int_mset, fmset))
return false;
}
}
#endif
return true;
}
}}} //boost::container::test
template<class VoidAllocator, boost::container::tree_type_enum tree_type_value>
struct GetAllocatorSet
{
template<class ValueType>
struct apply
{
typedef set < ValueType
, std::less<ValueType>
, typename allocator_traits<VoidAllocator>
::template portable_rebind_alloc<ValueType>::type
, typename boost::container::tree_assoc_options
< boost::container::tree_type<tree_type_value>
>::type
> set_type;
typedef multiset < ValueType
, std::less<ValueType>
, typename allocator_traits<VoidAllocator>
::template portable_rebind_alloc<ValueType>::type
, typename boost::container::tree_assoc_options
< boost::container::tree_type<tree_type_value>
>::type
> multiset_type;
};
};
void test_merge_from_different_comparison()
{
set<int> set1;
set<int, std::greater<int> > set2;
set1.merge(set2);
}
bool test_heterogeneous_lookups()
{
typedef set<int, test::less_transparent> set_t;
typedef multiset<int, test::less_transparent> mset_t;
set_t set1;
mset_t mset1;
const set_t &cset1 = set1;
const mset_t &cmset1 = mset1;
set1.insert(1);
set1.insert(1);
set1.insert(2);
set1.insert(2);
set1.insert(3);
mset1.insert(1);
mset1.insert(1);
mset1.insert(2);
mset1.insert(2);
mset1.insert(3);
const test::non_copymovable_int find_me(2);
//find
if(*set1.find(find_me) != 2)
return false;
if(*cset1.find(find_me) != 2)
return false;
if(*mset1.find(find_me) != 2)
return false;
if(*cmset1.find(find_me) != 2)
return false;
//count
if(set1.count(find_me) != 1)
return false;
if(cset1.count(find_me) != 1)
return false;
if(mset1.count(find_me) != 2)
return false;
if(cmset1.count(find_me) != 2)
return false;
//contains
if(!set1.contains(find_me))
return false;
if(!cset1.contains(find_me))
return false;
if(!mset1.contains(find_me))
return false;
if(!cmset1.contains(find_me))
return false;
//lower_bound
if(*set1.lower_bound(find_me) != 2)
return false;
if(*cset1.lower_bound(find_me) != 2)
return false;
if(*mset1.lower_bound(find_me) != 2)
return false;
if(*cmset1.lower_bound(find_me) != 2)
return false;
//upper_bound
if(*set1.upper_bound(find_me) != 3)
return false;
if(*cset1.upper_bound(find_me) != 3)
return false;
if(*mset1.upper_bound(find_me) != 3)
return false;
if(*cmset1.upper_bound(find_me) != 3)
return false;
//equal_range
if(*set1.equal_range(find_me).first != 2)
return false;
if(*cset1.equal_range(find_me).second != 3)
return false;
if(*mset1.equal_range(find_me).first != 2)
return false;
if(*cmset1.equal_range(find_me).second != 3)
return false;
return true;
}
int main ()
{
//Recursive container instantiation
{
set<recursive_set> set_;
multiset<recursive_multiset> multiset_;
}
//Allocator argument container
{
set<int> set_((set<int>::allocator_type()));
multiset<int> multiset_((multiset<int>::allocator_type()));
}
//Now test move semantics
{
test_move<set<recursive_set> >();
test_move<multiset<recursive_multiset> >();
}
//Test std::pair value type as tree has workarounds to make old std::pair
//implementations movable that can break things
{
boost::container::set<std::pair<int,int> > s;
std::pair<int,int> p(0, 0);
s.insert(p);
s.emplace(p);
}
if (!boost::container::test::instantiate_constructors<set<int>, multiset<int> >())
return 1;
test_merge_from_different_comparison();
////////////////////////////////////
// Constructor Template Auto Deduction test
////////////////////////////////////
if (!test::constructor_template_auto_deduction_test()) {
return 1;
}
if(!test_heterogeneous_lookups())
return 1;
////////////////////////////////////
// Testing allocator implementations
////////////////////////////////////
{
typedef std::set<int> MyStdSet;
typedef std::multiset<int> MyStdMultiSet;
if (0 != test::set_test
< GetAllocatorSet<std::allocator<void>, red_black_tree>::apply<int>::set_type
, MyStdSet
, GetAllocatorSet<std::allocator<void>, red_black_tree>::apply<int>::multiset_type
, MyStdMultiSet>()) {
std::cout << "Error in set_test<std::allocator<void>, red_black_tree>" << std::endl;
return 1;
}
if (0 != test::set_test
< GetAllocatorSet<new_allocator<void>, avl_tree>::apply<int>::set_type
, MyStdSet
, GetAllocatorSet<new_allocator<void>, avl_tree>::apply<int>::multiset_type
, MyStdMultiSet>()) {
std::cout << "Error in set_test<new_allocator<void>, avl_tree>" << std::endl;
return 1;
}
if (0 != test::set_test
< GetAllocatorSet<adaptive_pool<void>, scapegoat_tree>::apply<int>::set_type
, MyStdSet
, GetAllocatorSet<adaptive_pool<void>, scapegoat_tree>::apply<int>::multiset_type
, MyStdMultiSet>()) {
std::cout << "Error in set_test<adaptive_pool<void>, scapegoat_tree>" << std::endl;
return 1;
}
///////////
if (0 != test::set_test
< GetAllocatorSet<new_allocator<void>, splay_tree>::apply<test::movable_int>::set_type
, MyStdSet
, GetAllocatorSet<new_allocator<void>, splay_tree>::apply<test::movable_int>::multiset_type
, MyStdMultiSet>()) {
std::cout << "Error in set_test<new_allocator<void>, splay_tree>" << std::endl;
return 1;
}
if (0 != test::set_test
< GetAllocatorSet<new_allocator<void>, red_black_tree>::apply<test::copyable_int>::set_type
, MyStdSet
, GetAllocatorSet<new_allocator<void>, red_black_tree>::apply<test::copyable_int>::multiset_type
, MyStdMultiSet>()) {
std::cout << "Error in set_test<new_allocator<void>, red_black_tree>" << std::endl;
return 1;
}
if (0 != test::set_test
< GetAllocatorSet<new_allocator<void>, red_black_tree>::apply<test::movable_and_copyable_int>::set_type
, MyStdSet
, GetAllocatorSet<new_allocator<void>, red_black_tree>::apply<test::movable_and_copyable_int>::multiset_type
, MyStdMultiSet>()) {
std::cout << "Error in set_test<new_allocator<void>, red_black_tree>" << std::endl;
return 1;
}
}
////////////////////////////////////
// Emplace testing
////////////////////////////////////
const test::EmplaceOptions SetOptions = (test::EmplaceOptions)(test::EMPLACE_HINT | test::EMPLACE_ASSOC);
if(!boost::container::test::test_emplace<set<test::EmplaceInt>, SetOptions>())
return 1;
if(!boost::container::test::test_emplace<multiset<test::EmplaceInt>, SetOptions>())
return 1;
////////////////////////////////////
// Allocator propagation testing
////////////////////////////////////
if(!boost::container::test::test_propagate_allocator<boost_container_set>())
return 1;
if(!boost::container::test::test_propagate_allocator<boost_container_multiset>())
return 1;
if (!boost::container::test::test_set_methods_with_initializer_list_as_argument_for<set<int> >())
return 1;
if (!boost::container::test::test_set_methods_with_initializer_list_as_argument_for<multiset<int> >())
return 1;
////////////////////////////////////
// Test optimize_size option
////////////////////////////////////
//
// set
//
typedef set< int*, std::less<int*>, std::allocator<int*>
, tree_assoc_options< optimize_size<false>, tree_type<red_black_tree> >::type > rbset_size_optimized_no;
typedef set< int*, std::less<int*>, std::allocator<int*>
, tree_assoc_options< optimize_size<true>, tree_type<avl_tree> >::type > avlset_size_optimized_yes;
//
// multiset
//
typedef multiset< int*, std::less<int*>, std::allocator<int*>
, tree_assoc_options< optimize_size<true>, tree_type<red_black_tree> >::type > rbmset_size_optimized_yes;
typedef multiset< int*, std::less<int*>, std::allocator<int*>
, tree_assoc_options< optimize_size<false>, tree_type<avl_tree> >::type > avlmset_size_optimized_no;
BOOST_STATIC_ASSERT(sizeof(rbmset_size_optimized_yes) < sizeof(rbset_size_optimized_no));
BOOST_STATIC_ASSERT(sizeof(avlset_size_optimized_yes) < sizeof(avlmset_size_optimized_no));
////////////////////////////////////
// Iterator testing
////////////////////////////////////
{
typedef boost::container::set<int> cont_int;
cont_int a; a.insert(0); a.insert(1); a.insert(2);
boost::intrusive::test::test_iterator_bidirectional< cont_int >(a);
if(boost::report_errors() != 0) {
return 1;
}
}
{
typedef boost::container::multiset<int> cont_int;
cont_int a; a.insert(0); a.insert(1); a.insert(2);
boost::intrusive::test::test_iterator_bidirectional< cont_int >(a);
if(boost::report_errors() != 0) {
return 1;
}
}
////////////////////////////////////
// Node extraction/insertion testing functions
////////////////////////////////////
if(!node_type_test())
return 1;
////////////////////////////////////
// has_trivial_destructor_after_move testing
////////////////////////////////////
// set, default allocator
{
typedef boost::container::set<int> cont;
typedef boost::container::dtl::tree<int, void, std::less<int>, void, void> tree;
if (boost::has_trivial_destructor_after_move<cont>::value !=
boost::has_trivial_destructor_after_move<tree>::value) {
std::cerr << "has_trivial_destructor_after_move(set, default allocator) test failed" << std::endl;
return 1;
}
}
// set, std::allocator
{
typedef boost::container::set<int, std::less<int>, std::allocator<int> > cont;
typedef boost::container::dtl::tree<int, void, std::less<int>, std::allocator<int>, void> tree;
if (boost::has_trivial_destructor_after_move<cont>::value !=
boost::has_trivial_destructor_after_move<tree>::value) {
std::cerr << "has_trivial_destructor_after_move(set, std::allocator) test failed" << std::endl;
return 1;
}
}
// multiset, default allocator
{
typedef boost::container::multiset<int> cont;
typedef boost::container::dtl::tree<int, void, std::less<int>, void, void> tree;
if (boost::has_trivial_destructor_after_move<cont>::value !=
boost::has_trivial_destructor_after_move<tree>::value) {
std::cerr << "has_trivial_destructor_after_move(multiset, default allocator) test failed" << std::endl;
return 1;
}
}
// multiset, std::allocator
{
typedef boost::container::multiset<int, std::less<int>, std::allocator<int> > cont;
typedef boost::container::dtl::tree<int, void, std::less<int>, std::allocator<int>, void> tree;
if (boost::has_trivial_destructor_after_move<cont>::value !=
boost::has_trivial_destructor_after_move<tree>::value) {
std::cerr << "has_trivial_destructor_after_move(multiset, std::allocator) test failed" << std::endl;
return 1;
}
}
return 0;
}
#include <boost/container/detail/config_end.hpp>
|