From: khizmax Date: Wed, 9 Mar 2016 21:03:46 +0000 (+0300) Subject: Migrated some unit tests to gtest framework X-Git-Tag: v2.2.0~372 X-Git-Url: http://plrg.eecs.uci.edu/git/?p=libcds.git;a=commitdiff_plain;h=8b9c9024eadab6f6bb9a5c086b74193be787f18e Migrated some unit tests to gtest framework --- diff --git a/projects/Win/vc14/gtest-set.vcxproj b/projects/Win/vc14/gtest-set.vcxproj index 4544b3ee..a7e63cb1 100644 --- a/projects/Win/vc14/gtest-set.vcxproj +++ b/projects/Win/vc14/gtest-set.vcxproj @@ -1,239 +1,242 @@ - - - - - DebugVLD - Win32 - - - DebugVLD - x64 - - - Debug - Win32 - - - Release - Win32 - - - Debug - x64 - - - Release - x64 - - - - - - - - - - {A589D3F1-A749-4268-ADEC-D0CE13D1E359} - Win32Proj - set - 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) - - - - - + + + + + DebugVLD + Win32 + + + DebugVLD + x64 + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + + + + + + + + + + {A589D3F1-A749-4268-ADEC-D0CE13D1E359} + Win32Proj + set + 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-set.vcxproj.filters b/projects/Win/vc14/gtest-set.vcxproj.filters index acef4efc..09bd8367 100644 --- a/projects/Win/vc14/gtest-set.vcxproj.filters +++ b/projects/Win/vc14/gtest-set.vcxproj.filters @@ -1,27 +1,36 @@ - - - - - {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 - - + + + + + {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 + + + Source Files + + + Source Files + + + + + Header Files + + + Header Files + + \ No newline at end of file diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 9774275d..730a3072 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -6,5 +6,7 @@ include_directories( ${GTEST_INCLUDE_DIRS} ) +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DGTEST_LANG_CXX11") + add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/unit) add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/stress) diff --git a/test/unit/CMakeLists.txt b/test/unit/CMakeLists.txt index 42d34b89..8be50916 100644 --- a/test/unit/CMakeLists.txt +++ b/test/unit/CMakeLists.txt @@ -2,4 +2,5 @@ add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/deque) add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/list) add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/pqueue) add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/queue) +add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/set) add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/stack) diff --git a/test/unit/set/CMakeLists.txt b/test/unit/set/CMakeLists.txt index ad1a79d6..111ef264 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_michael_hp.cpp + intrusive_michael_michael_dhp.cpp ) include_directories( diff --git a/test/unit/set/intrusive_michael_michael_dhp.cpp b/test/unit/set/intrusive_michael_michael_dhp.cpp new file mode 100644 index 00000000..0ce9bd8f --- /dev/null +++ b/test/unit/set/intrusive_michael_michael_dhp.cpp @@ -0,0 +1,204 @@ +/* + 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 + +namespace { + namespace ci = cds::intrusive; + typedef cds::gc::DHP gc_type; + + class IntrusiveMichaelSet_DHP : public cds_test::intrusive_set_hp + { + typedef cds_test::intrusive_set_hp base_class; + + protected: + typedef typename base_class::base_int_item< ci::michael_list::node> base_item_type; + typedef typename base_class::member_int_item< ci::michael_list::node> member_item_type; + + void SetUp() + { + struct list_traits : public ci::michael_list::traits + { + typedef ci::michael_list::base_hook< ci::opt::gc> hook; + }; + typedef ci::MichaelList< 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::hp::GarbageCollector::Destruct(); + } + }; + + + TEST_F( IntrusiveMichaelSet_DHP, base_cmp ) + { + typedef ci::MichaelList< gc_type + , base_item_type + ,ci::michael_list::make_traits< + ci::opt::hook< ci::michael_list::base_hook< ci::opt::gc< gc_type >>> + ,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( IntrusiveMichaelSet_DHP, base_less ) + { + typedef ci::MichaelList< gc_type + , base_item_type + ,ci::michael_list::make_traits< + ci::opt::hook< ci::michael_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( IntrusiveMichaelSet_DHP, base_cmpmix ) + { + struct list_traits : public ci::michael_list::traits + { + typedef ci::michael_list::base_hook< ci::opt::gc> hook; + typedef base_class::less less; + typedef cmp compare; + typedef mock_disposer disposer; + }; + typedef ci::MichaelList< 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( IntrusiveMichaelSet_DHP, member_cmp ) + { + typedef ci::MichaelList< gc_type + ,member_item_type + ,ci::michael_list::make_traits< + ci::opt::hook< ci::michael_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( IntrusiveMichaelSet_DHP, member_less ) + { + typedef ci::MichaelList< gc_type + , member_item_type + ,ci::michael_list::make_traits< + ci::opt::hook< ci::michael_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( IntrusiveMichaelSet_DHP, member_cmpmix ) + { + struct list_traits : public ci::michael_list::traits + { + typedef ci::michael_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::MichaelList< 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 ); + } + +} // namespace diff --git a/test/unit/set/intrusive_michael_michael_hp.cpp b/test/unit/set/intrusive_michael_michael_hp.cpp new file mode 100644 index 00000000..7aca853a --- /dev/null +++ b/test/unit/set/intrusive_michael_michael_hp.cpp @@ -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. +*/ + +#include "test_intrusive_set_hp.h" + +#include +#include + +namespace { + namespace ci = cds::intrusive; + typedef cds::gc::HP gc_type; + + class IntrusiveMichaelSet_HP : public cds_test::intrusive_set_hp + { + typedef cds_test::intrusive_set_hp base_class; + + protected: + typedef typename base_class::base_int_item< ci::michael_list::node> base_item_type; + typedef typename base_class::member_int_item< ci::michael_list::node> member_item_type; + + void SetUp() + { + struct list_traits : public ci::michael_list::traits + { + typedef ci::michael_list::base_hook< ci::opt::gc> hook; + }; + typedef ci::MichaelList< 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( IntrusiveMichaelSet_HP, base_cmp ) + { + typedef ci::MichaelList< gc_type + , base_item_type + ,ci::michael_list::make_traits< + ci::opt::hook< ci::michael_list::base_hook< ci::opt::gc< gc_type > > > + ,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( IntrusiveMichaelSet_HP, base_less ) + { + typedef ci::MichaelList< gc_type + , base_item_type + ,ci::michael_list::make_traits< + ci::opt::hook< ci::michael_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( IntrusiveMichaelSet_HP, base_cmpmix ) + { + struct list_traits : public ci::michael_list::traits + { + typedef ci::michael_list::base_hook< ci::opt::gc> hook; + typedef base_class::less less; + typedef cmp compare; + typedef mock_disposer disposer; + }; + typedef ci::MichaelList< 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( IntrusiveMichaelSet_HP, member_cmp ) + { + typedef ci::MichaelList< gc_type + ,member_item_type + ,ci::michael_list::make_traits< + ci::opt::hook< ci::michael_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( IntrusiveMichaelSet_HP, member_less ) + { + typedef ci::MichaelList< gc_type + , member_item_type + ,ci::michael_list::make_traits< + ci::opt::hook< ci::michael_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( IntrusiveMichaelSet_HP, member_cmpmix ) + { + struct list_traits : public ci::michael_list::traits + { + typedef ci::michael_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::MichaelList< 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 ); + } + +} // namespace diff --git a/test/unit/set/test_intrusive_set.h b/test/unit/set/test_intrusive_set.h index c84da82d..2930596c 100644 --- a/test/unit/set/test_intrusive_set.h +++ b/test/unit/set/test_intrusive_set.h @@ -48,6 +48,8 @@ namespace cds_test { class intrusive_set: public fixture { protected: + static size_t const kSize = 100; + struct stat { unsigned int nDisposeCount ; // count of disposer calling @@ -58,7 +60,12 @@ namespace cds_test { stat() { - memset( this, 0, sizeof(*this)); + clear_stat(); + } + + void clear_stat() + { + memset( this, 0, sizeof( *this ) ); } }; @@ -251,45 +258,6 @@ namespace cds_test { } }; - struct find_functor - { - template - void operator()( Item& item, T& /*val*/ ) - { - ++item.nFindCount; - } - }; - - struct insert_functor - { - template - void operator()(Item& item ) - { - item.nVal = item.nKey * 100; - } - }; - - struct update_functor - { - template - void operator()( bool bNew, Item& item, Item& /*val*/ ) - { - if ( bNew ) - ++item.nUpdateNewCount; - else - ++item.nUpdateCount; - } - }; - - struct erase_functor - { - template - void operator()( Item const& item ) - { - item.nEraseCount++; - } - }; - template void test( Set& s ) { @@ -302,21 +270,161 @@ namespace cds_test { typedef typename Set::value_type value_type; std::vector< value_type > data; - size_t const kSize = 100; + 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( data.begin(), data.end() ); + shuffle( indices.begin(), indices.end() ); // insert/find - for ( auto& i : data ) { + for ( auto idx : indices ) { + auto& i = data[ idx ]; + + ASSERT_FALSE( s.contains( i.nKey )); + ASSERT_FALSE( s.contains( i )); + ASSERT_FALSE( s.contains( other_item( i.key()), other_less())); + ASSERT_FALSE( s.find( i.nKey, []( value_type&, int ) {} )); + ASSERT_FALSE( s.find_with( other_item( i.key()), other_less(), []( value_type&, other_item const& ) {} )); + + std::pair updResult; + + updResult = s.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( s.insert( i )); + ASSERT_FALSE( s.insert( i )); + updResult = s.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( s.insert( i, []( value_type& v ) { ++v.nUpdateNewCount;} )); + EXPECT_EQ( i.nUpdateNewCount, 1 ); + ASSERT_FALSE( s.insert( i, []( value_type& v ) { ++v.nUpdateNewCount;} ) ); + EXPECT_EQ( i.nUpdateNewCount, 1 ); + i.nUpdateNewCount = 0; + break; + case 2: + updResult = s.update( i, []( bool bNew, value_type& val, value_type& arg ) + { + EXPECT_TRUE( bNew ); + EXPECT_EQ( &val, &arg ); + }); + EXPECT_TRUE( updResult.first ); + EXPECT_TRUE( updResult.second ); + break; + } + + ASSERT_TRUE( s.contains( i.nKey ) ); + ASSERT_TRUE( s.contains( i ) ); + ASSERT_TRUE( s.contains( other_item( i.key() ), other_less())); + EXPECT_EQ( i.nFindCount, 0 ); + ASSERT_TRUE( s.find( i.nKey, []( value_type& v, int ) { ++v.nFindCount; } )); + EXPECT_EQ( i.nFindCount, 1 ); + ASSERT_TRUE( s.find_with( other_item( i.key() ), other_less(), []( value_type& v, other_item const& ) { ++v.nFindCount; } )); + EXPECT_EQ( i.nFindCount, 2 ); + } + ASSERT_FALSE( s.empty() ); + ASSERT_CONTAINER_SIZE( s, kSize ); + + 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 ) ); + ASSERT_TRUE( s.contains( i ) ); + ASSERT_TRUE( s.contains( other_item( i.key() ), other_less() ) ); + EXPECT_EQ( i.nFindCount, 0 ); + ASSERT_TRUE( s.find( i.nKey, []( value_type& v, int ) { ++v.nFindCount; } ) ); + EXPECT_EQ( i.nFindCount, 1 ); + ASSERT_TRUE( s.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( 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: + ASSERT_TRUE( s.erase( v )); + ASSERT_FALSE( s.erase( v ) ); + break; + case 3: + ASSERT_TRUE( s.erase_with( other_item( i.key()), other_less())); + ASSERT_FALSE( s.erase_with( other_item( i.key() ), other_less() ) ); + break; + case 4: + EXPECT_EQ( i.nEraseCount, 0 ); + ASSERT_TRUE( s.erase( v, []( value_type& val ) { ++val.nEraseCount; } )); + EXPECT_EQ( i.nEraseCount, 1 ); + ASSERT_FALSE( s.erase( v, []( value_type& val ) { ++val.nEraseCount; } )); + EXPECT_EQ( i.nEraseCount, 1 ); + break; + case 5: + EXPECT_EQ( i.nEraseCount, 0 ); + ASSERT_TRUE( s.erase_with( other_item( i.key() ), other_less(), []( value_type& val ) { ++val.nEraseCount; } )); + EXPECT_EQ( i.nEraseCount, 1 ); + ASSERT_FALSE( s.erase_with( other_item( i.key() ), other_less(), []( value_type& val ) { ++val.nEraseCount; } )); + EXPECT_EQ( i.nEraseCount, 1 ); + break; + } + ASSERT_FALSE( s.contains( i.nKey )); ASSERT_FALSE( s.contains( i )); - ASSERT_FALSE( s.contains( other_item( i.key()), other_less()); - ASSERT_FALSE( s.find( i.nKey, []( value_type&, int ) {} ); + ASSERT_FALSE( s.contains( other_item( i.key()), other_less())); + ASSERT_FALSE( s.find( i.nKey, []( value_type&, int ) {} )); ASSERT_FALSE( s.find_with( other_item( i.key()), other_less(), []( value_type&, other_item 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 ); + } + + // clear + for ( auto& i : data ) { + i.nDisposeCount = 0; + ASSERT_TRUE( s.insert( i )); + } + ASSERT_FALSE( s.empty() ); + ASSERT_CONTAINER_SIZE( s, kSize ); + + s.clear(); + + 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 ); + } } }; diff --git a/test/unit/set/test_intrusive_set_hp.h b/test/unit/set/test_intrusive_set_hp.h new file mode 100644 index 00000000..7c122c52 --- /dev/null +++ b/test/unit/set/test_intrusive_set_hp.h @@ -0,0 +1,160 @@ +/* + 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_HP_H +#define CDSUNIT_SET_TEST_INTRUSIVE_SET_HP_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_hp: 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::guarded_ptr gp; + + // get/extract from empty set + for ( auto idx : indices ) { + auto& i = data[idx]; + + gp = s.get( i ); + ASSERT_TRUE( !gp ); + gp = s.get( i.key() ); + ASSERT_TRUE( !gp ); + gp = s.get_with( other_item( i.key()), other_less()); + ASSERT_TRUE( !gp ); + + gp = s.extract( i ); + ASSERT_TRUE( !gp ); + gp = s.extract( i.key()); + ASSERT_TRUE( !gp ); + gp = s.extract_with( other_item( i.key()), other_less()); + ASSERT_TRUE( !gp ); + } + + // fill set + for ( auto& i : data ) { + i.nDisposeCount = 0; + ASSERT_TRUE( s.insert( i ) ); + } + + // get/extract + for ( auto idx : indices ) { + auto& i = data[idx]; + + EXPECT_EQ( i.nFindCount, 0 ); + gp = s.get( i ); + ASSERT_TRUE( gp ); + ++gp->nFindCount; + EXPECT_EQ( i.nFindCount, 1 ); + + gp = s.get( i.key() ); + ASSERT_TRUE( gp ); + ++gp->nFindCount; + EXPECT_EQ( i.nFindCount, 2 ); + + gp = s.get_with( other_item( i.key()), other_less()); + ASSERT_TRUE( gp ); + ++gp->nFindCount; + EXPECT_EQ( i.nFindCount, 3 ); + + EXPECT_EQ( i.nEraseCount, 0 ); + switch ( i.key() % 3 ) { + case 0: + gp = s.extract( i.key()); + break; + case 1: + gp = s.extract( i ); + break; + case 2: + gp = s.extract_with( other_item( i.key() ), other_less() ); + break; + } + ASSERT_TRUE( gp ); + ++gp->nEraseCount; + EXPECT_EQ( i.nEraseCount, 1 ); + + gp = s.extract( i ); + ASSERT_TRUE( !gp ); + gp = s.extract( i.key() ); + ASSERT_TRUE( !gp ); + gp = s.extract_with( other_item( i.key() ), other_less() ); + 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_SET_HP_H