d69dd36f99bb881964f5d9f898bbad129260439d
[libcds.git] / test / unit / map / test_michael_lazy_rcu.h
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 #ifndef CDSUNIT_MAP_TEST_MICHAEL_LAZY_RCU_H
31 #define CDSUNIT_MAP_TEST_MICHAEL_LAZY_RCU_H
32
33 #include "test_map_rcu.h"
34 #include <cds/container/lazy_kvlist_rcu.h>
35 #include <cds/container/michael_map_rcu.h>
36
37 namespace {
38     namespace cc = cds::container;
39
40     template <class RCU>
41     class MichaelLazyMap: public cds_test::container_map_rcu
42     {
43         typedef cds_test::container_map_rcu base_class;
44     public:
45         typedef cds::urcu::gc<RCU> rcu_type;
46
47     protected:
48         void SetUp()
49         {
50             RCU::Construct();
51             cds::threading::Manager::attachThread();
52         }
53
54         void TearDown()
55         {
56             cds::threading::Manager::detachThread();
57             RCU::Destruct();
58         }
59     };
60
61     TYPED_TEST_CASE_P( MichaelLazyMap );
62
63     TYPED_TEST_P( MichaelLazyMap, compare )
64     {
65         typedef typename TestFixture::rcu_type rcu_type;
66         typedef typename TestFixture::key_type key_type;
67         typedef typename TestFixture::value_type value_type;
68
69         typedef cc::LazyKVList< rcu_type, key_type, value_type,
70             typename cc::lazy_list::make_traits<
71                 cds::opt::compare< typename TestFixture::cmp >
72             >::type
73         > list_type;
74
75         typedef cc::MichaelHashMap< rcu_type, list_type, 
76             typename cc::michael_map::make_traits<
77                 cds::opt::hash< typename TestFixture::hash1 >
78             >::type
79         > map_type;
80
81         map_type m( TestFixture::kSize, 2 );
82         this->test( m );
83     }
84
85     TYPED_TEST_P( MichaelLazyMap, less )
86     {
87         typedef typename TestFixture::rcu_type rcu_type;
88         typedef typename TestFixture::key_type key_type;
89         typedef typename TestFixture::value_type value_type;
90
91         typedef cc::LazyKVList< rcu_type, key_type, value_type,
92             typename cc::lazy_list::make_traits<
93                 cds::opt::less< typename TestFixture::less >
94             >::type
95         > list_type;
96
97         typedef cc::MichaelHashMap< rcu_type, list_type, 
98             typename cc::michael_map::make_traits<
99                 cds::opt::hash< typename TestFixture::hash1 >
100             >::type
101         > map_type;
102
103         map_type m( TestFixture::kSize, 2 );
104         this->test( m );
105     }
106
107     TYPED_TEST_P( MichaelLazyMap, cmpmix )
108     {
109         typedef typename TestFixture::rcu_type rcu_type;
110         typedef typename TestFixture::key_type key_type;
111         typedef typename TestFixture::value_type value_type;
112
113         struct list_traits : public cc::lazy_list::traits
114         {
115             typedef typename TestFixture::less less;
116             typedef typename TestFixture::cmp compare;
117         };
118         typedef cc::LazyKVList< rcu_type, key_type, value_type, list_traits > list_type;
119
120         typedef cc::MichaelHashMap< rcu_type, list_type,
121             typename cc::michael_map::make_traits<
122                 cds::opt::hash< typename TestFixture::hash1 >
123             >::type
124         > map_type;
125
126         map_type m( TestFixture::kSize, 2 );
127         this->test( m );
128     }
129
130     TYPED_TEST_P( MichaelLazyMap, backoff )
131     {
132         typedef typename TestFixture::rcu_type rcu_type;
133         typedef typename TestFixture::key_type key_type;
134         typedef typename TestFixture::value_type value_type;
135
136         struct list_traits : public cc::lazy_list::traits
137         {
138             typedef typename TestFixture::cmp compare;
139             typedef cds::backoff::exponential<cds::backoff::pause, cds::backoff::yield> back_off;
140         };
141         typedef cc::LazyKVList< rcu_type, key_type, value_type, list_traits > list_type;
142
143         struct set_traits : public cc::michael_map::traits
144         {
145             typedef typename TestFixture::hash1 hash;
146             typedef cds::atomicity::item_counter item_counter;
147         };
148         typedef cc::MichaelHashMap< rcu_type, list_type, set_traits >map_type;
149
150         map_type m( TestFixture::kSize, 4 );
151         this->test( m );
152     }
153
154     TYPED_TEST_P( MichaelLazyMap, seq_cst )
155     {
156         typedef typename TestFixture::rcu_type rcu_type;
157         typedef typename TestFixture::key_type key_type;
158         typedef typename TestFixture::value_type value_type;
159
160         struct list_traits : public cc::lazy_list::traits
161         {
162             typedef typename TestFixture::less less;
163             typedef cds::backoff::pause back_off;
164             typedef cds::opt::v::sequential_consistent memory_model;
165         };
166         typedef cc::LazyKVList< rcu_type, key_type, value_type, list_traits > list_type;
167
168         struct set_traits : public cc::michael_map::traits
169         {
170             typedef typename TestFixture::hash1 hash;
171             typedef cds::atomicity::item_counter item_counter;
172         };
173         typedef cc::MichaelHashMap< rcu_type, list_type, set_traits >map_type;
174
175         map_type m( TestFixture::kSize, 4 );
176         this->test( m );
177     }
178
179     TYPED_TEST_P( MichaelLazyMap, mutex )
180     {
181         typedef typename TestFixture::rcu_type rcu_type;
182         typedef typename TestFixture::key_type key_type;
183         typedef typename TestFixture::value_type value_type;
184
185         struct list_traits : public cc::lazy_list::traits
186         {
187             typedef typename TestFixture::less less;
188             typedef cds::backoff::pause back_off;
189             typedef std::mutex lock_type;
190         };
191         typedef cc::LazyKVList< rcu_type, key_type, value_type, list_traits > list_type;
192
193         struct set_traits : public cc::michael_map::traits
194         {
195             typedef typename TestFixture::hash1 hash;
196             typedef cds::atomicity::item_counter item_counter;
197         };
198         typedef cc::MichaelHashMap< rcu_type, list_type, set_traits >map_type;
199
200         map_type m( TestFixture::kSize, 4 );
201         this->test( m );
202     }
203
204     TYPED_TEST_P( MichaelLazyMap, stat )
205     {
206         typedef typename TestFixture::rcu_type rcu_type;
207         typedef typename TestFixture::key_type key_type;
208         typedef typename TestFixture::value_type value_type;
209
210         struct list_traits: public cc::lazy_list::traits
211         {
212             typedef typename TestFixture::less less;
213             typedef cds::backoff::pause back_off;
214             typedef cc::lazy_list::stat<> stat;
215         };
216         typedef cc::LazyKVList< rcu_type, key_type, value_type, list_traits > list_type;
217
218         struct set_traits: public cc::michael_map::traits
219         {
220             typedef typename TestFixture::hash1 hash;
221             typedef cds::atomicity::item_counter item_counter;
222         };
223         typedef cc::MichaelHashMap< rcu_type, list_type, set_traits >map_type;
224
225         map_type m( TestFixture::kSize, 4 );
226         this->test( m );
227         EXPECT_GE( m.statistics().m_nInsertSuccess, 0 );
228     }
229
230     TYPED_TEST_P( MichaelLazyMap, wrapped_stat )
231     {
232         typedef typename TestFixture::rcu_type rcu_type;
233         typedef typename TestFixture::key_type key_type;
234         typedef typename TestFixture::value_type value_type;
235
236         struct list_traits: public cc::lazy_list::traits
237         {
238             typedef typename TestFixture::less less;
239             typedef cds::backoff::pause back_off;
240             typedef cc::lazy_list::wrapped_stat<> stat;
241         };
242         typedef cc::LazyKVList< rcu_type, key_type, value_type, list_traits > list_type;
243
244         struct set_traits: public cc::michael_map::traits
245         {
246             typedef typename TestFixture::hash1 hash;
247             typedef cds::atomicity::item_counter item_counter;
248         };
249         typedef cc::MichaelHashMap< rcu_type, list_type, set_traits >map_type;
250
251         map_type m( TestFixture::kSize, 4 );
252         this->test( m );
253         EXPECT_GE( m.statistics().m_nInsertSuccess, 0 );
254     }
255
256
257     // GCC 5: All test names should be written on single line, otherwise a runtime error will be encountered like as
258     // "No test named <test_name> can be found in this test case"
259     REGISTER_TYPED_TEST_CASE_P( MichaelLazyMap,
260         compare, less, cmpmix, backoff, seq_cst, mutex, stat, wrapped_stat
261     );
262 } // namespace
263
264 #endif // CDSUNIT_MAP_TEST_MICHAEL_LAZY_RCU_H
265