From: khizmax Date: Mon, 14 Mar 2016 20:17:52 +0000 (+0300) Subject: Migrated intrusive FeldmanHashSet unit test to gtest framework X-Git-Tag: v2.2.0~355 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=716e8b11bdf78a021542a90b266c053ea6c70b4a;p=libcds.git Migrated intrusive FeldmanHashSet unit test to gtest framework --- diff --git a/projects/Win/vc14/gtest-set.vcxproj b/projects/Win/vc14/gtest-set.vcxproj index 59826150..36ac7da7 100644 --- a/projects/Win/vc14/gtest-set.vcxproj +++ b/projects/Win/vc14/gtest-set.vcxproj @@ -28,6 +28,8 @@ + + @@ -189,6 +191,8 @@ + + diff --git a/projects/Win/vc14/gtest-set.vcxproj.filters b/projects/Win/vc14/gtest-set.vcxproj.filters index 82af08ec..776f5fa4 100644 --- a/projects/Win/vc14/gtest-set.vcxproj.filters +++ b/projects/Win/vc14/gtest-set.vcxproj.filters @@ -22,6 +22,9 @@ {9c07e86e-bd5b-4ae1-a730-c768234ba9f0} + + {852d2603-551b-460b-b17f-f370d91f79e7} + @@ -147,6 +150,12 @@ Source Files\intrusive::SkipList + + Source Files\intrusive:FeldmanHashSet + + + Source Files\intrusive:FeldmanHashSet + @@ -176,5 +185,11 @@ Header Files + + 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 50464827..efff233b 100644 --- a/test/unit/set/CMakeLists.txt +++ b/test/unit/set/CMakeLists.txt @@ -4,6 +4,8 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-invalid-offsetof") set(CDSGTEST_SET_SOURCES ../main.cpp + intrusive_feldman_hashset_hp.cpp + intrusive_feldman_hashset_dhp.cpp intrusive_michael_lazy_hp.cpp intrusive_michael_lazy_dhp.cpp intrusive_michael_lazy_nogc.cpp diff --git a/test/unit/set/intrusive_feldman_hashset_dhp.cpp b/test/unit/set/intrusive_feldman_hashset_dhp.cpp new file mode 100644 index 00000000..c2e6a6b7 --- /dev/null +++ b/test/unit/set/intrusive_feldman_hashset_dhp.cpp @@ -0,0 +1,146 @@ +/* + 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_feldman_hashset_hp.h" + +#include + +namespace { + namespace ci = cds::intrusive; + typedef cds::gc::DHP gc_type; + + class IntrusiveFeldmanHashSet_DHP : public cds_test::intrusive_feldman_hashset_hp + { + protected: + typedef cds_test::intrusive_feldman_hashset_hp base_class; + + protected: + + void SetUp() + { + typedef ci::FeldmanHashSet< gc_type, int_item, + typename ci::feldman_hashset::make_traits< + ci::feldman_hashset::hash_accessor< hash_accessor > + ,ci::opt::less< std::less> + ,ci::opt::disposer + >::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( IntrusiveFeldmanHashSet_DHP, compare ) + { + struct traits : public ci::feldman_hashset::traits + { + typedef base_class::hash_accessor hash_accessor; + typedef cmp compare; + typedef mock_disposer disposer; + }; + + typedef ci::FeldmanHashSet< gc_type, int_item, traits > set_type; + + set_type s; + test( s ); + } + + TEST_F( IntrusiveFeldmanHashSet_DHP, less ) + { + typedef ci::FeldmanHashSet< gc_type, int_item, + typename ci::feldman_hashset::make_traits< + ci::feldman_hashset::hash_accessor< hash_accessor > + , ci::opt::less< std::less> + , ci::opt::disposer + >::type + > set_type; + + set_type s( 5, 2 ); + test( s ); + } + + TEST_F( IntrusiveFeldmanHashSet_DHP, cmpmix ) + { + struct traits : public ci::feldman_hashset::traits + { + typedef base_class::hash_accessor hash_accessor; + typedef cmp compare; + typedef std::less less; + typedef mock_disposer disposer; + typedef simple_item_counter item_counter; + }; + + typedef ci::FeldmanHashSet< gc_type, int_item, traits > set_type; + + set_type s( 3, 4 ); + test( s ); + } + + TEST_F( IntrusiveFeldmanHashSet_DHP, backoff ) + { + struct traits : public ci::feldman_hashset::traits + { + typedef base_class::hash_accessor hash_accessor; + typedef cmp compare; + typedef mock_disposer disposer; + typedef cds::backoff::empty back_off; + typedef ci::opt::v::sequential_consistent memory_model; + }; + + typedef ci::FeldmanHashSet< gc_type, int_item, traits > set_type; + + set_type s( 8, 3 ); + test( s ); + } + + TEST_F( IntrusiveFeldmanHashSet_DHP, stat ) + { + struct traits : public ci::feldman_hashset::traits + { + typedef base_class::hash_accessor hash_accessor; + typedef cmp compare; + typedef mock_disposer disposer; + typedef ci::feldman_hashset::stat<> stat; + }; + + typedef ci::FeldmanHashSet< gc_type, int_item, traits > set_type; + + set_type s( 8, 3 ); + test( s ); + } + +} // namespace diff --git a/test/unit/set/intrusive_feldman_hashset_hp.cpp b/test/unit/set/intrusive_feldman_hashset_hp.cpp new file mode 100644 index 00000000..e9ca7b5e --- /dev/null +++ b/test/unit/set/intrusive_feldman_hashset_hp.cpp @@ -0,0 +1,147 @@ +/* + 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_feldman_hashset_hp.h" + +#include + +namespace { + namespace ci = cds::intrusive; + typedef cds::gc::HP gc_type; + + class IntrusiveFeldmanHashSet_HP : public cds_test::intrusive_feldman_hashset_hp + { + protected: + typedef cds_test::intrusive_feldman_hashset_hp base_class; + + protected: + + void SetUp() + { + typedef ci::FeldmanHashSet< gc_type, int_item, + typename ci::feldman_hashset::make_traits< + ci::feldman_hashset::hash_accessor< hash_accessor > + ,ci::opt::less< std::less> + ,ci::opt::disposer + >::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( IntrusiveFeldmanHashSet_HP, compare ) + { + struct traits : public ci::feldman_hashset::traits + { + typedef base_class::hash_accessor hash_accessor; + typedef cmp compare; + typedef mock_disposer disposer; + }; + + typedef ci::FeldmanHashSet< gc_type, int_item, traits > set_type; + + set_type s; + test( s ); + } + + TEST_F( IntrusiveFeldmanHashSet_HP, less ) + { + typedef ci::FeldmanHashSet< gc_type, int_item, + typename ci::feldman_hashset::make_traits< + ci::feldman_hashset::hash_accessor< hash_accessor > + , ci::opt::less< std::less> + , ci::opt::disposer + >::type + > set_type; + + set_type s( 5, 2 ); + test( s ); + } + + TEST_F( IntrusiveFeldmanHashSet_HP, cmpmix ) + { + struct traits : public ci::feldman_hashset::traits + { + typedef base_class::hash_accessor hash_accessor; + typedef cmp compare; + typedef std::less less; + typedef mock_disposer disposer; + typedef simple_item_counter item_counter; + }; + + typedef ci::FeldmanHashSet< gc_type, int_item, traits > set_type; + + set_type s( 3, 4 ); + test( s ); + } + + TEST_F( IntrusiveFeldmanHashSet_HP, backoff ) + { + struct traits : public ci::feldman_hashset::traits + { + typedef base_class::hash_accessor hash_accessor; + typedef cmp compare; + typedef mock_disposer disposer; + typedef cds::backoff::empty back_off; + typedef ci::opt::v::sequential_consistent memory_model; + }; + + typedef ci::FeldmanHashSet< gc_type, int_item, traits > set_type; + + set_type s( 8, 3 ); + test( s ); + } + + TEST_F( IntrusiveFeldmanHashSet_HP, stat ) + { + struct traits : public ci::feldman_hashset::traits + { + typedef base_class::hash_accessor hash_accessor; + typedef cmp compare; + typedef mock_disposer disposer; + typedef ci::feldman_hashset::stat<> stat; + }; + + typedef ci::FeldmanHashSet< gc_type, int_item, traits > set_type; + + set_type s( 8, 3 ); + test( s ); + } + +} // namespace diff --git a/test/unit/set/test_intrusive_feldman_hashset.h b/test/unit/set/test_intrusive_feldman_hashset.h new file mode 100644 index 00000000..3960161c --- /dev/null +++ b/test/unit/set/test_intrusive_feldman_hashset.h @@ -0,0 +1,351 @@ +/* + 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_FELDMAN_HASHSET_H +#define CDSUNIT_SET_TEST_INTRUSIVE_FELDMAN_HASHSET_H + +#include +#include + +#include +#include // ref + +// forward declaration +namespace cds { namespace intrusive {}} + +namespace cds_test { + + namespace ci = cds::intrusive; + namespace co = cds::opt; + + class intrusive_feldman_hashset: public fixture + { + public: + struct stat + { + unsigned int nDisposeCount ; // count of disposer calling + unsigned int nFindCount ; // count of find-functor calling + unsigned int nInsertCount ; + mutable unsigned int nEraseCount; + + stat() + { + clear_stat(); + } + + void clear_stat() + { + memset( this, 0, sizeof( *this ) ); + } + }; + + struct int_item: public stat + { + int nKey; + int nVal; + + int_item() + {} + + explicit int_item( int key ) + : nKey( key ) + , nVal( key ) + {} + + int_item(int key, int val) + : nKey( key ) + , nVal(val) + {} + + int_item( int_item const& v ) + : stat() + , nKey( v.nKey ) + , nVal( v.nVal ) + {} + + int key() const + { + return nKey; + } + }; + + struct hash_accessor { + int operator()( int_item const& v ) const + { + return v.key(); + } + }; + + struct simple_item_counter { + size_t m_nCount; + + simple_item_counter() + : m_nCount(0) + {} + + size_t operator ++() + { + return ++m_nCount; + } + + size_t operator --() + { + return --m_nCount; + } + + void reset() + { + m_nCount = 0; + } + + operator size_t() const + { + return m_nCount; + } + }; + + struct cmp { + int operator ()( int lhs, int rhs ) const + { + if ( lhs < rhs ) + return -1; + return lhs > rhs ? 1 : 0; + } + }; + + struct mock_disposer + { + template + void operator ()( T * p ) + { + ++p->nDisposeCount; + } + }; + + protected: + template + void test( Set& s ) + { + // Precondition: set is empty + // Postcondition: set is empty + + ASSERT_TRUE( s.empty() ); + ASSERT_CONTAINER_SIZE( s, 0 ); + size_t const nSetSize = std::max( s.head_size() * 2, static_cast(100) ); + + typedef typename Set::value_type value_type; + + std::vector< value_type > data; + std::vector< size_t> indices; + data.reserve( nSetSize ); + indices.reserve( nSetSize ); + for ( size_t key = 0; key < nSetSize; ++key ) { + data.push_back( value_type( static_cast( key ))); + indices.push_back( key ); + } + shuffle( indices.begin(), indices.end() ); + + // insert/find + for ( auto idx : indices ) { + auto& i = data[ idx ]; + + ASSERT_FALSE( s.contains( i.nKey )); + ASSERT_FALSE( s.find( i.nKey, []( value_type& ) {} )); + + std::pair updResult; + + updResult = s.update( i, false ); + EXPECT_FALSE( updResult.first ); + EXPECT_FALSE( updResult.second ); + + switch ( i.key() % 3 ) { + case 0: + ASSERT_TRUE( s.insert( i )); + ASSERT_FALSE( s.insert( i )); + updResult = s.update( i, false ); + EXPECT_TRUE( updResult.first ); + EXPECT_FALSE( updResult.second ); + break; + case 1: + EXPECT_EQ( i.nInsertCount, 0 ); + ASSERT_TRUE( s.insert( i, []( value_type& v ) { ++v.nInsertCount;} )); + EXPECT_EQ( i.nInsertCount, 1 ); + ASSERT_FALSE( s.insert( i, []( value_type& v ) { ++v.nInsertCount;} ) ); + EXPECT_EQ( i.nInsertCount, 1 ); + i.nInsertCount = 0; + break; + case 2: + updResult = s.update( i ); + EXPECT_TRUE( updResult.first ); + EXPECT_TRUE( updResult.second ); + break; + } + + ASSERT_TRUE( s.contains( i.nKey ) ); + EXPECT_EQ( i.nFindCount, 0 ); + ASSERT_TRUE( s.find( i.nKey, []( value_type& v ) { ++v.nFindCount; } )); + EXPECT_EQ( i.nFindCount, 1 ); + } + ASSERT_FALSE( s.empty() ); + ASSERT_CONTAINER_SIZE( s, nSetSize ); + + std::for_each( data.begin(), data.end(), []( value_type& v ) { v.clear_stat(); }); + + // erase + shuffle( indices.begin(), indices.end() ); + for ( auto idx : indices ) { + auto& i = data[ idx ]; + + ASSERT_TRUE( s.contains( i.nKey ) ); + EXPECT_EQ( i.nFindCount, 0 ); + ASSERT_TRUE( s.find( i.nKey, []( value_type& v ) { ++v.nFindCount; } ) ); + EXPECT_EQ( i.nFindCount, 1 ); + + value_type v( i ); + switch ( i.key() % 3 ) { + case 0: + ASSERT_FALSE( s.unlink( v )); + ASSERT_TRUE( s.unlink( i )); + ASSERT_FALSE( s.unlink( i ) ); + break; + case 1: + ASSERT_TRUE( s.erase( i.key())); + ASSERT_FALSE( s.erase( i.key() ) ); + break; + case 2: + EXPECT_EQ( i.nEraseCount, 0 ); + ASSERT_TRUE( s.erase( v.key(), []( value_type& val ) { ++val.nEraseCount; } )); + EXPECT_EQ( i.nEraseCount, 1 ); + ASSERT_FALSE( s.erase( v.key(), []( value_type& val ) { ++val.nEraseCount; } )); + EXPECT_EQ( i.nEraseCount, 1 ); + break; + } + + ASSERT_FALSE( s.contains( i.nKey )); + ASSERT_FALSE( s.find( i.nKey, []( value_type const& ) {} )); + } + 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 ); + } + + // erase_at( iterator ) + for ( auto& i : data ) { + i.clear_stat(); + ASSERT_TRUE( s.insert( i ) ); + } + ASSERT_FALSE( s.empty() ); + ASSERT_CONTAINER_SIZE( s, nSetSize ); + + for ( auto it = s.begin(); it != s.end(); ++it ) { + ASSERT_TRUE( s.erase_at( it )); + ASSERT_FALSE( s.erase_at( it )); + } + 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 ); + } + + // erase_at( reverse_iterator ) + for ( auto& i : data ) { + i.clear_stat(); + ASSERT_TRUE( s.insert( i ) ); + } + ASSERT_FALSE( s.empty() ); + ASSERT_CONTAINER_SIZE( s, nSetSize ); + + for ( auto it = s.rbegin(); it != s.rend(); ++it ) { + ASSERT_TRUE( s.erase_at( it ) ); + ASSERT_FALSE( s.erase_at( it ) ); + } + 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 ); + } + + // clear + for ( auto& i : data ) { + i.clear_stat(); + ASSERT_TRUE( s.insert( i )); + } + ASSERT_FALSE( s.empty() ); + ASSERT_CONTAINER_SIZE( s, nSetSize ); + + // Forward 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 ); + } + + // Reverse iterator test + for ( auto it = s.rbegin(); it != s.rend(); ++it ) { + ++it->nFindCount; + } + for ( auto it = s.crbegin(); it != s.crend(); ++it ) { + EXPECT_EQ( it->nFindCount, 2 ); + } + + for ( auto& i : data ) { + EXPECT_EQ( i.nFindCount, 2 ); + } + + // 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 ); + } + } + }; + +} // namespace cds_test + +#endif // #ifndef CDSUNIT_SET_TEST_INTRUSIVE_FELDMAN_HASHSET_H diff --git a/test/unit/set/test_intrusive_feldman_hashset_hp.h b/test/unit/set/test_intrusive_feldman_hashset_hp.h new file mode 100644 index 00000000..81872c9e --- /dev/null +++ b/test/unit/set/test_intrusive_feldman_hashset_hp.h @@ -0,0 +1,137 @@ +/* + 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_FELDMAN_HASHSET_HP_H +#define CDSUNIT_SET_TEST_INTRUSIVE_FELDMAN_HASHSET_HP_H + +#include "test_intrusive_feldman_hashset.h" + +// forward declaration +namespace cds { namespace intrusive {}} + +namespace cds_test { + + namespace ci = cds::intrusive; + namespace co = cds::opt; + + class intrusive_feldman_hashset_hp: public intrusive_feldman_hashset + { + typedef intrusive_feldman_hashset 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; + size_t const nSetSize = std::max( s.head_size() * 2, static_cast( 100 )); + + std::vector< value_type > data; + std::vector< size_t> indices; + data.reserve( nSetSize ); + indices.reserve( nSetSize ); + for ( size_t key = 0; key < nSetSize; ++key ) { + data.push_back( value_type( static_cast(key) ) ); + indices.push_back( key ); + } + shuffle( indices.begin(), indices.end() ); + + typename Set::guarded_ptr gp; + + // get/extract from empty set + for ( auto idx : indices ) { + auto& i = data[idx]; + + gp = s.get( i.key() ); + ASSERT_TRUE( !gp ); + + gp = s.extract( i.key()); + ASSERT_TRUE( !gp ); + } + + // fill set + for ( auto& i : data ) { + i.nDisposeCount = 0; + ASSERT_TRUE( s.insert( i ) ); + } + + // get_level_statistics + { + std::vector< ci::feldman_hashset::level_statistics > level_stat; + s.get_level_statistics( level_stat ); + EXPECT_GT( level_stat.size(), 0 ); + } + + // get/extract + for ( auto idx : indices ) { + auto& i = data[idx]; + + EXPECT_EQ( i.nFindCount, 0 ); + gp = s.get( i.key() ); + ASSERT_FALSE( !gp ); + ++gp->nFindCount; + EXPECT_EQ( i.nFindCount, 1 ); + + gp = s.extract( i.key()); + ASSERT_FALSE( !gp ); + ++gp->nEraseCount; + EXPECT_EQ( i.nEraseCount, 1 ); + + gp = s.extract( i.key() ); + ASSERT_TRUE( !gp ); + + gp = s.get( i.key() ); + ASSERT_TRUE( !gp ); + } + gp.release(); + + 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_FELDMAN_HASHSET_HP_H