Merged branch 'master' of https://github.com/Nemo1369/libcds
[libcds.git] / test / unit / set / split_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_set_hp.h"
32
33 #include <cds/container/lazy_list_dhp.h>
34 #include <cds/container/split_list_set.h>
35 #include <cds/intrusive/free_list.h>
36
37 namespace {
38     namespace cc = cds::container;
39     typedef cds::gc::DHP gc_type;
40
41     class SplitListLazySet_DHP : public cds_test::container_set_hp
42     {
43     protected:
44         typedef cds_test::container_set_hp base_class;
45
46         void SetUp()
47         {
48             struct set_traits: public cc::split_list::traits {
49                 typedef cc::lazy_list_tag ordered_list;
50                 typedef hash_int hash;
51                 struct ordered_list_traits: public cc::lazy_list::traits
52                 {
53                     typedef cmp compare;
54                 };
55             };
56             typedef cc::SplitListSet< gc_type, int_item, set_traits >   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     TEST_F( SplitListLazySet_DHP, compare )
70     {
71         typedef cc::SplitListSet< gc_type, int_item,
72             typename cc::split_list::make_traits<
73                 cc::split_list::ordered_list< cc::lazy_list_tag >
74                 , cds::opt::hash< hash_int >
75                 , cc::split_list::ordered_list_traits<
76                     typename cc::lazy_list::make_traits<
77                         cds::opt::compare< cmp >
78                     >::type
79                 >
80             >::type
81         > set_type;
82
83         set_type s( kSize, 2 );
84         test( s );
85     }
86
87     TEST_F( SplitListLazySet_DHP, less )
88     {
89         typedef cc::SplitListSet< gc_type, int_item,
90             typename cc::split_list::make_traits<
91                 cc::split_list::ordered_list< cc::lazy_list_tag >
92                 , cds::opt::hash< hash_int >
93                 , cc::split_list::ordered_list_traits<
94                     typename cc::lazy_list::make_traits<
95                         cds::opt::less< less >
96                     >::type
97                 >
98             >::type
99         > set_type;
100
101         set_type s( kSize, 1 );
102         test( s );
103     }
104
105     TEST_F( SplitListLazySet_DHP, cmpmix )
106     {
107         typedef cc::SplitListSet< gc_type, int_item,
108             typename cc::split_list::make_traits<
109                 cc::split_list::ordered_list< cc::lazy_list_tag >
110                 , cds::opt::hash< hash_int >
111                 , cc::split_list::ordered_list_traits<
112                     typename cc::lazy_list::make_traits<
113                         cds::opt::less< less >
114                         , cds::opt::compare< cmp >
115                     >::type
116                 >
117             >::type
118         > set_type;
119
120         set_type s( kSize, 2 );
121         test( s );
122     }
123
124     TEST_F( SplitListLazySet_DHP, item_counting )
125     {
126         struct set_traits: public cc::split_list::traits
127         {
128             typedef cc::lazy_list_tag ordered_list;
129             typedef hash_int hash;
130             typedef cds::atomicity::item_counter item_counter;
131
132             struct ordered_list_traits: public cc::lazy_list::traits
133             {
134                 typedef cmp compare;
135                 typedef base_class::less less;
136                 typedef cds::backoff::empty back_off;
137             };
138         };
139         typedef cc::SplitListSet< gc_type, int_item, set_traits > set_type;
140
141         set_type s( kSize, 4 );
142         test( s );
143     }
144
145     TEST_F( SplitListLazySet_DHP, stat )
146     {
147         struct set_traits: public cc::split_list::traits
148         {
149             typedef cc::lazy_list_tag ordered_list;
150             typedef hash_int hash;
151             typedef cds::atomicity::item_counter item_counter;
152             typedef cc::split_list::stat<> stat;
153
154             struct ordered_list_traits: public cc::lazy_list::traits
155             {
156                 typedef base_class::less less;
157                 typedef cds::opt::v::sequential_consistent memory_model;
158             };
159         };
160         typedef cc::SplitListSet< gc_type, int_item, set_traits > set_type;
161
162         set_type s( kSize, 2 );
163         test( s );
164     }
165
166     TEST_F( SplitListLazySet_DHP, back_off )
167     {
168         struct set_traits: public cc::split_list::traits
169         {
170             typedef cc::lazy_list_tag ordered_list;
171             typedef hash_int hash;
172             typedef cds::atomicity::item_counter item_counter;
173             typedef cds::backoff::yield back_off;
174             typedef cds::opt::v::sequential_consistent memory_model;
175
176             struct ordered_list_traits: public cc::lazy_list::traits
177             {
178                 typedef cmp compare;
179                 typedef cds::backoff::empty back_off;
180             };
181         };
182         typedef cc::SplitListSet< gc_type, int_item, set_traits > set_type;
183
184         set_type s( kSize, 3 );
185         test( s );
186     }
187
188     TEST_F( SplitListLazySet_DHP, mutex )
189     {
190         struct set_traits: public cc::split_list::traits
191         {
192             typedef cc::lazy_list_tag ordered_list;
193             typedef hash_int hash;
194             typedef cds::atomicity::item_counter item_counter;
195             typedef cds::backoff::yield back_off;
196             typedef cds::opt::v::sequential_consistent memory_model;
197
198             struct ordered_list_traits: public cc::lazy_list::traits
199             {
200                 typedef cmp compare;
201                 typedef cds::backoff::pause back_off;
202                 typedef std::mutex lock_type;
203             };
204         };
205         typedef cc::SplitListSet< gc_type, int_item, set_traits > set_type;
206
207         set_type s( kSize, 2 );
208         test( s );
209     }
210
211     struct set_static_traits: public cc::split_list::traits
212     {
213         static bool const dynamic_bucket_table = false;
214     };
215
216     TEST_F( SplitListLazySet_DHP, static_bucket_table )
217     {
218         struct set_traits: public set_static_traits
219         {
220             typedef cc::lazy_list_tag ordered_list;
221             typedef hash_int hash;
222             typedef cds::atomicity::item_counter item_counter;
223
224             struct ordered_list_traits: public cc::lazy_list::traits
225             {
226                 typedef cmp compare;
227                 typedef cds::backoff::pause back_off;
228             };
229         };
230         typedef cc::SplitListSet< gc_type, int_item, set_traits > set_type;
231
232         set_type s( kSize, 4 );
233         test( s );
234     }
235
236     TEST_F( SplitListLazySet_DHP, free_list )
237     {
238         struct set_traits: public cc::split_list::traits
239         {
240             typedef cc::lazy_list_tag ordered_list;
241             typedef hash_int hash;
242             typedef cds::intrusive::FreeList free_list;
243
244             struct ordered_list_traits: public cc::lazy_list::traits
245             {
246                 typedef cmp compare;
247                 typedef base_class::less less;
248                 typedef cds::backoff::empty back_off;
249             };
250         };
251         typedef cc::SplitListSet< gc_type, int_item, set_traits > set_type;
252
253         set_type s( kSize, 4 );
254         test( s );
255     }
256
257     TEST_F( SplitListLazySet_DHP, static_bucket_table_free_list )
258     {
259         struct set_traits: public set_static_traits
260         {
261             typedef cc::lazy_list_tag ordered_list;
262             typedef hash_int hash;
263             typedef cds::atomicity::item_counter item_counter;
264             typedef cds::intrusive::FreeList free_list;
265
266             struct ordered_list_traits: public cc::lazy_list::traits
267             {
268                 typedef cmp compare;
269                 typedef cds::backoff::pause back_off;
270             };
271         };
272         typedef cc::SplitListSet< gc_type, int_item, set_traits > set_type;
273
274         set_type s( kSize, 4 );
275         test( s );
276     }
277
278 } // namespace