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