33ac26471d43ace6767f1b3c6096e1a982b99236
[libcds.git] / test / unit / map / michael_lazy_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-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_map_hp.h"
32
33 #include <cds/container/lazy_kvlist_dhp.h>
34 #include <cds/container/michael_map.h>
35
36 namespace {
37
38     namespace cc = cds::container;
39     typedef cds::gc::DHP gc_type;
40
41     class MichaelLazyMap_DHP: public cds_test::container_map_hp
42     {
43     protected:
44         typedef cds_test::container_map_hp base_class;
45
46         void SetUp()
47         {
48             typedef cc::LazyKVList< gc_type, key_type, value_type > list_type;
49             typedef cc::MichaelHashMap< gc_type, list_type >   map_type;
50
51             cds::gc::dhp::smr::construct( map_type::c_nHazardPtrCount );
52             cds::threading::Manager::attachThread();
53         }
54
55         void TearDown()
56         {
57             cds::threading::Manager::detachThread();
58             cds::gc::dhp::smr::destruct();
59         }
60     };
61
62     TEST_F( MichaelLazyMap_DHP, compare )
63     {
64         typedef cc::LazyKVList< gc_type, key_type, value_type,
65             typename cc::lazy_list::make_traits<
66                 cds::opt::compare< cmp >
67             >::type
68         > list_type;
69
70         typedef cc::MichaelHashMap< gc_type, list_type,
71             typename cc::michael_map::make_traits<
72                 cds::opt::hash< hash1 >
73             >::type
74         > map_type;
75
76         map_type m( kSize, 2 );
77         test( m );
78     }
79
80     TEST_F( MichaelLazyMap_DHP, less )
81     {
82         typedef cc::LazyKVList< gc_type, key_type, value_type,
83             typename cc::lazy_list::make_traits<
84                 cds::opt::less< less >
85             >::type
86         > list_type;
87
88         typedef cc::MichaelHashMap< gc_type, list_type,
89             typename cc::michael_map::make_traits<
90                 cds::opt::hash< hash1 >
91             >::type
92         > map_type;
93
94         map_type m( kSize, 1 );
95         test( m );
96     }
97
98     TEST_F( MichaelLazyMap_DHP, cmpmix )
99     {
100         typedef cc::LazyKVList< gc_type, key_type, value_type,
101             typename cc::lazy_list::make_traits<
102                 cds::opt::less< less >
103                 ,cds::opt::compare< cmp >
104             >::type
105         > list_type;
106
107         typedef cc::MichaelHashMap< gc_type, list_type,
108             typename cc::michael_map::make_traits<
109                 cds::opt::hash< hash1 >
110             >::type
111         > map_type;
112
113         map_type m( kSize, 2 );
114         test( m );
115     }
116
117     TEST_F( MichaelLazyMap_DHP, backoff )
118     {
119         struct list_traits: public cc::lazy_list::traits
120         {
121             typedef cmp compare;
122             typedef cds::backoff::exponential<cds::backoff::pause, cds::backoff::yield> back_off;
123         };
124         typedef cc::LazyKVList< gc_type, key_type, value_type, list_traits > list_type;
125
126         struct map_traits: public cc::michael_map::traits
127         {
128             typedef hash1 hash;
129             typedef cds::atomicity::item_counter item_counter;
130         };
131         typedef cc::MichaelHashMap< gc_type, list_type, map_traits > map_type;
132
133         map_type m( kSize, 4 );
134         test( m );
135     }
136
137     TEST_F( MichaelLazyMap_DHP, seq_cst )
138     {
139         struct list_traits: public cc::lazy_list::traits
140         {
141             typedef cmp compare;
142             typedef cds::backoff::yield back_off;
143             typedef cds::opt::v::sequential_consistent memory_model;
144         };
145         typedef cc::LazyKVList< gc_type, key_type, value_type, list_traits > list_type;
146
147         struct map_traits: public cc::michael_map::traits
148         {
149             typedef hash1 hash;
150         };
151         typedef cc::MichaelHashMap< gc_type, list_type, map_traits > map_type;
152
153         map_type m( kSize, 8 );
154         test( m );
155     }
156
157     TEST_F( MichaelLazyMap_DHP, mutex )
158     {
159         struct list_traits: public cc::lazy_list::traits
160         {
161             typedef cmp                 compare;
162             typedef cds::backoff::yield back_off;
163             typedef std::mutex          lock_type;
164         };
165         typedef cc::LazyKVList< gc_type, key_type, value_type, list_traits > list_type;
166
167         struct map_traits: public cc::michael_map::traits
168         {
169             typedef hash1 hash;
170         };
171         typedef cc::MichaelHashMap< gc_type, list_type, map_traits > map_type;
172
173         map_type m( kSize, 2 );
174         test( m );
175     }
176
177     TEST_F( MichaelLazyMap_DHP, stat )
178     {
179         struct list_traits: public cc::lazy_list::traits
180         {
181             typedef cmp compare;
182             typedef cds::backoff::yield back_off;
183             typedef cc::lazy_list::stat<> stat;
184         };
185         typedef cc::LazyKVList< gc_type, key_type, value_type, list_traits > list_type;
186
187         struct map_traits: public cc::michael_map::traits
188         {
189             typedef hash1 hash;
190         };
191         typedef cc::MichaelHashMap< gc_type, list_type, map_traits > map_type;
192
193         map_type m( kSize, 2 );
194         test( m );
195         EXPECT_GE( m.statistics().m_nInsertSuccess, 0u );
196     }
197
198     TEST_F( MichaelLazyMap_DHP, wrapped_stat )
199     {
200         struct list_traits: public cc::lazy_list::traits
201         {
202             typedef cmp compare;
203             typedef cds::backoff::yield back_off;
204             typedef cc::lazy_list::wrapped_stat<> stat;
205         };
206         typedef cc::LazyKVList< gc_type, key_type, value_type, list_traits > list_type;
207
208         struct map_traits: public cc::michael_map::traits
209         {
210             typedef hash1 hash;
211         };
212         typedef cc::MichaelHashMap< gc_type, list_type, map_traits > map_type;
213
214         map_type m( kSize, 2 );
215         test( m );
216         EXPECT_GE( m.statistics().m_nInsertSuccess, 0u );
217     }
218
219 } // namespace
220