HP and DHP SMR totally refactored
[libcds.git] / test / unit / intrusive-set / intrusive_michael_michael_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-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/michael_list_dhp.h>
34 #include <cds/intrusive/michael_set.h>
35
36 namespace {
37     namespace ci = cds::intrusive;
38     typedef cds::gc::DHP gc_type;
39
40     class IntrusiveMichaelSet_DHP : public cds_test::intrusive_set_hp
41     {
42     protected:
43         typedef cds_test::intrusive_set_hp base_class;
44
45     protected:
46         typedef typename base_class::base_int_item< ci::michael_list::node<gc_type>>   base_item_type;
47         typedef typename base_class::member_int_item< ci::michael_list::node<gc_type>> member_item_type;
48
49         void SetUp()
50         {
51             struct list_traits : public ci::michael_list::traits
52             {
53                 typedef ci::michael_list::base_hook< ci::opt::gc<gc_type>> hook;
54             };
55             typedef ci::MichaelList< gc_type, base_item_type, list_traits > list_type;
56             typedef ci::MichaelHashSet< gc_type, list_type >   set_type;
57
58             cds::gc::dhp::smr::construct( set_type::c_nHazardPtrCount );
59             cds::threading::Manager::attachThread();
60         }
61
62         void TearDown()
63         {
64             cds::threading::Manager::detachThread();
65             cds::gc::dhp::smr::destruct();
66         }
67     };
68
69
70     TEST_F( IntrusiveMichaelSet_DHP, base_cmp )
71     {
72         typedef ci::MichaelList< gc_type
73             , base_item_type
74             ,ci::michael_list::make_traits<
75                 ci::opt::hook< ci::michael_list::base_hook< ci::opt::gc< gc_type >>>
76                 ,ci::opt::compare< cmp<base_item_type> >
77                 ,ci::opt::disposer< mock_disposer >
78             >::type
79         > bucket_type;
80
81         typedef ci::MichaelHashSet< gc_type, bucket_type,
82             ci::michael_set::make_traits<
83                 ci::opt::hash< hash_int >
84             >::type
85         > set_type;
86
87         set_type s( kSize, 2 );
88         test( s );
89     }
90
91     TEST_F( IntrusiveMichaelSet_DHP, base_less )
92     {
93         typedef ci::MichaelList< gc_type
94             , base_item_type
95             ,ci::michael_list::make_traits<
96                 ci::opt::hook< ci::michael_list::base_hook< ci::opt::gc< gc_type >>>
97                 ,ci::opt::less< less<base_item_type> >
98                 ,ci::opt::disposer< mock_disposer >
99             >::type
100         > bucket_type;
101
102         typedef ci::MichaelHashSet< gc_type, bucket_type,
103             ci::michael_set::make_traits<
104                 ci::opt::hash< hash_int >
105             >::type
106         > set_type;
107
108         set_type s( kSize, 2 );
109         test( s );
110     }
111
112     TEST_F( IntrusiveMichaelSet_DHP, base_cmpmix )
113     {
114         struct list_traits : public ci::michael_list::traits
115         {
116             typedef ci::michael_list::base_hook< ci::opt::gc<gc_type>> hook;
117             typedef base_class::less<base_item_type> less;
118             typedef cmp<base_item_type> compare;
119             typedef mock_disposer disposer;
120         };
121         typedef ci::MichaelList< gc_type, base_item_type, list_traits > bucket_type;
122
123         struct set_traits : public ci::michael_set::traits
124         {
125             typedef hash_int hash;
126             typedef simple_item_counter item_counter;
127         };
128         typedef ci::MichaelHashSet< gc_type, bucket_type, set_traits > set_type;
129
130         set_type s( kSize, 2 );
131         test( s );
132     }
133
134     TEST_F( IntrusiveMichaelSet_DHP, base_stat )
135     {
136         struct list_traits: public ci::michael_list::traits
137         {
138             typedef ci::michael_list::base_hook< ci::opt::gc<gc_type>> hook;
139             typedef base_class::less<base_item_type> less;
140             typedef cmp<base_item_type> compare;
141             typedef mock_disposer disposer;
142             typedef ci::michael_list::stat<> stat;
143         };
144         typedef ci::MichaelList< gc_type, base_item_type, list_traits > bucket_type;
145
146         struct set_traits: public ci::michael_set::traits
147         {
148             typedef hash_int hash;
149             typedef simple_item_counter item_counter;
150         };
151         typedef ci::MichaelHashSet< gc_type, bucket_type, set_traits > set_type;
152
153         set_type s( kSize, 2 );
154         test( s );
155         EXPECT_GE( s.statistics().m_nInsertSuccess, 0u );
156     }
157
158     TEST_F( IntrusiveMichaelSet_DHP, base_wrapped_stat )
159     {
160         struct list_traits: public ci::michael_list::traits
161         {
162             typedef ci::michael_list::base_hook< ci::opt::gc<gc_type>> hook;
163             typedef base_class::less<base_item_type> less;
164             typedef cmp<base_item_type> compare;
165             typedef mock_disposer disposer;
166             typedef ci::michael_list::wrapped_stat<> stat;
167         };
168         typedef ci::MichaelList< gc_type, base_item_type, list_traits > bucket_type;
169
170         struct set_traits: public ci::michael_set::traits
171         {
172             typedef hash_int hash;
173             typedef simple_item_counter item_counter;
174         };
175         typedef ci::MichaelHashSet< gc_type, bucket_type, set_traits > set_type;
176
177         set_type s( kSize, 2 );
178         test( s );
179         EXPECT_GE( s.statistics().m_nInsertSuccess, 0u );
180     }
181
182     TEST_F( IntrusiveMichaelSet_DHP, member_cmp )
183     {
184         typedef ci::MichaelList< gc_type
185             ,member_item_type
186             ,ci::michael_list::make_traits<
187                 ci::opt::hook< ci::michael_list::member_hook<
188                     offsetof( member_item_type, hMember ),
189                     ci::opt::gc<gc_type>
190                 > >
191                 ,ci::opt::compare< cmp<member_item_type> >
192                 ,ci::opt::disposer< mock_disposer >
193             >::type
194         >    bucket_type;
195
196         typedef ci::MichaelHashSet< gc_type, bucket_type,
197             ci::michael_set::make_traits<
198                 ci::opt::hash< hash_int >
199             >::type
200         > set_type;
201
202         set_type s( kSize, 2 );
203         test( s );
204     }
205
206     TEST_F( IntrusiveMichaelSet_DHP, member_less )
207     {
208         typedef ci::MichaelList< gc_type
209             , member_item_type
210             ,ci::michael_list::make_traits<
211                 ci::opt::hook< ci::michael_list::member_hook<
212                     offsetof( member_item_type, hMember ),
213                     ci::opt::gc<gc_type>
214                 > >
215                 ,ci::opt::less< less<member_item_type> >
216                 ,ci::opt::disposer< mock_disposer >
217             >::type
218         > bucket_type;
219
220         typedef ci::MichaelHashSet< gc_type, bucket_type,
221             ci::michael_set::make_traits<
222                 ci::opt::hash< hash_int >
223             >::type
224         > set_type;
225
226         set_type s( kSize, 2 );
227         test( s );
228     }
229
230     TEST_F( IntrusiveMichaelSet_DHP, member_cmpmix )
231     {
232         struct list_traits : public ci::michael_list::traits
233         {
234             typedef ci::michael_list::member_hook< offsetof( member_item_type, hMember ), ci::opt::gc<gc_type>> hook;
235             typedef base_class::less<member_item_type> less;
236             typedef cmp<member_item_type> compare;
237             typedef mock_disposer disposer;
238         };
239         typedef ci::MichaelList< gc_type, member_item_type, list_traits > bucket_type;
240
241         struct set_traits : public ci::michael_set::traits
242         {
243             typedef hash_int hash;
244             typedef simple_item_counter item_counter;
245         };
246         typedef ci::MichaelHashSet< gc_type, bucket_type, set_traits > set_type;
247
248         set_type s( kSize, 2 );
249         test( s );
250     }
251
252     TEST_F( IntrusiveMichaelSet_DHP, member_stat )
253     {
254         struct list_traits: public ci::michael_list::traits
255         {
256             typedef ci::michael_list::member_hook< offsetof( member_item_type, hMember ), ci::opt::gc<gc_type>> hook;
257             typedef base_class::less<member_item_type> less;
258             typedef cmp<member_item_type> compare;
259             typedef mock_disposer disposer;
260             typedef ci::michael_list::stat<> stat;
261         };
262         typedef ci::MichaelList< 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         EXPECT_GE( s.statistics().m_nInsertSuccess, 0u );
274     }
275
276     TEST_F( IntrusiveMichaelSet_DHP, member_wrapped_stat )
277     {
278         struct list_traits: public ci::michael_list::traits
279         {
280             typedef ci::michael_list::member_hook< offsetof( member_item_type, hMember ), ci::opt::gc<gc_type>> hook;
281             typedef base_class::less<member_item_type> less;
282             typedef cmp<member_item_type> compare;
283             typedef mock_disposer disposer;
284             typedef ci::michael_list::wrapped_stat<> stat;
285         };
286         typedef ci::MichaelList< gc_type, member_item_type, list_traits > bucket_type;
287
288         struct set_traits: public ci::michael_set::traits
289         {
290             typedef hash_int hash;
291             typedef simple_item_counter item_counter;
292         };
293         typedef ci::MichaelHashSet< gc_type, bucket_type, set_traits > set_type;
294
295         set_type s( kSize, 2 );
296         test( s );
297         EXPECT_GE( s.statistics().m_nInsertSuccess, 0u );
298     }
299
300 } // namespace