eadc0232cabd198ca862c080c7aa268029290787
[libcds.git] / test / unit / intrusive-set / intrusive_split_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_set_hp.h"
32
33 #include <cds/intrusive/lazy_list_hp.h>
34 #include <cds/intrusive/split_list.h>
35
36 #include <mutex>
37
38 namespace {
39     namespace ci = cds::intrusive;
40     typedef cds::gc::HP gc_type;
41
42     class IntrusiveSplitListLazySet_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::split_list::node< ci::lazy_list::node<gc_type>>>   base_item_type;
49         typedef typename base_class::base_int_item< ci::split_list::node< ci::lazy_list::node<gc_type, std::mutex >>>   base_mutex_item_type;
50         typedef typename base_class::member_int_item< ci::split_list::node< ci::lazy_list::node<gc_type>>> member_item_type;
51         typedef typename base_class::member_int_item< ci::split_list::node< 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::SplitListSet< 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( IntrusiveSplitListLazySet_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::SplitListSet< gc_type, bucket_type,
88             ci::split_list::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( IntrusiveSplitListLazySet_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::SplitListSet< gc_type, bucket_type,
109             ci::split_list::make_traits<
110                 ci::opt::hash< hash_int >
111                 , ci::opt::item_counter< cds::atomicity::item_counter >
112             >::type
113         > set_type;
114
115         set_type s( kSize, 2 );
116         test( s );
117     }
118
119     TEST_F( IntrusiveSplitListLazySet_HP, base_cmpmix )
120     {
121         struct list_traits : public ci::lazy_list::traits
122         {
123             typedef ci::lazy_list::base_hook< ci::opt::gc<gc_type>> hook;
124             typedef base_class::less<base_item_type> less;
125             typedef cmp<base_item_type> compare;
126             typedef mock_disposer disposer;
127         };
128         typedef ci::LazyList< gc_type, base_item_type, list_traits > bucket_type;
129
130         struct set_traits : public ci::split_list::traits
131         {
132             typedef hash_int hash;
133             typedef simple_item_counter item_counter;
134             typedef ci::split_list::stat<> stat;
135         };
136         typedef ci::SplitListSet< gc_type, bucket_type, set_traits > set_type;
137
138         set_type s( kSize, 2 );
139         test( s );
140     }
141
142     TEST_F( IntrusiveSplitListLazySet_HP, base_mutex )
143     {
144         struct list_traits : public ci::lazy_list::traits
145         {
146             typedef ci::lazy_list::base_hook< ci::opt::gc<gc_type>, ci::opt::lock_type<std::mutex>> hook;
147             typedef base_class::less<base_mutex_item_type> less;
148             typedef cmp<base_mutex_item_type> compare;
149             typedef mock_disposer disposer;
150         };
151         typedef ci::LazyList< gc_type, base_mutex_item_type, list_traits > bucket_type;
152
153         struct set_traits : public ci::split_list::traits
154         {
155             typedef hash_int hash;
156             typedef simple_item_counter item_counter;
157             typedef cds::backoff::empty back_off;
158         };
159         typedef ci::SplitListSet< gc_type, bucket_type, set_traits > set_type;
160
161         set_type s( kSize, 2 );
162         test( s );
163     }
164
165
166     TEST_F( IntrusiveSplitListLazySet_HP, member_cmp )
167     {
168         typedef ci::LazyList< gc_type
169             ,member_item_type
170             ,ci::lazy_list::make_traits<
171                 ci::opt::hook< ci::lazy_list::member_hook<
172                     offsetof( member_item_type, hMember ),
173                     ci::opt::gc<gc_type>
174                 > >
175                 ,ci::opt::compare< cmp<member_item_type> >
176                 ,ci::opt::disposer< mock_disposer >
177             >::type
178         >    bucket_type;
179
180         typedef ci::SplitListSet< gc_type, bucket_type,
181             ci::split_list::make_traits<
182                 ci::opt::hash< hash_int >
183             >::type
184         > set_type;
185
186         set_type s( kSize, 2 );
187         test( s );
188     }
189
190     TEST_F( IntrusiveSplitListLazySet_HP, member_less )
191     {
192         typedef ci::LazyList< gc_type
193             , member_item_type
194             ,ci::lazy_list::make_traits<
195                 ci::opt::hook< ci::lazy_list::member_hook<
196                     offsetof( member_item_type, hMember ),
197                     ci::opt::gc<gc_type>
198                 > >
199                 ,ci::opt::less< less<member_item_type> >
200                 ,ci::opt::disposer< mock_disposer >
201             >::type
202         > bucket_type;
203
204         typedef ci::SplitListSet< gc_type, bucket_type,
205             ci::split_list::make_traits<
206                 ci::opt::hash< hash_int >
207                 , ci::opt::back_off< cds::backoff::pause >
208             >::type
209         > set_type;
210
211         set_type s( kSize, 2 );
212         test( s );
213     }
214
215     TEST_F( IntrusiveSplitListLazySet_HP, member_cmpmix )
216     {
217         struct list_traits : public ci::lazy_list::traits
218         {
219             typedef ci::lazy_list::member_hook< offsetof( member_item_type, hMember ), ci::opt::gc<gc_type>> hook;
220             typedef base_class::less<member_item_type> less;
221             typedef cmp<member_item_type> compare;
222             typedef mock_disposer disposer;
223         };
224         typedef ci::LazyList< gc_type, member_item_type, list_traits > bucket_type;
225
226         struct set_traits : public ci::split_list::traits
227         {
228             typedef hash_int hash;
229             typedef simple_item_counter item_counter;
230         };
231         typedef ci::SplitListSet< gc_type, bucket_type, set_traits > set_type;
232
233         set_type s( kSize, 2 );
234         test( s );
235     }
236
237     TEST_F( IntrusiveSplitListLazySet_HP, member_mutex )
238     {
239         struct list_traits : public ci::lazy_list::traits
240         {
241             typedef ci::lazy_list::member_hook< offsetof( member_mutex_item_type, hMember ), ci::opt::gc<gc_type>, ci::opt::lock_type<std::mutex>> hook;
242             typedef base_class::less<member_mutex_item_type> less;
243             typedef cmp<member_mutex_item_type> compare;
244             typedef mock_disposer disposer;
245         };
246         typedef ci::LazyList< gc_type, member_mutex_item_type, list_traits > bucket_type;
247
248         struct set_traits : public ci::split_list::traits
249         {
250             typedef hash_int hash;
251             typedef simple_item_counter item_counter;
252         };
253         typedef ci::SplitListSet< gc_type, bucket_type, set_traits > set_type;
254
255         set_type s( kSize, 2 );
256         test( s );
257     }
258
259 } // namespace