From db3232385353242bd0298ead219c3918a1f2c5a5 Mon Sep 17 00:00:00 2001 From: khizmax Date: Thu, 7 Apr 2016 18:35:19 +0300 Subject: [PATCH] Start migration tree unit test to gtest framework --- projects/Win/vc14/cds.sln | 17 +- projects/Win/vc14/gtest-tree.vcxproj | 240 ++++++++++ projects/Win/vc14/gtest-tree.vcxproj.filters | 30 ++ test/unit/CMakeLists.txt | 2 + test/unit/tree/CMakeLists.txt | 20 + test/unit/tree/test_intrusive_tree.h | 446 +++++++++++++++++++ test/unit/tree/test_intrusive_tree_hp.h | 216 +++++++++ 7 files changed, 970 insertions(+), 1 deletion(-) create mode 100644 projects/Win/vc14/gtest-tree.vcxproj create mode 100644 projects/Win/vc14/gtest-tree.vcxproj.filters create mode 100644 test/unit/tree/CMakeLists.txt create mode 100644 test/unit/tree/test_intrusive_tree.h create mode 100644 test/unit/tree/test_intrusive_tree_hp.h diff --git a/projects/Win/vc14/cds.sln b/projects/Win/vc14/cds.sln index 5041cf38..20e21be1 100644 --- a/projects/Win/vc14/cds.sln +++ b/projects/Win/vc14/cds.sln @@ -1,6 +1,6 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 14 -VisualStudioVersion = 14.0.25123.0 +VisualStudioVersion = 14.0.24720.0 MinimumVisualStudioVersion = 10.0.40219.1 Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "cds", "cds.vcxproj", "{408FE9BC-44F0-4E6A-89FA-D6F952584239}" EndProject @@ -212,6 +212,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "gtest-striped-map", "gtest- {408FE9BC-44F0-4E6A-89FA-D6F952584239} = {408FE9BC-44F0-4E6A-89FA-D6F952584239} EndProjectSection EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "gtest-tree", "gtest-tree.vcxproj", "{2ABD6A2E-BEA7-4C8C-982B-A609F83D2DCB}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Win32 = Debug|Win32 @@ -546,6 +548,18 @@ Global {00FD5CB8-E1A4-40CA-B613-30A06A75622B}.Release|Win32.Build.0 = Release|Win32 {00FD5CB8-E1A4-40CA-B613-30A06A75622B}.Release|x64.ActiveCfg = Release|x64 {00FD5CB8-E1A4-40CA-B613-30A06A75622B}.Release|x64.Build.0 = Release|x64 + {2ABD6A2E-BEA7-4C8C-982B-A609F83D2DCB}.Debug|Win32.ActiveCfg = Debug|Win32 + {2ABD6A2E-BEA7-4C8C-982B-A609F83D2DCB}.Debug|Win32.Build.0 = Debug|Win32 + {2ABD6A2E-BEA7-4C8C-982B-A609F83D2DCB}.Debug|x64.ActiveCfg = Debug|x64 + {2ABD6A2E-BEA7-4C8C-982B-A609F83D2DCB}.Debug|x64.Build.0 = Debug|x64 + {2ABD6A2E-BEA7-4C8C-982B-A609F83D2DCB}.DebugVLD|Win32.ActiveCfg = DebugVLD|Win32 + {2ABD6A2E-BEA7-4C8C-982B-A609F83D2DCB}.DebugVLD|Win32.Build.0 = DebugVLD|Win32 + {2ABD6A2E-BEA7-4C8C-982B-A609F83D2DCB}.DebugVLD|x64.ActiveCfg = DebugVLD|x64 + {2ABD6A2E-BEA7-4C8C-982B-A609F83D2DCB}.DebugVLD|x64.Build.0 = DebugVLD|x64 + {2ABD6A2E-BEA7-4C8C-982B-A609F83D2DCB}.Release|Win32.ActiveCfg = Release|Win32 + {2ABD6A2E-BEA7-4C8C-982B-A609F83D2DCB}.Release|Win32.Build.0 = Release|Win32 + {2ABD6A2E-BEA7-4C8C-982B-A609F83D2DCB}.Release|x64.ActiveCfg = Release|x64 + {2ABD6A2E-BEA7-4C8C-982B-A609F83D2DCB}.Release|x64.Build.0 = Release|x64 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -579,6 +593,7 @@ Global {648021D3-6E18-4B94-88B8-F6A59609E210} = {810490B7-31E5-49AE-8455-CAF99A9658B6} {42B7E892-DDDA-4D00-9AB7-378E0E7E9433} = {810490B7-31E5-49AE-8455-CAF99A9658B6} {00FD5CB8-E1A4-40CA-B613-30A06A75622B} = {810490B7-31E5-49AE-8455-CAF99A9658B6} + {2ABD6A2E-BEA7-4C8C-982B-A609F83D2DCB} = {810490B7-31E5-49AE-8455-CAF99A9658B6} EndGlobalSection GlobalSection(DPCodeReviewSolutionGUID) = preSolution DPCodeReviewSolutionGUID = {00000000-0000-0000-0000-000000000000} diff --git a/projects/Win/vc14/gtest-tree.vcxproj b/projects/Win/vc14/gtest-tree.vcxproj new file mode 100644 index 00000000..da809c9f --- /dev/null +++ b/projects/Win/vc14/gtest-tree.vcxproj @@ -0,0 +1,240 @@ + + + + + DebugVLD + Win32 + + + DebugVLD + x64 + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + + + + + + + + {2ABD6A2E-BEA7-4c8c-982B-A609F83D2DCB} + Win32Proj + tree + 8.1 + + + + Application + true + v140 + Unicode + + + Application + true + v140 + Unicode + + + Application + false + v140 + true + Unicode + + + Application + true + v140 + Unicode + + + Application + true + v140 + Unicode + + + Application + false + v140 + true + Unicode + + + + + + + + + + + + + + + + + + + + + + + + + + + true + $(SolutionDir)..\..\..\bin\vc.$(PlatformToolset)\$(Platform)\ + $(SolutionDir)..\..\..\obj\vc.$(PlatformToolset)\$(Platform)\$(ProjectName)\$(Configuration)\ + $(ProjectName)_d + + + true + $(SolutionDir)..\..\..\bin\vc.$(PlatformToolset)\$(Platform)\ + $(SolutionDir)..\..\..\obj\vc.$(PlatformToolset)\$(Platform)\$(ProjectName)\$(Configuration)\ + $(ProjectName)_d + + + true + $(SolutionDir)..\..\..\bin\vc.$(PlatformToolset)\$(Platform)\ + $(SolutionDir)..\..\..\obj\vc.$(PlatformToolset)\$(Platform)\$(ProjectName)\$(Configuration)\ + $(ProjectName)_d + + + true + $(SolutionDir)..\..\..\bin\vc.$(PlatformToolset)\$(Platform)\ + $(SolutionDir)..\..\..\obj\vc.$(PlatformToolset)\$(Platform)\$(ProjectName)\$(Configuration)\ + $(ProjectName)_d + + + false + $(SolutionDir)..\..\..\bin\vc.$(PlatformToolset)\$(Platform)-release\ + $(SolutionDir)..\..\..\obj\vc.$(PlatformToolset)\$(Platform)\$(ProjectName)\$(Configuration)\ + + + false + $(SolutionDir)..\..\..\bin\vc.$(PlatformToolset)\$(Platform)-release\ + $(SolutionDir)..\..\..\obj\vc.$(PlatformToolset)\$(Platform)\$(ProjectName)\$(Configuration)\ + + + + NotUsing + Level3 + Disabled + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + $(SolutionDir)..\..\..;$(GTEST_ROOT)/include;$(SolutionDir)..\..\..\test\include;$(BOOST_PATH);%(AdditionalIncludeDirectories) + + + Console + true + $(GTEST_LIB32);$(GTEST_ROOT)/lib/x86;$(BOOST_PATH)/stage32/lib;$(BOOST_PATH)/stage/lib;$(BOOST_PATH)/bin;%(AdditionalLibraryDirectories);$(OutDir) + gtestd.lib;%(AdditionalDependencies) + + + + + NotUsing + Level3 + Disabled + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + $(SolutionDir)..\..\..;$(GTEST_ROOT)/include;$(SolutionDir)..\..\..\test\include;$(BOOST_PATH);%(AdditionalIncludeDirectories) + + + Console + true + $(GTEST_LIB32);$(GTEST_ROOT)/lib/x86;$(BOOST_PATH)/stage32/lib;$(BOOST_PATH)/stage/lib;$(BOOST_PATH)/bin;%(AdditionalLibraryDirectories);$(OutDir) + gtestd.lib;%(AdditionalDependencies) + + + + + NotUsing + Level3 + Disabled + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + $(SolutionDir)..\..\..;$(GTEST_ROOT)/include;$(SolutionDir)..\..\..\test\include;$(BOOST_PATH);%(AdditionalIncludeDirectories) + + + Console + true + $(GTEST_LIB64);$(GTEST_ROOT)/lib/x64;$(BOOST_PATH)/stage64/lib;$(BOOST_PATH)/bin;%(AdditionalLibraryDirectories);$(OutDir) + gtestd.lib;%(AdditionalDependencies) + + + + + NotUsing + Level3 + Disabled + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + $(SolutionDir)..\..\..;$(GTEST_ROOT)/include;$(SolutionDir)..\..\..\test\include;$(BOOST_PATH);%(AdditionalIncludeDirectories) + + + Console + true + $(GTEST_LIB64);$(GTEST_ROOT)/lib/x64;$(BOOST_PATH)/stage64/lib;$(BOOST_PATH)/bin;%(AdditionalLibraryDirectories);$(OutDir) + gtestd.lib;%(AdditionalDependencies) + + + + + Level3 + NotUsing + MaxSpeed + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + $(SolutionDir)..\..\..;$(GTEST_ROOT)/include;$(SolutionDir)..\..\..\test\include;$(BOOST_PATH);%(AdditionalIncludeDirectories) + + + Console + true + true + true + $(GTEST_LIB32);$(GTEST_ROOT)/lib/x86;$(BOOST_PATH)/stage32/lib;$(BOOST_PATH)/stage/lib;$(BOOST_PATH)/bin;%(AdditionalLibraryDirectories);$(OutDir) + gtest.lib;%(AdditionalDependencies) + + + + + Level3 + NotUsing + MaxSpeed + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + $(SolutionDir)..\..\..;$(GTEST_ROOT)/include;$(SolutionDir)..\..\..\test\include;$(BOOST_PATH);%(AdditionalIncludeDirectories) + + + Console + true + true + true + $(GTEST_LIB64);$(GTEST_ROOT)/lib/x64;$(BOOST_PATH)/stage64/lib;$(BOOST_PATH)/bin;%(AdditionalLibraryDirectories);$(OutDir) + gtest.lib;%(AdditionalDependencies) + + + + + + \ No newline at end of file diff --git a/projects/Win/vc14/gtest-tree.vcxproj.filters b/projects/Win/vc14/gtest-tree.vcxproj.filters new file mode 100644 index 00000000..71e1f6c0 --- /dev/null +++ b/projects/Win/vc14/gtest-tree.vcxproj.filters @@ -0,0 +1,30 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;hm;inl;inc;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + + + Header Files + + + Header Files + + + \ No newline at end of file diff --git a/test/unit/CMakeLists.txt b/test/unit/CMakeLists.txt index 218c81ac..f8e78648 100644 --- a/test/unit/CMakeLists.txt +++ b/test/unit/CMakeLists.txt @@ -8,6 +8,7 @@ add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/set) add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/striped-map) add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/striped-set) add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/stack) +add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/tree) add_custom_target(unit-all DEPENDS @@ -20,4 +21,5 @@ add_custom_target(unit-all unit-set unit-striped-set unit-stack + unit-tree ) diff --git a/test/unit/tree/CMakeLists.txt b/test/unit/tree/CMakeLists.txt new file mode 100644 index 00000000..b569e8e7 --- /dev/null +++ b/test/unit/tree/CMakeLists.txt @@ -0,0 +1,20 @@ +set(PACKAGE_NAME unit-tree) + +set(CDSGTEST_TREE_SOURCES + ../main.cpp +) + +include_directories( + ${CMAKE_CURRENT_SOURCE_DIR} +) + +add_executable(${PACKAGE_NAME} ${CDSGTEST_TREE_SOURCES}) +target_link_libraries(${PACKAGE_NAME} + ${CDS_SHARED_LIBRARY} + ${GTEST_LIBRARY} + ${Boost_THREAD_LIBRARY} + ${Boost_SYSTEM_LIBRARY} + ${CMAKE_THREAD_LIBS_INIT} +) + +add_test(NAME ${PACKAGE_NAME} COMMAND ${PACKAGE_NAME} WORKING_DIRECTORY ${EXECUTABLE_OUTPUT_PATH}) \ No newline at end of file diff --git a/test/unit/tree/test_intrusive_tree.h b/test/unit/tree/test_intrusive_tree.h new file mode 100644 index 00000000..ea1016cb --- /dev/null +++ b/test/unit/tree/test_intrusive_tree.h @@ -0,0 +1,446 @@ +/* + 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_TREE_TEST_INTRUSIVE_TREE_H +#define CDSUNIT_TREE_TEST_INTRUSIVE_TREE_H + +#include +#include + +#include +// forward declaration +namespace cds { namespace intrusive {}} + +namespace cds_test { + + namespace ci = cds::intrusive; + + class intrusive_tree: public fixture + { + public: + static size_t const kSize = 1000; + + struct stat + { + unsigned int nDisposeCount ; // count of disposer calling + unsigned int nFindCount ; // count of find-functor calling + unsigned int nUpdateNewCount; + unsigned int nUpdateCount; + mutable unsigned int nEraseCount; + + stat() + { + clear_stat(); + } + + void clear_stat() + { + memset( this, 0, sizeof( *this ) ); + } + }; + + template + struct base_int_item + : public Node + , public stat + + { + int nKey; + int nVal; + + base_int_item() + {} + + explicit base_int_item( int key ) + : nKey( key ) + , nVal( key ) + {} + + base_int_item(int key, int val) + : nKey( key ) + , nVal(val) + {} + + base_int_item( base_int_item const& v ) + : Node() + , stat() + , nKey( v.nKey ) + , nVal( v.nVal ) + {} + + int key() const + { + return nKey; + } + }; + + template + struct member_int_item: public stat + { + int nKey; + int nVal; + + Node hMember; + + stat t; + + member_int_item() + {} + + explicit member_int_item( int key ) + : nKey( key ) + , nVal( key ) + {} + + member_int_item(int key, int val) + : nKey( key ) + , nVal(val) + {} + + member_int_item(member_int_item const& v ) + : stat() + , nKey( v.nKey ) + , nVal( v.nVal ) + {} + + int key() const + { + return nKey; + } + }; + + 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; + } + }; + + + template + struct less + { + bool operator ()(T const& v1, T const& v2 ) const + { + return v1.key() < v2.key(); + } + + bool operator()( T const& lhs, int rhs ) const + { + return lhs.key() < rhs; + } + + bool operator()( int lhs, T const& rhs ) const + { + return lhs < rhs.key(); + } + }; + + template + struct cmp + { + int operator ()(T const& v1, T const& v2 ) const + { + if ( v1.key() < v2.key() ) + return -1; + return v1.key() > v2.key() ? 1 : 0; + } + + bool operator()( T const& lhs, int rhs ) const + { + if ( lhs.key() < rhs ) + return -1; + return lhs.key() > rhs ? 1 : 0; + } + + bool operator()( int lhs, T const& rhs ) const + { + if ( lhs < rhs.key() ) + return -1; + return lhs > rhs.key() ? 1 : 0; + } + }; + + struct other_item { + int nKey; + + explicit other_item( int k ) + : nKey( k ) + {} + + int key() const + { + return nKey; + } + }; + + struct other_less { + template + bool operator()( Q const& lhs, T const& rhs ) const + { + return lhs.key() < rhs.key(); + } + }; + + struct mock_disposer + { + template + void operator ()( T * p ) + { + ++p->nDisposeCount; + } + }; + + protected: + template + void test( Tree& t ) + { + // Precondition: tree is empty + // Postcondition: tree is empty + + ASSERT_TRUE( t.empty() ); + ASSERT_CONTAINER_SIZE( t, 0 ); + size_t const nTreeSize = kSize; + + typedef typename Tree::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() ); + + // insert/find + for ( auto idx : indices ) { + auto& i = data[ idx ]; + + ASSERT_FALSE( t.contains( i.nKey )); + ASSERT_FALSE( t.contains( i )); + ASSERT_FALSE( t.contains( other_item( i.key()), other_less())); + ASSERT_FALSE( t.find( i.nKey, []( value_type&, int ) {} )); + ASSERT_FALSE( t.find_with( other_item( i.key()), other_less(), []( value_type&, other_item const& ) {} )); + + std::pair updResult; + + updResult = t.update( i, []( bool bNew, value_type&, value_type& ) + { + ASSERT_TRUE( false ); + }, false ); + EXPECT_FALSE( updResult.first ); + EXPECT_FALSE( updResult.second ); + + switch ( i.key() % 3 ) { + case 0: + ASSERT_TRUE( t.insert( i )); + ASSERT_FALSE( t.insert( i )); + updResult = t.update( i, []( bool bNew, value_type& val, value_type& arg) + { + EXPECT_FALSE( bNew ); + EXPECT_EQ( &val, &arg ); + }, false ); + EXPECT_TRUE( updResult.first ); + EXPECT_FALSE( updResult.second ); + break; + case 1: + EXPECT_EQ( i.nUpdateNewCount, 0 ); + ASSERT_TRUE( t.insert( i, []( value_type& v ) { ++v.nUpdateNewCount;} )); + EXPECT_EQ( i.nUpdateNewCount, 1 ); + ASSERT_FALSE( t.insert( i, []( value_type& v ) { ++v.nUpdateNewCount;} ) ); + EXPECT_EQ( i.nUpdateNewCount, 1 ); + i.nUpdateNewCount = 0; + break; + case 2: + updResult = t.update( i, []( bool bNew, value_type& val, value_type& arg ) + { + EXPECT_TRUE( false ); + }, false ); + EXPECT_FALSE( updResult.first ); + EXPECT_FALSE( updResult.second ); + + EXPECT_EQ( i.nUpdateNewCount, 0 ); + updResult = t.update( i, []( bool bNew, value_type& val, value_type& arg ) + { + EXPECT_TRUE( bNew ); + EXPECT_EQ( &val, &arg ); + ++val.nUpdateNewCount; + }); + EXPECT_TRUE( updResult.first ); + EXPECT_TRUE( updResult.second ); + EXPECT_EQ( i.nUpdateNewCount, 1 ); + i.nUpdateNewCount = 0; + + EXPECT_EQ( i.nUpdateCount, 0 ); + updResult = t.update( i, []( bool bNew, value_type& val, value_type& arg ) + { + EXPECT_FALSE( bNew ); + EXPECT_EQ( &val, &arg ); + ++val.nUpdateCount; + }, false ); + EXPECT_TRUE( updResult.first ); + EXPECT_TRUE( updResult.second ); + EXPECT_EQ( i.nUpdateCount, 1 ); + i.nUpdateCount = 0; + + break; + } + + ASSERT_TRUE( t.contains( i.nKey ) ); + ASSERT_TRUE( t.contains( i ) ); + ASSERT_TRUE( t.contains( other_item( i.key() ), other_less())); + EXPECT_EQ( i.nFindCount, 0 ); + ASSERT_TRUE( t.find( i.nKey, []( value_type& v, int ) { ++v.nFindCount; } )); + EXPECT_EQ( i.nFindCount, 1 ); + ASSERT_TRUE( t.find_with( other_item( i.key() ), other_less(), []( value_type& v, other_item const& ) { ++v.nFindCount; } )); + EXPECT_EQ( i.nFindCount, 2 ); + ASSERT_TRUE( t.find( i, []( value_type& v, value_type& ) { ++v.nFindCount; } ) ); + EXPECT_EQ( i.nFindCount, 3 ); + } + ASSERT_FALSE( t.empty() ); + ASSERT_CONTAINER_SIZE( t, nTreeSize ); + + 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( t.contains( i.nKey ) ); + ASSERT_TRUE( t.contains( i ) ); + ASSERT_TRUE( t.contains( other_item( i.key() ), other_less() ) ); + EXPECT_EQ( i.nFindCount, 0 ); + ASSERT_TRUE( t.find( i.nKey, []( value_type& v, int ) { ++v.nFindCount; } ) ); + EXPECT_EQ( i.nFindCount, 1 ); + ASSERT_TRUE( t.find_with( other_item( i.key() ), other_less(), []( value_type& v, other_item const& ) { ++v.nFindCount; } ) ); + EXPECT_EQ( i.nFindCount, 2 ); + + value_type v( i ); + switch ( i.key() % 6 ) { + case 0: + ASSERT_FALSE( t.unlink( v )); + ASSERT_TRUE( t.unlink( i )); + ASSERT_FALSE( t.unlink( i ) ); + break; + case 1: + ASSERT_TRUE( t.erase( i.key())); + ASSERT_FALSE( t.erase( i.key() ) ); + break; + case 2: + ASSERT_TRUE( t.erase( v )); + ASSERT_FALSE( t.erase( v ) ); + break; + case 3: + ASSERT_TRUE( t.erase_with( other_item( i.key()), other_less())); + ASSERT_FALSE( t.erase_with( other_item( i.key() ), other_less() ) ); + break; + case 4: + EXPECT_EQ( i.nEraseCount, 0 ); + ASSERT_TRUE( t.erase( v, []( value_type& val ) { ++val.nEraseCount; } )); + EXPECT_EQ( i.nEraseCount, 1 ); + ASSERT_FALSE( t.erase( v, []( value_type& val ) { ++val.nEraseCount; } )); + EXPECT_EQ( i.nEraseCount, 1 ); + break; + case 5: + EXPECT_EQ( i.nEraseCount, 0 ); + ASSERT_TRUE( t.erase_with( other_item( i.key() ), other_less(), []( value_type& val ) { ++val.nEraseCount; } )); + EXPECT_EQ( i.nEraseCount, 1 ); + ASSERT_FALSE( t.erase_with( other_item( i.key() ), other_less(), []( value_type& val ) { ++val.nEraseCount; } )); + EXPECT_EQ( i.nEraseCount, 1 ); + break; + } + + ASSERT_FALSE( t.contains( i.nKey )); + ASSERT_FALSE( t.contains( i )); + ASSERT_FALSE( t.contains( other_item( i.key()), other_less())); + ASSERT_FALSE( t.find( i.nKey, []( value_type&, int ) {} )); + ASSERT_FALSE( t.find( i, []( value_type&, value_type const& ) {} )); + ASSERT_FALSE( t.find_with( other_item( i.key()), other_less(), []( value_type&, other_item const& ) {} )); + } + ASSERT_TRUE( t.empty() ); + ASSERT_CONTAINER_SIZE( t, 0 ); + + // Force retiring cycle + Tree::gc::force_dispose(); + for ( auto& i : data ) { + EXPECT_EQ( i.nDisposeCount, 1 ); + } + + // clear + for ( auto& i : data ) { + i.clear_stat(); + ASSERT_TRUE( t.insert( i )); + } + ASSERT_FALSE( t.empty() ); + ASSERT_CONTAINER_SIZE( t, nTreeSize ); + + // clear test + t.clear(); + + ASSERT_TRUE( t.empty()); + ASSERT_CONTAINER_SIZE( t, 0 ); + ASSERT_TRUE( t.begin() == t.end() ); + ASSERT_TRUE( t.cbegin() == t.cend() ); + + // Force retiring cycle + Tree::gc::force_dispose(); + for ( auto& i : data ) { + EXPECT_EQ( i.nDisposeCount, 1 ); + } + } + }; + +} // namespace cds_test + +#endif // #ifndef CDSUNIT_TREE_TEST_INTRUSIVE_TREE_H diff --git a/test/unit/tree/test_intrusive_tree_hp.h b/test/unit/tree/test_intrusive_tree_hp.h new file mode 100644 index 00000000..8a5835d1 --- /dev/null +++ b/test/unit/tree/test_intrusive_tree_hp.h @@ -0,0 +1,216 @@ +/* + 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_TREE_TEST_INTRUSIVE_TREE_HP_H +#define CDSUNIT_TREE_TEST_INTRUSIVE_TREE_HP_H + +#include "test_intrusive_tree.h" + +// forward declaration +namespace cds { namespace intrusive {}} + +namespace cds_test { + + namespace ci = cds::intrusive; + + class intrusive_tree_hp: public intrusive_tree + { + typedef intrusive_tree base_class; + + protected: + + template + void test( Tree& t ) + { + // Precondition: tree is empty + // Postcondition: tree is empty + + base_class::test( t ); + + ASSERT_TRUE( t.empty() ); + ASSERT_CONTAINER_SIZE( t, 0 ); + + typedef typename Tree::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 Tree::guarded_ptr gp; + + // get/extract from empty tree + for ( auto idx : indices ) { + auto& i = data[idx]; + + gp = t.get( i ); + ASSERT_TRUE( !gp ); + gp = t.get( i.key() ); + ASSERT_TRUE( !gp ); + gp = t.get_with( other_item( i.key()), other_less()); + ASSERT_TRUE( !gp ); + + gp = t.extract( i ); + ASSERT_TRUE( !gp ); + gp = t.extract( i.key()); + ASSERT_TRUE( !gp ); + gp = t.extract_with( other_item( i.key()), other_less()); + ASSERT_TRUE( !gp ); + + gp = t.extract_min(); + ASSERT_TRUE( !gp ); + gp = t.extract_max(); + ASSERT_TRUE( !gp ); + } + + // fill tree + for ( auto& i : data ) { + i.nDisposeCount = 0; + ASSERT_TRUE( t.insert( i ) ); + } + + // get/extract + for ( auto idx : indices ) { + auto& i = data[idx]; + + EXPECT_EQ( i.nFindCount, 0 ); + gp = t.get( i ); + ASSERT_FALSE( !gp ); + ++gp->nFindCount; + EXPECT_EQ( i.nFindCount, 1 ); + + gp = t.get( i.key() ); + ASSERT_FALSE( !gp ); + ++gp->nFindCount; + EXPECT_EQ( i.nFindCount, 2 ); + + gp = t.get_with( other_item( i.key()), other_less()); + ASSERT_FALSE( !gp ); + ++gp->nFindCount; + EXPECT_EQ( i.nFindCount, 3 ); + + EXPECT_EQ( i.nEraseCount, 0 ); + switch ( i.key() % 3 ) { + case 0: + gp = t.extract( i.key()); + break; + case 1: + gp = t.extract( i ); + break; + case 2: + gp = t.extract_with( other_item( i.key() ), other_less() ); + break; + } + ASSERT_FALSE( !gp ); + ++gp->nEraseCount; + EXPECT_EQ( i.nEraseCount, 1 ); + + gp = t.extract( i ); + ASSERT_TRUE( !gp ); + gp = t.extract( i.key() ); + ASSERT_TRUE( !gp ); + gp = t.extract_with( other_item( i.key() ), other_less() ); + ASSERT_TRUE( !gp ); + } + + gp.release(); + + ASSERT_TRUE( t.empty() ); + ASSERT_CONTAINER_SIZE( t, 0 ); + + // Force retiring cycle + Tree::gc::force_dispose(); + for ( auto& i : data ) { + EXPECT_EQ( i.nDisposeCount, 1 ); + } + + // extract_min + for ( auto& i : data ) { + i.nDisposeCount = 0; + ASSERT_TRUE( t.insert( i ) ); + } + + size_t nCount = 0; + int nKey = -1; + while ( !t.empty() ) { + gp = t.extract_min(); + ASSERT_FALSE( !gp ); + EXPECT_EQ( gp->key(), nKey + 1 ); + ++nCount; + nKey = gp->key(); + } + gp.release(); + ASSERT_TRUE( t.empty() ); + ASSERT_CONTAINER_SIZE( t, 0 ); + EXPECT_EQ( nCount, data.size() ); + + // Force retiring cycle + Tree::gc::force_dispose(); + for ( auto& i : data ) { + EXPECT_EQ( i.nDisposeCount, 1 ); + } + + // extract_max + for ( auto& i : data ) { + i.nDisposeCount = 0; + ASSERT_TRUE( t.insert( i ) ); + } + + nCount = 0; + nKey = static_cast( data.size()); + while ( !t.empty() ) { + gp = t.extract_max(); + ASSERT_FALSE( !gp ); + EXPECT_EQ( gp->key(), nKey - 1 ); + ++nCount; + nKey = gp->key(); + } + gp.release(); + ASSERT_TRUE( t.empty() ); + ASSERT_CONTAINER_SIZE( t, 0 ); + EXPECT_EQ( nCount, data.size() ); + + // Force retiring cycle + Tree::gc::force_dispose(); + for ( auto& i : data ) { + EXPECT_EQ( i.nDisposeCount, 1 ); + } + + } + }; + +} // namespace cds_test + +#endif // #ifndef CDSUNIT_TREE_TEST_INTRUSIVE_TREE_HP_H -- 2.34.1