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