b69aa597745bc894237946c4f11381bc164037a1
[libcds.git] / test / unit / map / michael_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_kvlist_hp.h>
34 #include <cds/container/michael_map.h>
35
36 namespace {
37
38     namespace cc = cds::container;
39     typedef cds::gc::HP gc_type;
40
41     class MichaelMap_HP: 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::MichaelKVList< gc_type, key_type, value_type > list_type;
49             typedef cc::MichaelHashMap< gc_type, list_type >   map_type;
50
51             // +1 - for guarded_ptr and iterator
52             cds::gc::hp::GarbageCollector::Construct( map_type::c_nHazardPtrCount + 1, 1, 16 );
53             cds::threading::Manager::attachThread();
54         }
55
56         void TearDown()
57         {
58             cds::threading::Manager::detachThread();
59             cds::gc::hp::GarbageCollector::Destruct( true );
60         }
61     };
62
63     TEST_F( MichaelMap_HP, compare )
64     {
65         typedef cc::MichaelKVList< gc_type, key_type, value_type,
66             typename cc::michael_list::make_traits<
67                 cds::opt::compare< cmp >
68             >::type
69         > list_type;
70
71         typedef cc::MichaelHashMap< gc_type, list_type, 
72             typename cc::michael_map::make_traits<
73                 cds::opt::hash< hash1 >
74             >::type
75         > map_type;
76
77         map_type m( kSize, 2 );
78         test( m );
79     }
80
81     TEST_F( MichaelMap_HP, less )
82     {
83         typedef cc::MichaelKVList< gc_type, key_type, value_type,
84             typename cc::michael_list::make_traits<
85                 cds::opt::less< less >
86             >::type
87         > list_type;
88
89         typedef cc::MichaelHashMap< gc_type, list_type, 
90             typename cc::michael_map::make_traits<
91                 cds::opt::hash< hash1 >
92             >::type
93         > map_type;
94
95         map_type m( kSize, 1 );
96         test( m );
97     }
98
99     TEST_F( MichaelMap_HP, cmpmix )
100     {
101         typedef cc::MichaelKVList< gc_type, key_type, value_type,
102             typename cc::michael_list::make_traits<
103                 cds::opt::less< less >
104                 ,cds::opt::compare< cmp >
105             >::type
106         > list_type;
107
108         typedef cc::MichaelHashMap< gc_type, list_type, 
109             typename cc::michael_map::make_traits<
110                 cds::opt::hash< hash1 >
111             >::type
112         > map_type;
113
114         map_type m( kSize, 2 );
115         test( m );
116     }
117
118     TEST_F( MichaelMap_HP, backoff )
119     {
120         struct list_traits: public cc::michael_list::traits
121         {
122             typedef cmp compare;
123             typedef cds::backoff::exponential<cds::backoff::pause, cds::backoff::yield> back_off;
124         };
125         typedef cc::MichaelKVList< gc_type, key_type, value_type, list_traits > list_type;
126
127         struct map_traits: public cc::michael_map::traits
128         {
129             typedef hash1 hash;
130             typedef cds::atomicity::item_counter item_counter;
131         };
132         typedef cc::MichaelHashMap< gc_type, list_type, map_traits > map_type;
133
134         map_type m( kSize, 4 );
135         test( m );
136     }
137
138     TEST_F( MichaelMap_HP, seq_cst )
139     {
140         struct list_traits: public cc::michael_list::traits
141         {
142             typedef cmp compare;
143             typedef cds::backoff::yield back_off;
144             typedef cds::opt::v::sequential_consistent memory_model;
145         };
146         typedef cc::MichaelKVList< gc_type, key_type, value_type, list_traits > list_type;
147
148         struct map_traits: public cc::michael_map::traits
149         {
150             typedef hash1 hash;
151         };
152         typedef cc::MichaelHashMap< gc_type, list_type, map_traits > map_type;
153
154         map_type s( kSize, 8 );
155         test( s );
156     }
157
158 } // namespace
159