97a1be2b49dc9138ac48c1a5ff6b65b195a9549f
[libcds.git] / test / unit / set / michael_michael_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-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_set_hp.h"
32
33 #include <cds/container/michael_list_dhp.h>
34 #include <cds/container/michael_set.h>
35
36 namespace {
37     namespace cc = cds::container;
38     typedef cds::gc::DHP gc_type;
39
40     class MichaelSet_DHP : 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::MichaelList< gc_type, int_item > list_type;
48             typedef cc::MichaelHashSet< gc_type, list_type >   set_type;
49
50             cds::gc::dhp::GarbageCollector::Construct( 16, set_type::c_nHazardPtrCount );
51             cds::threading::Manager::attachThread();
52         }
53
54         void TearDown()
55         {
56             cds::threading::Manager::detachThread();
57             cds::gc::dhp::GarbageCollector::Destruct();
58         }
59     };
60
61     TEST_F( MichaelSet_DHP, compare )
62     {
63         typedef cc::MichaelList< gc_type, int_item,
64             typename cc::michael_list::make_traits<
65                 cds::opt::compare< cmp >
66             >::type
67         > list_type;
68
69         typedef cc::MichaelHashSet< gc_type, list_type, 
70             typename cc::michael_set::make_traits<
71                 cds::opt::hash< hash_int >
72             >::type
73         > set_type;
74
75         set_type s( kSize, 2 );
76         test( s );
77     }
78
79     TEST_F( MichaelSet_DHP, less )
80     {
81         typedef cc::MichaelList< gc_type, int_item,
82             typename cc::michael_list::make_traits<
83                 cds::opt::less< base_class::less >
84             >::type
85         > list_type;
86
87         typedef cc::MichaelHashSet< gc_type, list_type, 
88             typename cc::michael_set::make_traits<
89                 cds::opt::hash< hash_int >
90             >::type
91         > set_type;
92
93         set_type s( kSize, 2 );
94         test( s );
95     }
96
97     TEST_F( MichaelSet_DHP, cmpmix )
98     {
99         struct list_traits : public cc::michael_list::traits
100         {
101             typedef base_class::less less;
102             typedef cmp compare;
103         };
104         typedef cc::MichaelList< gc_type, int_item, list_traits > list_type;
105
106         typedef cc::MichaelHashSet< gc_type, list_type, 
107             typename cc::michael_set::make_traits<
108                 cds::opt::hash< hash_int >
109             >::type
110         > set_type;
111
112         set_type s( kSize, 2 );
113         test( s );
114     }
115
116     TEST_F( MichaelSet_DHP, item_counting )
117     {
118         struct list_traits : public cc::michael_list::traits
119         {
120             typedef cmp compare;
121         };
122         typedef cc::MichaelList< gc_type, int_item, list_traits > list_type;
123
124         struct set_traits: public cc::michael_set::traits
125         {
126             typedef hash_int hash;
127             typedef simple_item_counter item_counter;
128         };
129         typedef cc::MichaelHashSet< gc_type, list_type, set_traits >set_type;
130
131         set_type s( kSize, 3 );
132         test( s );
133     }
134
135     TEST_F( MichaelSet_DHP, backoff )
136     {
137         struct list_traits : public cc::michael_list::traits
138         {
139             typedef cmp compare;
140             typedef cds::backoff::exponential<cds::backoff::pause, cds::backoff::yield> back_off;
141         };
142         typedef cc::MichaelList< gc_type, int_item, list_traits > list_type;
143
144         struct set_traits : public cc::michael_set::traits
145         {
146             typedef hash_int hash;
147             typedef cds::atomicity::item_counter item_counter;
148         };
149         typedef cc::MichaelHashSet< gc_type, list_type, set_traits >set_type;
150
151         set_type s( kSize, 4 );
152         test( s );
153     }
154
155     TEST_F( MichaelSet_DHP, seq_cst )
156     {
157         struct list_traits : public cc::michael_list::traits
158         {
159             typedef base_class::less less;
160             typedef cds::backoff::pause back_off;
161             typedef cds::opt::v::sequential_consistent memory_model;
162         };
163         typedef cc::MichaelList< gc_type, int_item, list_traits > list_type;
164
165         struct set_traits : public cc::michael_set::traits
166         {
167             typedef hash_int hash;
168             typedef cds::atomicity::item_counter item_counter;
169         };
170         typedef cc::MichaelHashSet< gc_type, list_type, set_traits >set_type;
171
172         set_type s( kSize, 4 );
173         test( s );
174     }
175
176     TEST_F( MichaelSet_DHP, stat )
177     {
178         struct list_traits: public cc::michael_list::traits
179         {
180             typedef base_class::less less;
181             typedef cds::backoff::pause back_off;
182             typedef cc::michael_list::stat<> stat;
183         };
184         typedef cc::MichaelList< gc_type, int_item, list_traits > list_type;
185
186         struct set_traits: public cc::michael_set::traits
187         {
188             typedef hash_int hash;
189             typedef cds::atomicity::item_counter item_counter;
190         };
191         typedef cc::MichaelHashSet< gc_type, list_type, set_traits >set_type;
192
193         set_type s( kSize, 4 );
194         test( s );
195         EXPECT_GE( s.statistics().m_nInsertSuccess, 0 );
196     }
197
198     TEST_F( MichaelSet_DHP, wrapped_stat )
199     {
200         struct list_traits: public cc::michael_list::traits
201         {
202             typedef base_class::less less;
203             typedef cds::backoff::pause back_off;
204             typedef cc::michael_list::wrapped_stat<> stat;
205         };
206         typedef cc::MichaelList< 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, 0 );
218     }
219
220 } // namespace