Updated copyright
[libcds.git] / test / unit / set / michael_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-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_hp.h>
34 #include <cds/container/michael_set.h>
35
36 namespace {
37     namespace cc = cds::container;
38     typedef cds::gc::HP gc_type;
39
40     class MichaelLazySet_HP : public cds_test::container_set_hp
41     {
42     protected:
43         typedef cds_test::container_set_hp base_class;
44
45         void SetUp()
46         {
47             typedef cc::LazyList< gc_type, int_item > list_type;
48             typedef cc::MichaelHashSet< gc_type, list_type >   set_type;
49
50             // +1 - for guarded_ptr
51             cds::gc::hp::GarbageCollector::Construct( set_type::c_nHazardPtrCount + 1, 1, 16 );
52             cds::threading::Manager::attachThread();
53         }
54
55         void TearDown()
56         {
57             cds::threading::Manager::detachThread();
58             cds::gc::hp::GarbageCollector::Destruct( true );
59         }
60     };
61
62     TEST_F( MichaelLazySet_HP, compare )
63     {
64         typedef cc::LazyList< gc_type, int_item,
65             typename cc::lazy_list::make_traits<
66                 cds::opt::compare< cmp >
67             >::type
68         > list_type;
69
70         typedef cc::MichaelHashSet< gc_type, list_type,
71             typename cc::michael_set::make_traits<
72                 cds::opt::hash< hash_int >
73             >::type
74         > set_type;
75
76         set_type s( kSize, 2 );
77         test( s );
78     }
79
80     TEST_F( MichaelLazySet_HP, less )
81     {
82         typedef cc::LazyList< gc_type, int_item,
83             typename cc::lazy_list::make_traits<
84                 cds::opt::less< base_class::less >
85             >::type
86         > list_type;
87
88         typedef cc::MichaelHashSet< gc_type, list_type,
89             typename cc::michael_set::make_traits<
90                 cds::opt::hash< hash_int >
91             >::type
92         > set_type;
93
94         set_type s( kSize, 2 );
95         test( s );
96     }
97
98     TEST_F( MichaelLazySet_HP, cmpmix )
99     {
100         struct list_traits : public cc::lazy_list::traits
101         {
102             typedef base_class::less less;
103             typedef cmp compare;
104         };
105         typedef cc::LazyList< gc_type, int_item, list_traits > list_type;
106
107         typedef cc::MichaelHashSet< gc_type, list_type,
108             typename cc::michael_set::make_traits<
109                 cds::opt::hash< hash_int >
110             >::type
111         > set_type;
112
113         set_type s( kSize, 2 );
114         test( s );
115     }
116
117     TEST_F( MichaelLazySet_HP, item_counting )
118     {
119         struct list_traits : public cc::lazy_list::traits
120         {
121             typedef cmp compare;
122         };
123         typedef cc::LazyList< gc_type, int_item, list_traits > list_type;
124
125         struct set_traits: public cc::michael_set::traits
126         {
127             typedef hash_int hash;
128             typedef simple_item_counter item_counter;
129         };
130         typedef cc::MichaelHashSet< gc_type, list_type, set_traits >set_type;
131
132         set_type s( kSize, 3 );
133         test( s );
134     }
135
136     TEST_F( MichaelLazySet_HP, backoff )
137     {
138         struct list_traits : public cc::lazy_list::traits
139         {
140             typedef cmp compare;
141             typedef cds::backoff::exponential<cds::backoff::pause, cds::backoff::yield> back_off;
142         };
143         typedef cc::LazyList< gc_type, int_item, list_traits > list_type;
144
145         struct set_traits : public cc::michael_set::traits
146         {
147             typedef hash_int hash;
148             typedef cds::atomicity::item_counter item_counter;
149         };
150         typedef cc::MichaelHashSet< gc_type, list_type, set_traits >set_type;
151
152         set_type s( kSize, 4 );
153         test( s );
154     }
155
156     TEST_F( MichaelLazySet_HP, seq_cst )
157     {
158         struct list_traits : public cc::lazy_list::traits
159         {
160             typedef base_class::less less;
161             typedef cds::backoff::pause back_off;
162             typedef cds::opt::v::sequential_consistent memory_model;
163         };
164         typedef cc::LazyList< gc_type, int_item, list_traits > list_type;
165
166         struct set_traits : public cc::michael_set::traits
167         {
168             typedef hash_int hash;
169             typedef cds::atomicity::item_counter item_counter;
170         };
171         typedef cc::MichaelHashSet< gc_type, list_type, set_traits >set_type;
172
173         set_type s( kSize, 4 );
174         test( s );
175     }
176
177     TEST_F( MichaelLazySet_HP, mutex )
178     {
179         struct list_traits: public cc::lazy_list::traits
180         {
181             typedef base_class::less less;
182             typedef cds::backoff::pause back_off;
183             typedef std::mutex lock_type;
184         };
185         typedef cc::LazyList< gc_type, int_item, list_traits > list_type;
186
187         struct set_traits: public cc::michael_set::traits
188         {
189             typedef hash_int hash;
190             typedef cds::atomicity::item_counter item_counter;
191         };
192         typedef cc::MichaelHashSet< gc_type, list_type, set_traits >set_type;
193
194         set_type s( kSize, 4 );
195         test( s );
196     }
197
198     TEST_F( MichaelLazySet_HP, stat )
199     {
200         struct list_traits: public cc::lazy_list::traits
201         {
202             typedef base_class::less less;
203             typedef cds::backoff::pause back_off;
204             typedef cc::lazy_list::stat<> stat;
205         };
206         typedef cc::LazyList< gc_type, int_item, list_traits > list_type;
207
208         struct set_traits: public cc::michael_set::traits
209         {
210             typedef hash_int hash;
211             typedef cds::atomicity::item_counter item_counter;
212         };
213         typedef cc::MichaelHashSet< gc_type, list_type, set_traits >set_type;
214
215         set_type s( kSize, 4 );
216         test( s );
217         EXPECT_GE( s.statistics().m_nInsertSuccess, 0u );
218     }
219
220     TEST_F( MichaelLazySet_HP, wrapped_stat )
221     {
222         struct list_traits: public cc::lazy_list::traits
223         {
224             typedef base_class::less less;
225             typedef cds::backoff::pause back_off;
226             typedef cc::lazy_list::wrapped_stat<> stat;
227         };
228         typedef cc::LazyList< gc_type, int_item, list_traits > list_type;
229
230         struct set_traits: public cc::michael_set::traits
231         {
232             typedef hash_int hash;
233             typedef cds::atomicity::item_counter item_counter;
234         };
235         typedef cc::MichaelHashSet< gc_type, list_type, set_traits >set_type;
236
237         set_type s( kSize, 4 );
238         test( s );
239         EXPECT_GE( s.statistics().m_nInsertSuccess, 0u );
240     }
241
242 } // namespace