Merged branch 'master' of https://github.com/Nemo1369/libcds
[libcds.git] / test / unit / map / feldman_hashmap_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_feldman_hashmap_hp.h"
32
33 #include <cds/container/feldman_hashmap_hp.h>
34
35 namespace {
36     namespace cc = cds::container;
37     typedef cds::gc::HP gc_type;
38
39     class FeldmanHashMap_HP : public cds_test::feldman_hashmap_hp
40     {
41     protected:
42         typedef cds_test::feldman_hashmap_hp base_class;
43
44         void SetUp()
45         {
46             typedef cc::FeldmanHashMap< gc_type, key_type, value_type > map_type;
47
48             // +1 - for guarded_ptr
49             cds::gc::hp::GarbageCollector::Construct( map_type::c_nHazardPtrCount + 1, 1, 16 );
50             cds::threading::Manager::attachThread();
51         }
52
53         void TearDown()
54         {
55             cds::threading::Manager::detachThread();
56             cds::gc::hp::GarbageCollector::Destruct( true );
57         }
58     };
59
60     TEST_F( FeldmanHashMap_HP, defaulted )
61     {
62         typedef cc::FeldmanHashMap< gc_type, key_type, value_type > map_type;
63
64         map_type m;
65         test( m );
66     }
67
68     TEST_F( FeldmanHashMap_HP, compare )
69     {
70         typedef cc::FeldmanHashMap< gc_type, key_type, value_type,
71             typename cc::feldman_hashmap::make_traits<
72                 cds::opt::compare< cmp >
73             >::type
74         > map_type;
75
76         map_type m( 4, 5 );
77         EXPECT_GE( m.head_size(), static_cast<size_t>( 1 << 4 ));
78         EXPECT_EQ( m.array_node_size(), static_cast<size_t>( 1 << 5 ));
79         test( m );
80     }
81
82     TEST_F( FeldmanHashMap_HP, less )
83     {
84         typedef cc::FeldmanHashMap< gc_type, key_type, value_type,
85             typename cc::feldman_hashmap::make_traits<
86                 cds::opt::less< less >
87             >::type
88         > map_type;
89
90         map_type m( 3, 2 );
91         EXPECT_GE( m.head_size(), static_cast<size_t>( 1 << 4 )); // min = 2 ** 4
92         EXPECT_EQ( m.array_node_size(), static_cast<size_t>( 1 << 2 ));
93         test( m );
94     }
95
96     TEST_F( FeldmanHashMap_HP, cmpmix )
97     {
98         typedef cc::FeldmanHashMap< gc_type, key_type, value_type,
99             typename cc::feldman_hashmap::make_traits<
100                 cds::opt::less< less >
101                 ,cds::opt::compare< cmp >
102             >::type
103         > map_type;
104
105         map_type m( 4, 4 );
106         EXPECT_EQ( m.head_size(), static_cast<size_t>( 1 << 4 ));
107         EXPECT_EQ( m.array_node_size(), static_cast<size_t>( 1 << 4 ));
108         test( m );
109     }
110
111     TEST_F( FeldmanHashMap_HP, backoff )
112     {
113         struct map_traits: public cc::feldman_hashmap::traits
114         {
115             typedef cmp compare;
116             typedef cds::atomicity::item_counter item_counter;
117             typedef cds::backoff::yield back_off;
118         };
119         typedef cc::FeldmanHashMap< gc_type, key_type, value_type, map_traits > map_type;
120
121         map_type m( 8, 2 );
122         EXPECT_EQ( m.head_size(), static_cast<size_t>( 1 << 8 ));
123         EXPECT_EQ( m.array_node_size(), static_cast<size_t>( 1 << 2 ));
124         test( m );
125     }
126
127     TEST_F( FeldmanHashMap_HP, stat )
128     {
129         struct map_traits: public cc::feldman_hashmap::traits
130         {
131             typedef cds::backoff::yield back_off;
132             typedef cc::feldman_hashmap::stat<> stat;
133         };
134         typedef cc::FeldmanHashMap< gc_type, key_type, value_type, map_traits > map_type;
135
136         map_type m( 1, 1 );
137         EXPECT_EQ( m.head_size(), static_cast<size_t>( 1 << 4 )); // min = 2**4
138         EXPECT_EQ( m.array_node_size(), static_cast<size_t>( 1 << 2 )); // min = 2**2
139         test( m );
140     }
141
142     TEST_F( FeldmanHashMap_HP, explicit_key_size )
143     {
144         struct map_traits: public cc::feldman_hashmap::traits
145         {
146             enum: size_t {
147                 hash_size = sizeof(int) + sizeof( uint16_t)
148             };
149             typedef hash2 hash;
150             typedef less2 less;
151             typedef cc::feldman_hashmap::stat<> stat;
152         };
153         typedef cc::FeldmanHashMap< gc_type, key_type2, value_type, map_traits > map_type;
154
155         map_type m( 5, 3 );
156         EXPECT_EQ( m.head_size(), static_cast<size_t>(1 << 6));
157         EXPECT_EQ( m.array_node_size(), static_cast<size_t>(1 << 3));
158         test( m );
159     }
160
161 } // namespace