Added copyright and license
[libcds.git] / tests / unit / map2 / map_insdel_int.cpp
index a6ecc14989d77d6e1848a620f113fd8f4ed3b459..5b942298345b04614a84e51500fb3167e10f168b 100644 (file)
-//$$CDS-header$$
-
-#include "map2/map_types.h"
-#include "cppunit/thread.h"
-
-#include <vector>
+/*
+    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 "map2/map_insdel_int.h"
+#include <cds/os/topology.h>
 
 namespace map2 {
+    CPPUNIT_TEST_SUITE_REGISTRATION( Map_InsDel_int );
 
-#   define TEST_MAP(X)         void X() { test<MapTypes<key_type, value_type>::X >()    ; }
-#   define TEST_MAP_EXTRACT(X)  TEST_MAP(X)
-#   define TEST_MAP_NOLF(X)    void X() { test_nolf<MapTypes<key_type, value_type>::X >()    ; }
-#   define TEST_MAP_NOLF_EXTRACT(X) TEST_MAP_NOLF(X)
-
-    namespace {
-        static size_t  c_nMapSize = 1000000    ;  // map size
-        static size_t  c_nInsertThreadCount = 4;  // count of insertion thread
-        static size_t  c_nDeleteThreadCount = 4;  // count of deletion thread
-        static size_t  c_nThreadPassCount = 4  ;  // pass count for each thread
-        static size_t  c_nMaxLoadFactor = 8    ;  // maximum load factor
-        static bool    c_bPrintGCState = true;
-    }
-
-    class Map_InsDel_int: public CppUnitMini::TestCase
-    {
-        typedef size_t  key_type;
-        typedef size_t  value_type;
-
-        typedef std::vector<key_type>   key_array;
-        key_array                       m_arrValues;
-
-        template <class MAP>
-        class Inserter: public CppUnitMini::TestThread
-        {
-            MAP&     m_Map;
-
-            virtual Inserter *    clone()
-            {
-                return new Inserter( *this );
-            }
-        public:
-            size_t  m_nInsertSuccess;
-            size_t  m_nInsertFailed;
-
-        public:
-            Inserter( CppUnitMini::ThreadPool& pool, MAP& rMap )
-                : CppUnitMini::TestThread( pool )
-                , m_Map( rMap )
-            {}
-            Inserter( Inserter& src )
-                : CppUnitMini::TestThread( src )
-                , m_Map( src.m_Map )
-            {}
-
-            Map_InsDel_int&  getTest()
-            {
-                return reinterpret_cast<Map_InsDel_int&>( m_Pool.m_Test );
-            }
-
-            virtual void init() { cds::threading::Manager::attachThread()   ; }
-            virtual void fini() { cds::threading::Manager::detachThread()   ; }
-
-            virtual void test()
-            {
-                MAP& rMap = m_Map;
-
-                m_nInsertSuccess =
-                    m_nInsertFailed = 0;
-                key_array const& arr = getTest().m_arrValues;
-
-                if ( m_nThreadNo & 1 ) {
-                    for ( size_t nPass = 0; nPass < c_nThreadPassCount; ++nPass ) {
-                        for ( key_array::const_iterator it = arr.begin(), itEnd = arr.end(); it != itEnd; ++it ) {
-                            if ( rMap.insert( *it, *it * 8 ) )
-                                ++m_nInsertSuccess;
-                            else
-                                ++m_nInsertFailed;
-                        }
-                    }
-                }
-                else {
-                    for ( size_t nPass = 0; nPass < c_nThreadPassCount; ++nPass ) {
-                        for ( key_array::const_reverse_iterator it = arr.rbegin(), itEnd = arr.rend(); it != itEnd; ++it ) {
-                            if ( rMap.insert( *it, *it * 8 ) )
-                                ++m_nInsertSuccess;
-                            else
-                                ++m_nInsertFailed;
-                        }
-                    }
-                }
-            }
-        };
-
-        template <class MAP>
-        class Deleter: public CppUnitMini::TestThread
-        {
-            MAP&     m_Map;
-
-            virtual Deleter *    clone()
-            {
-                return new Deleter( *this );
-            }
-        public:
-            size_t  m_nDeleteSuccess;
-            size_t  m_nDeleteFailed;
-
-        public:
-            Deleter( CppUnitMini::ThreadPool& pool, MAP& rMap )
-                : CppUnitMini::TestThread( pool )
-                , m_Map( rMap )
-            {}
-            Deleter( Deleter& src )
-                : CppUnitMini::TestThread( src )
-                , m_Map( src.m_Map )
-            {}
-
-            Map_InsDel_int&  getTest()
-            {
-                return reinterpret_cast<Map_InsDel_int&>( m_Pool.m_Test );
-            }
-
-            virtual void init() { cds::threading::Manager::attachThread()   ; }
-            virtual void fini() { cds::threading::Manager::detachThread()   ; }
-
-            virtual void test()
-            {
-                MAP& rMap = m_Map;
-
-                m_nDeleteSuccess =
-                    m_nDeleteFailed = 0;
-                key_array const& arr = getTest().m_arrValues;
-
-                if ( m_nThreadNo & 1 ) {
-                    for ( size_t nPass = 0; nPass < c_nThreadPassCount; ++nPass ) {
-                        for ( key_array::const_iterator it = arr.begin(), itEnd = arr.end(); it != itEnd; ++it ) {
-                            if ( rMap.erase( *it ) )
-                                ++m_nDeleteSuccess;
-                            else
-                                ++m_nDeleteFailed;
-                        }
-                    }
-                }
-                else {
-                    for ( size_t nPass = 0; nPass < c_nThreadPassCount; ++nPass ) {
-                        for ( key_array::const_reverse_iterator it = arr.rbegin(), itEnd = arr.rend(); it != itEnd; ++it ) {
-                            if ( rMap.erase( *it ) )
-                                ++m_nDeleteSuccess;
-                            else
-                                ++m_nDeleteFailed;
-                        }
-                    }
-                }
-            }
-        };
-
-    protected:
-        template <class MAP>
-        void do_test( MAP& testMap )
-        {
-            typedef Inserter<MAP>       InserterThread;
-            typedef Deleter<MAP>        DeleterThread;
-            cds::OS::Timer    timer;
-
-            m_arrValues.clear();
-            m_arrValues.reserve( c_nMapSize );
-            for ( size_t i = 0; i < c_nMapSize; ++i )
-                m_arrValues.push_back( i );
-            std::random_shuffle( m_arrValues.begin(), m_arrValues.end() );
-
-            CppUnitMini::ThreadPool pool( *this );
-            pool.add( new InserterThread( pool, testMap ), c_nInsertThreadCount );
-            pool.add( new DeleterThread( pool, testMap ), c_nDeleteThreadCount );
-            pool.run();
-            CPPUNIT_MSG( "   Duration=" << pool.avgDuration() );
-
-            size_t nInsertSuccess = 0;
-            size_t nInsertFailed = 0;
-            size_t nDeleteSuccess = 0;
-            size_t nDeleteFailed = 0;
-            for ( CppUnitMini::ThreadPool::iterator it = pool.begin(); it != pool.end(); ++it ) {
-                InserterThread * pThread = dynamic_cast<InserterThread *>( *it );
-                if ( pThread ) {
-                    nInsertSuccess += pThread->m_nInsertSuccess;
-                    nInsertFailed += pThread->m_nInsertFailed;
-                }
-                else {
-                    DeleterThread * p = static_cast<DeleterThread *>( *it );
-                    nDeleteSuccess += p->m_nDeleteSuccess;
-                    nDeleteFailed += p->m_nDeleteFailed;
-                }
-            }
-
-            CPPUNIT_MSG( "    Totals: Ins succ=" << nInsertSuccess
-                << " Del succ=" << nDeleteSuccess << "\n"
-                << "          : Ins fail=" << nInsertFailed
-                << " Del fail=" << nDeleteFailed
-                << " Map size=" << testMap.size()
-                );
-
-            check_before_cleanup( testMap );
-
-            CPPUNIT_MSG( "  Clear map (single-threaded)..." );
-            timer.reset();
-            for ( size_t nItem = 0; nItem < c_nMapSize; ++nItem ) {
-                testMap.erase( nItem );
-            }
-            CPPUNIT_MSG( "   Duration=" << timer.duration() );
-            CPPUNIT_ASSERT_EX( testMap.empty(), ((long long) testMap.size()) );
-
-            additional_check( testMap );
-            print_stat( testMap );
-            additional_cleanup( testMap );
-        }
-
-        template <class MAP>
-        void test()
-        {
-            CPPUNIT_MSG( "Thread count: insert=" << c_nInsertThreadCount
-                << " delete=" << c_nDeleteThreadCount
-                << " pass count=" << c_nThreadPassCount
-                << " map size=" << c_nMapSize
-                );
-
-            for ( size_t nLoadFactor = 1; nLoadFactor <= c_nMaxLoadFactor; nLoadFactor *= 2 ) {
-                CPPUNIT_MSG( "Load factor=" << nLoadFactor );
-                MAP  testMap( c_nMapSize, nLoadFactor );
-                do_test( testMap );
-                if ( c_bPrintGCState )
-                    print_gc_state();
-            }
-        }
 
-        template <class MAP>
-        void test_nolf()
-        {
-            CPPUNIT_MSG( "Thread count: insert=" << c_nInsertThreadCount
-                << " delete=" << c_nDeleteThreadCount
-                << " pass count=" << c_nThreadPassCount
-                << " map size=" << c_nMapSize
-                );
+    void Map_InsDel_int::setUpParams( const CppUnitMini::TestCfg& cfg ) {
+        c_nInsertThreadCount = cfg.getSizeT("InsertThreadCount", c_nInsertThreadCount );
+        c_nDeleteThreadCount = cfg.getSizeT("DeleteThreadCount", c_nDeleteThreadCount );
+        c_nThreadPassCount = cfg.getSizeT("ThreadPassCount", c_nThreadPassCount );
+        c_nMapSize = cfg.getSizeT("MapSize", c_nMapSize );
+        c_nMaxLoadFactor = cfg.getSizeT("MaxLoadFactor", c_nMaxLoadFactor );
 
-            MAP testMap;
-            do_test( testMap );
-            if ( c_bPrintGCState )
-                print_gc_state();
-        }
+        c_nCuckooInitialSize = cfg.getULong("CuckooInitialSize", static_cast<unsigned long>(c_nCuckooInitialSize) );
+        c_nCuckooProbesetSize = cfg.getULong("CuckooProbesetSize", static_cast<unsigned long>(c_nCuckooProbesetSize) );
+        c_nCuckooProbesetThreshold = cfg.getULong("CuckooProbesetThreshold", static_cast<unsigned long>(c_nCuckooProbesetThreshold) );
 
-        void setUpParams( const CppUnitMini::TestCfg& cfg ) {
-            c_nInsertThreadCount = cfg.getULong("InsertThreadCount", 4 );
-            c_nDeleteThreadCount = cfg.getULong("DeleteThreadCount", 4 );
-            c_nThreadPassCount = cfg.getULong("ThreadPassCount", 4 );
-            c_nMapSize = cfg.getULong("MapSize", 1000000 );
-            c_nMaxLoadFactor = cfg.getULong("MaxLoadFactor", 8 );
-            c_bPrintGCState = cfg.getBool("PrintGCStateFlag", true );
-        }
+        c_nFeldmanMap_HeadBits = cfg.getULong("FeldmanMapHeadBits", static_cast<unsigned long>(c_nFeldmanMap_HeadBits) );
+        c_nFeldmanMap_ArrayBits = cfg.getULong("FeldmanMapArrayBits", static_cast<unsigned long>(c_nFeldmanMap_ArrayBits) );
 
-#   include "map2/map_defs.h"
-        CDSUNIT_DECLARE_MichaelMap
-        CDSUNIT_DECLARE_SplitList
-        CDSUNIT_DECLARE_SkipListMap
-        CDSUNIT_DECLARE_EllenBinTreeMap
-        CDSUNIT_DECLARE_BronsonAVLTreeMap
-        CDSUNIT_DECLARE_StripedMap
-        CDSUNIT_DECLARE_RefinableMap
-        CDSUNIT_DECLARE_CuckooMap
-        CDSUNIT_DECLARE_StdMap
+        c_bPrintGCState = cfg.getBool("PrintGCStateFlag", c_bPrintGCState );
 
-        CPPUNIT_TEST_SUITE( Map_InsDel_int )
-            CDSUNIT_TEST_MichaelMap
-            CDSUNIT_TEST_SplitList
-            CDSUNIT_TEST_SkipListMap
-            CDSUNIT_TEST_EllenBinTreeMap
-            CDSUNIT_TEST_BronsonAVLTreeMap
-            CDSUNIT_TEST_StripedMap
-            CDSUNIT_TEST_RefinableMap
-            CDSUNIT_TEST_CuckooMap
-            CDSUNIT_TEST_StdMap
-        CPPUNIT_TEST_SUITE_END()
+        if ( c_nInsertThreadCount == 0 )
+            c_nInsertThreadCount = cds::OS::topology::processor_count();
+        if ( c_nDeleteThreadCount == 0 )
+            c_nDeleteThreadCount = cds::OS::topology::processor_count();
 
-    };
+        m_arrValues.clear();
+        m_arrValues.reserve( c_nMapSize );
+        for ( size_t i = 0; i < c_nMapSize; ++i )
+            m_arrValues.push_back( i );
+        shuffle( m_arrValues.begin(), m_arrValues.end() );
+    }
 
-    CPPUNIT_TEST_SUITE_REGISTRATION( Map_InsDel_int );
 } // namespace map2