bddb5d4cf6621acb8a92e4609626648529365154
[libcds.git] / test / unit / list / intrusive_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-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_list_hp.h"
32 #include <cds/intrusive/lazy_list_hp.h>
33
34 namespace {
35     namespace ci = cds::intrusive;
36     typedef cds::gc::HP gc_type;
37
38     class IntrusiveLazyList_HP : 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             // +1 - for guarded_ptr
57             cds::gc::hp::GarbageCollector::Construct( list_type::c_nHazardPtrCount + 1, 1, 16 );
58             cds::threading::Manager::attachThread();
59         }
60
61         void TearDown()
62         {
63             cds::threading::Manager::detachThread();
64             cds::gc::hp::GarbageCollector::Destruct( true );
65         }
66     };
67
68     TEST_F( IntrusiveLazyList_HP, base_hook )
69     {
70         typedef ci::LazyList< gc_type, base_item,
71             typename ci::lazy_list::make_traits< 
72                 ci::opt::hook< ci::lazy_list::base_hook< cds::opt::gc< gc_type >>>
73                 ,ci::opt::disposer< mock_disposer >
74                 ,cds::opt::less< less< base_item >>
75             >::type 
76        > list_type;
77
78        list_type l;
79        test_common( l );
80        test_ordered_iterator( l );
81        test_hp( l );
82     }
83
84     TEST_F( IntrusiveLazyList_HP, base_hook_cmp )
85     {
86         typedef ci::LazyList< gc_type, base_item,
87             typename ci::lazy_list::make_traits<
88                 ci::opt::hook< ci::lazy_list::base_hook< cds::opt::gc< gc_type >>>
89                 , ci::opt::disposer< mock_disposer >
90                 , cds::opt::compare< cmp< base_item >>
91             >::type
92         > list_type;
93
94         list_type l;
95         test_common( l );
96         test_ordered_iterator( l );
97         test_hp( l );
98     }
99
100     TEST_F( IntrusiveLazyList_HP, base_hook_item_counting )
101     {
102         struct traits : public ci::lazy_list::traits {
103             typedef ci::lazy_list::base_hook< cds::opt::gc< gc_type >> hook;
104             typedef mock_disposer disposer;
105             typedef cmp< base_item > compare;
106             typedef intrusive_list_common::less< base_item > less;
107             typedef cds::atomicity::item_counter item_counter;
108         };
109         typedef ci::LazyList< gc_type, base_item, traits > list_type;
110
111         list_type l;
112         test_common( l );
113         test_ordered_iterator( l );
114         test_hp( l );
115     }
116
117     TEST_F( IntrusiveLazyList_HP, base_hook_mutex )
118     {
119         struct traits : public ci::lazy_list::traits {
120             typedef ci::lazy_list::base_hook< cds::opt::gc< gc_type >, cds::opt::lock_type< std::mutex>> hook;
121             typedef mock_disposer disposer;
122             typedef cmp< base_mutex_item > compare;
123             typedef intrusive_list_common::less< base_mutex_item > less;
124             typedef cds::atomicity::item_counter item_counter;
125         };
126         typedef ci::LazyList< gc_type, base_mutex_item, traits > list_type;
127
128         list_type l;
129         test_common( l );
130         test_ordered_iterator( l );
131         test_hp( l );
132     }
133
134     TEST_F( IntrusiveLazyList_HP, base_hook_backoff )
135     {
136         struct traits : public ci::lazy_list::traits {
137             typedef ci::lazy_list::base_hook< cds::opt::gc< gc_type >> hook;
138             typedef mock_disposer disposer;
139             typedef cmp< base_item > compare;
140             typedef intrusive_list_common::less< base_item > less;
141             typedef cds::atomicity::item_counter item_counter;
142             typedef cds::backoff::pause back_off;
143         };
144         typedef ci::LazyList< gc_type, base_item, traits > list_type;
145
146         list_type l;
147         test_common( l );
148         test_ordered_iterator( l );
149         test_hp( l );
150     }
151
152     TEST_F( IntrusiveLazyList_HP, base_hook_seqcst )
153     {
154         struct traits : public ci::lazy_list::traits {
155             typedef ci::lazy_list::base_hook< cds::opt::gc< gc_type >> hook;
156             typedef mock_disposer disposer;
157             typedef cmp< base_item > compare;
158             typedef intrusive_list_common::less< base_item > less;
159             typedef cds::atomicity::item_counter item_counter;
160             typedef cds::opt::v::sequential_consistent memory_model;
161         };
162         typedef ci::LazyList< gc_type, base_item, traits > list_type;
163
164         list_type l;
165         test_common( l );
166         test_ordered_iterator( l );
167         test_hp( l );
168     }
169
170     TEST_F( IntrusiveLazyList_HP, member_hook )
171     {
172         typedef ci::LazyList< gc_type, member_item,
173             typename ci::lazy_list::make_traits< 
174                 ci::opt::hook< ci::lazy_list::member_hook< offsetof( member_item, hMember ), cds::opt::gc< gc_type >>>
175                 ,ci::opt::disposer< mock_disposer >
176                 ,cds::opt::less< less< member_item >>
177             >::type 
178        > list_type;
179
180        list_type l;
181        test_common( l );
182        test_ordered_iterator( l );
183        test_hp( l );
184     }
185
186     TEST_F( IntrusiveLazyList_HP, member_hook_cmp )
187     {
188         typedef ci::LazyList< gc_type, member_item,
189             typename ci::lazy_list::make_traits<
190                 ci::opt::hook< ci::lazy_list::member_hook< offsetof( member_item, hMember ), cds::opt::gc< gc_type >>>
191                 ,ci::opt::disposer< mock_disposer >
192                 ,cds::opt::compare< cmp< member_item >>
193             >::type
194         > list_type;
195
196         list_type l;
197         test_common( l );
198         test_ordered_iterator( l );
199         test_hp( l );
200     }
201
202     TEST_F( IntrusiveLazyList_HP, member_hook_item_counting )
203     {
204         struct traits : public ci::lazy_list::traits {
205             typedef ci::lazy_list::member_hook< offsetof( member_item, hMember ), cds::opt::gc< gc_type >> hook;
206             typedef mock_disposer disposer;
207             typedef cmp< member_item > compare;
208             typedef intrusive_list_common::less< member_item > less;
209             typedef cds::atomicity::item_counter item_counter;
210         };
211         typedef ci::LazyList< gc_type, member_item, traits > list_type;
212
213         list_type l;
214         test_common( l );
215         test_ordered_iterator( l );
216         test_hp( l );
217     }
218
219     TEST_F( IntrusiveLazyList_HP, member_hook_seqcst )
220     {
221         struct traits : public ci::lazy_list::traits {
222             typedef ci::lazy_list::member_hook< offsetof( member_item, hMember ), cds::opt::gc< gc_type >> hook;
223             typedef mock_disposer disposer;
224             typedef cmp< member_item > compare;
225             typedef intrusive_list_common::less< member_item > less;
226             typedef cds::atomicity::item_counter item_counter;
227             typedef cds::opt::v::sequential_consistent memory_model;
228         };
229         typedef ci::LazyList< gc_type, member_item, traits > 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_HP, member_hook_mutex )
238     {
239         struct traits : public ci::lazy_list::traits {
240             typedef ci::lazy_list::member_hook< offsetof( member_mutex_item, hMember ), cds::opt::gc< gc_type >, cds::opt::lock_type< std::mutex >> hook;
241             typedef mock_disposer disposer;
242             typedef cmp< member_mutex_item > compare;
243             typedef intrusive_list_common::less< member_mutex_item > less;
244             typedef cds::atomicity::item_counter item_counter;
245             typedef cds::opt::v::sequential_consistent memory_model;
246         };
247         typedef ci::LazyList< gc_type, member_mutex_item, traits > list_type;
248
249         list_type l;
250         test_common( l );
251         test_ordered_iterator( l );
252         test_hp( l );
253     }
254
255     TEST_F( IntrusiveLazyList_HP, member_hook_back_off )
256     {
257         struct traits : public ci::lazy_list::traits {
258             typedef ci::lazy_list::member_hook< offsetof( member_item, hMember ), cds::opt::gc< gc_type >> hook;
259             typedef mock_disposer disposer;
260             typedef cmp< member_item > compare;
261             typedef intrusive_list_common::less< member_item > less;
262             typedef cds::atomicity::item_counter item_counter;
263             typedef cds::backoff::empty back_off;
264         };
265         typedef ci::LazyList< gc_type, member_item, traits > list_type;
266
267         list_type l;
268         test_common( l );
269         test_ordered_iterator( l );
270         test_hp( l );
271     }
272
273 } // namespace