2 This file is a part of libcds - Concurrent Data Structures library
4 (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2016
6 Source code repo: http://github.com/khizmax/libcds/
7 Download: http://sourceforge.net/projects/libcds/files/
9 Redistribution and use in source and binary forms, with or without
10 modification, are permitted provided that the following conditions are met:
12 * Redistributions of source code must retain the above copyright notice, this
13 list of conditions and the following disclaimer.
15 * Redistributions in binary form must reproduce the above copyright notice,
16 this list of conditions and the following disclaimer in the documentation
17 and/or other materials provided with the distribution.
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 class Map_find_string: public cds_test::stress_fixture
38 static size_t s_nThreadCount; // thread count
39 static size_t s_nMapSize; // map size (count of searching item)
40 static size_t s_nPercentExists; // percent of existing keys in searching sequence
41 static size_t s_nPassCount;
42 static size_t s_nMaxLoadFactor; // maximum load factor
44 static size_t s_nCuckooInitialSize; // initial size for CuckooMap
45 static size_t s_nCuckooProbesetSize; // CuckooMap probeset size (only for list-based probeset)
46 static size_t s_nCuckooProbesetThreshold; // CUckooMap probeset threshold (o - use default)
48 static size_t s_nFeldmanMap_HeadBits;
49 static size_t s_nFeldmanMap_ArrayBits;
51 static size_t s_nLoadFactor; // current load factor
53 typedef std::string key_type;
55 std::string const* pKey;
56 bool bExists ; // true - key in map, false - key not in map
59 typedef std::vector<value_type> value_vector;
60 static std::vector<std::string> s_arrString;
61 static value_vector s_Data;
63 static void SetUpTestCase();
64 static void TearDownTestCase();
66 static void setup_test_case();
67 static std::vector<size_t> get_load_factors();
70 template <typename Iterator, typename Map>
71 static bool check_result( Iterator const& it, Map const& map )
73 return it != map.end();
75 template <typename Map>
76 static bool check_result( bool b, Map const& )
82 class Worker: public cds_test::thread
84 typedef cds_test::thread base_class;
97 Worker( cds_test::thread_pool& pool, Map& map )
102 Worker( Worker& src )
107 virtual thread * clone()
109 return new Worker( *this );
113 size_t const nPassCount = s_nPassCount;
116 for ( size_t nPass = 0; nPass < nPassCount; ++nPass ) {
118 auto itEnd = s_Data.cend();
119 for ( auto it = s_Data.cbegin(); it != itEnd; ++it ) {
120 auto bFound = rMap.contains( *(it->pKey) );
122 if ( check_result(bFound, rMap))
123 ++m_KeyExists.nSuccess;
125 ++m_KeyExists.nFailed;
128 if ( check_result(bFound, rMap))
129 ++m_KeyNotExists.nFailed;
131 ++m_KeyNotExists.nSuccess;
136 auto itEnd = s_Data.crend();
137 for ( auto it = s_Data.crbegin(); it != itEnd; ++it ) {
138 auto bFound = rMap.contains( *(it->pKey) );
140 if ( check_result(bFound, rMap))
141 ++m_KeyExists.nSuccess;
143 ++m_KeyExists.nFailed;
146 if ( check_result( bFound, rMap ))
147 ++m_KeyNotExists.nFailed;
149 ++m_KeyNotExists.nSuccess;
158 template <typename Hash>
159 static void fill_string_array();
162 void test( Map& testMap )
164 typedef Worker<Map> worker;
167 for ( size_t i = 0; i < s_Data.size(); ++i ) {
168 // All keys in arrData are unique, insert() must be successful
169 if ( s_Data[i].bExists )
170 EXPECT_TRUE( check_result( testMap.insert( *(s_Data[i].pKey), s_Data[i] ), testMap ));
173 propout() << std::make_pair( "thread_count", s_nThreadCount )
174 << std::make_pair( "map_size", s_nMapSize )
175 << std::make_pair( "percent_exist", s_nPercentExists )
176 << std::make_pair( "pass_count", s_nPassCount );
178 cds_test::thread_pool& pool = get_pool();
179 pool.add( new worker( pool, testMap ), s_nThreadCount );
181 std::chrono::milliseconds duration = pool.run();
183 propout() << std::make_pair( "duration", duration );
185 size_t nExistSuccess = 0;
186 size_t nExistFailed = 0;
187 size_t nMissingSuccess = 0;
188 size_t nMissingFailed = 0;
190 // Postcondition: the number of success searching == the number of map item
191 for ( size_t i = 0; i < pool.size(); ++i ) {
192 worker& w = static_cast<worker&>(pool.get( i ));
193 nExistSuccess += w.m_KeyExists.nSuccess;
194 nExistFailed += w.m_KeyExists.nFailed;
195 nMissingSuccess += w.m_KeyNotExists.nSuccess;
196 nMissingFailed += w.m_KeyNotExists.nFailed;
198 EXPECT_EQ( w.m_KeyExists.nSuccess, s_nMapSize * s_nPassCount ) << "thread " << i;
199 EXPECT_EQ( w.m_KeyExists.nFailed, 0 ) << "thread " << i;
200 EXPECT_EQ( w.m_KeyNotExists.nSuccess, (s_Data.size() - s_nMapSize) * s_nPassCount ) << "thread " << i;
201 EXPECT_EQ( w.m_KeyNotExists.nFailed, 0 ) << "thread " << i;
204 check_before_cleanup( testMap );
207 additional_check( testMap );
208 print_stat( propout(), testMap );
209 additional_cleanup( testMap );
215 ASSERT_GT( s_Data.size(), 0 );
217 Map testMap( *this );
222 class Map_find_string_stdhash: public Map_find_string
225 static void SetUpTestCase();
230 Map_find_string::run_test<Map>();
234 #if CDS_BUILD_BITS == 64
235 class Map_find_string_city32: public Map_find_string
238 static void SetUpTestCase();
243 Map_find_string::run_test<Map>();
247 class Map_find_string_city64: public Map_find_string
250 static void SetUpTestCase();
255 Map_find_string::run_test<Map>();
259 class Map_find_string_city128: public Map_find_string
262 static void SetUpTestCase();
267 Map_find_string::run_test<Map>();
271 #endif // #if CDS_BUILD_BITS == 64
273 class Map_find_string_LF: public Map_find_string
274 , public ::testing::WithParamInterface<size_t>
280 s_nLoadFactor = GetParam();
281 propout() << std::make_pair( "load_factor", s_nLoadFactor );
282 Map_find_string::run_test<Map>();