2 This file is a part of libcds - Concurrent Data Structures library
4 (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2017
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_SET_TEST_MICHAEL_ITERABLE_H
32 #define CDSUNIT_SET_TEST_MICHAEL_ITERABLE_H
34 #include "test_set_data.h"
38 class michael_iterable_set: public container_set_data
41 template <typename Set>
44 // Precondition: set is empty
45 // Postcondition: set is empty
47 EXPECT_TRUE( s.empty());
48 EXPECT_CONTAINER_SIZE( s, 0 );
49 size_t const nSetSize = kSize;
51 typedef typename Set::value_type value_type;
53 std::vector< value_type > data;
54 std::vector< size_t> indices;
55 data.reserve( kSize );
56 indices.reserve( kSize );
57 for ( size_t key = 0; key < kSize; ++key ) {
58 data.push_back( value_type( static_cast<int>(key)));
59 indices.push_back( key );
61 shuffle( indices.begin(), indices.end());
64 for ( auto idx : indices ) {
67 EXPECT_FALSE( s.contains( i.nKey ));
68 EXPECT_FALSE( s.contains( i ));
69 EXPECT_FALSE( s.contains( other_item( i.key()), other_less()));
70 EXPECT_FALSE( s.find( i.nKey, []( value_type&, int ) {} ));
71 EXPECT_FALSE( s.find( i, []( value_type&, value_type const& ) {} ));
72 EXPECT_FALSE( s.find_with( other_item( i.key()), other_less(), []( value_type&, other_item const& ) {} ));
73 EXPECT_TRUE( s.find( i ) == s.end());
74 EXPECT_TRUE( s.find( i.nKey ) == s.end());
75 EXPECT_TRUE( s.find_with( other_item( i.key()), other_less()) == s.end());
77 std::pair<bool, bool> updResult;
80 updResult = s.update( i.key(), []( value_type&, value_type* old )
82 EXPECT_TRUE( old == nullptr );
84 EXPECT_FALSE( updResult.first );
85 EXPECT_FALSE( updResult.second );
89 EXPECT_TRUE( s.insert( i ));
90 EXPECT_FALSE( s.insert( i ));
91 updResult = s.update( i, []( value_type& val, value_type* old)
93 ASSERT_FALSE( old == nullptr );
94 EXPECT_EQ( val.key(), old->key());
96 EXPECT_TRUE( updResult.first );
97 EXPECT_FALSE( updResult.second );
100 EXPECT_TRUE( s.insert( i.key()));
101 EXPECT_FALSE( s.insert( i.key()));
102 updResult = s.update( i.key(), []( value_type& val, value_type* old)
104 ASSERT_FALSE( old == nullptr );
105 EXPECT_EQ( val.key(), old->key());
107 EXPECT_TRUE( updResult.first );
108 EXPECT_FALSE( updResult.second );
111 EXPECT_TRUE( s.insert( i, []( value_type& v ) { ++v.nFindCount; } ));
112 EXPECT_FALSE( s.insert( i, []( value_type& v ) { ++v.nFindCount; } ));
113 EXPECT_TRUE( s.find( i.nKey, []( value_type const& v, int key )
115 EXPECT_EQ( v.key(), key );
116 EXPECT_EQ( v.nFindCount, 1u );
120 EXPECT_TRUE( s.insert( i.key(), []( value_type& v ) { ++v.nFindCount; } ));
121 EXPECT_FALSE( s.insert( i.key(), []( value_type& v ) { ++v.nFindCount; } ));
122 EXPECT_TRUE( s.find( i.nKey, []( value_type const& v, int key )
124 EXPECT_EQ( v.key(), key );
125 EXPECT_EQ( v.nFindCount, 1u );
129 updResult = s.update( i, [&i]( value_type& v, value_type* old )
131 EXPECT_TRUE( old == nullptr );
132 EXPECT_EQ( v.key(), i.key());
135 EXPECT_TRUE( updResult.first );
136 EXPECT_TRUE( updResult.second );
138 updResult = s.update( i, []( value_type& v, value_type* old )
140 ASSERT_FALSE( old == nullptr );
141 EXPECT_EQ( v.key(), old->key());
142 EXPECT_EQ( old->nUpdateNewCount, 1u );
143 v.nUpdateNewCount = old->nUpdateNewCount;
146 EXPECT_TRUE( updResult.first );
147 EXPECT_FALSE( updResult.second );
149 EXPECT_TRUE( s.find( i.nKey, []( value_type const& v, int key )
151 EXPECT_EQ( v.key(), key );
152 EXPECT_EQ( v.nUpdateNewCount, 1u );
153 EXPECT_EQ( v.nUpdateCount, 1u );
157 updResult = s.update( i.key(), [&i]( value_type& v, value_type* old )
159 EXPECT_TRUE( old == nullptr );
160 EXPECT_EQ( v.key(), i.key());
163 EXPECT_TRUE( updResult.first );
164 EXPECT_TRUE( updResult.second );
166 updResult = s.update( i.key(), []( value_type& v, value_type* old )
168 EXPECT_FALSE( old == nullptr );
169 EXPECT_EQ( v.key(), old->key());
170 EXPECT_EQ( old->nUpdateNewCount, 1u );
171 v.nUpdateNewCount = old->nUpdateNewCount;
174 EXPECT_TRUE( updResult.first );
175 EXPECT_FALSE( updResult.second );
177 EXPECT_TRUE( s.find( i, []( value_type const& v, value_type const& arg )
179 EXPECT_EQ( v.key(), arg.key());
180 EXPECT_EQ( v.nUpdateNewCount, 1u );
181 EXPECT_EQ( v.nUpdateCount, 1u );
185 EXPECT_TRUE( s.emplace( i.key()));
186 EXPECT_TRUE( s.find( i, []( value_type const& v, value_type const& arg )
188 EXPECT_EQ( v.key(), arg.key());
189 EXPECT_EQ( v.nVal, arg.nVal );
194 EXPECT_TRUE( s.emplace( i.key(), std::move( str )));
195 EXPECT_TRUE( str.empty());
196 EXPECT_TRUE( s.find( i, []( value_type const& v, value_type const& arg )
198 EXPECT_EQ( v.key(), arg.key());
199 EXPECT_EQ( v.nVal, arg.nVal );
200 EXPECT_EQ( v.strVal, std::string( "Hello!" ));
205 EXPECT_TRUE( s.insert( value_type( i.key(), std::move( str ))));
206 EXPECT_TRUE( str.empty());
207 EXPECT_TRUE( s.find( i, []( value_type const& v, value_type const& arg )
209 EXPECT_EQ( v.key(), arg.key());
210 EXPECT_EQ( v.nVal, arg.nVal );
211 EXPECT_EQ( v.strVal, std::string( "Hello!" ));
215 updResult = s.upsert( i.key(), false );
216 EXPECT_FALSE( updResult.first );
217 EXPECT_FALSE( updResult.second );
219 updResult = s.upsert( i.key());
220 EXPECT_TRUE( updResult.first );
221 EXPECT_TRUE( updResult.second );
223 updResult = s.upsert( i.key(), false );
224 EXPECT_TRUE( updResult.first );
225 EXPECT_FALSE( updResult.second );
227 EXPECT_TRUE( s.find( i, []( value_type const& v, value_type const& arg )
229 EXPECT_EQ( v.key(), arg.key());
233 updResult = s.upsert( i, false );
234 EXPECT_FALSE( updResult.first );
235 EXPECT_FALSE( updResult.second );
237 updResult = s.upsert( i );
238 EXPECT_TRUE( updResult.first );
239 EXPECT_TRUE( updResult.second );
241 updResult = s.upsert( i, false );
242 EXPECT_TRUE( updResult.first );
243 EXPECT_FALSE( updResult.second );
245 EXPECT_TRUE( s.find( i, []( value_type const& v, value_type const& arg )
247 EXPECT_EQ( v.key(), arg.key());
251 // forgot anything?..
252 EXPECT_TRUE( false );
255 EXPECT_TRUE( s.contains( i.nKey ));
256 EXPECT_TRUE( s.contains( i ));
257 EXPECT_TRUE( s.contains( other_item( i.key()), other_less()));
258 EXPECT_TRUE( s.find( i.nKey, []( value_type&, int ) {} ));
259 EXPECT_TRUE( s.find( i, []( value_type&, value_type const& ) {} ));
260 EXPECT_TRUE( s.find_with( other_item( i.key()), other_less(), []( value_type&, other_item const& ) {} ));
261 EXPECT_FALSE( s.find( i.nKey ) == s.end());
262 EXPECT_FALSE( s.find( i ) == s.end());
263 EXPECT_FALSE( s.find_with( other_item( i.key()), other_less()) == s.end());
266 EXPECT_FALSE( s.empty());
267 EXPECT_CONTAINER_SIZE( s, nSetSize );
270 shuffle( indices.begin(), indices.end());
271 for ( auto idx : indices ) {
274 EXPECT_TRUE( s.contains( i.nKey ));
275 EXPECT_TRUE( s.contains( i ));
276 EXPECT_TRUE( s.contains( other_item( i.key()), other_less()));
277 EXPECT_TRUE( s.find( i.nKey, []( value_type& v, int )
281 EXPECT_TRUE( s.find( i, []( value_type& v, value_type const& )
283 EXPECT_EQ( ++v.nFindCount, 2u );
285 EXPECT_TRUE( s.find_with( other_item( i.key()), other_less(), []( value_type& v, other_item const& )
287 EXPECT_EQ( ++v.nFindCount, 3u );
290 int nKey = i.key() - 1;
293 EXPECT_TRUE( s.erase( i.key()));
294 EXPECT_FALSE( s.erase( i.key()));
297 EXPECT_TRUE( s.erase( i ));
298 EXPECT_FALSE( s.erase( i ));
301 EXPECT_TRUE( s.erase_with( other_item( i.key()), other_less()));
302 EXPECT_FALSE( s.erase_with( other_item( i.key()), other_less()));
305 EXPECT_TRUE( s.erase( i.key(), [&nKey]( value_type const& v )
309 EXPECT_EQ( i.key(), nKey );
312 EXPECT_FALSE( s.erase( i.key(), [&nKey]( value_type const& v )
316 EXPECT_EQ( i.key(), nKey + 1 );
319 EXPECT_TRUE( s.erase( i, [&nKey]( value_type const& v )
323 EXPECT_EQ( i.key(), nKey );
326 EXPECT_FALSE( s.erase( i, [&nKey]( value_type const& v )
330 EXPECT_EQ( i.key(), nKey + 1 );
333 EXPECT_TRUE( s.erase_with( other_item( i.key()), other_less(), [&nKey]( value_type const& v )
337 EXPECT_EQ( i.key(), nKey );
340 EXPECT_FALSE( s.erase_with( other_item( i.key()), other_less(), [&nKey]( value_type const& v )
344 EXPECT_EQ( i.key(), nKey + 1 );
348 EXPECT_FALSE( s.contains( i.nKey ));
349 EXPECT_FALSE( s.contains( i ));
350 EXPECT_FALSE( s.contains( other_item( i.key()), other_less()));
351 EXPECT_FALSE( s.find( i.nKey, []( value_type&, int ) {} ));
352 EXPECT_FALSE( s.find( i, []( value_type&, value_type const& ) {} ));
353 EXPECT_FALSE( s.find_with( other_item( i.key()), other_less(), []( value_type&, other_item const& ) {} ));
355 EXPECT_TRUE( s.empty());
356 EXPECT_CONTAINER_SIZE( s, 0u );
360 for ( auto& i : data ) {
361 EXPECT_TRUE( s.insert( i ));
364 EXPECT_FALSE( s.empty());
365 EXPECT_CONTAINER_SIZE( s, nSetSize );
369 EXPECT_TRUE( s.empty());
370 EXPECT_CONTAINER_SIZE( s, 0u );
372 EXPECT_TRUE( s.begin() == s.end());
373 EXPECT_TRUE( s.cbegin() == s.cend());
377 } // namespace cds_test
379 #endif // CDSUNIT_SET_TEST_MICHAEL_ITERABLE_H