Removing unused vars
[libcds.git] / tests / unit / map2 / map_insdel_int.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_EXTRACT(X)  TEST_MAP(X)
12 #   define TEST_MAP_NOLF(X)    void X() { test_nolf<MapTypes<key_type, value_type>::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_nInsertThreadCount = 4;  // count of insertion thread
18         static size_t  c_nDeleteThreadCount = 4;  // count of deletion thread
19         static size_t  c_nThreadPassCount = 4  ;  // pass count 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_int: public CppUnitMini::TestCase
25     {
26         typedef size_t  key_type;
27         typedef size_t  value_type;
28
29         typedef std::vector<key_type>   key_array;
30         key_array                       m_arrValues;
31
32         template <class MAP>
33         class Inserter: public CppUnitMini::TestThread
34         {
35             MAP&     m_Map;
36
37             virtual Inserter *    clone()
38             {
39                 return new Inserter( *this );
40             }
41         public:
42             size_t  m_nInsertSuccess;
43             size_t  m_nInsertFailed;
44
45         public:
46             Inserter( CppUnitMini::ThreadPool& pool, MAP& rMap )
47                 : CppUnitMini::TestThread( pool )
48                 , m_Map( rMap )
49             {}
50             Inserter( Inserter& src )
51                 : CppUnitMini::TestThread( src )
52                 , m_Map( src.m_Map )
53             {}
54
55             Map_InsDel_int&  getTest()
56             {
57                 return reinterpret_cast<Map_InsDel_int&>( m_Pool.m_Test );
58             }
59
60             virtual void init() { cds::threading::Manager::attachThread()   ; }
61             virtual void fini() { cds::threading::Manager::detachThread()   ; }
62
63             virtual void test()
64             {
65                 MAP& rMap = m_Map;
66
67                 m_nInsertSuccess =
68                     m_nInsertFailed = 0;
69                 key_array const& arr = getTest().m_arrValues;
70
71                 if ( m_nThreadNo & 1 ) {
72                     for ( size_t nPass = 0; nPass < c_nThreadPassCount; ++nPass ) {
73                         for ( key_array::const_iterator it = arr.begin(), itEnd = arr.end(); it != itEnd; ++it ) {
74                             if ( rMap.insert( *it, *it * 8 ) )
75                                 ++m_nInsertSuccess;
76                             else
77                                 ++m_nInsertFailed;
78                         }
79                     }
80                 }
81                 else {
82                     for ( size_t nPass = 0; nPass < c_nThreadPassCount; ++nPass ) {
83                         for ( key_array::const_reverse_iterator it = arr.rbegin(), itEnd = arr.rend(); it != itEnd; ++it ) {
84                             if ( rMap.insert( *it, *it * 8 ) )
85                                 ++m_nInsertSuccess;
86                             else
87                                 ++m_nInsertFailed;
88                         }
89                     }
90                 }
91             }
92         };
93
94         template <class MAP>
95         class Deleter: public CppUnitMini::TestThread
96         {
97             MAP&     m_Map;
98
99             virtual Deleter *    clone()
100             {
101                 return new Deleter( *this );
102             }
103         public:
104             size_t  m_nDeleteSuccess;
105             size_t  m_nDeleteFailed;
106
107         public:
108             Deleter( CppUnitMini::ThreadPool& pool, MAP& rMap )
109                 : CppUnitMini::TestThread( pool )
110                 , m_Map( rMap )
111             {}
112             Deleter( Deleter& src )
113                 : CppUnitMini::TestThread( src )
114                 , m_Map( src.m_Map )
115             {}
116
117             Map_InsDel_int&  getTest()
118             {
119                 return reinterpret_cast<Map_InsDel_int&>( m_Pool.m_Test );
120             }
121
122             virtual void init() { cds::threading::Manager::attachThread()   ; }
123             virtual void fini() { cds::threading::Manager::detachThread()   ; }
124
125             virtual void test()
126             {
127                 MAP& rMap = m_Map;
128
129                 m_nDeleteSuccess =
130                     m_nDeleteFailed = 0;
131                 key_array const& arr = getTest().m_arrValues;
132
133                 if ( m_nThreadNo & 1 ) {
134                     for ( size_t nPass = 0; nPass < c_nThreadPassCount; ++nPass ) {
135                         for ( key_array::const_iterator it = arr.begin(), itEnd = arr.end(); it != itEnd; ++it ) {
136                             if ( rMap.erase( *it ) )
137                                 ++m_nDeleteSuccess;
138                             else
139                                 ++m_nDeleteFailed;
140                         }
141                     }
142                 }
143                 else {
144                     for ( size_t nPass = 0; nPass < c_nThreadPassCount; ++nPass ) {
145                         for ( key_array::const_reverse_iterator it = arr.rbegin(), itEnd = arr.rend(); it != itEnd; ++it ) {
146                             if ( rMap.erase( *it ) )
147                                 ++m_nDeleteSuccess;
148                             else
149                                 ++m_nDeleteFailed;
150                         }
151                     }
152                 }
153             }
154         };
155
156     protected:
157         template <class MAP>
158         void do_test( MAP& testMap )
159         {
160             typedef Inserter<MAP>       InserterThread;
161             typedef Deleter<MAP>        DeleterThread;
162             cds::OS::Timer    timer;
163
164             m_arrValues.clear();
165             m_arrValues.reserve( c_nMapSize );
166             for ( size_t i = 0; i < c_nMapSize; ++i )
167                 m_arrValues.push_back( i );
168             std::random_shuffle( m_arrValues.begin(), m_arrValues.end() );
169
170             CppUnitMini::ThreadPool pool( *this );
171             pool.add( new InserterThread( pool, testMap ), c_nInsertThreadCount );
172             pool.add( new DeleterThread( pool, testMap ), c_nDeleteThreadCount );
173             pool.run();
174             CPPUNIT_MSG( "   Duration=" << pool.avgDuration() );
175
176             size_t nInsertSuccess = 0;
177             size_t nInsertFailed = 0;
178             size_t nDeleteSuccess = 0;
179             size_t nDeleteFailed = 0;
180             for ( CppUnitMini::ThreadPool::iterator it = pool.begin(); it != pool.end(); ++it ) {
181                 InserterThread * pThread = dynamic_cast<InserterThread *>( *it );
182                 if ( pThread ) {
183                     nInsertSuccess += pThread->m_nInsertSuccess;
184                     nInsertFailed += pThread->m_nInsertFailed;
185                 }
186                 else {
187                     DeleterThread * p = static_cast<DeleterThread *>( *it );
188                     nDeleteSuccess += p->m_nDeleteSuccess;
189                     nDeleteFailed += p->m_nDeleteFailed;
190                 }
191             }
192
193             CPPUNIT_MSG( "    Totals: Ins succ=" << nInsertSuccess
194                 << " Del succ=" << nDeleteSuccess << "\n"
195                 << "          : Ins fail=" << nInsertFailed
196                 << " Del fail=" << nDeleteFailed
197                 << " Map size=" << testMap.size()
198                 );
199
200
201             CPPUNIT_MSG( "  Clear map (single-threaded)..." );
202             timer.reset();
203             for ( size_t nItem = 0; nItem < c_nMapSize; ++nItem ) {
204                 testMap.erase( nItem );
205             }
206             CPPUNIT_MSG( "   Duration=" << timer.duration() );
207             CPPUNIT_ASSERT_EX( testMap.empty(), ((long long) testMap.size()) );
208
209             additional_check( testMap );
210             print_stat( testMap );
211             additional_cleanup( testMap );
212         }
213
214         template <class MAP>
215         void test()
216         {
217             CPPUNIT_MSG( "Thread count: insert=" << c_nInsertThreadCount
218                 << " delete=" << c_nDeleteThreadCount
219                 << " pass count=" << c_nThreadPassCount
220                 << " map size=" << c_nMapSize
221                 );
222
223             for ( size_t nLoadFactor = 1; nLoadFactor <= c_nMaxLoadFactor; nLoadFactor *= 2 ) {
224                 CPPUNIT_MSG( "Load factor=" << nLoadFactor );
225                 MAP  testMap( c_nMapSize, nLoadFactor );
226                 do_test( testMap );
227                 if ( c_bPrintGCState )
228                     print_gc_state();
229             }
230         }
231
232         template <class MAP>
233         void test_nolf()
234         {
235             CPPUNIT_MSG( "Thread count: insert=" << c_nInsertThreadCount
236                 << " delete=" << c_nDeleteThreadCount
237                 << " pass count=" << c_nThreadPassCount
238                 << " map size=" << c_nMapSize
239                 );
240
241             MAP testMap;
242             do_test( testMap );
243             if ( c_bPrintGCState )
244                 print_gc_state();
245         }
246
247         void setUpParams( const CppUnitMini::TestCfg& cfg ) {
248             c_nInsertThreadCount = cfg.getULong("InsertThreadCount", 4 );
249             c_nDeleteThreadCount = cfg.getULong("DeleteThreadCount", 4 );
250             c_nThreadPassCount = cfg.getULong("ThreadPassCount", 4 );
251             c_nMapSize = cfg.getULong("MapSize", 1000000 );
252             c_nMaxLoadFactor = cfg.getULong("MaxLoadFactor", 8 );
253             c_bPrintGCState = cfg.getBool("PrintGCStateFlag", true );
254         }
255
256 #   include "map2/map_defs.h"
257         CDSUNIT_DECLARE_MichaelMap
258         CDSUNIT_DECLARE_SplitList
259         CDSUNIT_DECLARE_SkipListMap
260         CDSUNIT_DECLARE_EllenBinTreeMap
261         CDSUNIT_DECLARE_StripedMap
262         CDSUNIT_DECLARE_RefinableMap
263         CDSUNIT_DECLARE_CuckooMap
264         CDSUNIT_DECLARE_StdMap
265
266         CPPUNIT_TEST_SUITE( Map_InsDel_int )
267             CDSUNIT_TEST_MichaelMap
268             CDSUNIT_TEST_SplitList
269             CDSUNIT_TEST_SkipListMap
270             CDSUNIT_TEST_EllenBinTreeMap
271             CDSUNIT_TEST_StripedMap
272             CDSUNIT_TEST_RefinableMap
273             CDSUNIT_TEST_CuckooMap
274             CDSUNIT_TEST_StdMap
275         CPPUNIT_TEST_SUITE_END()
276
277     };
278
279     CPPUNIT_TEST_SUITE_REGISTRATION( Map_InsDel_int );
280 } // namespace map2