3 #include "map2/map_type.h"
4 #include "cppunit/thread.h"
10 # define TEST_MAP(IMPL, C, X) void C::X() { test<map_type<IMPL, key_type, value_type>::X >(); }
11 # define TEST_MAP_NOLF(IMPL, C, X) void C::X() { test_nolf<map_type<IMPL, key_type, value_type>::X >(); }
12 # define TEST_MAP_EXTRACT(IMPL, C, X) TEST_MAP(IMPL, C, X)
13 # define TEST_MAP_NOLF_EXTRACT(IMPL, C, X) TEST_MAP_NOLF(IMPL, C, X)
15 class Map_InsDel_Item_int: public CppUnitMini::TestCase
17 static size_t c_nMapSize; // map size
18 static size_t c_nThreadCount; // thread count
19 static size_t c_nGoalItem;
20 static size_t c_nAttemptCount; // count of SUCCESS insert/delete for each thread
21 static size_t c_nMaxLoadFactor; // maximum load factor
22 static bool c_bPrintGCState;
24 typedef CppUnitMini::TestCase Base;
25 typedef size_t key_type;
26 typedef size_t value_type;
29 class Inserter: public CppUnitMini::TestThread
33 virtual Inserter * clone()
35 return new Inserter( *this );
40 void operator()( bool bNew, std::pair<key_type const, value_type>& item )
43 item.second = item.first;
45 // for boost::container::flat_map
46 void operator()( bool bNew, std::pair<key_type, value_type>& item )
49 item.second = item.first;
52 // for BronsonAVLTreeMap
53 void operator()( bool bNew, key_type key, value_type& val )
61 size_t m_nInsertSuccess;
62 size_t m_nInsertFailed;
65 Inserter( CppUnitMini::ThreadPool& pool, MAP& rMap )
66 : CppUnitMini::TestThread( pool )
69 Inserter( Inserter& src )
70 : CppUnitMini::TestThread( src )
74 Map_InsDel_Item_int& getTest()
76 return reinterpret_cast<Map_InsDel_Item_int&>( m_Pool.m_Test );
79 virtual void init() { cds::threading::Manager::attachThread() ; }
80 virtual void fini() { cds::threading::Manager::detachThread() ; }
89 size_t nGoalItem = c_nGoalItem;
90 for ( size_t nAttempt = 0; nAttempt < c_nAttemptCount; ) {
91 if ( nAttempt % 2 == 0 ) {
92 if ( rMap.insert( nGoalItem, nGoalItem )) {
100 std::pair<bool, bool> ensureResult = rMap.ensure( nGoalItem, ensure_func() );
101 if ( ensureResult.second ) {
113 class Deleter: public CppUnitMini::TestThread
117 virtual Deleter * clone()
119 return new Deleter( *this );
122 size_t m_nDeleteSuccess;
123 size_t m_nDeleteFailed;
126 Deleter( CppUnitMini::ThreadPool& pool, MAP& rMap )
127 : CppUnitMini::TestThread( pool )
130 Deleter( Deleter& src )
131 : CppUnitMini::TestThread( src )
135 Map_InsDel_Item_int& getTest()
137 return reinterpret_cast<Map_InsDel_Item_int&>( m_Pool.m_Test );
140 virtual void init() { cds::threading::Manager::attachThread() ; }
141 virtual void fini() { cds::threading::Manager::detachThread() ; }
150 size_t nGoalItem = c_nGoalItem;
151 for ( size_t nAttempt = 0; nAttempt < c_nAttemptCount; ) {
152 if ( rMap.erase( nGoalItem )) {
165 void do_test( MAP& testMap )
167 typedef Inserter<MAP> InserterThread;
168 typedef Deleter<MAP> DeleterThread;
169 cds::OS::Timer timer;
172 CPPUNIT_MSG( " Fill map (" << c_nMapSize << " items)...");
175 std::vector<key_type> v;
176 v.reserve( c_nMapSize );
177 for ( size_t i = 0; i < c_nMapSize; ++i )
179 shuffle( v.begin(), v.end() );
180 for ( size_t i = 0; i < v.size(); ++i ) {
181 CPPUNIT_ASSERT( testMap.insert( v[i], v[i] ));
184 CPPUNIT_MSG( " Duration=" << timer.duration() );
186 CPPUNIT_MSG( " Insert/delete the key " << c_nGoalItem << " (" << c_nAttemptCount << " successful times)...");
187 CppUnitMini::ThreadPool pool( *this );
188 pool.add( new InserterThread( pool, testMap ), (c_nThreadCount + 1) / 2 );
189 pool.add( new DeleterThread( pool, testMap ), (c_nThreadCount + 1) / 2 );
191 CPPUNIT_MSG( " Duration=" << pool.avgDuration() );
193 size_t nInsertSuccess = 0;
194 size_t nInsertFailed = 0;
195 size_t nDeleteSuccess = 0;
196 size_t nDeleteFailed = 0;
197 for ( CppUnitMini::ThreadPool::iterator it = pool.begin(); it != pool.end(); ++it ) {
198 InserterThread * pThread = dynamic_cast<InserterThread *>( *it );
200 CPPUNIT_CHECK( pThread->m_nInsertSuccess == c_nAttemptCount );
201 nInsertSuccess += pThread->m_nInsertSuccess;
202 nInsertFailed += pThread->m_nInsertFailed;
205 DeleterThread * p = static_cast<DeleterThread *>( *it );
206 CPPUNIT_CHECK( p->m_nDeleteSuccess == c_nAttemptCount );
207 nDeleteSuccess += p->m_nDeleteSuccess;
208 nDeleteFailed += p->m_nDeleteFailed;
211 CPPUNIT_CHECK( nInsertSuccess == nDeleteSuccess );
212 size_t nGoalItem = c_nGoalItem;
213 CPPUNIT_CHECK( testMap.find( nGoalItem ));
216 CPPUNIT_MSG( " Totals: Ins fail=" << nInsertFailed << " Del fail=" << nDeleteFailed );
218 // Check if the map contains all items
219 CPPUNIT_MSG( " Check if the map contains all items" );
221 for ( size_t i = 0; i < c_nMapSize; ++i ) {
222 CPPUNIT_CHECK_EX( testMap.find( i ), "key " << i );
224 CPPUNIT_MSG( " Duration=" << timer.duration() );
226 check_before_cleanup( testMap );
229 additional_check( testMap );
230 print_stat( testMap );
231 additional_cleanup( testMap );
237 for ( size_t nLoadFactor = 1; nLoadFactor <= c_nMaxLoadFactor; nLoadFactor *= 2 ) {
238 CPPUNIT_MSG( "Load factor=" << nLoadFactor );
239 MAP testMap( c_nMapSize, nLoadFactor );
241 if ( c_bPrintGCState )
250 do_test<MAP>( testMap );
251 if ( c_bPrintGCState )
255 void setUpParams( const CppUnitMini::TestCfg& cfg );
257 void run_MichaelMap(const char *in_name, bool invert = false);
258 void run_SplitList(const char *in_name, bool invert = false);
259 void run_SkipListMap(const char *in_name, bool invert = false);
260 void run_StripedMap(const char *in_name, bool invert = false);
261 void run_RefinableMap(const char *in_name, bool invert = false);
262 void run_CuckooMap(const char *in_name, bool invert = false);
263 void run_EllenBinTreeMap(const char *in_name, bool invert = false);
264 void run_BronsonAVLTreeMap(const char *in_name, bool invert = false);
266 virtual void myRun(const char *in_name, bool invert = false);
268 # include "map2/map_defs.h"
269 CDSUNIT_DECLARE_MichaelMap
270 CDSUNIT_DECLARE_SplitList
271 CDSUNIT_DECLARE_SkipListMap
272 CDSUNIT_DECLARE_EllenBinTreeMap
273 CDSUNIT_DECLARE_BronsonAVLTreeMap
274 CDSUNIT_DECLARE_StripedMap
275 CDSUNIT_DECLARE_RefinableMap
276 CDSUNIT_DECLARE_CuckooMap
277 //CDSUNIT_DECLARE_StdMap // very slow!