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_INTRUSIVE_SET_H
32 #define CDSUNIT_SET_TEST_INTRUSIVE_SET_H
34 #include <cds_test/check_size.h>
35 #include <cds_test/fixture.h>
37 #include <cds/opt/hash.h>
38 #include <functional> // ref
40 // forward declaration
41 namespace cds { namespace intrusive {}}
45 namespace ci = cds::intrusive;
46 namespace co = cds::opt;
48 class intrusive_set: public fixture
51 static size_t const kSize = 100;
55 unsigned int nDisposeCount ; // count of disposer calling
56 unsigned int nFindCount ; // count of find-functor calling
57 unsigned int nUpdateNewCount;
58 unsigned int nUpdateCount;
59 mutable unsigned int nEraseCount;
68 memset( this, 0, sizeof( *this ));
72 template <typename Node>
84 explicit base_int_item( int key )
89 base_int_item(int key, int val)
94 base_int_item( base_int_item const& v )
107 template <typename Node>
108 struct member_int_item: public stat
120 explicit member_int_item( int key )
125 member_int_item(int key, int val)
130 member_int_item(member_int_item const& v )
143 size_t operator()( int i ) const
145 return co::v::hash<int>()( i );
147 template <typename Item>
148 size_t operator()( const Item& i ) const
150 return (*this)( i.key());
154 struct simple_item_counter {
157 simple_item_counter()
176 operator size_t() const
184 template <typename T>
187 bool operator ()(const T& v1, const T& v2 ) const
189 return v1.key() < v2.key();
192 template <typename Q>
193 bool operator ()(const T& v1, const Q& v2 ) const
195 return v1.key() < v2;
198 template <typename Q>
199 bool operator ()(const Q& v1, const T& v2 ) const
201 return v1 < v2.key();
205 template <typename T>
207 int operator ()(const T& v1, const T& v2 ) const
209 if ( v1.key() < v2.key())
211 return v1.key() > v2.key() ? 1 : 0;
214 template <typename Q>
215 int operator ()(const T& v1, const Q& v2 ) const
219 return v1.key() > v2 ? 1 : 0;
222 template <typename Q>
223 int operator ()(const Q& v1, const T& v2 ) const
227 return v1 > v2.key() ? 1 : 0;
234 explicit other_item( int k )
245 template <typename Q, typename T>
246 bool operator()( Q const& lhs, T const& rhs ) const
248 return lhs.key() < rhs.key();
254 template <typename T>
255 void operator ()( T * p )
265 // Precondition: set is empty
266 // Postcondition: set is empty
268 ASSERT_TRUE( s.empty());
269 ASSERT_CONTAINER_SIZE( s, 0 );
270 size_t const nSetSize = kSize;
272 typedef typename Set::value_type value_type;
274 std::vector< value_type > data;
275 std::vector< size_t> indices;
276 data.reserve( kSize );
277 indices.reserve( kSize );
278 for ( size_t key = 0; key < kSize; ++key ) {
279 data.push_back( value_type( static_cast<int>( key )));
280 indices.push_back( key );
282 shuffle( indices.begin(), indices.end());
285 for ( auto idx : indices ) {
286 auto& i = data[ idx ];
288 ASSERT_FALSE( s.contains( i.nKey ));
289 ASSERT_FALSE( s.contains( i ));
290 ASSERT_FALSE( s.contains( other_item( i.key()), other_less()));
291 ASSERT_FALSE( s.find( i.nKey, []( value_type&, int ) {} ));
292 ASSERT_FALSE( s.find_with( other_item( i.key()), other_less(), []( value_type&, other_item const& ) {} ));
294 std::pair<bool, bool> updResult;
296 updResult = s.update( i, []( bool, value_type&, value_type& )
298 ASSERT_TRUE( false );
300 EXPECT_FALSE( updResult.first );
301 EXPECT_FALSE( updResult.second );
303 switch ( i.key() % 3 ) {
305 ASSERT_TRUE( s.insert( i ));
306 ASSERT_FALSE( s.insert( i ));
307 updResult = s.update( i, []( bool bNew, value_type& val, value_type& arg)
309 EXPECT_FALSE( bNew );
310 EXPECT_EQ( &val, &arg );
312 EXPECT_TRUE( updResult.first );
313 EXPECT_FALSE( updResult.second );
316 EXPECT_EQ( i.nUpdateNewCount, 0u );
317 ASSERT_TRUE( s.insert( i, []( value_type& v ) { ++v.nUpdateNewCount;} ));
318 EXPECT_EQ( i.nUpdateNewCount, 1u );
319 ASSERT_FALSE( s.insert( i, []( value_type& v ) { ++v.nUpdateNewCount;} ));
320 EXPECT_EQ( i.nUpdateNewCount, 1u );
321 i.nUpdateNewCount = 0;
324 updResult = s.update( i, []( bool bNew, value_type& val, value_type& arg )
327 EXPECT_EQ( &val, &arg );
329 EXPECT_TRUE( updResult.first );
330 EXPECT_TRUE( updResult.second );
334 ASSERT_TRUE( s.contains( i.nKey ));
335 ASSERT_TRUE( s.contains( i ));
336 ASSERT_TRUE( s.contains( other_item( i.key()), other_less()));
337 EXPECT_EQ( i.nFindCount, 0u );
338 ASSERT_TRUE( s.find( i.nKey, []( value_type& v, int ) { ++v.nFindCount; } ));
339 EXPECT_EQ( i.nFindCount, 1u );
340 ASSERT_TRUE( s.find_with( other_item( i.key()), other_less(), []( value_type& v, other_item const& ) { ++v.nFindCount; } ));
341 EXPECT_EQ( i.nFindCount, 2u );
343 ASSERT_FALSE( s.empty());
344 ASSERT_CONTAINER_SIZE( s, nSetSize );
346 std::for_each( data.begin(), data.end(), []( value_type& v ) { v.clear_stat(); });
349 shuffle( indices.begin(), indices.end());
350 for ( auto idx : indices ) {
351 auto& i = data[ idx ];
353 ASSERT_TRUE( s.contains( i.nKey ));
354 ASSERT_TRUE( s.contains( i ));
355 ASSERT_TRUE( s.contains( other_item( i.key()), other_less()));
356 EXPECT_EQ( i.nFindCount, 0u );
357 ASSERT_TRUE( s.find( i.nKey, []( value_type& v, int ) { ++v.nFindCount; } ));
358 EXPECT_EQ( i.nFindCount, 1u );
359 ASSERT_TRUE( s.find_with( other_item( i.key()), other_less(), []( value_type& v, other_item const& ) { ++v.nFindCount; } ));
360 EXPECT_EQ( i.nFindCount, 2u );
363 switch ( i.key() % 6 ) {
365 ASSERT_FALSE( s.unlink( v ));
366 ASSERT_TRUE( s.unlink( i ));
367 ASSERT_FALSE( s.unlink( i ));
370 ASSERT_TRUE( s.erase( i.key()));
371 ASSERT_FALSE( s.erase( i.key()));
374 ASSERT_TRUE( s.erase( v ));
375 ASSERT_FALSE( s.erase( v ));
378 ASSERT_TRUE( s.erase_with( other_item( i.key()), other_less()));
379 ASSERT_FALSE( s.erase_with( other_item( i.key()), other_less()));
382 EXPECT_EQ( i.nEraseCount, 0u );
383 ASSERT_TRUE( s.erase( v, []( value_type& val ) { ++val.nEraseCount; } ));
384 EXPECT_EQ( i.nEraseCount, 1u );
385 ASSERT_FALSE( s.erase( v, []( value_type& val ) { ++val.nEraseCount; } ));
386 EXPECT_EQ( i.nEraseCount, 1u );
389 EXPECT_EQ( i.nEraseCount, 0u );
390 ASSERT_TRUE( s.erase_with( other_item( i.key()), other_less(), []( value_type& val ) { ++val.nEraseCount; } ));
391 EXPECT_EQ( i.nEraseCount, 1u );
392 ASSERT_FALSE( s.erase_with( other_item( i.key()), other_less(), []( value_type& val ) { ++val.nEraseCount; } ));
393 EXPECT_EQ( i.nEraseCount, 1u );
397 ASSERT_FALSE( s.contains( i.nKey ));
398 ASSERT_FALSE( s.contains( i ));
399 ASSERT_FALSE( s.contains( other_item( i.key()), other_less()));
400 ASSERT_FALSE( s.find( i.nKey, []( value_type&, int ) {} ));
401 ASSERT_FALSE( s.find_with( other_item( i.key()), other_less(), []( value_type&, other_item const& ) {} ));
403 ASSERT_TRUE( s.empty());
404 ASSERT_CONTAINER_SIZE( s, 0u );
406 // Force retiring cycle
407 Set::gc::force_dispose();
408 for ( auto& i : data ) {
409 EXPECT_EQ( i.nDisposeCount, 1u );
413 for ( auto& i : data ) {
415 ASSERT_TRUE( s.insert( i ));
417 ASSERT_FALSE( s.empty());
418 ASSERT_CONTAINER_SIZE( s, nSetSize );
421 for ( auto it = s.begin(); it != s.end(); ++it ) {
424 for ( auto it = s.cbegin(); it != s.cend(); ++it ) {
425 EXPECT_EQ( it->nFindCount, 1u );
427 for ( auto& i : data ) {
428 EXPECT_EQ( i.nFindCount, 1u );
434 ASSERT_TRUE( s.empty());
435 ASSERT_CONTAINER_SIZE( s, 0u );
436 ASSERT_TRUE( s.begin() == s.end());
437 ASSERT_TRUE( s.cbegin() == s.cend());
439 // Force retiring cycle
440 Set::gc::force_dispose();
441 for ( auto& i : data ) {
442 EXPECT_EQ( i.nDisposeCount, 1u );
448 } // namespace cds_test
450 #endif // #ifndef CDSUNIT_SET_TEST_INTRUSIVE_SET_H