From 16bc6cab98e83948b45075098a5e2b9d69cc7137 Mon Sep 17 00:00:00 2001 From: khizmax Date: Thu, 10 Mar 2016 09:42:30 +0300 Subject: [PATCH] Migrated set unit tests to gtest framework --- projects/Win/vc14/gtest-set.vcxproj | 3 + projects/Win/vc14/gtest-set.vcxproj.filters | 9 + test/unit/set/CMakeLists.txt | 2 + test/unit/set/intrusive_michael_lazy_dhp.cpp | 253 +++++++++++++++++ test/unit/set/intrusive_michael_lazy_hp.cpp | 254 ++++++++++++++++++ .../set/intrusive_michael_michael_dhp.cpp | 2 +- test/unit/set/test_intrusive_set.h | 17 +- test/unit/set/test_intrusive_set_rcu.h | 205 ++++++++++++++ 8 files changed, 743 insertions(+), 2 deletions(-) create mode 100644 test/unit/set/intrusive_michael_lazy_dhp.cpp create mode 100644 test/unit/set/intrusive_michael_lazy_hp.cpp create mode 100644 test/unit/set/test_intrusive_set_rcu.h diff --git a/projects/Win/vc14/gtest-set.vcxproj b/projects/Win/vc14/gtest-set.vcxproj index a7e63cb1..82b5276f 100644 --- a/projects/Win/vc14/gtest-set.vcxproj +++ b/projects/Win/vc14/gtest-set.vcxproj @@ -28,12 +28,15 @@ + + + {A589D3F1-A749-4268-ADEC-D0CE13D1E359} diff --git a/projects/Win/vc14/gtest-set.vcxproj.filters b/projects/Win/vc14/gtest-set.vcxproj.filters index 09bd8367..5048a8b2 100644 --- a/projects/Win/vc14/gtest-set.vcxproj.filters +++ b/projects/Win/vc14/gtest-set.vcxproj.filters @@ -24,6 +24,12 @@ Source Files + + Source Files + + + Source Files + @@ -32,5 +38,8 @@ Header Files + + Header Files + \ No newline at end of file diff --git a/test/unit/set/CMakeLists.txt b/test/unit/set/CMakeLists.txt index 111ef264..7f9eb504 100644 --- a/test/unit/set/CMakeLists.txt +++ b/test/unit/set/CMakeLists.txt @@ -2,6 +2,8 @@ set(PACKAGE_NAME unit-set) set(CDSGTEST_SET_SOURCES ../main.cpp + intrusive_michael_lazy_hp.cpp + intrusive_michael_lazy_dhp.cpp intrusive_michael_michael_hp.cpp intrusive_michael_michael_dhp.cpp ) diff --git a/test/unit/set/intrusive_michael_lazy_dhp.cpp b/test/unit/set/intrusive_michael_lazy_dhp.cpp new file mode 100644 index 00000000..72449894 --- /dev/null +++ b/test/unit/set/intrusive_michael_lazy_dhp.cpp @@ -0,0 +1,253 @@ +/* + This file is a part of libcds - Concurrent Data Structures library + + (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2016 + + Source code repo: http://github.com/khizmax/libcds/ + Download: http://sourceforge.net/projects/libcds/files/ + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#include "test_intrusive_set_hp.h" + +#include +#include + +#include + +namespace { + namespace ci = cds::intrusive; + typedef cds::gc::DHP gc_type; + + class IntrusiveMichaelLazySet_DHP : public cds_test::intrusive_set_hp + { + typedef cds_test::intrusive_set_hp base_class; + + protected: + typedef typename base_class::base_int_item< ci::lazy_list::node> base_item_type; + typedef typename base_class::base_int_item< ci::lazy_list::node> base_mutex_item_type; + typedef typename base_class::member_int_item< ci::lazy_list::node> member_item_type; + typedef typename base_class::member_int_item< ci::lazy_list::node> member_mutex_item_type; + + void SetUp() + { + struct list_traits : public ci::lazy_list::traits + { + typedef ci::lazy_list::base_hook< ci::opt::gc> hook; + }; + typedef ci::LazyList< gc_type, base_item_type, list_traits > list_type; + typedef ci::MichaelHashSet< gc_type, list_type > set_type; + + cds::gc::dhp::GarbageCollector::Construct( 16, set_type::c_nHazardPtrCount ); + cds::threading::Manager::attachThread(); + } + + void TearDown() + { + cds::threading::Manager::detachThread(); + cds::gc::dhp::GarbageCollector::Destruct(); + } + }; + + + TEST_F( IntrusiveMichaelLazySet_DHP, base_cmp ) + { + typedef ci::LazyList< gc_type + , base_item_type + ,ci::lazy_list::make_traits< + ci::opt::hook< ci::lazy_list::base_hook< ci::opt::gc< gc_type > > > + ,ci::opt::compare< cmp > + ,ci::opt::disposer< mock_disposer > + ,ci::opt::back_off< cds::backoff::pause > + >::type + > bucket_type; + + typedef ci::MichaelHashSet< gc_type, bucket_type, + ci::michael_set::make_traits< + ci::opt::hash< hash_int > + >::type + > set_type; + + set_type s( kSize, 2 ); + test( s ); + } + + TEST_F( IntrusiveMichaelLazySet_DHP, base_less ) + { + typedef ci::LazyList< gc_type + , base_item_type + ,ci::lazy_list::make_traits< + ci::opt::hook< ci::lazy_list::base_hook< ci::opt::gc< gc_type >>> + ,ci::opt::less< less > + ,ci::opt::disposer< mock_disposer > + >::type + > bucket_type; + + typedef ci::MichaelHashSet< gc_type, bucket_type, + ci::michael_set::make_traits< + ci::opt::hash< hash_int > + >::type + > set_type; + + set_type s( kSize, 2 ); + test( s ); + } + + TEST_F( IntrusiveMichaelLazySet_DHP, base_cmpmix ) + { + struct list_traits : public ci::lazy_list::traits + { + typedef ci::lazy_list::base_hook< ci::opt::gc> hook; + typedef base_class::less less; + typedef cmp compare; + typedef mock_disposer disposer; + }; + typedef ci::LazyList< gc_type, base_item_type, list_traits > bucket_type; + + struct set_traits : public ci::michael_set::traits + { + typedef hash_int hash; + typedef simple_item_counter item_counter; + }; + typedef ci::MichaelHashSet< gc_type, bucket_type, set_traits > set_type; + + set_type s( kSize, 2 ); + test( s ); + } + + TEST_F( IntrusiveMichaelLazySet_DHP, base_mutex ) + { + struct list_traits : public ci::lazy_list::traits + { + typedef ci::lazy_list::base_hook< ci::opt::gc, ci::opt::lock_type> hook; + typedef base_class::less less; + typedef cmp compare; + typedef mock_disposer disposer; + }; + typedef ci::LazyList< gc_type, base_mutex_item_type, list_traits > bucket_type; + + struct set_traits : public ci::michael_set::traits + { + typedef hash_int hash; + typedef simple_item_counter item_counter; + }; + typedef ci::MichaelHashSet< gc_type, bucket_type, set_traits > set_type; + + set_type s( kSize, 2 ); + test( s ); + } + + + TEST_F( IntrusiveMichaelLazySet_DHP, member_cmp ) + { + typedef ci::LazyList< gc_type + ,member_item_type + ,ci::lazy_list::make_traits< + ci::opt::hook< ci::lazy_list::member_hook< + offsetof( member_item_type, hMember ), + ci::opt::gc + > > + ,ci::opt::compare< cmp > + ,ci::opt::disposer< mock_disposer > + >::type + > bucket_type; + + typedef ci::MichaelHashSet< gc_type, bucket_type, + ci::michael_set::make_traits< + ci::opt::hash< hash_int > + >::type + > set_type; + + set_type s( kSize, 2 ); + test( s ); + } + + TEST_F( IntrusiveMichaelLazySet_DHP, member_less ) + { + typedef ci::LazyList< gc_type + , member_item_type + ,ci::lazy_list::make_traits< + ci::opt::hook< ci::lazy_list::member_hook< + offsetof( member_item_type, hMember ), + ci::opt::gc + > > + ,ci::opt::less< less > + ,ci::opt::disposer< mock_disposer > + >::type + > bucket_type; + + typedef ci::MichaelHashSet< gc_type, bucket_type, + ci::michael_set::make_traits< + ci::opt::hash< hash_int > + >::type + > set_type; + + set_type s( kSize, 2 ); + test( s ); + } + + TEST_F( IntrusiveMichaelLazySet_DHP, member_cmpmix ) + { + struct list_traits : public ci::lazy_list::traits + { + typedef ci::lazy_list::member_hook< offsetof( member_item_type, hMember ), ci::opt::gc> hook; + typedef base_class::less less; + typedef cmp compare; + typedef mock_disposer disposer; + }; + typedef ci::LazyList< gc_type, member_item_type, list_traits > bucket_type; + + struct set_traits : public ci::michael_set::traits + { + typedef hash_int hash; + typedef simple_item_counter item_counter; + }; + typedef ci::MichaelHashSet< gc_type, bucket_type, set_traits > set_type; + + set_type s( kSize, 2 ); + test( s ); + } + + TEST_F( IntrusiveMichaelLazySet_DHP, member_mutex ) + { + struct list_traits : public ci::lazy_list::traits + { + typedef ci::lazy_list::member_hook< offsetof( member_mutex_item_type, hMember ), ci::opt::gc, ci::opt::lock_type> hook; + typedef base_class::less less; + typedef cmp compare; + typedef mock_disposer disposer; + }; + typedef ci::LazyList< gc_type, member_mutex_item_type, list_traits > bucket_type; + + struct set_traits : public ci::michael_set::traits + { + typedef hash_int hash; + typedef simple_item_counter item_counter; + }; + typedef ci::MichaelHashSet< gc_type, bucket_type, set_traits > set_type; + + set_type s( kSize, 2 ); + test( s ); + } + +} // namespace diff --git a/test/unit/set/intrusive_michael_lazy_hp.cpp b/test/unit/set/intrusive_michael_lazy_hp.cpp new file mode 100644 index 00000000..ebcb94f9 --- /dev/null +++ b/test/unit/set/intrusive_michael_lazy_hp.cpp @@ -0,0 +1,254 @@ +/* + This file is a part of libcds - Concurrent Data Structures library + + (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2016 + + Source code repo: http://github.com/khizmax/libcds/ + Download: http://sourceforge.net/projects/libcds/files/ + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#include "test_intrusive_set_hp.h" + +#include +#include + +#include + +namespace { + namespace ci = cds::intrusive; + typedef cds::gc::HP gc_type; + + class IntrusiveMichaelLazySet_HP : public cds_test::intrusive_set_hp + { + typedef cds_test::intrusive_set_hp base_class; + + protected: + typedef typename base_class::base_int_item< ci::lazy_list::node> base_item_type; + typedef typename base_class::base_int_item< ci::lazy_list::node> base_mutex_item_type; + typedef typename base_class::member_int_item< ci::lazy_list::node> member_item_type; + typedef typename base_class::member_int_item< ci::lazy_list::node> member_mutex_item_type; + + void SetUp() + { + struct list_traits : public ci::lazy_list::traits + { + typedef ci::lazy_list::base_hook< ci::opt::gc> hook; + }; + typedef ci::LazyList< gc_type, base_item_type, list_traits > list_type; + typedef ci::MichaelHashSet< gc_type, list_type > set_type; + + // +1 - for guarded_ptr + cds::gc::hp::GarbageCollector::Construct( set_type::c_nHazardPtrCount + 1, 1, 16 ); + cds::threading::Manager::attachThread(); + } + + void TearDown() + { + cds::threading::Manager::detachThread(); + cds::gc::hp::GarbageCollector::Destruct( true ); + } + }; + + + TEST_F( IntrusiveMichaelLazySet_HP, base_cmp ) + { + typedef ci::LazyList< gc_type + , base_item_type + ,ci::lazy_list::make_traits< + ci::opt::hook< ci::lazy_list::base_hook< ci::opt::gc< gc_type > > > + ,ci::opt::compare< cmp > + ,ci::opt::disposer< mock_disposer > + ,ci::opt::back_off< cds::backoff::pause > + >::type + > bucket_type; + + typedef ci::MichaelHashSet< gc_type, bucket_type, + ci::michael_set::make_traits< + ci::opt::hash< hash_int > + >::type + > set_type; + + set_type s( kSize, 2 ); + test( s ); + } + + TEST_F( IntrusiveMichaelLazySet_HP, base_less ) + { + typedef ci::LazyList< gc_type + , base_item_type + ,ci::lazy_list::make_traits< + ci::opt::hook< ci::lazy_list::base_hook< ci::opt::gc< gc_type >>> + ,ci::opt::less< less > + ,ci::opt::disposer< mock_disposer > + >::type + > bucket_type; + + typedef ci::MichaelHashSet< gc_type, bucket_type, + ci::michael_set::make_traits< + ci::opt::hash< hash_int > + >::type + > set_type; + + set_type s( kSize, 2 ); + test( s ); + } + + TEST_F( IntrusiveMichaelLazySet_HP, base_cmpmix ) + { + struct list_traits : public ci::lazy_list::traits + { + typedef ci::lazy_list::base_hook< ci::opt::gc> hook; + typedef base_class::less less; + typedef cmp compare; + typedef mock_disposer disposer; + }; + typedef ci::LazyList< gc_type, base_item_type, list_traits > bucket_type; + + struct set_traits : public ci::michael_set::traits + { + typedef hash_int hash; + typedef simple_item_counter item_counter; + }; + typedef ci::MichaelHashSet< gc_type, bucket_type, set_traits > set_type; + + set_type s( kSize, 2 ); + test( s ); + } + + TEST_F( IntrusiveMichaelLazySet_HP, base_mutex ) + { + struct list_traits : public ci::lazy_list::traits + { + typedef ci::lazy_list::base_hook< ci::opt::gc, ci::opt::lock_type> hook; + typedef base_class::less less; + typedef cmp compare; + typedef mock_disposer disposer; + }; + typedef ci::LazyList< gc_type, base_mutex_item_type, list_traits > bucket_type; + + struct set_traits : public ci::michael_set::traits + { + typedef hash_int hash; + typedef simple_item_counter item_counter; + }; + typedef ci::MichaelHashSet< gc_type, bucket_type, set_traits > set_type; + + set_type s( kSize, 2 ); + test( s ); + } + + + TEST_F( IntrusiveMichaelLazySet_HP, member_cmp ) + { + typedef ci::LazyList< gc_type + ,member_item_type + ,ci::lazy_list::make_traits< + ci::opt::hook< ci::lazy_list::member_hook< + offsetof( member_item_type, hMember ), + ci::opt::gc + > > + ,ci::opt::compare< cmp > + ,ci::opt::disposer< mock_disposer > + >::type + > bucket_type; + + typedef ci::MichaelHashSet< gc_type, bucket_type, + ci::michael_set::make_traits< + ci::opt::hash< hash_int > + >::type + > set_type; + + set_type s( kSize, 2 ); + test( s ); + } + + TEST_F( IntrusiveMichaelLazySet_HP, member_less ) + { + typedef ci::LazyList< gc_type + , member_item_type + ,ci::lazy_list::make_traits< + ci::opt::hook< ci::lazy_list::member_hook< + offsetof( member_item_type, hMember ), + ci::opt::gc + > > + ,ci::opt::less< less > + ,ci::opt::disposer< mock_disposer > + >::type + > bucket_type; + + typedef ci::MichaelHashSet< gc_type, bucket_type, + ci::michael_set::make_traits< + ci::opt::hash< hash_int > + >::type + > set_type; + + set_type s( kSize, 2 ); + test( s ); + } + + TEST_F( IntrusiveMichaelLazySet_HP, member_cmpmix ) + { + struct list_traits : public ci::lazy_list::traits + { + typedef ci::lazy_list::member_hook< offsetof( member_item_type, hMember ), ci::opt::gc> hook; + typedef base_class::less less; + typedef cmp compare; + typedef mock_disposer disposer; + }; + typedef ci::LazyList< gc_type, member_item_type, list_traits > bucket_type; + + struct set_traits : public ci::michael_set::traits + { + typedef hash_int hash; + typedef simple_item_counter item_counter; + }; + typedef ci::MichaelHashSet< gc_type, bucket_type, set_traits > set_type; + + set_type s( kSize, 2 ); + test( s ); + } + + TEST_F( IntrusiveMichaelLazySet_HP, member_mutex ) + { + struct list_traits : public ci::lazy_list::traits + { + typedef ci::lazy_list::member_hook< offsetof( member_mutex_item_type, hMember ), ci::opt::gc, ci::opt::lock_type> hook; + typedef base_class::less less; + typedef cmp compare; + typedef mock_disposer disposer; + }; + typedef ci::LazyList< gc_type, member_mutex_item_type, list_traits > bucket_type; + + struct set_traits : public ci::michael_set::traits + { + typedef hash_int hash; + typedef simple_item_counter item_counter; + }; + typedef ci::MichaelHashSet< gc_type, bucket_type, set_traits > set_type; + + set_type s( kSize, 2 ); + test( s ); + } + +} // namespace diff --git a/test/unit/set/intrusive_michael_michael_dhp.cpp b/test/unit/set/intrusive_michael_michael_dhp.cpp index 0ce9bd8f..9b5719f2 100644 --- a/test/unit/set/intrusive_michael_michael_dhp.cpp +++ b/test/unit/set/intrusive_michael_michael_dhp.cpp @@ -61,7 +61,7 @@ namespace { void TearDown() { cds::threading::Manager::detachThread(); - cds::gc::hp::GarbageCollector::Destruct(); + cds::gc::dhp::GarbageCollector::Destruct(); } }; diff --git a/test/unit/set/test_intrusive_set.h b/test/unit/set/test_intrusive_set.h index 2930596c..139c92a9 100644 --- a/test/unit/set/test_intrusive_set.h +++ b/test/unit/set/test_intrusive_set.h @@ -409,22 +409,37 @@ namespace cds_test { // clear for ( auto& i : data ) { - i.nDisposeCount = 0; + i.clear_stat(); ASSERT_TRUE( s.insert( i )); } ASSERT_FALSE( s.empty() ); ASSERT_CONTAINER_SIZE( s, kSize ); + // Iterator test + for ( auto it = s.begin(); it != s.end(); ++it ) { + ++it->nFindCount; + } + for ( auto it = s.cbegin(); it != s.cend(); ++it ) { + EXPECT_EQ( it->nFindCount, 1 ); + } + for ( auto& i : data ) { + EXPECT_EQ( i.nFindCount, 1 ); + } + + // clear test s.clear(); ASSERT_TRUE( s.empty()); ASSERT_CONTAINER_SIZE( s, 0 ); + ASSERT_TRUE( s.begin() == s.end() ); + ASSERT_TRUE( s.cbegin() == s.cend() ); // Force retiring cycle Set::gc::force_dispose(); for ( auto& i : data ) { EXPECT_EQ( i.nDisposeCount, 1 ); } + } }; diff --git a/test/unit/set/test_intrusive_set_rcu.h b/test/unit/set/test_intrusive_set_rcu.h new file mode 100644 index 00000000..416f3e94 --- /dev/null +++ b/test/unit/set/test_intrusive_set_rcu.h @@ -0,0 +1,205 @@ +/* + This file is a part of libcds - Concurrent Data Structures library + + (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2016 + + Source code repo: http://github.com/khizmax/libcds/ + Download: http://sourceforge.net/projects/libcds/files/ + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#ifndef CDSUNIT_SET_TEST_INTRUSIVE_SET_RCU_H +#define CDSUNIT_SET_TEST_INTRUSIVE_SET_RCU_H + +#include "test_intrusive_set.h" + +// forward declaration +namespace cds { namespace intrusive {}} + +namespace cds_test { + + namespace ci = cds::intrusive; + namespace co = cds::opt; + + class intrusive_set_rcu: public intrusive_set + { + typedef intrusive_set base_class; + + protected: + + template + void test( Set& s ) + { + // Precondition: set is empty + // Postcondition: set is empty + + base_class::test( s ); + + ASSERT_TRUE( s.empty() ); + ASSERT_CONTAINER_SIZE( s, 0 ); + + typedef typename Set::value_type value_type; + + std::vector< value_type > data; + std::vector< size_t> indices; + data.reserve( kSize ); + indices.reserve( kSize ); + for ( size_t key = 0; key < kSize; ++key ) { + data.push_back( value_type( static_cast(key) ) ); + indices.push_back( key ); + } + shuffle( indices.begin(), indices.end() ); + + typename Set::exempt_ptr xp; + typename Set::raw_ptr rp; + typedef typename Set::rcu_lock rcu_lock; + + // get/extract from empty set + for ( auto idx : indices ) { + auto& i = data[idx]; + + { + rcu_lock l; + rp = s.get( i ); + ASSERT_TRUE( !rp ); + rp = s.get( i.key() ); + ASSERT_TRUE( !rp ); + rp = s.get_with( other_item( i.key()), other_less()); + ASSERT_TRUE( !rp ); + } + + if ( Set::c_bExtractLockExternal ) { + rcu_lock l; + + xp = s.extract( i ); + ASSERT_TRUE( !xp ); + xp = s.extract( i.key() ); + ASSERT_TRUE( !xp ); + xp = s.extract_with( other_item( i.key() ), other_less() ); + ASSERT_TRUE( !xp ); + } + else { + xp = s.extract( i ); + ASSERT_TRUE( !xp ); + xp = s.extract( i.key() ); + ASSERT_TRUE( !xp ); + xp = s.extract_with( other_item( i.key() ), other_less() ); + ASSERT_TRUE( !xp ); + } + } + + // fill set + for ( auto& i : data ) { + i.nDisposeCount = 0; + ASSERT_TRUE( s.insert( i ) ); + } + + // get/extract + for ( auto idx : indices ) { + auto& i = data[idx]; + + { + rcu_lock l; + EXPECT_EQ( i.nFindCount, 0 ); + rp = s.get( i ); + ASSERT_TRUE( rp ); + ++rp->nFindCount; + EXPECT_EQ( i.nFindCount, 1 ); + + rp = s.get( i.key() ); + ASSERT_TRUE( rp ); + ++rp->nFindCount; + EXPECT_EQ( i.nFindCount, 2 ); + + rp = s.get_with( other_item( i.key()), other_less()); + ASSERT_TRUE( rp ); + ++rp->nFindCount; + EXPECT_EQ( i.nFindCount, 3 ); + } + + if ( Set::c_bExtractLockExternal ) { + rcu_lock l; + + EXPECT_EQ( i.nEraseCount, 0 ); + switch ( i.key() % 3 ) { + case 0: + xp = s.extract( i.key()); + break; + case 1: + xp = s.extract( i ); + break; + case 2: + xp = s.extract_with( other_item( i.key() ), other_less() ); + break; + } + ASSERT_TRUE( xp ); + ++xp->nEraseCount; + EXPECT_EQ( i.nEraseCount, 1 ); + + xp = s.extract( i ); + ASSERT_TRUE( !xp ); + xp = s.extract( i.key() ); + ASSERT_TRUE( !xp ); + xp = s.extract_with( other_item( i.key() ), other_less() ); + ASSERT_TRUE( !xp ); + } + else { + EXPECT_EQ( i.nEraseCount, 0 ); + switch ( i.key() % 3 ) { + case 0: + xp = s.extract( i.key()); + break; + case 1: + xp = s.extract( i ); + break; + case 2: + xp = s.extract_with( other_item( i.key() ), other_less() ); + break; + } + ASSERT_TRUE( xp ); + ++xp->nEraseCount; + EXPECT_EQ( i.nEraseCount, 1 ); + + xp = s.extract( i ); + ASSERT_TRUE( !xp ); + xp = s.extract( i.key() ); + ASSERT_TRUE( !xp ); + xp = s.extract_with( other_item( i.key() ), other_less() ); + ASSERT_TRUE( !xp ); + } + } + + ASSERT_TRUE( s.empty() ); + ASSERT_CONTAINER_SIZE( s, 0 ); + + // Force retiring cycle + Set::gc::force_dispose(); + for ( auto& i : data ) { + EXPECT_EQ( i.nDisposeCount, 1 ); + } + } + }; + +} // namespace cds_test + +#endif // #ifndef CDSUNIT_SET_TEST_INTRUSIVE_SET_RCU_H -- 2.34.1