3 #include "map2/map_type.h"
4 #include "cppunit/thread.h"
10 #define TEST_CASE(TAG, X) void X();
12 class Map_find_string: public CppUnitMini::TestCase
15 size_t c_nThreadCount = 8; // thread count
16 size_t c_nMapSize = 10000000; // map size (count of searching item)
17 size_t c_nPercentExists = 50; // percent of existing keys in searching sequence
18 size_t c_nPassCount = 2;
19 size_t c_nMaxLoadFactor = 8; // maximum load factor
20 bool c_bPrintGCState = true;
22 size_t c_nCuckooInitialSize = 1024;// initial size for CuckooMap
23 size_t c_nCuckooProbesetSize = 16; // CuckooMap probeset size (only for list-based probeset)
24 size_t c_nCuckooProbesetThreshold = 0; // CUckooMap probeset threshold (o - use default)
26 size_t c_nMultiLevelMap_HeadBits = 10;
27 size_t c_nMultiLevelMap_ArrayBits = 4;
29 size_t c_nLoadFactor; // current load factor
32 typedef CppUnitMini::TestCase Base;
33 typedef std::string key_type;
35 std::string const * pKey;
36 bool bExists ; // true - key in map, false - key not in map
39 typedef std::vector<value_type> ValueVector;
42 template <typename Iterator, typename Map>
43 static bool check_result( Iterator const& it, Map const& map )
45 return it != map.end();
47 template <typename Map>
48 static bool check_result( bool b, Map const& )
54 class TestThread: public CppUnitMini::TestThread
58 virtual TestThread * clone()
60 return new TestThread( *this );
77 TestThread( CppUnitMini::ThreadPool& pool, Map& rMap )
78 : CppUnitMini::TestThread( pool )
81 TestThread( TestThread& src )
82 : CppUnitMini::TestThread( src )
86 Map_find_string& getTest()
88 return reinterpret_cast<Map_find_string&>( m_Pool.m_Test );
91 virtual void init() { cds::threading::Manager::attachThread() ; }
92 virtual void fini() { cds::threading::Manager::detachThread() ; }
96 ValueVector& arr = getTest().m_Arr;
97 size_t const nPassCount = getTest().c_nPassCount;
100 for ( size_t nPass = 0; nPass < nPassCount; ++nPass ) {
101 if ( m_nThreadNo & 1 ) {
102 ValueVector::const_iterator itEnd = arr.end();
103 for ( ValueVector::const_iterator it = arr.begin(); it != itEnd; ++it ) {
104 auto bFound = rMap.contains( *(it->pKey) );
106 if ( check_result(bFound, rMap))
107 ++m_KeyExists.nSuccess;
109 ++m_KeyExists.nFailed;
112 if ( check_result(bFound, rMap))
113 ++m_KeyNotExists.nFailed;
115 ++m_KeyNotExists.nSuccess;
120 ValueVector::const_reverse_iterator itEnd = arr.rend();
121 for ( ValueVector::const_reverse_iterator it = arr.rbegin(); it != itEnd; ++it ) {
122 auto bFound = rMap.contains( *(it->pKey) );
124 if ( check_result(bFound, rMap))
125 ++m_KeyExists.nSuccess;
127 ++m_KeyExists.nFailed;
130 if ( check_result( bFound, rMap ))
131 ++m_KeyNotExists.nFailed;
133 ++m_KeyNotExists.nSuccess;
148 void generateSequence();
151 void find_string_test( Map& testMap )
153 typedef TestThread<Map> Thread;
154 cds::OS::Timer timer;
157 CPPUNIT_MSG( " Fill map...");
159 for ( size_t i = 0; i < m_Arr.size(); ++i ) {
160 // All keys in arrData are unique, insert() must be successful
161 if ( m_Arr[i].bExists )
162 CPPUNIT_ASSERT( check_result( testMap.insert( *(m_Arr[i].pKey), m_Arr[i] ), testMap ));
164 CPPUNIT_MSG( " Duration=" << timer.duration() );
166 CPPUNIT_MSG( " Searching...");
167 CppUnitMini::ThreadPool pool( *this );
168 pool.add( new Thread( pool, testMap ), c_nThreadCount );
170 CPPUNIT_MSG( " Duration=" << pool.avgDuration() );
172 // Postcondition: the number of success searching == the number of map item
173 for ( CppUnitMini::ThreadPool::iterator it = pool.begin(); it != pool.end(); ++it ) {
174 Thread * pThread = static_cast<Thread *>( *it );
175 CPPUNIT_CHECK( pThread->m_KeyExists.nSuccess == c_nMapSize * c_nPassCount );
176 CPPUNIT_CHECK( pThread->m_KeyExists.nFailed == 0 );
177 CPPUNIT_CHECK( pThread->m_KeyNotExists.nSuccess == (m_Arr.size() - c_nMapSize) * c_nPassCount );
178 CPPUNIT_CHECK( pThread->m_KeyNotExists.nFailed == 0 );
181 check_before_cleanup( testMap );
184 additional_check( testMap );
185 print_stat( testMap );
186 additional_cleanup( testMap );
192 if ( Map::c_bLoadFactorDepended ) {
193 for ( size_t nLoadFactor = 1; nLoadFactor <= c_nMaxLoadFactor; nLoadFactor *= 2 ) {
194 CPPUNIT_MSG( "Load factor=" << nLoadFactor );
195 Map testMap( *this );
196 find_string_test( testMap );
197 if ( c_bPrintGCState )
202 Map testMap( *this );
203 find_string_test( testMap );
204 if ( c_bPrintGCState )
215 find_string_test( testMap );
216 if ( c_bPrintGCState )
220 void setUpParams( const CppUnitMini::TestCfg& cfg );
222 # include "map2/map_defs.h"
223 CDSUNIT_DECLARE_MichaelMap
224 CDSUNIT_DECLARE_MichaelMap_nogc
225 CDSUNIT_DECLARE_SplitList
226 CDSUNIT_DECLARE_SplitList_nogc
227 CDSUNIT_DECLARE_SkipListMap
228 CDSUNIT_DECLARE_SkipListMap_nogc
229 CDSUNIT_DECLARE_EllenBinTreeMap
230 CDSUNIT_DECLARE_BronsonAVLTreeMap
231 CDSUNIT_DECLARE_MultiLevelHashMap
232 CDSUNIT_DECLARE_StripedMap
233 CDSUNIT_DECLARE_RefinableMap
234 CDSUNIT_DECLARE_CuckooMap
235 CDSUNIT_DECLARE_StdMap
236 CDSUNIT_DECLARE_StdMap_NoLock
238 CPPUNIT_TEST_SUITE(Map_find_string)
239 CDSUNIT_TEST_MichaelMap
240 CDSUNIT_TEST_MichaelMap_nogc
241 CDSUNIT_TEST_SplitList
242 CDSUNIT_TEST_SplitList_nogc
243 CDSUNIT_TEST_SkipListMap
244 CDSUNIT_TEST_SkipListMap_nogc
245 CDSUNIT_TEST_EllenBinTreeMap
246 CDSUNIT_TEST_BronsonAVLTreeMap
247 CDSUNIT_TEST_MultiLevelHashMap
248 CDSUNIT_TEST_CuckooMap
249 CDSUNIT_TEST_StripedMap
250 CDSUNIT_TEST_RefinableMap
252 CDSUNIT_TEST_StdMap_NoLock
253 CPPUNIT_TEST_SUITE_END();