Imported Upstream version 1.51.0
[platform/upstream/boost.git] / libs / container / test / tree_test.cpp
1 //////////////////////////////////////////////////////////////////////////////
2 //
3 // (C) Copyright Ion Gaztanaga 2004-2012. Distributed under the Boost
4 // Software License, Version 1.0. (See accompanying file
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //
7 // See http://www.boost.org/libs/container for documentation.
8 //
9 //////////////////////////////////////////////////////////////////////////////
10 #include <boost/container/detail/config_begin.hpp>
11 #include <set>
12 #include <boost/container/set.hpp>
13 #include <boost/container/map.hpp>
14 #include "print_container.hpp"
15 #include "movable_int.hpp"
16 #include "dummy_test_allocator.hpp"
17 #include "set_test.hpp"
18 #include "map_test.hpp"
19 #include "propagate_allocator_test.hpp"
20 #include "emplace_test.hpp"
21
22 using namespace boost::container;
23
24 //Alias standard types
25 typedef std::set<int>                                          MyStdSet;
26 typedef std::multiset<int>                                     MyStdMultiSet;
27 typedef std::map<int, int>                                     MyStdMap;
28 typedef std::multimap<int, int>                                MyStdMultiMap;
29
30 //Alias non-movable types
31 typedef set<int>           MyBoostSet;
32 typedef multiset<int>      MyBoostMultiSet;
33 typedef map<int, int>      MyBoostMap;
34 typedef multimap<int, int> MyBoostMultiMap;
35
36 //Alias movable types
37 typedef set<test::movable_int>                           MyMovableBoostSet;
38 typedef multiset<test::movable_int>                      MyMovableBoostMultiSet;
39 typedef map<test::movable_int, test::movable_int>        MyMovableBoostMap;
40 typedef multimap<test::movable_int, test::movable_int>   MyMovableBoostMultiMap;
41 typedef set<test::movable_and_copyable_int>              MyMoveCopyBoostSet;
42 typedef set<test::copyable_int>                          MyCopyBoostSet;
43 typedef multiset<test::movable_and_copyable_int>         MyMoveCopyBoostMultiSet;
44 typedef multiset<test::copyable_int>                     MyCopyBoostMultiSet;
45 typedef map<test::movable_and_copyable_int
46            ,test::movable_and_copyable_int>              MyMoveCopyBoostMap;
47 typedef multimap<test::movable_and_copyable_int
48                 ,test::movable_and_copyable_int>         MyMoveCopyBoostMultiMap;
49 typedef map<test::copyable_int
50            ,test::copyable_int>                          MyCopyBoostMap;
51 typedef multimap<test::copyable_int
52                 ,test::copyable_int>                     MyCopyBoostMultiMap;
53
54 namespace boost {
55 namespace container {
56
57 //Explicit instantiation to detect compilation errors
58
59 //map
60 template class map
61    < test::movable_and_copyable_int
62    , test::movable_and_copyable_int
63    , std::less<test::movable_and_copyable_int>
64    , test::dummy_test_allocator
65       < std::pair<const test::movable_and_copyable_int, test::movable_and_copyable_int> >
66    >;
67
68 template class map
69    < test::movable_and_copyable_int
70    , test::movable_and_copyable_int
71    , std::less<test::movable_and_copyable_int>
72    , test::simple_allocator
73       < std::pair<const test::movable_and_copyable_int, test::movable_and_copyable_int> >
74    >;
75
76 template class map
77    < test::movable_and_copyable_int
78    , test::movable_and_copyable_int
79    , std::less<test::movable_and_copyable_int>
80    , std::allocator
81       < std::pair<const test::movable_and_copyable_int, test::movable_and_copyable_int> >
82    >;
83
84 //multimap
85 template class multimap
86    < test::movable_and_copyable_int
87    , test::movable_and_copyable_int
88    , std::less<test::movable_and_copyable_int>
89    , test::dummy_test_allocator
90       < std::pair<const test::movable_and_copyable_int, test::movable_and_copyable_int> >
91    >;
92
93 template class multimap
94    < test::movable_and_copyable_int
95    , test::movable_and_copyable_int
96    , std::less<test::movable_and_copyable_int>
97    , test::simple_allocator
98       < std::pair<const test::movable_and_copyable_int, test::movable_and_copyable_int> >
99    >;
100
101 template class multimap
102    < test::movable_and_copyable_int
103    , test::movable_and_copyable_int
104    , std::less<test::movable_and_copyable_int>
105    , std::allocator
106       < std::pair<const test::movable_and_copyable_int, test::movable_and_copyable_int> >
107    >;
108
109 //set
110 template class set
111    < test::movable_and_copyable_int
112    , std::less<test::movable_and_copyable_int>
113    , test::dummy_test_allocator<test::movable_and_copyable_int>
114    >;
115
116 template class set
117    < test::movable_and_copyable_int
118    , std::less<test::movable_and_copyable_int>
119    , test::simple_allocator<test::movable_and_copyable_int>
120    >;
121
122 template class set
123    < test::movable_and_copyable_int
124    , std::less<test::movable_and_copyable_int>
125    , std::allocator<test::movable_and_copyable_int>
126    >;
127
128 //multiset
129 template class multiset
130    < test::movable_and_copyable_int
131    , std::less<test::movable_and_copyable_int>
132    , test::dummy_test_allocator<test::movable_and_copyable_int>
133    >;
134
135 template class multiset
136    < test::movable_and_copyable_int
137    , std::less<test::movable_and_copyable_int>
138    , test::simple_allocator<test::movable_and_copyable_int>
139    >;
140
141 template class multiset
142    < test::movable_and_copyable_int
143    , std::less<test::movable_and_copyable_int>
144    , std::allocator<test::movable_and_copyable_int>
145    >;
146
147 }} //boost::container
148
149 //Test recursive structures
150 class recursive_set
151 {
152 public:
153    recursive_set & operator=(const recursive_set &x)
154    {  id_ = x.id_;  set_ = x.set_; return *this; }
155
156    int id_;
157    set<recursive_set> set_;
158    friend bool operator< (const recursive_set &a, const recursive_set &b)
159    {  return a.id_ < b.id_;   }
160 };
161
162 class recursive_map
163 {
164    public:
165    recursive_map & operator=(const recursive_map &x)
166    {  id_ = x.id_;  map_ = x.map_; return *this;  }
167
168    int id_;
169    map<recursive_map, recursive_map> map_;
170    friend bool operator< (const recursive_map &a, const recursive_map &b)
171    {  return a.id_ < b.id_;   }
172 };
173
174 //Test recursive structures
175 class recursive_multiset
176 {
177    public:
178    recursive_multiset & operator=(const recursive_multiset &x)
179    {  id_ = x.id_;  multiset_ = x.multiset_; return *this;  }
180
181    int id_;
182    multiset<recursive_multiset> multiset_;
183    friend bool operator< (const recursive_multiset &a, const recursive_multiset &b)
184    {  return a.id_ < b.id_;   }
185 };
186
187 class recursive_multimap
188 {
189    public:
190    recursive_multimap & operator=(const recursive_multimap &x)
191    {  id_ = x.id_;  multimap_ = x.multimap_; return *this;  }
192
193    int id_;
194    multimap<recursive_multimap, recursive_multimap> multimap_;
195    friend bool operator< (const recursive_multimap &a, const recursive_multimap &b)
196    {  return a.id_ < b.id_;   }
197 };
198
199 template<class C>
200 void test_move()
201 {
202    //Now test move semantics
203    C original;
204    original.emplace();
205    C move_ctor(boost::move(original));
206    C move_assign;
207    move_assign.emplace();
208    move_assign = boost::move(move_ctor);
209    move_assign.swap(original);
210 }
211
212 template<class T, class A>
213 class tree_propagate_test_wrapper
214    : public container_detail::rbtree<T, T, container_detail::identity<T>, std::less<T>, A>
215 {
216    BOOST_COPYABLE_AND_MOVABLE(tree_propagate_test_wrapper)
217    typedef container_detail::rbtree<T, T, container_detail::identity<T>, std::less<T>, A> Base;
218    public:
219    tree_propagate_test_wrapper()
220       : Base()
221    {}
222
223    tree_propagate_test_wrapper(const tree_propagate_test_wrapper &x)
224       : Base(x)
225    {}
226
227    tree_propagate_test_wrapper(BOOST_RV_REF(tree_propagate_test_wrapper) x)
228       : Base(boost::move(static_cast<Base&>(x)))
229    {}
230
231    tree_propagate_test_wrapper &operator=(BOOST_COPY_ASSIGN_REF(tree_propagate_test_wrapper) x)
232    {  this->Base::operator=(x);  return *this; }
233
234    tree_propagate_test_wrapper &operator=(BOOST_RV_REF(tree_propagate_test_wrapper) x)
235    {  this->Base::operator=(boost::move(static_cast<Base&>(x)));  return *this; }
236
237    void swap(tree_propagate_test_wrapper &x)
238    {  this->Base::swap(x);  }
239 };
240
241 int main ()
242 {
243    //Recursive container instantiation
244    {
245       set<recursive_set> set_;
246       multiset<recursive_multiset> multiset_;
247       map<recursive_map, recursive_map> map_;
248       multimap<recursive_multimap, recursive_multimap> multimap_;
249    }
250    //Now test move semantics
251    {
252       test_move<set<recursive_set> >();
253       test_move<multiset<recursive_multiset> >();
254       test_move<map<recursive_map, recursive_map> >();
255       test_move<multimap<recursive_multimap, recursive_multimap> >();
256    }
257
258    //using namespace boost::container::detail;
259
260    if(0 != test::set_test<MyBoostSet
261                         ,MyStdSet
262                         ,MyBoostMultiSet
263                         ,MyStdMultiSet>()){
264       return 1;
265    }
266
267    if(0 != test::set_test_copyable<MyBoostSet
268                         ,MyStdSet
269                         ,MyBoostMultiSet
270                         ,MyStdMultiSet>()){
271       return 1;
272    }
273
274    if(0 != test::set_test<MyMovableBoostSet
275                         ,MyStdSet
276                         ,MyMovableBoostMultiSet
277                         ,MyStdMultiSet>()){
278       return 1;
279    }
280
281    if(0 != test::set_test<MyMoveCopyBoostSet
282                         ,MyStdSet
283                         ,MyMoveCopyBoostMultiSet
284                         ,MyStdMultiSet>()){
285       return 1;
286    }
287
288    if(0 != test::set_test_copyable<MyMoveCopyBoostSet
289                         ,MyStdSet
290                         ,MyMoveCopyBoostMultiSet
291                         ,MyStdMultiSet>()){
292       return 1;
293    }
294
295    if(0 != test::set_test<MyCopyBoostSet
296                         ,MyStdSet
297                         ,MyCopyBoostMultiSet
298                         ,MyStdMultiSet>()){
299       return 1;
300    }
301
302    if(0 != test::set_test_copyable<MyCopyBoostSet
303                         ,MyStdSet
304                         ,MyCopyBoostMultiSet
305                         ,MyStdMultiSet>()){
306       return 1;
307    }
308
309    if (0 != test::map_test<MyBoostMap
310                   ,MyStdMap
311                   ,MyBoostMultiMap
312                   ,MyStdMultiMap>()){
313       return 1;
314    }
315
316    if(0 != test::map_test_copyable<MyBoostMap
317                         ,MyStdMap
318                         ,MyBoostMultiMap
319                         ,MyStdMultiMap>()){
320       return 1;
321    }
322
323    if (0 != test::map_test<MyMovableBoostMap
324                   ,MyStdMap
325                   ,MyMovableBoostMultiMap
326                   ,MyStdMultiMap>()){
327       return 1;
328    }
329
330    if (0 != test::map_test<MyMoveCopyBoostMap
331                   ,MyStdMap
332                   ,MyMoveCopyBoostMultiMap
333                   ,MyStdMultiMap>()){
334       return 1;
335    }
336
337    if (0 != test::map_test_copyable<MyMoveCopyBoostMap
338                   ,MyStdMap
339                   ,MyMoveCopyBoostMultiMap
340                   ,MyStdMultiMap>()){
341       return 1;
342    }
343
344    if (0 != test::map_test<MyCopyBoostMap
345                   ,MyStdMap
346                   ,MyCopyBoostMultiMap
347                   ,MyStdMultiMap>()){
348       return 1;
349    }
350
351    if (0 != test::map_test_copyable<MyCopyBoostMap
352                   ,MyStdMap
353                   ,MyCopyBoostMultiMap
354                   ,MyStdMultiMap>()){
355       return 1;
356    }
357
358    const test::EmplaceOptions SetOptions = (test::EmplaceOptions)(test::EMPLACE_HINT | test::EMPLACE_ASSOC);
359    if(!boost::container::test::test_emplace<set<test::EmplaceInt>, SetOptions>())
360       return 1;
361    if(!boost::container::test::test_emplace<multiset<test::EmplaceInt>, SetOptions>())
362       return 1;
363    const test::EmplaceOptions MapOptions = (test::EmplaceOptions)(test::EMPLACE_HINT_PAIR | test::EMPLACE_ASSOC_PAIR);
364    if(!boost::container::test::test_emplace<map<test::EmplaceInt, test::EmplaceInt>, MapOptions>())
365       return 1;
366    if(!boost::container::test::test_emplace<multimap<test::EmplaceInt, test::EmplaceInt>, MapOptions>())
367       return 1;
368    if(!boost::container::test::test_propagate_allocator<tree_propagate_test_wrapper>())
369       return 1;
370
371    return 0;
372 }
373
374 #include <boost/container/detail/config_end.hpp>