5cfaf2ab2bab4f228d612108cc22b8a913278185
[libcds.git] / tests / test-hdr / set / hdr_intrusive_refinable_hashset_uset.cpp
1 /*
2     This file is a part of libcds - Concurrent Data Structures library
3
4     (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2016
5
6     Source code repo: http://github.com/khizmax/libcds/
7     Download: http://sourceforge.net/projects/libcds/files/
8     
9     Redistribution and use in source and binary forms, with or without
10     modification, are permitted provided that the following conditions are met:
11
12     * Redistributions of source code must retain the above copyright notice, this
13       list of conditions and the following disclaimer.
14
15     * Redistributions in binary form must reproduce the above copyright notice,
16       this list of conditions and the following disclaimer in the documentation
17       and/or other materials provided with the distribution.
18
19     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20     AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21     IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23     FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24     DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25     SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26     CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27     OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28     OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.     
29 */
30
31 #include "set/hdr_intrusive_striped_set.h"
32 #include <cds/intrusive/striped_set/boost_unordered_set.h>
33 #include <cds/intrusive/striped_set.h>
34
35 namespace set {
36     namespace bi = boost::intrusive;
37
38     namespace {
39         typedef IntrusiveStripedSetHdrTest::base_item< bi::unordered_set_base_hook<> > base_item_type;
40         typedef IntrusiveStripedSetHdrTest::member_item< bi::unordered_set_member_hook<> > member_item_type;
41
42         struct hasher: private IntrusiveStripedSetHdrTest::hash_int
43         {
44             typedef IntrusiveStripedSetHdrTest::hash_int base_class;
45
46             size_t operator()( int i ) const
47             {
48                 return ~( base_class::operator()(i));
49             }
50             template <typename Item>
51             size_t operator()( const Item& i ) const
52             {
53                 return ~( base_class::operator()(i));
54             }
55             size_t operator()( IntrusiveStripedSetHdrTest::find_key const& i) const
56             {
57                 return ~( base_class::operator()(i));
58             }
59         };
60
61         template <typename T>
62         struct is_equal: private IntrusiveStripedSetHdrTest::cmp<T>
63         {
64             typedef IntrusiveStripedSetHdrTest::cmp<T> base_class;
65
66             bool operator ()(const T& v1, const T& v2 ) const
67             {
68                 return base_class::operator()( v1, v2 ) == 0;
69             }
70
71             template <typename Q>
72             bool operator ()(const T& v1, const Q& v2 ) const
73             {
74                 return base_class::operator()( v1, v2 ) == 0;
75             }
76
77             template <typename Q>
78             bool operator ()(const Q& v1, const T& v2 ) const
79             {
80                 return base_class::operator()( v1, v2 ) == 0;
81             }
82         };
83
84         template <size_t Capacity, typename T, class Alloc = CDS_DEFAULT_ALLOCATOR>
85         struct dyn_buffer: public co::v::dynamic_buffer< T, Alloc >
86         {
87             typedef co::v::dynamic_buffer< T, Alloc >   base_class;
88         public:
89             template <typename Q>
90             struct rebind {
91                 typedef dyn_buffer<Capacity, Q, Alloc> other   ;  ///< Rebinding result type
92             };
93
94             dyn_buffer()
95                 : base_class( Capacity )
96             {}
97         };
98     }
99
100     void IntrusiveStripedSetHdrTest::Refinable_unordered_set_basehook()
101     {
102         typedef ci::StripedSet<
103             bi::unordered_set<base_item_type
104                 , bi::hash< hasher >
105                 , bi::equal< is_equal<base_item_type> >
106                 , bi::power_2_buckets<true>
107                 , bi::incremental<true>
108             >
109             ,co::hash< IntrusiveStripedSetHdrTest::hash_int >
110             ,co::mutex_policy< ci::striped_set::refinable<> >
111         > set_type;
112
113         test<set_type>();
114     }
115
116     void IntrusiveStripedSetHdrTest::Refinable_unordered_set_basehook_bucket_threshold()
117     {
118         typedef ci::StripedSet<
119             bi::unordered_set<base_item_type
120                 , bi::hash< hasher >
121                 , bi::equal< is_equal<base_item_type> >
122                 , bi::power_2_buckets<true>
123                 , bi::incremental<true>
124             >
125             ,co::mutex_policy< ci::striped_set::refinable<> >
126             ,co::hash< IntrusiveStripedSetHdrTest::hash_int >
127             ,co::buffer< co::v::static_buffer< cds::any_type, 64 > >
128             ,co::resizing_policy< ci::striped_set::single_bucket_size_threshold<256> >
129         > set_type;
130
131         test<set_type>();
132     }
133
134     void IntrusiveStripedSetHdrTest::Refinable_unordered_set_basehook_bucket_threshold_rt()
135     {
136         typedef ci::StripedSet<
137             bi::unordered_set<base_item_type
138                 , bi::hash< hasher >
139                 , bi::equal< is_equal<base_item_type> >
140                 , bi::power_2_buckets<true>
141                 , bi::incremental<true>
142             >
143             ,co::mutex_policy< ci::striped_set::refinable<> >
144             ,co::hash< IntrusiveStripedSetHdrTest::hash_int >
145             ,co::buffer< dyn_buffer< 256, cds::any_type, std::allocator<int> > >
146             ,co::resizing_policy< ci::striped_set::single_bucket_size_threshold<0> >
147         > set_type;
148
149         set_type s( 64, ci::striped_set::single_bucket_size_threshold<0>(512) );
150         test_with( s );
151     }
152
153     void IntrusiveStripedSetHdrTest::Refinable_unordered_set_memberhook()
154     {
155         typedef ci::StripedSet<
156             bi::unordered_set<member_item_type
157                 , bi::member_hook< member_item_type, bi::unordered_set_member_hook<>, &member_item_type::hMember>
158                 , bi::hash< hasher >
159                 , bi::equal< is_equal<member_item_type> >
160                 , bi::power_2_buckets<true>
161                 , bi::incremental<true>
162             >
163             ,co::mutex_policy< ci::striped_set::refinable<> >
164             ,co::hash< IntrusiveStripedSetHdrTest::hash_int >
165         > set_type;
166
167         test<set_type>();
168     }
169
170     void IntrusiveStripedSetHdrTest::Refinable_unordered_set_memberhook_bucket_threshold()
171     {
172         typedef ci::StripedSet<
173             bi::unordered_set<member_item_type
174                 , bi::member_hook< member_item_type, bi::unordered_set_member_hook<>, &member_item_type::hMember>
175                 , bi::hash< hasher >
176                 , bi::equal< is_equal<member_item_type> >
177                 , bi::power_2_buckets<true>
178                 , bi::incremental<true>
179             >
180             ,co::hash< IntrusiveStripedSetHdrTest::hash_int >
181             ,co::buffer< dyn_buffer< 64, cds::any_type, std::allocator<int> > >
182             ,co::resizing_policy< ci::striped_set::single_bucket_size_threshold<256> >
183             ,co::mutex_policy< ci::striped_set::refinable<> >
184         > set_type;
185
186         test<set_type>();
187     }
188
189     void IntrusiveStripedSetHdrTest::Refinable_unordered_set_memberhook_bucket_threshold_rt()
190     {
191         typedef ci::StripedSet<
192             bi::unordered_set<member_item_type
193                 , bi::member_hook< member_item_type, bi::unordered_set_member_hook<>, &member_item_type::hMember>
194                 , bi::hash< hasher >
195                 , bi::equal< is_equal<member_item_type> >
196                 , bi::power_2_buckets<true>
197                 , bi::incremental<true>
198             >
199             ,co::mutex_policy< ci::striped_set::refinable<> >
200             ,co::hash< IntrusiveStripedSetHdrTest::hash_int >
201             ,co::buffer< co::v::static_buffer< cds::any_type, 128 > >
202             ,co::resizing_policy< ci::striped_set::single_bucket_size_threshold<0> >
203         > set_type;
204
205         set_type s( 64, ci::striped_set::single_bucket_size_threshold<0>(256) );
206         test_with( s );
207     }
208
209 } // namespace set
210
211