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