Merged branch 'master' of https://github.com/Nemo1369/libcds
[libcds.git] / test / unit / intrusive-set / intrusive_michael_lazy_hp.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-2017
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 "test_intrusive_set_hp.h"
32
33 #include <cds/intrusive/lazy_list_hp.h>
34 #include <cds/intrusive/michael_set.h>
35
36 #include <mutex>
37
38 namespace {
39     namespace ci = cds::intrusive;
40     typedef cds::gc::HP gc_type;
41
42     class IntrusiveMichaelLazySet_HP : public cds_test::intrusive_set_hp
43     {
44     protected:
45         typedef cds_test::intrusive_set_hp base_class;
46
47     protected:
48         typedef typename base_class::base_int_item< ci::lazy_list::node<gc_type>>   base_item_type;
49         typedef typename base_class::base_int_item< ci::lazy_list::node<gc_type, std::mutex>>   base_mutex_item_type;
50         typedef typename base_class::member_int_item< ci::lazy_list::node<gc_type>> member_item_type;
51         typedef typename base_class::member_int_item< ci::lazy_list::node<gc_type, std::mutex>> member_mutex_item_type;
52
53         void SetUp()
54         {
55             struct list_traits : public ci::lazy_list::traits
56             {
57                 typedef ci::lazy_list::base_hook< ci::opt::gc<gc_type>> hook;
58             };
59             typedef ci::LazyList< gc_type, base_item_type, list_traits > list_type;
60             typedef ci::MichaelHashSet< gc_type, list_type >   set_type;
61
62             // +1 - for guarded_ptr
63             cds::gc::hp::GarbageCollector::Construct( set_type::c_nHazardPtrCount + 1, 1, 16 );
64             cds::threading::Manager::attachThread();
65         }
66
67         void TearDown()
68         {
69             cds::threading::Manager::detachThread();
70             cds::gc::hp::GarbageCollector::Destruct( true );
71         }
72     };
73
74
75     TEST_F( IntrusiveMichaelLazySet_HP, base_cmp )
76     {
77         typedef ci::LazyList< gc_type
78             , base_item_type
79             ,ci::lazy_list::make_traits<
80                 ci::opt::hook< ci::lazy_list::base_hook< ci::opt::gc< gc_type > > >
81                 ,ci::opt::compare< cmp<base_item_type> >
82                 ,ci::opt::disposer< mock_disposer >
83                 ,ci::opt::back_off< cds::backoff::pause >
84             >::type
85         > bucket_type;
86
87         typedef ci::MichaelHashSet< gc_type, bucket_type,
88             ci::michael_set::make_traits<
89                 ci::opt::hash< hash_int >
90             >::type
91         > set_type;
92
93         set_type s( kSize, 2 );
94         test( s );
95     }
96
97     TEST_F( IntrusiveMichaelLazySet_HP, base_less )
98     {
99         typedef ci::LazyList< gc_type
100             , base_item_type
101             ,ci::lazy_list::make_traits<
102                 ci::opt::hook< ci::lazy_list::base_hook< ci::opt::gc< gc_type >>>
103                 ,ci::opt::less< less<base_item_type> >
104                 ,ci::opt::disposer< mock_disposer >
105             >::type
106         > bucket_type;
107
108         typedef ci::MichaelHashSet< gc_type, bucket_type,
109             ci::michael_set::make_traits<
110                 ci::opt::hash< hash_int >
111             >::type
112         > set_type;
113
114         set_type s( kSize, 2 );
115         test( s );
116     }
117
118     TEST_F( IntrusiveMichaelLazySet_HP, base_cmpmix )
119     {
120         struct list_traits : public ci::lazy_list::traits
121         {
122             typedef ci::lazy_list::base_hook< ci::opt::gc<gc_type>> hook;
123             typedef base_class::less<base_item_type> less;
124             typedef cmp<base_item_type> compare;
125             typedef mock_disposer disposer;
126         };
127         typedef ci::LazyList< gc_type, base_item_type, list_traits > bucket_type;
128
129         struct set_traits : public ci::michael_set::traits
130         {
131             typedef hash_int hash;
132             typedef simple_item_counter item_counter;
133         };
134         typedef ci::MichaelHashSet< gc_type, bucket_type, set_traits > set_type;
135
136         set_type s( kSize, 2 );
137         test( s );
138     }
139
140     TEST_F( IntrusiveMichaelLazySet_HP, base_mutex )
141     {
142         struct list_traits : public ci::lazy_list::traits
143         {
144             typedef ci::lazy_list::base_hook< ci::opt::gc<gc_type>, ci::opt::lock_type<std::mutex>> hook;
145             typedef base_class::less<base_mutex_item_type> less;
146             typedef cmp<base_mutex_item_type> compare;
147             typedef mock_disposer disposer;
148         };
149         typedef ci::LazyList< gc_type, base_mutex_item_type, list_traits > bucket_type;
150
151         struct set_traits : public ci::michael_set::traits
152         {
153             typedef hash_int hash;
154             typedef simple_item_counter item_counter;
155         };
156         typedef ci::MichaelHashSet< gc_type, bucket_type, set_traits > set_type;
157
158         set_type s( kSize, 2 );
159         test( s );
160     }
161
162     TEST_F( IntrusiveMichaelLazySet_HP, base_stat )
163     {
164         struct list_traits: public ci::lazy_list::traits
165         {
166             typedef ci::lazy_list::base_hook< ci::opt::gc<gc_type>, ci::opt::lock_type<std::mutex>> hook;
167             typedef cmp<base_mutex_item_type> compare;
168             typedef mock_disposer disposer;
169             typedef ci::lazy_list::stat<> stat;
170         };
171         typedef ci::LazyList< gc_type, base_mutex_item_type, list_traits > bucket_type;
172
173         struct set_traits: public ci::michael_set::traits
174         {
175             typedef hash_int hash;
176             typedef simple_item_counter item_counter;
177         };
178         typedef ci::MichaelHashSet< gc_type, bucket_type, set_traits > set_type;
179
180         set_type s( kSize, 2 );
181         test( s );
182         EXPECT_GE( s.statistics().m_nInsertSuccess, 0u );
183     }
184
185     TEST_F( IntrusiveMichaelLazySet_HP, base_wrapped_stat )
186     {
187         struct list_traits: public ci::lazy_list::traits
188         {
189             typedef ci::lazy_list::base_hook< ci::opt::gc<gc_type>> hook;
190             typedef cmp<base_item_type> compare;
191             typedef mock_disposer disposer;
192             typedef ci::lazy_list::wrapped_stat<> stat;
193         };
194         typedef ci::LazyList< gc_type, base_item_type, list_traits > bucket_type;
195
196         struct set_traits: public ci::michael_set::traits
197         {
198             typedef hash_int hash;
199             typedef simple_item_counter item_counter;
200         };
201         typedef ci::MichaelHashSet< gc_type, bucket_type, set_traits > set_type;
202
203         set_type s( kSize, 2 );
204         test( s );
205         EXPECT_GE( s.statistics().m_nInsertSuccess, 0u );
206     }
207
208
209     TEST_F( IntrusiveMichaelLazySet_HP, member_cmp )
210     {
211         typedef ci::LazyList< gc_type
212             ,member_item_type
213             ,ci::lazy_list::make_traits<
214                 ci::opt::hook< ci::lazy_list::member_hook<
215                     offsetof( member_item_type, hMember ),
216                     ci::opt::gc<gc_type>
217                 > >
218                 ,ci::opt::compare< cmp<member_item_type> >
219                 ,ci::opt::disposer< mock_disposer >
220             >::type
221         >    bucket_type;
222
223         typedef ci::MichaelHashSet< gc_type, bucket_type,
224             ci::michael_set::make_traits<
225                 ci::opt::hash< hash_int >
226             >::type
227         > set_type;
228
229         set_type s( kSize, 2 );
230         test( s );
231     }
232
233     TEST_F( IntrusiveMichaelLazySet_HP, member_less )
234     {
235         typedef ci::LazyList< gc_type
236             , member_item_type
237             ,ci::lazy_list::make_traits<
238                 ci::opt::hook< ci::lazy_list::member_hook<
239                     offsetof( member_item_type, hMember ),
240                     ci::opt::gc<gc_type>
241                 > >
242                 ,ci::opt::less< less<member_item_type> >
243                 ,ci::opt::disposer< mock_disposer >
244             >::type
245         > bucket_type;
246
247         typedef ci::MichaelHashSet< gc_type, bucket_type,
248             ci::michael_set::make_traits<
249                 ci::opt::hash< hash_int >
250             >::type
251         > set_type;
252
253         set_type s( kSize, 2 );
254         test( s );
255     }
256
257     TEST_F( IntrusiveMichaelLazySet_HP, member_cmpmix )
258     {
259         struct list_traits : public ci::lazy_list::traits
260         {
261             typedef ci::lazy_list::member_hook< offsetof( member_item_type, hMember ), ci::opt::gc<gc_type>> hook;
262             typedef base_class::less<member_item_type> less;
263             typedef cmp<member_item_type> compare;
264             typedef mock_disposer disposer;
265         };
266         typedef ci::LazyList< gc_type, member_item_type, list_traits > bucket_type;
267
268         struct set_traits : public ci::michael_set::traits
269         {
270             typedef hash_int hash;
271             typedef simple_item_counter item_counter;
272         };
273         typedef ci::MichaelHashSet< gc_type, bucket_type, set_traits > set_type;
274
275         set_type s( kSize, 2 );
276         test( s );
277     }
278
279     TEST_F( IntrusiveMichaelLazySet_HP, member_mutex )
280     {
281         struct list_traits : public ci::lazy_list::traits
282         {
283             typedef ci::lazy_list::member_hook< offsetof( member_mutex_item_type, hMember ), ci::opt::gc<gc_type>, ci::opt::lock_type<std::mutex>> hook;
284             typedef base_class::less<member_mutex_item_type> less;
285             typedef cmp<member_mutex_item_type> compare;
286             typedef mock_disposer disposer;
287         };
288         typedef ci::LazyList< gc_type, member_mutex_item_type, list_traits > bucket_type;
289
290         struct set_traits : public ci::michael_set::traits
291         {
292             typedef hash_int hash;
293             typedef simple_item_counter item_counter;
294         };
295         typedef ci::MichaelHashSet< gc_type, bucket_type, set_traits > set_type;
296
297         set_type s( kSize, 2 );
298         test( s );
299     }
300
301     TEST_F( IntrusiveMichaelLazySet_HP, member_stat )
302     {
303         struct list_traits: public ci::lazy_list::traits
304         {
305             typedef ci::lazy_list::member_hook< offsetof( member_item_type, hMember ), ci::opt::gc<gc_type>> hook;
306             typedef base_class::less<member_item_type> less;
307             typedef mock_disposer disposer;
308             typedef ci::lazy_list::stat<> stat;
309         };
310         typedef ci::LazyList< gc_type, member_item_type, list_traits > bucket_type;
311
312         struct set_traits: public ci::michael_set::traits
313         {
314             typedef hash_int hash;
315             typedef simple_item_counter item_counter;
316         };
317         typedef ci::MichaelHashSet< gc_type, bucket_type, set_traits > set_type;
318
319         set_type s( kSize, 2 );
320         test( s );
321         EXPECT_GE( s.statistics().m_nInsertSuccess, 0u );
322     }
323
324     TEST_F( IntrusiveMichaelLazySet_HP, member_wrapped_stat )
325     {
326         struct list_traits: public ci::lazy_list::traits
327         {
328             typedef ci::lazy_list::member_hook< offsetof( member_item_type, hMember ), ci::opt::gc<gc_type>> hook;
329             typedef base_class::less<member_item_type> less;
330             typedef mock_disposer disposer;
331             typedef ci::lazy_list::wrapped_stat<> stat;
332         };
333         typedef ci::LazyList< gc_type, member_item_type, list_traits > bucket_type;
334
335         struct set_traits: public ci::michael_set::traits
336         {
337             typedef hash_int hash;
338             typedef simple_item_counter item_counter;
339         };
340         typedef ci::MichaelHashSet< gc_type, bucket_type, set_traits > set_type;
341
342         set_type s( kSize, 2 );
343         test( s );
344         EXPECT_GE( s.statistics().m_nInsertSuccess, 0u );
345     }
346
347 } // namespace