Removing unused vars
[libcds.git] / tests / unit / map2 / map_insdel_item_string.cpp
1 //$$CDS-header$$
2
3 #include "map2/map_types.h"
4 #include "cppunit/thread.h"
5
6 #include <vector>
7
8 namespace map2 {
9
10 #   define TEST_MAP(X)         void X() { test<MapTypes<key_type, value_type>::X >()    ; }
11 #   define TEST_MAP_NOLF(X)    void X() { test_nolf<MapTypes<key_type, value_type>::X >()    ; }
12 #   define TEST_MAP_EXTRACT(X)  TEST_MAP(X)
13 #   define TEST_MAP_NOLF_EXTRACT(X) TEST_MAP_NOLF(X)
14
15     namespace {
16         static size_t  c_nMapSize = 1000000    ;  // map size
17         static size_t  c_nThreadCount = 4      ;  // thread count
18         static size_t  c_nGoalItem = c_nMapSize / 2;
19         static size_t  c_nAttemptCount = 100000       ;   // count of SUCCESS insert/delete for each thread
20         static size_t  c_nMaxLoadFactor = 8    ;  // maximum load factor
21         static bool    c_bPrintGCState = true;
22     }
23
24     class Map_InsDel_Item_string: public CppUnitMini::TestCase
25     {
26         typedef std::string  key_type;
27         typedef size_t  value_type;
28
29         const std::vector<std::string> *  m_parrString;
30
31         template <class Map>
32         class Inserter: public CppUnitMini::TestThread
33         {
34             Map&     m_Map;
35
36             virtual Inserter *    clone()
37             {
38                 return new Inserter( *this );
39             }
40         public:
41             size_t  m_nInsertSuccess;
42             size_t  m_nInsertFailed;
43
44         public:
45             Inserter( CppUnitMini::ThreadPool& pool, Map& rMap )
46                 : CppUnitMini::TestThread( pool )
47                 , m_Map( rMap )
48             {}
49             Inserter( Inserter& src )
50                 : CppUnitMini::TestThread( src )
51                 , m_Map( src.m_Map )
52             {}
53
54             Map_InsDel_Item_string&  getTest()
55             {
56                 return reinterpret_cast<Map_InsDel_Item_string&>( m_Pool.m_Test );
57             }
58
59             virtual void init() { cds::threading::Manager::attachThread()   ; }
60             virtual void fini() { cds::threading::Manager::detachThread()   ; }
61
62             virtual void test()
63             {
64                 Map& rMap = m_Map;
65
66                 m_nInsertSuccess =
67                     m_nInsertFailed = 0;
68
69                 size_t nGoalItem = c_nGoalItem;
70                 std::string strGoal = (*getTest().m_parrString)[nGoalItem];
71
72                 for ( size_t nAttempt = 0; nAttempt < c_nAttemptCount; ) {
73                     if ( rMap.insert( strGoal, nGoalItem )) {
74                         ++m_nInsertSuccess;
75                         ++nAttempt;
76                     }
77                     else
78                         ++m_nInsertFailed;
79                 }
80             }
81         };
82
83         template <class Map>
84         class Deleter: public CppUnitMini::TestThread
85         {
86             Map&     m_Map;
87
88             struct erase_cleaner {
89                 void operator ()(std::pair<typename Map::key_type const, typename Map::mapped_type>& val )
90                 {
91                     val.second = 0;
92                 }
93                 // for boost::container::flat_map
94                 void operator ()(std::pair< typename std::remove_const< typename Map::key_type >::type, typename Map::mapped_type>& val )
95                 {
96                     val.second = 0;
97                 }
98             };
99
100             virtual Deleter *    clone()
101             {
102                 return new Deleter( *this );
103             }
104         public:
105             size_t  m_nDeleteSuccess;
106             size_t  m_nDeleteFailed;
107
108         public:
109             Deleter( CppUnitMini::ThreadPool& pool, Map& rMap )
110                 : CppUnitMini::TestThread( pool )
111                 , m_Map( rMap )
112             {}
113             Deleter( Deleter& src )
114                 : CppUnitMini::TestThread( src )
115                 , m_Map( src.m_Map )
116             {}
117
118             Map_InsDel_Item_string&  getTest()
119             {
120                 return reinterpret_cast<Map_InsDel_Item_string&>( m_Pool.m_Test );
121             }
122
123             virtual void init() { cds::threading::Manager::attachThread()   ; }
124             virtual void fini() { cds::threading::Manager::detachThread()   ; }
125
126             virtual void test()
127             {
128                 Map& rMap = m_Map;
129
130                 m_nDeleteSuccess =
131                     m_nDeleteFailed = 0;
132
133                 size_t nGoalItem = c_nGoalItem;
134                 std::string strGoal = (*getTest().m_parrString)[nGoalItem];
135
136                 for ( size_t nAttempt = 0; nAttempt < c_nAttemptCount; ) {
137                     if ( rMap.erase( strGoal, erase_cleaner() )) {
138                         ++m_nDeleteSuccess;
139                         ++nAttempt;
140                     }
141                     else
142                         ++m_nDeleteFailed;
143                 }
144             }
145         };
146
147     protected:
148
149         template <class Map>
150         void do_test( Map& testMap )
151         {
152             typedef Inserter<Map>       InserterThread;
153             typedef Deleter<Map>        DeleterThread;
154             cds::OS::Timer    timer;
155
156             // Fill the map
157             CPPUNIT_MSG( "  Fill map (" << c_nMapSize << " items)...");
158             timer.reset();
159             for ( size_t i = 0; i < c_nMapSize; ++i ) {
160                 CPPUNIT_ASSERT_EX( testMap.insert( (*m_parrString)[i], i ), i );
161             }
162             CPPUNIT_MSG( "   Duration=" << timer.duration() );
163
164             CPPUNIT_MSG( "  Insert/delete the key " << c_nGoalItem << " (" << c_nAttemptCount << " successful times)...");
165             CppUnitMini::ThreadPool pool( *this );
166             pool.add( new InserterThread( pool, testMap ), (c_nThreadCount + 1) / 2 );
167             pool.add( new DeleterThread( pool, testMap ), (c_nThreadCount + 1) / 2 );
168             pool.run();
169             CPPUNIT_MSG( "   Duration=" << pool.avgDuration() );
170
171             size_t nInsertSuccess = 0;
172             size_t nInsertFailed = 0;
173             size_t nDeleteSuccess = 0;
174             size_t nDeleteFailed = 0;
175             for ( CppUnitMini::ThreadPool::iterator it = pool.begin(); it != pool.end(); ++it ) {
176                 InserterThread * pThread = dynamic_cast<InserterThread *>( *it );
177                 if ( pThread ) {
178                     CPPUNIT_CHECK( pThread->m_nInsertSuccess == c_nAttemptCount );
179                     nInsertSuccess += pThread->m_nInsertSuccess;
180                     nInsertFailed += pThread->m_nInsertFailed;
181                 }
182                 else {
183                     DeleterThread * p = static_cast<DeleterThread *>( *it );
184                     CPPUNIT_CHECK( p->m_nDeleteSuccess == c_nAttemptCount );
185                     nDeleteSuccess += p->m_nDeleteSuccess;
186                     nDeleteFailed += p->m_nDeleteFailed;
187                 }
188             }
189             CPPUNIT_CHECK_EX( nInsertSuccess == nDeleteSuccess, "nInsertSuccess=" << nInsertSuccess << ", nDeleteSuccess=" << nDeleteSuccess );
190             CPPUNIT_MSG( "    Totals: Ins fail=" << nInsertFailed << " Del fail=" << nDeleteFailed );
191
192             // Check if the map contains all items
193             CPPUNIT_MSG( "    Check if the map contains all items" );
194             timer.reset();
195             for ( size_t i = 0; i < c_nMapSize; ++i ) {
196                 CPPUNIT_CHECK_EX( testMap.find( (*m_parrString)[i] ), "Key \"" << (*m_parrString)[i] << "\" not found" );
197             }
198             CPPUNIT_MSG( "    Duration=" << timer.duration() );
199
200             testMap.clear();
201             additional_check( testMap );
202             print_stat( testMap );
203             additional_cleanup( testMap );
204         }
205
206         template <class Map>
207         void test()
208         {
209             m_parrString = &CppUnitMini::TestCase::getTestStrings();
210             if ( c_nMapSize > m_parrString->size() )
211                 c_nMapSize = m_parrString->size();
212             if ( c_nGoalItem > m_parrString->size() )
213                 c_nGoalItem = m_parrString->size() / 2;
214
215             CPPUNIT_MSG( "Thread count= " << c_nThreadCount
216                 << " pass count=" << c_nAttemptCount
217                 << " map size=" << c_nMapSize
218                 );
219
220             for ( size_t nLoadFactor = 1; nLoadFactor <= c_nMaxLoadFactor; nLoadFactor *= 2 ) {
221                 CPPUNIT_MSG( "Load factor=" << nLoadFactor );
222                 Map  testMap( c_nMapSize, nLoadFactor );
223                 do_test( testMap );
224                 if ( c_bPrintGCState )
225                     print_gc_state();
226             }
227         }
228
229         template <typename Map>
230         void test_nolf()
231         {
232             m_parrString = &CppUnitMini::TestCase::getTestStrings();
233             if ( c_nMapSize > m_parrString->size() )
234                 c_nMapSize = m_parrString->size();
235             if ( c_nGoalItem > m_parrString->size() )
236                 c_nGoalItem = m_parrString->size() / 2;
237
238             CPPUNIT_MSG( "Thread count= " << c_nThreadCount
239                 << " pass count=" << c_nAttemptCount
240                 << " map size=" << c_nMapSize
241                 );
242
243             Map testMap;
244             do_test( testMap );
245             if ( c_bPrintGCState )
246                 print_gc_state();
247         }
248
249         void setUpParams( const CppUnitMini::TestCfg& cfg ) {
250             c_nThreadCount = cfg.getULong("ThreadCount", 8 )        ; // thread count
251             c_nMapSize = cfg.getULong("MapSize", 1000000 );
252             c_nGoalItem = cfg.getULong("GoalItemIndex", (unsigned long) (c_nMapSize / 2) );
253             c_nAttemptCount = cfg.getULong("AttemptCount", 100000 );
254             c_nMaxLoadFactor = cfg.getULong("MaxLoadFactor", 8 );
255             c_bPrintGCState = cfg.getBool("PrintGCStateFlag", true );
256         }
257
258 #   include "map2/map_defs.h"
259         CDSUNIT_DECLARE_MichaelMap
260         CDSUNIT_DECLARE_SplitList
261         CDSUNIT_DECLARE_SkipListMap
262         CDSUNIT_DECLARE_EllenBinTreeMap
263         CDSUNIT_DECLARE_StripedMap
264         CDSUNIT_DECLARE_RefinableMap
265         CDSUNIT_DECLARE_CuckooMap
266         CDSUNIT_DECLARE_StdMap
267
268         CPPUNIT_TEST_SUITE( Map_InsDel_Item_string )
269             CDSUNIT_TEST_MichaelMap
270             CDSUNIT_TEST_SplitList
271             CDSUNIT_TEST_SkipListMap
272             CDSUNIT_TEST_EllenBinTreeMap
273             CDSUNIT_TEST_StripedMap
274             CDSUNIT_TEST_RefinableMap
275             CDSUNIT_TEST_CuckooMap
276             //CDSUNIT_TEST_StdMap       // very slow!!!
277         CPPUNIT_TEST_SUITE_END()
278
279     };
280
281     CPPUNIT_TEST_SUITE_REGISTRATION( Map_InsDel_Item_string );
282 } // namespace map2