Imported Upstream version 1.57.0
[platform/upstream/boost.git] / libs / intrusive / test / make_functions_test.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 //
3 // (C) Copyright Ion Gaztanaga  2007-2013
4 //
5 // Distributed under the Boost Software License, Version 1.0.
6 //    (See accompanying file LICENSE_1_0.txt or copy at
7 //          http://www.boost.org/LICENSE_1_0.txt)
8 //
9 // See http://www.boost.org/libs/intrusive for documentation.
10 //
11 /////////////////////////////////////////////////////////////////////////////
12 #include <boost/intrusive/list.hpp>
13 #include <boost/intrusive/slist.hpp>
14 #include <boost/intrusive/set.hpp>
15 #include <boost/intrusive/unordered_set.hpp>
16 #include <boost/intrusive/avl_set.hpp>
17 #include <boost/intrusive/sg_set.hpp>
18 #include <boost/intrusive/splay_set.hpp>
19 #include <boost/intrusive/treap_set.hpp>
20 #include <boost/intrusive/detail/mpl.hpp>
21 #include <boost/intrusive/pointer_traits.hpp>
22 #include <boost/static_assert.hpp>
23 #include "smart_ptr.hpp"
24 #include <vector>
25
26 using namespace boost::intrusive;
27
28 struct my_tag;
29 struct my_tag2;
30
31 typedef make_bs_set_base_hook
32    < void_pointer<smart_ptr<void> >, link_mode<normal_link>
33    , tag<my_tag> >::type TreapHook;
34 typedef make_bs_set_base_hook
35    < void_pointer<smart_ptr<void> >, link_mode<normal_link>
36    , tag<my_tag2> >::type SplayHook;
37
38 class MyClass
39 :  public make_list_base_hook
40    < void_pointer<smart_ptr<void> >, link_mode<normal_link> >::type
41 ,  public make_slist_base_hook
42    < void_pointer<smart_ptr<void> >, link_mode<normal_link> >::type
43 ,  public make_set_base_hook
44    < void_pointer<smart_ptr<void> >, link_mode<normal_link> >::type
45 ,  public make_unordered_set_base_hook
46    < void_pointer<smart_ptr<void> >, link_mode<normal_link> >::type
47 ,  public make_avl_set_base_hook
48    < void_pointer<smart_ptr<void> >, link_mode<normal_link> >::type
49 ,  public make_bs_set_base_hook
50    < void_pointer<smart_ptr<void> >, link_mode<normal_link> >::type
51 ,  public TreapHook
52 ,  public SplayHook
53 {
54    int int_;
55
56    public:
57    MyClass(int i)
58       :  int_(i)
59    {}
60
61    friend bool operator<(const MyClass &l, const MyClass &r)
62    {  return l.int_ < r.int_; }
63
64    friend bool operator==(const MyClass &l, const MyClass &r)
65    {  return l.int_ == r.int_; }
66
67    friend std::size_t hash_value(const MyClass &v)
68    {  return boost::hash_value(v.int_); }
69
70    friend bool priority_order(const MyClass &l, const MyClass &r)
71    {  return l.int_ < r.int_; }
72 };
73
74 //Define a list that will store MyClass using the public base hook
75 typedef make_list<MyClass>::type          List;
76 typedef make_slist<MyClass>::type         Slist;
77 typedef make_set<MyClass>::type           Set;
78 typedef make_unordered_set<MyClass>::type USet;
79
80 typedef make_avl_set<MyClass>::type       AvlSet;
81 typedef make_sg_set<MyClass>::type        SgSet;
82 typedef make_treap_set<MyClass
83    , base_hook<TreapHook> >::type         TreapSet;
84 typedef make_splay_set<MyClass
85    , base_hook<SplayHook> >::type         SplaySet;
86
87 int main()
88 {
89    typedef std::vector<MyClass>::iterator VectIt;
90    typedef std::vector<MyClass>::reverse_iterator VectRit;
91
92    //Create several MyClass objects, each one with a different value
93    std::vector<MyClass> values;
94    for(int i = 0; i < 100; ++i)  values.push_back(MyClass(i));
95
96    USet::bucket_type buckets[100];
97
98    List  my_list;
99    Slist my_slist;
100    Set   my_set;
101    USet  my_uset(USet::bucket_traits
102       (pointer_traits<USet::bucket_ptr>::pointer_to(buckets[0]), 100));
103
104    AvlSet      my_avlset;
105    SplaySet    my_splayset;
106    SgSet       my_sgset;
107    TreapSet    my_treapset;
108
109    //Now insert them in containers
110    for(VectIt it(values.begin()), itend(values.end()); it != itend; ++it){
111       my_list.push_front(*it);
112       my_slist.push_front(*it);
113       my_set.insert(*it);
114       my_uset.insert(*it);
115       my_avlset.insert(*it);
116       my_splayset.insert(*it);
117       my_sgset.insert(*it);
118       my_treapset.insert(*it);
119    }
120
121    //Now test lists
122    {
123       List::const_iterator  list_it(my_list.cbegin());
124       Slist::const_iterator slist_it(my_slist.cbegin());
125       Set::const_reverse_iterator set_rit(my_set.crbegin());
126
127       AvlSet::const_reverse_iterator avlset_rit(my_avlset.crbegin());
128       SplaySet::const_reverse_iterator splayset_rit(my_splayset.crbegin());
129       SgSet::const_reverse_iterator sgset_rit(my_sgset.crbegin());
130       TreapSet::const_reverse_iterator treapset_rit(my_treapset.crbegin());
131
132       VectRit vect_it(values.rbegin()), vect_itend(values.rend());
133
134       //Test the objects inserted in the base hook list
135       for( ; vect_it != vect_itend
136          ; ++vect_it, ++list_it, ++slist_it, ++set_rit
137          , ++avlset_rit, ++splayset_rit, ++sgset_rit, ++treapset_rit
138          ){
139          if(&*list_it  != &*vect_it)   return 1;
140          if(&*slist_it != &*vect_it)   return 1;
141          if(&*set_rit  != &*vect_it)   return 1;
142          if(my_uset.find(*set_rit) == my_uset.cend())  return 1;
143          if(&*avlset_rit   != &*vect_it)  return 1;
144          if(&*splayset_rit != &*vect_it)  return 1;
145          if(&*sgset_rit    != &*vect_it)  return 1;
146          if(&*treapset_rit != &*vect_it)  return 1;
147       }
148    }
149
150    //Check defined types and implicitly defined types are equal
151    BOOST_STATIC_ASSERT((detail::is_same<make_list_base_hook<void_pointer<void*>, link_mode<safe_link> >::type
152                      ,make_list_base_hook<>::type
153                      >::value));
154
155    BOOST_STATIC_ASSERT((detail::is_same<make_slist_base_hook<void_pointer<void*>, link_mode<safe_link> >::type
156                      ,make_slist_base_hook<>::type
157                      >::value));
158
159    BOOST_STATIC_ASSERT((detail::is_same<make_set_base_hook<void_pointer<void*>, link_mode<safe_link> >::type
160                      ,make_set_base_hook<>::type
161                      >::value));
162
163    BOOST_STATIC_ASSERT((detail::is_same<make_unordered_set_base_hook<void_pointer<void*>, link_mode<safe_link> >::type
164                      ,make_unordered_set_base_hook<>::type
165                      >::value));
166
167    BOOST_STATIC_ASSERT((detail::is_same<make_avl_set_base_hook<void_pointer<void*>, link_mode<safe_link> >::type
168                      ,make_avl_set_base_hook<>::type
169                      >::value));
170
171    BOOST_STATIC_ASSERT((detail::is_same<make_bs_set_base_hook<void_pointer<void*>, link_mode<safe_link> >::type
172                      ,make_bs_set_base_hook<>::type
173                      >::value));
174
175    //Check defined types and implicitly defined types are unequal
176    BOOST_STATIC_ASSERT(!(detail::is_same<make_list_base_hook<void_pointer<void*>, link_mode<normal_link> >::type
177                      ,make_list_base_hook<>::type
178                      >::value));
179
180    BOOST_STATIC_ASSERT(!(detail::is_same<make_slist_base_hook<void_pointer<void*>, link_mode<normal_link> >::type
181                      ,make_slist_base_hook<>::type
182                      >::value));
183
184    BOOST_STATIC_ASSERT(!(detail::is_same<make_set_base_hook<void_pointer<void*>, link_mode<normal_link> >::type
185                      ,make_set_base_hook<>::type
186                      >::value));
187
188    BOOST_STATIC_ASSERT(!(detail::is_same<make_unordered_set_base_hook<void_pointer<void*>, link_mode<normal_link> >::type
189                      ,make_unordered_set_base_hook<>::type
190                      >::value));
191
192    BOOST_STATIC_ASSERT(!(detail::is_same<make_avl_set_base_hook<void_pointer<void*>, link_mode<normal_link> >::type
193                      ,make_avl_set_base_hook<>::type
194                      >::value));
195
196    BOOST_STATIC_ASSERT(!(detail::is_same<make_bs_set_base_hook<void_pointer<void*>, link_mode<normal_link> >::type
197                      ,make_bs_set_base_hook<>::type
198                      >::value));
199
200    return 0;
201 }