Fixed minor gcc warnings
[libcds.git] / test / unit / map / test_feldman_hashmap_hp.h
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 #ifndef CDSUNIT_MAP_TEST_FELDMAN_HASHMAP_HP_H
32 #define CDSUNIT_MAP_TEST_FELDMAN_HASHMAP_HP_H
33
34 #include "test_feldman_hashmap.h"
35
36 namespace cds_test {
37
38     class feldman_hashmap_hp: public feldman_hashmap
39     {
40         typedef feldman_hashmap base_class;
41
42     protected:
43         template <class Map>
44         void test( Map& m )
45         {
46             // Precondition: map is empty
47             // Postcondition: map is empty
48
49             base_class::test( m );
50
51             ASSERT_TRUE( m.empty());
52             ASSERT_CONTAINER_SIZE( m, 0 );
53
54             //typedef typename Map::value_type map_pair;
55             size_t const kkSize = base_class::kSize;
56
57             std::vector<key_type> arrKeys;
58             for ( int i = 0; i < static_cast<int>(kkSize); ++i )
59                 arrKeys.push_back( key_type( i ));
60             shuffle( arrKeys.begin(), arrKeys.end());
61
62             std::vector< value_type > arrVals;
63             for ( size_t i = 0; i < kkSize; ++i ) {
64                 value_type val;
65                 val.nVal = static_cast<int>( i );
66                 val.strVal = std::to_string( i );
67                 arrVals.push_back( val );
68             }
69
70             for ( auto const& i : arrKeys )
71                 ASSERT_TRUE( m.insert( i ) );
72             ASSERT_FALSE( m.empty() );
73             ASSERT_CONTAINER_SIZE( m, kkSize );
74
75             // iterators
76             size_t nCount = 0;
77             for ( auto it = m.begin(); it != m.end(); ++it ) {
78                 EXPECT_EQ( it->second.nVal, 0 );
79                 it->second.nVal = it->first.nKey * 2;
80                 ++nCount;
81             }
82             EXPECT_EQ( nCount, kkSize );
83
84             nCount = 0;
85             for ( auto it = m.cbegin(); it != m.cend(); ++it ) {
86                 EXPECT_EQ( it->second.nVal, it->first.nKey * 2 );
87                 ++nCount;
88             }
89             EXPECT_EQ( nCount, kkSize );
90
91             nCount = 0;
92             for ( auto it = m.rbegin(); it != m.rend(); ++it ) {
93                 EXPECT_EQ( it->second.nVal, it->first.nKey * 2 );
94                 it->second.nVal = it->first.nKey * 4;
95                 ++nCount;
96             }
97             EXPECT_EQ( nCount, kkSize );
98
99             nCount = 0;
100             for ( auto it = m.crbegin(); it != m.crend(); ++it ) {
101                 EXPECT_EQ( it->second.nVal, it->first.nKey * 4 );
102                 ++nCount;
103             }
104             EXPECT_EQ( nCount, kkSize );
105
106             // get/extract
107             typedef typename Map::guarded_ptr guarded_ptr;
108             guarded_ptr gp;
109
110             for ( auto const& i : arrKeys ) {
111                 value_type const& val = arrVals.at( i.nKey );
112
113                 gp = m.get( i.nKey );
114                 ASSERT_FALSE( !gp );
115                 ASSERT_EQ( gp->first.nKey, i.nKey );
116                 gp.release();
117                 gp = m.get( i );
118                 ASSERT_FALSE( !gp );
119                 ASSERT_EQ( gp->first.nKey, i.nKey );
120                 gp.release();
121
122                 switch ( i.nKey % 3 ) {
123                 case 0:
124                     gp = m.extract( i.nKey );
125                     break;
126                 case 1:
127                     gp = m.extract( i );
128                     break;
129                 case 2:
130                     gp = m.extract( val.strVal );
131                     break;
132                 }
133                 ASSERT_FALSE( !gp );
134                 ASSERT_EQ( gp->first.nKey, i.nKey );
135                 gp.release();
136
137                 gp = m.get( i.nKey );
138                 ASSERT_TRUE( !gp );
139                 gp = m.get( i );
140                 ASSERT_TRUE( !gp );
141             }
142             ASSERT_TRUE( m.empty() );
143             ASSERT_CONTAINER_SIZE( m, 0 );
144
145             // erase_at( iterator )
146             for ( auto const& i : arrKeys )
147                 ASSERT_TRUE( m.insert( i ) );
148             ASSERT_FALSE( m.empty() );
149             ASSERT_CONTAINER_SIZE( m, kkSize );
150
151             nCount = 0;
152             for ( auto it = m.begin(); it != m.end(); ++it ) {
153                 EXPECT_EQ( it->second.nVal, 0 );
154                 ASSERT_TRUE( m.erase_at( it ));
155                 ++nCount;
156             }
157             EXPECT_EQ( nCount, kkSize );
158             ASSERT_TRUE( m.empty() );
159             ASSERT_CONTAINER_SIZE( m, 0 );
160
161             // erase_at( reverse_iterator )
162             for ( auto const& i : arrKeys )
163                 ASSERT_TRUE( m.insert( i ) );
164             ASSERT_FALSE( m.empty() );
165             ASSERT_CONTAINER_SIZE( m, kkSize );
166
167             nCount = 0;
168             for ( auto it = m.rbegin(); it != m.rend(); ++it ) {
169                 EXPECT_EQ( it->second.nVal, 0 );
170                 ASSERT_TRUE( m.erase_at( it ) );
171                 ++nCount;
172             }
173             EXPECT_EQ( nCount, kkSize );
174             ASSERT_TRUE( m.empty() );
175             ASSERT_CONTAINER_SIZE( m, 0 );
176
177             // erase_at( const_reverse_iterator )
178             for ( auto const& i : arrKeys )
179                 ASSERT_TRUE( m.insert( i ) );
180             ASSERT_FALSE( m.empty() );
181             ASSERT_CONTAINER_SIZE( m, kkSize );
182
183             nCount = 0;
184             for ( auto it = m.crbegin(); it != m.crend(); ++it ) {
185                 EXPECT_EQ( it->second.nVal, 0 );
186                 ASSERT_TRUE( m.erase_at( it ) );
187                 ++nCount;
188             }
189             EXPECT_EQ( nCount, kkSize );
190             ASSERT_TRUE( m.empty() );
191             ASSERT_CONTAINER_SIZE( m, 0 );
192
193             // erase_at( const_iterator )
194             for ( auto const& i : arrKeys )
195                 ASSERT_TRUE( m.insert( i ) );
196             ASSERT_FALSE( m.empty() );
197             ASSERT_CONTAINER_SIZE( m, kkSize );
198
199             nCount = 0;
200             for ( auto it = m.cbegin(); it != m.cend(); ++it ) {
201                 EXPECT_EQ( it->second.nVal, 0 );
202                 ASSERT_TRUE( m.erase_at( it ) );
203                 ++nCount;
204             }
205             EXPECT_EQ( nCount, kkSize );
206             ASSERT_TRUE( m.empty() );
207             ASSERT_CONTAINER_SIZE( m, 0 );
208         }
209     };
210
211 } // namespace cds_test
212
213 #endif // #ifndef CDSUNIT_MAP_TEST_FELDMAN_HASHMAP_HP_H