Uses different pass count for different parallel queue test cases
[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_nBronsonAVLTreeMapPassCount;
43         static size_t s_nEllenBinTreeMapPassCount;
44         static size_t s_nFeldmanPassCount;
45         static size_t s_nMichaelMapPassCount;
46         static size_t s_nSkipListMapPassCount;
47         static size_t s_nSplitListMapPassCount;
48
49         static size_t s_nMaxLoadFactor;     // maximum load factor
50
51         static size_t s_nCuckooInitialSize;         // initial size for CuckooMap
52         static size_t s_nCuckooProbesetSize;        // CuckooMap probeset size (only for list-based probeset)
53         static size_t s_nCuckooProbesetThreshold;   // CUckooMap probeset threshold (o - use default)
54
55         static size_t s_nFeldmanMap_HeadBits;
56         static size_t s_nFeldmanMap_ArrayBits;
57
58         static size_t s_nLoadFactor;  // current load factor
59
60         typedef std::string  key_type;
61         struct value_type {
62             std::string const* pKey;
63             bool        bExists ;   // true - key in map, false - key not in map
64         };
65
66         typedef std::vector<value_type> value_vector;
67         static std::vector<std::string> s_arrString;
68         static value_vector s_Data;
69
70         static void SetUpTestCase();
71         static void TearDownTestCase();
72
73         static void setup_test_case();
74         static std::vector<size_t> get_load_factors();
75
76     private:
77         template <typename Iterator, typename Map>
78         static bool check_result( Iterator const& it, Map const& map )
79         {
80             return it != map.end();
81         }
82         template <typename Map>
83         static bool check_result( bool b, Map const& )
84         {
85             return b;
86         }
87
88         template <class Map>
89         class Worker: public cds_test::thread
90         {
91             typedef cds_test::thread base_class;
92             Map&     m_Map;
93
94         public:
95             struct Stat {
96                 size_t      nSuccess = 0;
97                 size_t      nFailed = 0;
98             };
99
100             Stat    m_KeyExists;
101             Stat    m_KeyNotExists;
102
103         public:
104             Worker( cds_test::thread_pool& pool, Map& map )
105                 : base_class( pool )
106                 , m_Map( map )
107                 , m_KeyExists()
108                 , m_KeyNotExists()
109             {}
110
111             Worker( Worker& src )
112                 : base_class( src )
113                 , m_Map( src.m_Map )
114                 , m_KeyExists()
115                 , m_KeyNotExists()
116             {}
117
118             virtual thread * clone()
119             {
120                 return new Worker( *this );
121             }
122             virtual void test()
123             {
124                 size_t const nPassCount = s_nPassCount;
125
126                 Map& rMap = m_Map;
127                 for (size_t nPass = 0; nPass < nPassCount; ++nPass) {
128                   auto itEnd = s_Data.cend();
129                   for (auto it = s_Data.cbegin(); it != itEnd; ++it) {
130                     auto bFound = rMap.contains(*(it->pKey));
131                     if (it->bExists) {
132                       if (check_result(bFound, rMap))
133                         ++m_KeyExists.nSuccess;
134                       else
135                         ++m_KeyExists.nFailed;
136                     } else {
137                       if (check_result(bFound, rMap))
138                         ++m_KeyNotExists.nFailed;
139                       else
140                         ++m_KeyNotExists.nSuccess;
141                     }
142                   }
143                 }
144             }
145         };
146
147     protected:
148         template <typename Hash>
149         static void fill_string_array();
150
151         template <class Map>
152         void test( Map& testMap )
153         {
154             typedef Worker<Map> worker;
155
156             // Fill the map
157             for ( size_t i = 0; i < s_Data.size(); ++i ) {
158                 // All keys in arrData are unique, insert() must be successful
159                 if ( s_Data[i].bExists ) {
160                     EXPECT_TRUE( check_result( testMap.insert( *(s_Data[i].pKey), s_Data[i] ), testMap ));
161                 }
162             }
163
164             cds_test::thread_pool &pool = get_pool();
165             std::unique_ptr<worker> worker_thread(new worker(pool, testMap));
166             worker_thread->test();
167
168             testMap.clear();
169             additional_cleanup(testMap);
170         }
171
172         template <class Map>
173         void run_test()
174         {
175             ASSERT_GT( s_Data.size(), 0u );
176
177             Map testMap( *this );
178             test( testMap );
179         }
180
181         template <class Map>
182         void run_bronson_avl_tree() {
183           Map_find_string::s_nPassCount =
184               Map_find_string::s_nBronsonAVLTreeMapPassCount;
185           run_test<Map>();
186         }
187
188         template <class Map>
189         void run_ellen_bin_tree() {
190           Map_find_string::s_nPassCount =
191               Map_find_string::s_nEllenBinTreeMapPassCount;
192           run_test<Map>();
193         }
194
195         template <class Map>
196         void run_feldman() {
197           Map_find_string::s_nPassCount =
198               Map_find_string::s_nFeldmanPassCount;
199           run_test<Map>();
200         }
201
202         template <class Map>
203         void run_michael() {
204           Map_find_string::s_nPassCount =
205               Map_find_string::s_nMichaelMapPassCount;
206           run_test<Map>();
207         }
208
209         template <class Map>
210         void run_skip_list() {
211           Map_find_string::s_nPassCount =
212               Map_find_string::s_nSkipListMapPassCount;
213           run_test<Map>();
214         }
215
216         template <class Map>
217         void run_split_list() {
218           Map_find_string::s_nPassCount =
219               Map_find_string::s_nSplitListMapPassCount;
220           run_test<Map>();
221         }
222
223     };
224
225     class Map_find_string_stdhash: public Map_find_string
226     {
227     public:
228         static void SetUpTestCase();
229
230         template <class Map>
231         void run_test()
232         {
233             Map_find_string::run_test<Map>();
234         }
235     };
236
237 #if CDS_BUILD_BITS == 64
238     class Map_find_string_city32: public Map_find_string
239     {
240     public:
241         static void SetUpTestCase();
242
243         template <class Map>
244         void run_test()
245         {
246             Map_find_string::run_test<Map>();
247         }
248     };
249
250     class Map_find_string_city64: public Map_find_string
251     {
252     public:
253         static void SetUpTestCase();
254
255         template <class Map>
256         void run_test()
257         {
258             Map_find_string::run_test<Map>();
259         }
260     };
261
262     class Map_find_string_city128: public Map_find_string
263     {
264     public:
265         static void SetUpTestCase();
266
267         template <class Map>
268         void run_test()
269         {
270             Map_find_string::run_test<Map>();
271         }
272     };
273
274 #endif // #if CDS_BUILD_BITS == 64
275
276     class Map_find_string_LF: public Map_find_string
277         , public ::testing::WithParamInterface<size_t>
278     {
279     public:
280         template <class Map>
281         void run_test()
282         {
283             s_nLoadFactor = GetParam();
284             propout() << std::make_pair( "load_factor", s_nLoadFactor );
285             Map_find_string::run_test<Map>();
286         }
287     };
288
289 } // namespace map