3 #include "map2/map_types.h"
4 #include "cppunit/thread.h"
7 #include <algorithm> // random_shuffle
11 # define TEST_MAP(X) void X() { test<MapTypes<key_type, value_type>::X >() ; }
12 # define TEST_MAP_NOLF(X) void X() { test_nolf<MapTypes<key_type, value_type>::X >() ; }
13 # define TEST_MAP_EXTRACT(X) TEST_MAP(X)
14 # define TEST_MAP_NOLF_EXTRACT(X) TEST_MAP_NOLF(X)
16 class Map_InsDel_Item_int: public CppUnitMini::TestCase
18 static size_t c_nMapSize; // map size
19 static size_t c_nThreadCount; // thread count
20 static size_t c_nGoalItem;
21 static size_t c_nAttemptCount; // count of SUCCESS insert/delete for each thread
22 static size_t c_nMaxLoadFactor; // maximum load factor
23 static bool c_bPrintGCState;
25 typedef CppUnitMini::TestCase Base;
26 typedef size_t key_type;
27 typedef size_t value_type;
30 class Inserter: public CppUnitMini::TestThread
34 virtual Inserter * clone()
36 return new Inserter( *this );
41 void operator()( bool bNew, std::pair<key_type const, value_type>& item )
44 item.second = item.first;
46 // for boost::container::flat_map
47 void operator()( bool bNew, std::pair<key_type, value_type>& item )
50 item.second = item.first;
53 // for BronsonAVLTreeMap
54 void operator()( bool bNew, key_type key, value_type& val )
62 size_t m_nInsertSuccess;
63 size_t m_nInsertFailed;
66 Inserter( CppUnitMini::ThreadPool& pool, MAP& rMap )
67 : CppUnitMini::TestThread( pool )
70 Inserter( Inserter& src )
71 : CppUnitMini::TestThread( src )
75 Map_InsDel_Item_int& getTest()
77 return reinterpret_cast<Map_InsDel_Item_int&>( m_Pool.m_Test );
80 virtual void init() { cds::threading::Manager::attachThread() ; }
81 virtual void fini() { cds::threading::Manager::detachThread() ; }
90 size_t nGoalItem = c_nGoalItem;
91 for ( size_t nAttempt = 0; nAttempt < c_nAttemptCount; ) {
92 if ( nAttempt % 2 == 0 ) {
93 if ( rMap.insert( nGoalItem, nGoalItem )) {
101 std::pair<bool, bool> ensureResult = rMap.ensure( nGoalItem, ensure_func() );
102 if ( ensureResult.second ) {
114 class Deleter: public CppUnitMini::TestThread
118 virtual Deleter * clone()
120 return new Deleter( *this );
123 size_t m_nDeleteSuccess;
124 size_t m_nDeleteFailed;
127 Deleter( CppUnitMini::ThreadPool& pool, MAP& rMap )
128 : CppUnitMini::TestThread( pool )
131 Deleter( Deleter& src )
132 : CppUnitMini::TestThread( src )
136 Map_InsDel_Item_int& getTest()
138 return reinterpret_cast<Map_InsDel_Item_int&>( m_Pool.m_Test );
141 virtual void init() { cds::threading::Manager::attachThread() ; }
142 virtual void fini() { cds::threading::Manager::detachThread() ; }
151 size_t nGoalItem = c_nGoalItem;
152 for ( size_t nAttempt = 0; nAttempt < c_nAttemptCount; ) {
153 if ( rMap.erase( nGoalItem )) {
166 void do_test( MAP& testMap )
168 typedef Inserter<MAP> InserterThread;
169 typedef Deleter<MAP> DeleterThread;
170 cds::OS::Timer timer;
173 CPPUNIT_MSG( " Fill map (" << c_nMapSize << " items)...");
176 std::vector<key_type> v;
177 v.reserve( c_nMapSize );
178 for ( size_t i = 0; i < c_nMapSize; ++i )
180 std::random_shuffle( v.begin(), v.end() );
181 for ( size_t i = 0; i < v.size(); ++i ) {
182 CPPUNIT_ASSERT( testMap.insert( v[i], v[i] ));
185 CPPUNIT_MSG( " Duration=" << timer.duration() );
187 CPPUNIT_MSG( " Insert/delete the key " << c_nGoalItem << " (" << c_nAttemptCount << " successful times)...");
188 CppUnitMini::ThreadPool pool( *this );
189 pool.add( new InserterThread( pool, testMap ), (c_nThreadCount + 1) / 2 );
190 pool.add( new DeleterThread( pool, testMap ), (c_nThreadCount + 1) / 2 );
192 CPPUNIT_MSG( " Duration=" << pool.avgDuration() );
194 size_t nInsertSuccess = 0;
195 size_t nInsertFailed = 0;
196 size_t nDeleteSuccess = 0;
197 size_t nDeleteFailed = 0;
198 for ( CppUnitMini::ThreadPool::iterator it = pool.begin(); it != pool.end(); ++it ) {
199 InserterThread * pThread = dynamic_cast<InserterThread *>( *it );
201 CPPUNIT_CHECK( pThread->m_nInsertSuccess == c_nAttemptCount );
202 nInsertSuccess += pThread->m_nInsertSuccess;
203 nInsertFailed += pThread->m_nInsertFailed;
206 DeleterThread * p = static_cast<DeleterThread *>( *it );
207 CPPUNIT_CHECK( p->m_nDeleteSuccess == c_nAttemptCount );
208 nDeleteSuccess += p->m_nDeleteSuccess;
209 nDeleteFailed += p->m_nDeleteFailed;
212 CPPUNIT_CHECK( nInsertSuccess == nDeleteSuccess );
213 size_t nGoalItem = c_nGoalItem;
214 CPPUNIT_CHECK( testMap.find( nGoalItem ));
217 CPPUNIT_MSG( " Totals: Ins fail=" << nInsertFailed << " Del fail=" << nDeleteFailed );
219 // Check if the map contains all items
220 CPPUNIT_MSG( " Check if the map contains all items" );
222 for ( size_t i = 0; i < c_nMapSize; ++i ) {
223 CPPUNIT_CHECK_EX( testMap.find( i ), "key " << i );
225 CPPUNIT_MSG( " Duration=" << timer.duration() );
227 check_before_cleanup( testMap );
230 additional_check( testMap );
231 print_stat( testMap );
232 additional_cleanup( testMap );
238 for ( size_t nLoadFactor = 1; nLoadFactor <= c_nMaxLoadFactor; nLoadFactor *= 2 ) {
239 CPPUNIT_MSG( "Load factor=" << nLoadFactor );
240 MAP testMap( c_nMapSize, nLoadFactor );
242 if ( c_bPrintGCState )
251 do_test<MAP>( testMap );
252 if ( c_bPrintGCState )
256 void setUpParams( const CppUnitMini::TestCfg& cfg );
258 void run_MichaelMap(const char *in_name, bool invert = false);
259 void run_SplitList(const char *in_name, bool invert = false);
260 void run_SkipListMap(const char *in_name, bool invert = false);
261 void run_StripedMap(const char *in_name, bool invert = false);
262 void run_RefinableMap(const char *in_name, bool invert = false);
263 void run_CuckooMap(const char *in_name, bool invert = false);
264 void run_EllenBinTreeMap(const char *in_name, bool invert = false);
265 void run_BronsonAVLTreeMap(const char *in_name, bool invert = false);
267 virtual void myRun(const char *in_name, bool invert = false);
269 # include "map2/map_defs.h"
270 CDSUNIT_DECLARE_MichaelMap
271 CDSUNIT_DECLARE_SplitList
272 CDSUNIT_DECLARE_SkipListMap
273 CDSUNIT_DECLARE_EllenBinTreeMap
274 CDSUNIT_DECLARE_BronsonAVLTreeMap
275 CDSUNIT_DECLARE_StripedMap
276 CDSUNIT_DECLARE_RefinableMap
277 CDSUNIT_DECLARE_CuckooMap
278 //CDSUNIT_DECLARE_StdMap // very slow!