Merged branch 'master' of https://github.com/Nemo1369/libcds
[libcds.git] / test / unit / set / michael_iterable_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_michael_iterable_hp.h"
32
33 #include <cds/container/iterable_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 MichaelIterableSet_HP : public cds_test::michael_iterable_set_hp
41     {
42     protected:
43         typedef cds_test::michael_iterable_set_hp base_class;
44
45         void SetUp()
46         {
47             typedef cc::IterableList< gc_type, int_item > list_type;
48             typedef cc::MichaelHashSet< gc_type, list_type >   set_type;
49
50             // +3 - for guarded_ptr, iterators
51             cds::gc::hp::GarbageCollector::Construct( set_type::c_nHazardPtrCount + 3, 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( MichaelIterableSet_HP, compare )
63     {
64         typedef cc::IterableList< gc_type, int_item,
65             typename cc::iterable_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( MichaelIterableSet_HP, less )
81     {
82         typedef cc::IterableList< gc_type, int_item,
83             typename cc::iterable_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( MichaelIterableSet_HP, cmpmix )
99     {
100         struct list_traits : public cc::iterable_list::traits
101         {
102             typedef base_class::less less;
103             typedef cmp compare;
104         };
105         typedef cc::IterableList< 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( MichaelIterableSet_HP, item_counting )
118     {
119         struct list_traits : public cc::iterable_list::traits
120         {
121             typedef cmp compare;
122         };
123         typedef cc::IterableList< 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( MichaelIterableSet_HP, backoff )
137     {
138         struct list_traits : public cc::iterable_list::traits
139         {
140             typedef cmp compare;
141             typedef cds::backoff::exponential<cds::backoff::pause, cds::backoff::yield> back_off;
142         };
143         typedef cc::IterableList< 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( MichaelIterableSet_HP, seq_cst )
157     {
158         struct list_traits : public cc::iterable_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::IterableList< 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( MichaelIterableSet_HP, stat )
178     {
179         struct list_traits: public cc::iterable_list::traits
180         {
181             typedef base_class::less less;
182             typedef cds::backoff::pause back_off;
183             typedef cc::iterable_list::stat<> stat;
184         };
185         typedef cc::IterableList< 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         EXPECT_GE( s.statistics().m_nInsertSuccess, 0u );
197     }
198
199     TEST_F( MichaelIterableSet_HP, wrapped_stat )
200     {
201         struct list_traits: public cc::iterable_list::traits
202         {
203             typedef base_class::less less;
204             typedef cds::backoff::pause back_off;
205             typedef cc::iterable_list::wrapped_stat<> stat;
206         };
207         typedef cc::IterableList< gc_type, int_item, list_traits > list_type;
208
209         struct set_traits: public cc::michael_set::traits
210         {
211             typedef hash_int hash;
212             typedef cds::atomicity::item_counter item_counter;
213         };
214         typedef cc::MichaelHashSet< gc_type, list_type, set_traits > set_type;
215
216         set_type s( kSize, 4 );
217         test( s );
218         EXPECT_GE( s.statistics().m_nInsertSuccess, 0u );
219     }
220
221 } // namespace