2 This file is a part of libcds - Concurrent Data Structures library
4 (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2016
6 Source code repo: http://github.com/khizmax/libcds/
7 Download: http://sourceforge.net/projects/libcds/files/
9 Redistribution and use in source and binary forms, with or without
10 modification, are permitted provided that the following conditions are met:
12 * Redistributions of source code must retain the above copyright notice, this
13 list of conditions and the following disclaimer.
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.
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.
31 #ifndef CDSUNIT_MAP_TEST_MICHAEL_ITERABLE_MAP_H
32 #define CDSUNIT_MAP_TEST_MICHAEL_ITERABLE_MAP_H
34 #include "test_map_data.h"
36 // forward declaration
37 namespace cds { namespace container {} }
41 class michael_iterable_map: public map_fixture
44 static size_t const kSize = 1000;
50 // Precondition: map is empty
51 // Postcondition: map is empty
53 EXPECT_TRUE( m.empty());
54 EXPECT_CONTAINER_SIZE( m, 0 );
56 typedef typename Map::value_type map_pair;
57 size_t const kkSize = kSize;
59 std::vector<key_type> arrKeys;
60 for ( int i = 0; i < static_cast<int>(kkSize); ++i )
61 arrKeys.push_back( key_type( i ));
62 shuffle( arrKeys.begin(), arrKeys.end());
64 std::vector< value_type > arrVals;
65 for ( size_t i = 0; i < kkSize; ++i ) {
67 val.nVal = static_cast<int>( i );
68 val.strVal = std::to_string( i );
69 arrVals.push_back( val );
73 for ( auto const& i : arrKeys ) {
74 value_type const& val( arrVals.at( i.nKey ));
76 EXPECT_FALSE( m.contains( i.nKey ));
77 EXPECT_FALSE( m.contains( i ));
78 EXPECT_FALSE( m.contains( other_item( i.nKey ), other_less()));
79 EXPECT_FALSE( m.find( i, []( map_pair const& ) {
82 EXPECT_FALSE( m.find( i.nKey, []( map_pair const& ) {
85 EXPECT_FALSE( m.find_with( other_item( i.nKey ), other_less(), []( map_pair const& ) {
89 std::pair< bool, bool > updResult;
91 switch ( i.nKey % 17 ) {
93 EXPECT_TRUE( m.insert( i ));
94 EXPECT_FALSE( m.insert( i ));
95 EXPECT_TRUE( m.find( i.nKey, []( map_pair& v ) {
96 v.second.nVal = v.first.nKey;
97 v.second.strVal = std::to_string( v.first.nKey );
101 EXPECT_TRUE( m.insert( i.nKey ));
102 EXPECT_FALSE( m.insert( i.nKey ));
103 EXPECT_TRUE( m.find( i.nKey, []( map_pair& v ) {
104 v.second.nVal = v.first.nKey;
105 v.second.strVal = std::to_string( v.first.nKey );
109 EXPECT_TRUE( m.insert( std::to_string( i.nKey )));
110 EXPECT_FALSE( m.insert( std::to_string( i.nKey )));
111 EXPECT_TRUE( m.find( i.nKey, []( map_pair& v ) {
112 v.second.nVal = v.first.nKey;
113 v.second.strVal = std::to_string( v.first.nKey );
117 EXPECT_TRUE( m.insert( i, val ));
118 EXPECT_FALSE( m.insert( i, val ));
121 EXPECT_TRUE( m.insert( i.nKey, val.strVal ));
122 EXPECT_FALSE( m.insert( i.nKey, val.strVal ));
125 EXPECT_TRUE( m.insert( val.strVal, i.nKey ));
126 EXPECT_FALSE( m.insert( val.strVal, i.nKey ));
129 EXPECT_TRUE( m.insert_with( i, []( map_pair& v ) {
130 v.second.nVal = v.first.nKey;
131 v.second.strVal = std::to_string( v.first.nKey );
133 EXPECT_FALSE( m.insert_with( i, []( map_pair& ) {
134 EXPECT_TRUE( false );
138 EXPECT_TRUE( m.insert_with( i.nKey, []( map_pair& v ) {
139 v.second.nVal = v.first.nKey;
140 v.second.strVal = std::to_string( v.first.nKey );
142 EXPECT_FALSE( m.insert_with( i.nKey, []( map_pair& ) {
143 EXPECT_TRUE( false );
147 EXPECT_TRUE( m.insert_with( val.strVal, []( map_pair& v ) {
148 v.second.nVal = v.first.nKey;
149 v.second.strVal = std::to_string( v.first.nKey );
151 EXPECT_FALSE( m.insert_with( val.strVal, []( map_pair& ) {
152 EXPECT_TRUE( false );
156 updResult = m.update( i.nKey, []( map_pair&, map_pair* ) {
157 EXPECT_TRUE( false );
159 EXPECT_FALSE( updResult.first );
160 EXPECT_FALSE( updResult.second );
162 updResult = m.update( i.nKey, []( map_pair& v, map_pair* old ) {
163 EXPECT_TRUE( old == nullptr );
164 v.second.nVal = v.first.nKey;
166 EXPECT_TRUE( updResult.first );
167 EXPECT_TRUE( updResult.second );
169 updResult = m.update( i.nKey, []( map_pair& v, map_pair* old ) {
170 ASSERT_FALSE( old == nullptr );
171 EXPECT_EQ( v.first.nKey, old->second.nVal );
172 v.second.nVal = old->second.nVal;
173 v.second.strVal = std::to_string( old->second.nVal );
175 EXPECT_TRUE( updResult.first );
176 EXPECT_FALSE( updResult.second );
179 updResult = m.update( i, []( map_pair&, map_pair* ) {
180 EXPECT_TRUE( false );
182 EXPECT_FALSE( updResult.first );
183 EXPECT_FALSE( updResult.second );
185 updResult = m.update( i, []( map_pair& v, map_pair* old ) {
186 EXPECT_TRUE( old == nullptr );
187 v.second.nVal = v.first.nKey;
189 EXPECT_TRUE( updResult.first );
190 EXPECT_TRUE( updResult.second );
192 updResult = m.update( i, []( map_pair& v, map_pair* old ) {
193 ASSERT_FALSE( old == nullptr );
194 EXPECT_EQ( v.first.nKey, old->second.nVal );
195 v.second.nVal = old->second.nVal;
196 v.second.strVal = std::to_string( v.second.nVal );
198 EXPECT_TRUE( updResult.first );
199 EXPECT_FALSE( updResult.second );
202 updResult = m.update( val.strVal, []( map_pair&, map_pair* ) {
203 EXPECT_TRUE( false );
205 EXPECT_FALSE( updResult.first );
206 EXPECT_FALSE( updResult.second );
208 updResult = m.update( val.strVal, []( map_pair& v, map_pair* old ) {
209 EXPECT_TRUE( old == nullptr );
210 v.second.nVal = v.first.nKey;
212 EXPECT_TRUE( updResult.first );
213 EXPECT_TRUE( updResult.second );
215 updResult = m.update( val.strVal, []( map_pair& v, map_pair* old ) {
216 ASSERT_FALSE( old == nullptr );
217 EXPECT_EQ( v.first.nKey, old->second.nVal );
218 v.second.nVal = old->second.nVal;
219 v.second.strVal = std::to_string( v.second.nVal );
221 EXPECT_TRUE( updResult.first );
222 EXPECT_FALSE( updResult.second );
225 EXPECT_TRUE( m.emplace( i.nKey ));
226 EXPECT_FALSE( m.emplace( i.nKey ));
227 EXPECT_TRUE( m.find( i.nKey, []( map_pair& v ) {
228 v.second.nVal = v.first.nKey;
229 v.second.strVal = std::to_string( v.first.nKey );
233 EXPECT_TRUE( m.emplace( i, i.nKey ));
234 EXPECT_FALSE( m.emplace( i, i.nKey ));
238 std::string str = val.strVal;
239 EXPECT_TRUE( m.emplace( i, std::move( str )));
240 EXPECT_TRUE( str.empty());
242 EXPECT_FALSE( m.emplace( i, std::move( str )));
243 EXPECT_TRUE( str.empty());
248 std::string str = val.strVal;
249 EXPECT_TRUE( m.emplace( i, i.nKey, std::move( str )));
250 EXPECT_TRUE( str.empty());
252 EXPECT_FALSE( m.emplace( i, i.nKey, std::move( str )));
253 EXPECT_TRUE( str.empty());
258 auto res = m.upsert( i, i.nKey, false );
259 EXPECT_FALSE( res.first );
260 EXPECT_FALSE( res.second );
262 res = m.upsert( i, i.nKey );
263 EXPECT_TRUE( res.first );
264 EXPECT_TRUE( res.second );
266 std::string str = val.strVal;
267 res = m.upsert( i, std::move( str ));
268 EXPECT_TRUE( res.first );
269 EXPECT_FALSE( res.second );
270 EXPECT_TRUE( str.empty() );
275 EXPECT_TRUE( m.contains( i.nKey ));
276 EXPECT_TRUE( m.contains( i ));
277 EXPECT_TRUE( m.contains( other_item( i.nKey ), other_less()));
278 EXPECT_TRUE( m.find( i, []( map_pair const& v ) {
279 EXPECT_EQ( v.first.nKey, v.second.nVal );
280 EXPECT_EQ( std::to_string( v.first.nKey ), v.second.strVal );
282 EXPECT_TRUE( m.find( i.nKey, []( map_pair const& v ) {
283 EXPECT_EQ( v.first.nKey, v.second.nVal );
284 EXPECT_EQ( std::to_string( v.first.nKey ), v.second.strVal );
286 EXPECT_TRUE( m.find_with( other_item( i.nKey ), other_less(), []( map_pair const& v ) {
287 EXPECT_EQ( v.first.nKey, v.second.nVal );
288 EXPECT_EQ( std::to_string( v.first.nKey ), v.second.strVal );
291 EXPECT_FALSE( m.empty() );
292 EXPECT_CONTAINER_SIZE( m, kkSize );
293 EXPECT_FALSE( m.begin() == m.end() );
294 EXPECT_FALSE( m.cbegin() == m.cend() );
296 shuffle( arrKeys.begin(), arrKeys.end() );
299 for ( auto const& i : arrKeys ) {
300 value_type const& val( arrVals.at( i.nKey ) );
302 EXPECT_TRUE( m.contains( i.nKey ));
303 EXPECT_TRUE( m.contains( val.strVal ) );
304 EXPECT_TRUE( m.contains( i ));
305 EXPECT_TRUE( m.contains( other_item( i.nKey ), other_less()));
306 EXPECT_TRUE( m.find( i, []( map_pair const& v ) {
307 EXPECT_EQ( v.first.nKey, v.second.nVal );
308 EXPECT_EQ( std::to_string( v.first.nKey ), v.second.strVal );
310 EXPECT_TRUE( m.find( i.nKey, []( map_pair const& v ) {
311 EXPECT_EQ( v.first.nKey, v.second.nVal );
312 EXPECT_EQ( std::to_string( v.first.nKey ), v.second.strVal );
314 EXPECT_TRUE( m.find_with( other_item( i.nKey ), other_less(), []( map_pair const& v ) {
315 EXPECT_EQ( v.first.nKey, v.second.nVal );
316 EXPECT_EQ( std::to_string( v.first.nKey ), v.second.strVal );
320 switch ( i.nKey % 8 ) {
322 EXPECT_TRUE( m.erase( i ));
323 EXPECT_FALSE( m.erase( i ));
326 EXPECT_TRUE( m.erase( i.nKey ));
327 EXPECT_FALSE( m.erase( i.nKey ));
330 EXPECT_TRUE( m.erase( val.strVal ));
331 EXPECT_FALSE( m.erase( val.strVal ));
334 EXPECT_TRUE( m.erase_with( other_item( i.nKey ), other_less()));
335 EXPECT_FALSE( m.erase_with( other_item( i.nKey ), other_less()));
338 EXPECT_TRUE( m.erase( i, []( map_pair& v ) {
339 EXPECT_EQ( v.first.nKey, v.second.nVal );
340 EXPECT_EQ( std::to_string( v.first.nKey ), v.second.strVal );
342 EXPECT_FALSE( m.erase( i, []( map_pair& ) {
343 EXPECT_TRUE( false );
347 EXPECT_TRUE( m.erase( i.nKey, []( map_pair& v ) {
348 EXPECT_EQ( v.first.nKey, v.second.nVal );
349 EXPECT_EQ( std::to_string( v.first.nKey ), v.second.strVal );
351 EXPECT_FALSE( m.erase( i.nKey, []( map_pair& ) {
352 EXPECT_TRUE( false );
356 EXPECT_TRUE( m.erase( val.strVal, []( map_pair& v ) {
357 EXPECT_EQ( v.first.nKey, v.second.nVal );
358 EXPECT_EQ( std::to_string( v.first.nKey ), v.second.strVal );
360 EXPECT_FALSE( m.erase( val.strVal, []( map_pair& ) {
361 EXPECT_TRUE( false );
365 EXPECT_TRUE( m.erase_with( other_item( i.nKey ), other_less(), []( map_pair& v ) {
366 EXPECT_EQ( v.first.nKey, v.second.nVal );
367 EXPECT_EQ( std::to_string( v.first.nKey ), v.second.strVal );
369 EXPECT_FALSE( m.erase_with( other_item( i.nKey ), other_less(), []( map_pair& ) {
370 EXPECT_TRUE( false );
375 EXPECT_FALSE( m.contains( i.nKey ));
376 EXPECT_FALSE( m.contains( i ));
377 EXPECT_FALSE( m.contains( val.strVal ));
378 EXPECT_FALSE( m.contains( other_item( i.nKey ), other_less()));
379 EXPECT_FALSE( m.find( i, []( map_pair const& ) {
380 EXPECT_TRUE( false );
382 EXPECT_FALSE( m.find( i.nKey, []( map_pair const& ) {
383 EXPECT_TRUE( false );
385 EXPECT_FALSE( m.find_with( other_item( i.nKey ), other_less(), []( map_pair const& ) {
386 EXPECT_TRUE( false );
389 EXPECT_TRUE( m.empty() );
390 EXPECT_CONTAINER_SIZE( m, 0 );
392 EXPECT_TRUE( m.begin() == m.end());
393 EXPECT_TRUE( m.cbegin() == m.cend());
396 for ( auto const& i : arrKeys )
397 EXPECT_TRUE( m.insert( i ));
399 EXPECT_FALSE( m.empty() );
400 EXPECT_CONTAINER_SIZE( m, kkSize );
404 EXPECT_TRUE( m.empty() );
405 EXPECT_CONTAINER_SIZE( m, 0 );
409 } // namespace cds_test
411 #endif // #ifndef CDSUNIT_MAP_TEST_MICHAEL_ITERABLE_MAP_H