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_SET_TEST_SPLIT_ITERABLE_H
32 #define CDSUNIT_SET_TEST_SPLIT_ITERABLE_H
34 #include "test_set_data.h"
36 #include <cds/opt/hash.h>
40 class split_iterable_set : public container_set_data
43 template <typename Set>
46 // Precondition: set is empty
47 // Postcondition: set is empty
49 ASSERT_TRUE( s.empty());
50 ASSERT_CONTAINER_SIZE( s, 0 );
51 size_t const nSetSize = kSize;
53 typedef typename Set::value_type value_type;
55 std::vector< value_type > data;
56 std::vector< size_t> indices;
57 data.reserve( kSize );
58 indices.reserve( kSize );
59 for ( size_t key = 0; key < kSize; ++key ) {
60 data.push_back( value_type( static_cast<int>(key)));
61 indices.push_back( key );
63 shuffle( indices.begin(), indices.end());
66 for ( auto idx : indices ) {
69 EXPECT_FALSE( s.contains( i.nKey ));
70 EXPECT_FALSE( s.contains( i ));
71 EXPECT_FALSE( s.contains( other_item( i.key()), other_less()));
72 EXPECT_FALSE( s.find( i.nKey, []( value_type&, int ) {} ));
73 EXPECT_FALSE( s.find( i, []( value_type&, value_type const& ) {} ));
74 EXPECT_FALSE( s.find_with( other_item( i.key()), other_less(), []( value_type&, other_item const& ) {} ));
76 EXPECT_TRUE( s.find( i.nKey ) == s.end());
77 EXPECT_TRUE( s.find( i ) == s.end());
78 EXPECT_TRUE( s.find_with( other_item( i.key()), other_less()) == s.end());
80 std::pair<bool, bool> updResult;
83 updResult = s.update( i.key(), []( value_type&, value_type* )
87 EXPECT_FALSE( updResult.first );
88 EXPECT_FALSE( updResult.second );
92 EXPECT_TRUE( s.insert( i ));
93 EXPECT_FALSE( s.insert( i ));
94 updResult = s.update( i, []( value_type& cur, value_type* old )
96 EXPECT_FALSE( old == nullptr );
97 EXPECT_EQ( cur.key(), old->key());
99 EXPECT_TRUE( updResult.first );
100 EXPECT_FALSE( updResult.second );
103 EXPECT_TRUE( s.insert( i.key()));
104 EXPECT_FALSE( s.insert( i.key()));
105 updResult = s.update( i.key(), []( value_type& cur, value_type* old )
107 EXPECT_FALSE( old == nullptr );
108 EXPECT_EQ( cur.key(), old->key());
110 EXPECT_TRUE( updResult.first );
111 EXPECT_FALSE( updResult.second );
114 EXPECT_TRUE( s.insert( i, []( value_type& v ) { ++v.nFindCount; } ));
115 EXPECT_FALSE( s.insert( i, []( value_type& v ) { ++v.nFindCount; } ));
116 EXPECT_TRUE( s.find( i.nKey, []( value_type const& v, int key )
118 EXPECT_EQ( v.key(), key );
119 EXPECT_EQ( v.nFindCount, 1u );
123 EXPECT_TRUE( s.insert( i.key(), []( value_type& v ) { ++v.nFindCount; } ));
124 EXPECT_FALSE( s.insert( i.key(), []( value_type& v ) { ++v.nFindCount; } ));
125 EXPECT_TRUE( s.find( i.nKey, []( value_type const& v, int key )
127 EXPECT_EQ( v.key(), key );
128 EXPECT_EQ( v.nFindCount, 1u );
132 updResult = s.update( i, []( value_type& v, value_type* old )
134 EXPECT_TRUE( old == nullptr );
137 EXPECT_TRUE( updResult.first );
138 EXPECT_TRUE( updResult.second );
140 updResult = s.update( i, []( value_type& v, value_type* old )
142 EXPECT_FALSE( old == nullptr );
143 EXPECT_EQ( v.key(), old->key());
144 v.nUpdateNewCount = old->nUpdateNewCount + 1;
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, 2u );
156 updResult = s.update( i.key(), [&i]( value_type& v, value_type* old )
158 EXPECT_TRUE( old == nullptr );
159 EXPECT_EQ( i.key(), v.key());
162 EXPECT_TRUE( updResult.first );
163 EXPECT_TRUE( updResult.second );
165 updResult = s.update( i.key(), []( value_type& v, value_type* old )
167 EXPECT_FALSE( old == nullptr );
168 EXPECT_EQ( v.key(), old->key());
169 v.nUpdateNewCount = old->nUpdateNewCount + 1;
171 EXPECT_TRUE( updResult.first );
172 EXPECT_FALSE( updResult.second );
174 EXPECT_TRUE( s.find( i, []( value_type const& v, value_type const& arg )
176 EXPECT_EQ( v.key(), arg.key());
177 EXPECT_EQ( v.nUpdateNewCount, 2u );
181 EXPECT_TRUE( s.emplace( i.key()));
182 EXPECT_TRUE( s.find( i, []( value_type const& v, value_type const& arg )
184 EXPECT_EQ( v.key(), arg.key());
185 EXPECT_EQ( v.nVal, arg.nVal );
190 EXPECT_TRUE( s.emplace( i.key(), std::move( str )));
191 EXPECT_TRUE( str.empty());
192 EXPECT_TRUE( s.find( i, []( value_type const& v, value_type const& arg )
194 EXPECT_EQ( v.key(), arg.key());
195 EXPECT_EQ( v.nVal, arg.nVal );
196 EXPECT_EQ( v.strVal, std::string( "Hello!" ));
201 updResult = s.upsert( i.key(), false );
202 EXPECT_FALSE( updResult.first );
203 EXPECT_FALSE( updResult.second );
204 EXPECT_TRUE( s.find( i.key()) == s.end());
206 updResult = s.upsert( i.key());
207 EXPECT_TRUE( updResult.first );
208 EXPECT_TRUE( updResult.second );
210 auto it = s.find( i.key());
211 ASSERT_FALSE( it == s.end());
212 EXPECT_EQ( it->key(), i.key());
214 updResult = s.upsert( i.key(), false );
215 EXPECT_TRUE( updResult.first );
216 EXPECT_FALSE( updResult.second );
218 EXPECT_TRUE( s.find( i, []( value_type const& v, value_type const& arg )
220 EXPECT_EQ( v.key(), arg.key());
226 updResult = s.upsert( i, false );
227 EXPECT_FALSE( updResult.first );
228 EXPECT_FALSE( updResult.second );
229 EXPECT_TRUE( s.find( i ) == s.end());
231 updResult = s.upsert( i );
232 EXPECT_TRUE( updResult.first );
233 EXPECT_TRUE( updResult.second );
235 auto it = s.find( i );
236 ASSERT_FALSE( it == s.end());
237 EXPECT_EQ( it->key(), i.key());
239 updResult = s.upsert( i, false );
240 EXPECT_TRUE( updResult.first );
241 EXPECT_FALSE( updResult.second );
243 EXPECT_TRUE( s.find( i, []( value_type const& v, value_type const& arg )
245 EXPECT_EQ( v.key(), arg.key());
251 // forgot anything?..
252 ASSERT_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& ) {} ));
262 auto it = s.find( i );
263 ASSERT_FALSE( it == s.end());
264 EXPECT_EQ( it->key(), i.key());
266 it = s.find_with( other_item( i.key()), other_less());
267 ASSERT_FALSE( it == s.end());
268 EXPECT_EQ( it->key(), i.key());
271 EXPECT_FALSE( s.empty());
272 EXPECT_CONTAINER_SIZE( s, nSetSize );
275 shuffle( indices.begin(), indices.end());
276 for ( auto idx : indices ) {
279 EXPECT_TRUE( s.contains( i.nKey ));
280 EXPECT_TRUE( s.contains( i ));
281 EXPECT_TRUE( s.contains( other_item( i.key()), other_less()));
282 EXPECT_TRUE( s.find( i.nKey, []( value_type& v, int )
286 EXPECT_TRUE( s.find( i, []( value_type& v, value_type const& )
288 EXPECT_EQ( ++v.nFindCount, 2u );
290 EXPECT_TRUE( s.find_with( other_item( i.key()), other_less(), []( value_type& v, other_item const& )
292 EXPECT_EQ( ++v.nFindCount, 3u );
295 auto it = s.find( i );
296 ASSERT_FALSE( it == s.end());
297 EXPECT_EQ( it->key(), i.key());
298 it = s.find_with( other_item( i.key()), other_less());
299 ASSERT_FALSE( it == s.end());
300 EXPECT_EQ( it->key(), i.key());
303 int nKey = i.key() - 1;
306 EXPECT_TRUE( s.erase( i.key()));
307 EXPECT_FALSE( s.erase( i.key()));
310 EXPECT_TRUE( s.erase( i ));
311 EXPECT_FALSE( s.erase( i ));
314 EXPECT_TRUE( s.erase_with( other_item( i.key()), other_less()));
315 EXPECT_FALSE( s.erase_with( other_item( i.key()), other_less()));
318 EXPECT_TRUE( s.erase( i.key(), [&nKey]( value_type const& v )
322 EXPECT_EQ( i.key(), nKey );
325 EXPECT_FALSE( s.erase( i.key(), [&nKey]( value_type const& v )
329 EXPECT_EQ( i.key(), nKey + 1 );
332 EXPECT_TRUE( s.erase( i, [&nKey]( value_type const& v )
336 EXPECT_EQ( i.key(), nKey );
339 EXPECT_FALSE( s.erase( i, [&nKey]( value_type const& v )
343 EXPECT_EQ( i.key(), nKey + 1 );
346 EXPECT_TRUE( s.erase_with( other_item( i.key()), other_less(), [&nKey]( value_type const& v )
350 EXPECT_EQ( i.key(), nKey );
353 EXPECT_FALSE( s.erase_with( other_item( i.key()), other_less(), [&nKey]( value_type const& v )
357 EXPECT_EQ( i.key(), nKey + 1 );
361 EXPECT_FALSE( s.contains( i.nKey ));
362 EXPECT_FALSE( s.contains( i ));
363 EXPECT_FALSE( s.contains( other_item( i.key()), other_less()));
364 EXPECT_FALSE( s.find( i.nKey, []( value_type&, int ) {} ));
365 EXPECT_FALSE( s.find( i, []( value_type&, value_type const& ) {} ));
366 EXPECT_FALSE( s.find_with( other_item( i.key()), other_less(), []( value_type&, other_item const& ) {} ));
368 EXPECT_TRUE( s.find( i.nKey ) == s.end());
369 EXPECT_TRUE( s.find( i ) == s.end());
370 EXPECT_TRUE( s.find_with( other_item( i.key()), other_less()) == s.end());
372 EXPECT_TRUE( s.empty());
373 EXPECT_CONTAINER_SIZE( s, 0u );
376 for ( auto& i : data ) {
377 EXPECT_TRUE( s.insert( i ));
380 EXPECT_FALSE( s.empty());
381 EXPECT_CONTAINER_SIZE( s, nSetSize );
385 EXPECT_TRUE( s.empty());
386 EXPECT_CONTAINER_SIZE( s, 0u );
388 EXPECT_TRUE( s.begin() == s.end());
389 EXPECT_TRUE( s.cbegin() == s.cend());
393 } // namespace cds_test
395 #endif // CDSUNIT_SET_TEST_SPLIT_ITERABLE_H