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