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.
33 // find int test in map<int> in mutithreaded mode
36 class Map_find_int: public cds_test::stress_fixture
39 static size_t s_nThreadCount; // thread count
40 static size_t s_nMapSize; // map size (count of searching item)
41 static size_t s_nPercentExists; // percent of existing keys in searching sequence
42 static size_t s_nPassCount;
43 static size_t s_nMaxLoadFactor; // maximum load factor
45 static size_t s_nCuckooInitialSize; // initial size for CuckooMap
46 static size_t s_nCuckooProbesetSize; // CuckooMap probeset size (only for list-based probeset)
47 static size_t s_nCuckooProbesetThreshold; // CUckooMap probeset threshold (o - use default)
49 static size_t s_nFeldmanMap_HeadBits;
50 static size_t s_nFeldmanMap_ArrayBits;
52 static size_t s_nLoadFactor; // current load factor
54 typedef size_t key_type;
57 bool bExists; // true - key in map, false - key not in map
60 typedef std::vector<value_type> value_vector;
61 static value_vector s_Data;
63 static void SetUpTestCase();
64 static void TearDownTestCase();
67 static size_t s_nRealMapSize;
69 static void generateSequence();
71 template <typename Iterator, typename Map>
72 static bool check_result( Iterator const& it, Map const& map )
74 return it != map.end();
76 template <typename Map>
77 static bool check_result( bool b, Map const& )
83 class Worker: public cds_test::thread
85 typedef cds_test::thread base_class;
98 Worker( cds_test::thread_pool& pool, Map& map )
105 Worker( Worker& src )
112 virtual thread * clone()
114 return new Worker( *this );
119 size_t const nPassCount = s_nPassCount;
122 for ( size_t nPass = 0; nPass < nPassCount; ++nPass ) {
124 auto itEnd = s_Data.cend();
125 for ( auto it = s_Data.cbegin(); it != itEnd; ++it ) {
126 auto bFound = rMap.contains( it->nKey );
128 if ( check_result( bFound, rMap ))
129 ++m_KeyExists.nSuccess;
131 ++m_KeyExists.nFailed;
134 if ( check_result( bFound, rMap ))
135 ++m_KeyNotExists.nFailed;
137 ++m_KeyNotExists.nSuccess;
142 auto itEnd = s_Data.crend();
143 for ( auto it = s_Data.crbegin(); it != itEnd; ++it ) {
144 auto bFound = rMap.contains( it->nKey );
146 if ( check_result( bFound, rMap ))
147 ++m_KeyExists.nSuccess;
149 ++m_KeyExists.nFailed;
152 if ( check_result( bFound, rMap ))
153 ++m_KeyNotExists.nFailed;
155 ++m_KeyNotExists.nSuccess;
166 void test( Map& testMap )
168 typedef Worker<Map> worker;
171 for ( auto const& it: s_Data ) {
173 EXPECT_TRUE( check_result( testMap.insert( it.nKey, it ), testMap ));
177 propout() << std::make_pair( "thread_count", s_nThreadCount )
178 << std::make_pair( "map_size", s_nMapSize )
179 << std::make_pair( "percent_exist", s_nPercentExists )
180 << std::make_pair( "pass_count", s_nPassCount );
182 cds_test::thread_pool& pool = get_pool();
183 pool.add( new worker( pool, testMap ), s_nThreadCount );
185 std::chrono::milliseconds duration = pool.run();
187 propout() << std::make_pair( "duration", duration );
189 size_t nExistSuccess = 0;
190 size_t nExistFailed = 0;
191 size_t nMissingSuccess = 0;
192 size_t nMissingFailed = 0;
194 for ( size_t i = 0; i < pool.size(); ++i ) {
195 worker& w = static_cast<worker&>( pool.get( i ));
196 nExistSuccess += w.m_KeyExists.nSuccess;
197 nExistFailed += w.m_KeyExists.nFailed;
198 nMissingSuccess += w.m_KeyNotExists.nSuccess;
199 nMissingFailed += w.m_KeyNotExists.nFailed;
201 EXPECT_EQ( w.m_KeyExists.nFailed, 0u ) << "thread " << i;
202 EXPECT_EQ( w.m_KeyExists.nSuccess, s_nRealMapSize * s_nPassCount ) << "thread " << i;
203 EXPECT_EQ( w.m_KeyNotExists.nFailed, 0u ) << "thread " << i;
204 EXPECT_EQ( w.m_KeyNotExists.nSuccess, (s_Data.size() - s_nRealMapSize) * s_nPassCount ) << "thread " << i;
208 << std::make_pair( "exist_found", nExistSuccess )
209 << std::make_pair( "exist_not_found", nExistFailed ) // must = 0
210 << std::make_pair( "missing_not_found", nMissingSuccess )
211 << std::make_pair( "missing_found", nMissingFailed ); // must = 0
213 check_before_cleanup( testMap );
216 additional_check( testMap );
217 print_stat( propout(), testMap );
218 additional_cleanup( testMap );
224 Map testMap( *this );
229 class Map_find_int_LF: public Map_find_int
230 , public ::testing::WithParamInterface<size_t>
236 s_nLoadFactor = GetParam();
237 propout() << std::make_pair( "load_factor", s_nLoadFactor );
238 Map_find_int::run_test<Map>();
241 static std::vector<size_t> get_load_factors();