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