5479e3b854b541b4923a21251aefbf7cb3fc3cce
[libcds.git] / test / stress / sequential / sequential-map / find_string / map_find_string.h
1 /*
2     This file is a part of libcds - Concurrent Data Structures library
3
4     (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2017
5
6     Source code repo: http://github.com/khizmax/libcds/
7     Download: http://sourceforge.net/projects/libcds/files/
8
9     Redistribution and use in source and binary forms, with or without
10     modification, are permitted provided that the following conditions are met:
11
12     * Redistributions of source code must retain the above copyright notice, this
13       list of conditions and the following disclaimer.
14
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.
18
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.
29 */
30
31 #include "map_type.h"
32
33 namespace map {
34
35     class Map_find_string: public cds_test::stress_fixture
36     {
37     public:
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
43
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)
47
48         static size_t s_nFeldmanMap_HeadBits;
49         static size_t s_nFeldmanMap_ArrayBits;
50
51         static size_t s_nLoadFactor;  // current load factor
52
53         typedef std::string  key_type;
54         struct value_type {
55             std::string const* pKey;
56             bool        bExists ;   // true - key in map, false - key not in map
57         };
58
59         typedef std::vector<value_type> value_vector;
60         static std::vector<std::string> s_arrString;
61         static value_vector s_Data;
62
63         static void SetUpTestCase();
64         static void TearDownTestCase();
65
66         static void setup_test_case();
67         static std::vector<size_t> get_load_factors();
68
69     private:
70         template <typename Iterator, typename Map>
71         static bool check_result( Iterator const& it, Map const& map )
72         {
73             return it != map.end();
74         }
75         template <typename Map>
76         static bool check_result( bool b, Map const& )
77         {
78             return b;
79         }
80
81         template <class Map>
82         class Worker: public cds_test::thread
83         {
84             typedef cds_test::thread base_class;
85             Map&     m_Map;
86
87         public:
88             struct Stat {
89                 size_t      nSuccess = 0;
90                 size_t      nFailed = 0;
91             };
92
93             Stat    m_KeyExists;
94             Stat    m_KeyNotExists;
95
96         public:
97             Worker( cds_test::thread_pool& pool, Map& map )
98                 : base_class( pool )
99                 , m_Map( map )
100                 , m_KeyExists()
101                 , m_KeyNotExists()
102             {}
103
104             Worker( Worker& src )
105                 : base_class( src )
106                 , m_Map( src.m_Map )
107                 , m_KeyExists()
108                 , m_KeyNotExists()
109             {}
110
111             virtual thread * clone()
112             {
113                 return new Worker( *this );
114             }
115             virtual void test()
116             {
117                 size_t const nPassCount = s_nPassCount;
118
119                 Map& rMap = m_Map;
120                 for ( size_t nPass = 0; nPass < nPassCount; ++nPass ) {
121                     if ( id() & 1 ) {
122                         auto itEnd = s_Data.cend();
123                         for ( auto it = s_Data.cbegin(); it != itEnd; ++it ) {
124                             auto bFound = rMap.contains( *(it->pKey));
125                             if ( it->bExists ) {
126                                 if ( check_result(bFound, rMap))
127                                     ++m_KeyExists.nSuccess;
128                                 else
129                                     ++m_KeyExists.nFailed;
130                             }
131                             else {
132                                 if ( check_result(bFound, rMap))
133                                     ++m_KeyNotExists.nFailed;
134                                 else
135                                     ++m_KeyNotExists.nSuccess;
136                             }
137                         }
138                     }
139                     else {
140                         auto itEnd = s_Data.crend();
141                         for ( auto it = s_Data.crbegin(); it != itEnd; ++it ) {
142                             auto bFound = rMap.contains( *(it->pKey));
143                             if ( it->bExists ) {
144                                 if ( check_result(bFound, rMap))
145                                     ++m_KeyExists.nSuccess;
146                                 else
147                                     ++m_KeyExists.nFailed;
148                             }
149                             else {
150                                 if ( check_result( bFound, rMap ))
151                                     ++m_KeyNotExists.nFailed;
152                                 else
153                                     ++m_KeyNotExists.nSuccess;
154                             }
155                         }
156                     }
157                 }
158             }
159         };
160
161     protected:
162         template <typename Hash>
163         static void fill_string_array();
164
165         template <class Map>
166         void test( Map& testMap )
167         {
168             typedef Worker<Map> worker;
169
170             // Fill the map
171             for ( size_t i = 0; i < s_Data.size(); ++i ) {
172                 // All keys in arrData are unique, insert() must be successful
173                 if ( s_Data[i].bExists ) {
174                     EXPECT_TRUE( check_result( testMap.insert( *(s_Data[i].pKey), s_Data[i] ), testMap ));
175                 }
176             }
177
178             propout() << std::make_pair( "thread_count", s_nThreadCount )
179                 << std::make_pair( "map_size", s_nMapSize )
180                 << std::make_pair( "percent_exist", s_nPercentExists )
181                 << std::make_pair( "pass_count", s_nPassCount );
182
183             cds_test::thread_pool& pool = get_pool();
184             pool.add( new worker( pool, testMap ), s_nThreadCount );
185
186             std::chrono::milliseconds duration = pool.run();
187
188             propout() << std::make_pair( "duration", duration );
189
190             size_t nExistSuccess = 0;
191             size_t nExistFailed = 0;
192             size_t nMissingSuccess = 0;
193             size_t nMissingFailed = 0;
194
195             // Postcondition: the number of success searching == the number of map item
196             for ( size_t i = 0; i < pool.size(); ++i ) {
197                 worker& w = static_cast<worker&>(pool.get( i ));
198                 nExistSuccess += w.m_KeyExists.nSuccess;
199                 nExistFailed += w.m_KeyExists.nFailed;
200                 nMissingSuccess += w.m_KeyNotExists.nSuccess;
201                 nMissingFailed += w.m_KeyNotExists.nFailed;
202
203                 EXPECT_EQ( w.m_KeyExists.nSuccess, s_nMapSize * s_nPassCount ) << "thread " << i;
204                 EXPECT_EQ( w.m_KeyExists.nFailed, 0u ) << "thread " << i;
205                 EXPECT_EQ( w.m_KeyNotExists.nSuccess, (s_Data.size() - s_nMapSize) * s_nPassCount ) << "thread " << i;
206                 EXPECT_EQ( w.m_KeyNotExists.nFailed, 0u ) << "thread " << i;
207             }
208
209             propout()
210                 << std::make_pair( "exist_found", nExistSuccess )
211                 << std::make_pair( "exist_not_found", nExistFailed )  // must = 0
212                 << std::make_pair( "missing_not_found", nMissingSuccess )
213                 << std::make_pair( "missing_found", nMissingFailed );  // must = 0
214
215             check_before_cleanup( testMap );
216
217             testMap.clear();
218             additional_check( testMap );
219             print_stat( propout(), testMap );
220             additional_cleanup( testMap );
221         }
222
223         template <class Map>
224         void run_test()
225         {
226             ASSERT_GT( s_Data.size(), 0u );
227
228             Map testMap( *this );
229             test( testMap );
230         }
231     };
232
233     class Map_find_string_stdhash: public Map_find_string
234     {
235     public:
236         static void SetUpTestCase();
237
238         template <class Map>
239         void run_test()
240         {
241             Map_find_string::run_test<Map>();
242         }
243     };
244
245 #if CDS_BUILD_BITS == 64
246     class Map_find_string_city32: public Map_find_string
247     {
248     public:
249         static void SetUpTestCase();
250
251         template <class Map>
252         void run_test()
253         {
254             Map_find_string::run_test<Map>();
255         }
256     };
257
258     class Map_find_string_city64: public Map_find_string
259     {
260     public:
261         static void SetUpTestCase();
262
263         template <class Map>
264         void run_test()
265         {
266             Map_find_string::run_test<Map>();
267         }
268     };
269
270     class Map_find_string_city128: public Map_find_string
271     {
272     public:
273         static void SetUpTestCase();
274
275         template <class Map>
276         void run_test()
277         {
278             Map_find_string::run_test<Map>();
279         }
280     };
281
282 #endif // #if CDS_BUILD_BITS == 64
283
284     class Map_find_string_LF: public Map_find_string
285         , public ::testing::WithParamInterface<size_t>
286     {
287     public:
288         template <class Map>
289         void run_test()
290         {
291             s_nLoadFactor = GetParam();
292             propout() << std::make_pair( "load_factor", s_nLoadFactor );
293             Map_find_string::run_test<Map>();
294         }
295     };
296
297 } // namespace map