Test tuning
[libcds.git] / tests / unit / map2 / map_insdel_func.h
index 78fa6bb7921dc90f1d27b2df9335bbc33a4587bf..83d64b243fb375a9efdccaa854a02e8eb6e2b77d 100644 (file)
@@ -39,7 +39,7 @@ namespace map2 {
             size_t      nData;
             atomics::atomic<size_t> nUpdateCall;
             atomics::atomic<bool>   bInitialized;
-            cds::OS::ThreadId          threadId     ;   // insert thread id
+            cds::OS::ThreadId       threadId;   // inserter thread id
 
             typedef cds::sync::spin_lock< cds::backoff::pause > lock_type;
             mutable lock_type   m_access;
@@ -49,15 +49,15 @@ namespace map2 {
                 , nData(0)
                 , nUpdateCall(0)
                 , bInitialized( false )
-                , threadId( cds::OS::get_current_thread_id() )
+                , threadId( cds::OS::get_current_thread_id())
             {}
 
             value_type( value_type const& s )
                 : nKey(s.nKey)
                 , nData(s.nData)
                 , nUpdateCall(s.nUpdateCall.load(atomics::memory_order_relaxed))
-                , bInitialized( s.bInitialized.load(atomics::memory_order_relaxed) )
-                , threadId( cds::OS::get_current_thread_id() )
+                , bInitialized( s.bInitialized.load(atomics::memory_order_relaxed))
+                , threadId( cds::OS::get_current_thread_id())
             {}
 
             // boost::container::flat_map requires operator =
@@ -101,7 +101,7 @@ namespace map2 {
                 template <typename Key, typename Val >
                 void operator()( Key const& key, Val& v )
                 {
-                    std::unique_lock< typename value_type::lock_type>    ac( v.m_access );
+                    std::unique_lock< typename value_type::lock_type> ac( v.m_access );
 
                     v.nKey  = key;
                     v.nData = key * 8;
@@ -151,7 +151,7 @@ namespace map2 {
                 if ( m_nThreadNo & 1 ) {
                     for ( size_t nPass = 0; nPass < nPassCount; ++nPass ) {
                         for ( key_array::const_iterator it = arr.begin(), itEnd = arr.end(); it != itEnd; ++it ) {
-                            if ( rMap.insert_with( *it, std::ref(func) ) )
+                            if ( rMap.insert_with( *it, std::ref(func)))
                                 ++m_nInsertSuccess;
                             else
                                 ++m_nInsertFailed;
@@ -161,7 +161,7 @@ namespace map2 {
                 else {
                     for ( size_t nPass = 0; nPass < nPassCount; ++nPass ) {
                         for ( key_array::const_reverse_iterator it = arr.rbegin(), itEnd = arr.rend(); it != itEnd; ++it ) {
-                            if ( rMap.insert_with( *it, std::ref(func) ) )
+                            if ( rMap.insert_with( *it, std::ref(func)))
                                 ++m_nInsertSuccess;
                             else
                                 ++m_nInsertFailed;
@@ -195,7 +195,7 @@ namespace map2 {
                 template <typename Key, typename Val>
                 void operator()( bool bNew, Key const& key, Val& v )
                 {
-                    std::unique_lock<typename value_type::lock_type>    ac( v.m_access );
+                    std::unique_lock<typename value_type::lock_type> ac( v.m_access );
                     if ( bNew ) {
                         ++nCreated;
                         v.nKey = key;
@@ -203,6 +203,7 @@ namespace map2 {
                         v.bInitialized.store( true, atomics::memory_order_relaxed);
                     }
                     else {
+                        assert( v.bInitialized.load( atomics::memory_order_relaxed ));
                         v.nUpdateCall.fetch_add( 1, atomics::memory_order_relaxed );
                         ++nModified;
                     }
@@ -218,7 +219,9 @@ namespace map2 {
                 template <typename Val>
                 void operator()( Val& cur, Val * old )
                 {
-                    operator()( old != nullptr, cur.first, cur.second );
+                    if ( old )
+                        cur.second.bInitialized.store( true, atomics::memory_order_release );
+                    operator()( old == nullptr, cur.first, cur.second );
                 }
 
             private:
@@ -266,8 +269,7 @@ namespace map2 {
                 if ( m_nThreadNo & 1 ) {
                     for ( size_t nPass = 0; nPass < nPassCount; ++nPass ) {
                         for ( key_array::const_iterator it = arr.begin(), itEnd = arr.end(); it != itEnd; ++it ) {
-                        //for ( size_t nItem = 0; nItem < c_nMapSize; ++nItem ) {
-                            std::pair<bool, bool> ret = rMap.update( *it, std::ref( func ) );
+                            std::pair<bool, bool> ret = rMap.update( *it, std::ref( func ));
                             if ( ret.first  ) {
                                 if ( ret.second )
                                     ++m_nUpdateCreated;
@@ -282,7 +284,7 @@ namespace map2 {
                 else {
                     for ( size_t nPass = 0; nPass < nPassCount; ++nPass ) {
                         for ( key_array::const_reverse_iterator it = arr.rbegin(), itEnd = arr.rend(); it != itEnd; ++it ) {
-                            std::pair<bool, bool> ret = rMap.update( *it, std::ref( func ) );
+                            std::pair<bool, bool> ret = rMap.update( *it, std::ref( func ));
                             if ( ret.first  ) {
                                 if ( ret.second )
                                     ++m_nUpdateCreated;
@@ -394,7 +396,7 @@ namespace map2 {
                     for ( size_t nPass = 0; nPass < nPassCount; ++nPass ) {
                         for ( key_array::const_iterator it = arr.begin(), itEnd = arr.end(); it != itEnd; ++it ) {
                             func.m_cnt.nKeyExpected = *it;
-                            if ( rMap.erase( *it, std::ref(func) ))
+                            if ( rMap.erase( *it, std::ref(func)))
                                 ++m_nDeleteSuccess;
                             else
                                 ++m_nDeleteFailed;
@@ -405,7 +407,7 @@ namespace map2 {
                     for ( size_t nPass = 0; nPass < nPassCount; ++nPass ) {
                         for ( key_array::const_reverse_iterator it = arr.rbegin(), itEnd = arr.rend(); it != itEnd; ++it ) {
                             func.m_cnt.nKeyExpected = *it;
-                            if ( rMap.erase( *it, std::ref(func) ))
+                            if ( rMap.erase( *it, std::ref(func)))
                                 ++m_nDeleteSuccess;
                             else
                                 ++m_nDeleteFailed;
@@ -432,14 +434,14 @@ namespace map2 {
             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() );
+            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.add( new UpdaterThread( pool, testMap ), c_nUpdateThreadCount );
             pool.run();
-            CPPUNIT_MSG( "   Duration=" << pool.avgDuration() );
+            CPPUNIT_MSG( "   Duration=" << pool.avgDuration());
 
             size_t nInsertSuccess = 0;
             size_t nInsertFailed = 0;
@@ -452,14 +454,14 @@ namespace map2 {
             size_t nUpdateModified = 0;
             size_t nEnsFuncCreated = 0;
             size_t nEnsFuncModified = 0;
-            size_t nTestFunctorRef = 0;
+            size_t nInsFuncCalled = 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;
-                    nTestFunctorRef += pThread->m_nTestFunctorRef;
+                    nInsFuncCalled += pThread->m_nTestFunctorRef;
                 }
                 else {
                     DeleterThread * p = dynamic_cast<DeleterThread *>( *it );
@@ -497,8 +499,8 @@ namespace map2 {
             CPPUNIT_CHECK_EX( nUpdateCreated == nEnsFuncCreated, "Update created=" << nUpdateCreated << " functor=" << nEnsFuncCreated );
             CPPUNIT_CHECK_EX( nUpdateModified == nEnsFuncModified, "Update modified=" << nUpdateModified << " functor=" << nEnsFuncModified );
 
-            // nTestFunctorRef is call count of insert functor
-            CPPUNIT_CHECK_EX( nTestFunctorRef == nInsertSuccess, "nInsertSuccess=" << nInsertSuccess << " functor nTestFunctorRef=" << nTestFunctorRef );
+            // nInsFuncCalled is call count of insert functor
+            CPPUNIT_CHECK_EX( nInsFuncCalled == nInsertSuccess, "nInsertSuccess=" << nInsertSuccess << " functor nInsFuncCalled=" << nInsFuncCalled );
 
             check_before_cleanup( testMap );
 
@@ -507,8 +509,8 @@ namespace map2 {
             for ( size_t nItem = 0; nItem < c_nMapSize; ++nItem ) {
                 testMap.erase( nItem );
             }
-            CPPUNIT_MSG( "   Duration=" << timer.duration() );
-            CPPUNIT_CHECK( testMap.empty() );
+            CPPUNIT_MSG( "   Duration=" << timer.duration());
+            CPPUNIT_CHECK( testMap.empty());
 
             additional_check( testMap );
             print_stat( testMap );