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