fixed adding file problem
[c11concurrency-benchmarks.git] / gdax-orderbook-hpp / demo / dependencies / libcds-2.3.2 / 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-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 #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             typedef typename Map::key_type key_type;
58             typedef typename Map::mapped_type value_type;
59
60             std::vector<key_type> arrKeys;
61             for ( int i = 0; i < static_cast<int>(kkSize); ++i )
62                 arrKeys.push_back( key_type( i ));
63             shuffle( arrKeys.begin(), arrKeys.end());
64
65             std::vector< value_type > arrVals;
66             for ( size_t i = 0; i < kkSize; ++i ) {
67                 value_type val;
68                 val.nVal = static_cast<int>( i );
69                 val.strVal = std::to_string( i );
70                 arrVals.push_back( val );
71             }
72
73             for ( auto const& i : arrKeys )
74                 ASSERT_TRUE( m.insert( i ));
75             ASSERT_FALSE( m.empty());
76             ASSERT_CONTAINER_SIZE( m, kkSize );
77
78             // iterators
79             size_t nCount = 0;
80             for ( auto it = m.begin(); it != m.end(); ++it ) {
81                 EXPECT_EQ( it->second.nVal, 0 );
82                 it->second.nVal = it->first.nKey * 2;
83                 ++nCount;
84             }
85             EXPECT_EQ( nCount, kkSize );
86
87             nCount = 0;
88             for ( auto it = m.cbegin(); it != m.cend(); ++it ) {
89                 EXPECT_EQ( it->second.nVal, it->first.nKey * 2 );
90                 ++nCount;
91             }
92             EXPECT_EQ( nCount, kkSize );
93
94             nCount = 0;
95             for ( auto it = m.rbegin(); it != m.rend(); ++it ) {
96                 EXPECT_EQ( it->second.nVal, it->first.nKey * 2 );
97                 it->second.nVal = it->first.nKey * 4;
98                 ++nCount;
99             }
100             EXPECT_EQ( nCount, kkSize );
101
102             nCount = 0;
103             for ( auto it = m.crbegin(); it != m.crend(); ++it ) {
104                 EXPECT_EQ( it->second.nVal, it->first.nKey * 4 );
105                 ++nCount;
106             }
107             EXPECT_EQ( nCount, kkSize );
108
109             // get/extract
110             typedef typename Map::guarded_ptr guarded_ptr;
111             guarded_ptr gp;
112
113             for ( auto const& i : arrKeys ) {
114                 value_type const& val = arrVals.at( i.nKey );
115
116                 gp = m.get( i.nKey );
117                 ASSERT_FALSE( !gp );
118                 ASSERT_EQ( gp->first.nKey, i.nKey );
119                 gp.release();
120                 gp = m.get( i );
121                 ASSERT_FALSE( !gp );
122                 ASSERT_EQ( gp->first.nKey, i.nKey );
123                 gp.release();
124
125                 switch ( i.nKey % 3 ) {
126                 case 0:
127                     gp = m.extract( i.nKey );
128                     break;
129                 case 1:
130                     gp = m.extract( i );
131                     break;
132                 case 2:
133                     gp = m.extract( val.strVal );
134                     break;
135                 }
136                 ASSERT_FALSE( !gp );
137                 ASSERT_EQ( gp->first.nKey, i.nKey );
138                 gp.release();
139
140                 gp = m.get( i.nKey );
141                 ASSERT_TRUE( !gp );
142                 gp = m.get( i );
143                 ASSERT_TRUE( !gp );
144             }
145             ASSERT_TRUE( m.empty());
146             ASSERT_CONTAINER_SIZE( m, 0 );
147
148             // erase_at( iterator )
149             for ( auto const& i : arrKeys )
150                 ASSERT_TRUE( m.insert( i ));
151             ASSERT_FALSE( m.empty());
152             ASSERT_CONTAINER_SIZE( m, kkSize );
153
154             nCount = 0;
155             for ( auto it = m.begin(); it != m.end(); ++it ) {
156                 EXPECT_EQ( it->second.nVal, 0 );
157                 ASSERT_TRUE( m.erase_at( it ));
158                 ++nCount;
159             }
160             EXPECT_EQ( nCount, kkSize );
161             ASSERT_TRUE( m.empty());
162             ASSERT_CONTAINER_SIZE( m, 0 );
163
164             // erase_at( reverse_iterator )
165             for ( auto const& i : arrKeys )
166                 ASSERT_TRUE( m.insert( i ));
167             ASSERT_FALSE( m.empty());
168             ASSERT_CONTAINER_SIZE( m, kkSize );
169
170             nCount = 0;
171             for ( auto it = m.rbegin(); it != m.rend(); ++it ) {
172                 EXPECT_EQ( it->second.nVal, 0 );
173                 ASSERT_TRUE( m.erase_at( it ));
174                 ++nCount;
175             }
176             EXPECT_EQ( nCount, kkSize );
177             ASSERT_TRUE( m.empty());
178             ASSERT_CONTAINER_SIZE( m, 0 );
179
180             // erase_at( const_reverse_iterator )
181             for ( auto const& i : arrKeys )
182                 ASSERT_TRUE( m.insert( i ));
183             ASSERT_FALSE( m.empty());
184             ASSERT_CONTAINER_SIZE( m, kkSize );
185
186             nCount = 0;
187             for ( auto it = m.crbegin(); it != m.crend(); ++it ) {
188                 EXPECT_EQ( it->second.nVal, 0 );
189                 ASSERT_TRUE( m.erase_at( it ));
190                 ++nCount;
191             }
192             EXPECT_EQ( nCount, kkSize );
193             ASSERT_TRUE( m.empty());
194             ASSERT_CONTAINER_SIZE( m, 0 );
195
196             // erase_at( const_iterator )
197             for ( auto const& i : arrKeys )
198                 ASSERT_TRUE( m.insert( i ));
199             ASSERT_FALSE( m.empty());
200             ASSERT_CONTAINER_SIZE( m, kkSize );
201
202             nCount = 0;
203             for ( auto it = m.cbegin(); it != m.cend(); ++it ) {
204                 EXPECT_EQ( it->second.nVal, 0 );
205                 ASSERT_TRUE( m.erase_at( it ));
206                 ++nCount;
207             }
208             EXPECT_EQ( nCount, kkSize );
209             ASSERT_TRUE( m.empty());
210             ASSERT_CONTAINER_SIZE( m, 0 );
211         }
212     };
213
214 } // namespace cds_test
215
216 #endif // #ifndef CDSUNIT_MAP_TEST_FELDMAN_HASHMAP_HP_H