Merge branch 'dev'
[libcds.git] / test / stress / map / find_int / map_find_int.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-2016
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 // find int test in map<int> in mutithreaded mode
34 namespace map {
35
36     class Map_find_int: public cds_test::stress_fixture
37     {
38     public:
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
44
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)
48
49         static size_t s_nFeldmanMap_HeadBits;
50         static size_t s_nFeldmanMap_ArrayBits;
51
52         static size_t s_nLoadFactor;  // current load factor
53
54         typedef size_t   key_type;
55         struct value_type {
56             key_type    nKey;   // key
57             bool        bExists;   // true - key in map, false - key not in map
58         };
59
60         typedef std::vector<value_type> value_vector;
61         static value_vector s_Data;
62
63         static void SetUpTestCase();
64         static void TearDownTestCase();
65
66     private:
67         static size_t s_nRealMapSize;
68
69         static void generateSequence();
70
71         template <typename Iterator, typename Map>
72         static bool check_result( Iterator const& it, Map const& map )
73         {
74             return it != map.end();
75         }
76         template <typename Map>
77         static bool check_result( bool b, Map const& )
78         {
79             return b;
80         }
81
82         template <class Map>
83         class Worker: public cds_test::thread
84         {
85             typedef cds_test::thread base_class;
86             Map&  m_Map;
87
88         public:
89             struct Stat {
90                 size_t      nSuccess = 0;
91                 size_t      nFailed = 0;
92             };
93
94             Stat    m_KeyExists;
95             Stat    m_KeyNotExists;
96
97         public:
98             Worker( cds_test::thread_pool& pool, Map& map )
99                 : base_class( pool )
100                 , m_Map( map )
101             {}
102
103             Worker( Worker& src )
104                 : base_class( src )
105                 , m_Map( src.m_Map )
106             {}
107
108             virtual thread * clone()
109             {
110                 return new Worker( *this );
111             }
112
113             virtual void test()
114             {
115                 size_t const nPassCount = s_nPassCount;
116
117                 Map& rMap = m_Map;
118                 for ( size_t nPass = 0; nPass < nPassCount; ++nPass ) {
119                     if ( id() & 1 ) {
120                         auto itEnd = s_Data.cend();
121                         for ( auto it = s_Data.cbegin(); it != itEnd; ++it ) {
122                             auto bFound = rMap.contains( it->nKey );
123                             if ( it->bExists ) {
124                                 if ( check_result( bFound, rMap ))
125                                     ++m_KeyExists.nSuccess;
126                                 else
127                                     ++m_KeyExists.nFailed;
128                             }
129                             else {
130                                 if ( check_result( bFound, rMap ))
131                                     ++m_KeyNotExists.nFailed;
132                                 else
133                                     ++m_KeyNotExists.nSuccess;
134                             }
135                         }
136                     }
137                     else {
138                         auto itEnd = s_Data.crend();
139                         for ( auto it = s_Data.crbegin(); it != itEnd; ++it ) {
140                             auto bFound = rMap.contains( it->nKey );
141                             if ( it->bExists ) {
142                                 if ( check_result( bFound, rMap ))
143                                     ++m_KeyExists.nSuccess;
144                                 else
145                                     ++m_KeyExists.nFailed;
146                             }
147                             else {
148                                 if ( check_result( bFound, rMap ))
149                                     ++m_KeyNotExists.nFailed;
150                                 else
151                                     ++m_KeyNotExists.nSuccess;
152                             }
153                         }
154                     }
155                 }
156             }
157         };
158
159     protected:
160
161         template <class Map>
162         void test( Map& testMap )
163         {
164             typedef Worker<Map> worker;
165
166             // Fill the map
167             for ( auto const& it: s_Data ) {
168                 if ( it.bExists ) {
169                     EXPECT_TRUE( check_result( testMap.insert( it.nKey, it ), testMap ));
170                 }
171             }
172
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 );
177
178             cds_test::thread_pool& pool = get_pool();
179             pool.add( new worker( pool, testMap ), s_nThreadCount );
180
181             std::chrono::milliseconds duration = pool.run();
182
183             propout() << std::make_pair( "duration", duration );
184
185             size_t nExistSuccess = 0;
186             size_t nExistFailed = 0;
187             size_t nMissingSuccess = 0;
188             size_t nMissingFailed = 0;
189
190             for ( size_t i = 0; i < pool.size(); ++i ) {
191                 worker& w = static_cast<worker&>( pool.get( i ));
192                 nExistSuccess += w.m_KeyExists.nSuccess;
193                 nExistFailed += w.m_KeyExists.nFailed;
194                 nMissingSuccess += w.m_KeyNotExists.nSuccess;
195                 nMissingFailed += w.m_KeyNotExists.nFailed;
196
197                 EXPECT_EQ( w.m_KeyExists.nFailed, 0 ) << "thread " << i;
198                 EXPECT_EQ( w.m_KeyExists.nSuccess, s_nRealMapSize * s_nPassCount ) << "thread " << i;
199                 EXPECT_EQ( w.m_KeyNotExists.nFailed, 0 ) << "thread " << i;
200                 EXPECT_EQ( w.m_KeyNotExists.nSuccess, (s_Data.size() - s_nRealMapSize) * s_nPassCount ) << "thread " << i;
201             }
202
203             propout() 
204                 << std::make_pair( "exist_found", nExistSuccess )
205                 << std::make_pair( "exist_not_found",  nExistFailed )  // must = 0
206                 << std::make_pair( "missing_not_found", nMissingSuccess )
207                 << std::make_pair( "missing_found", nMissingFailed );  // must = 0
208
209             check_before_cleanup( testMap );
210
211             testMap.clear();
212             additional_check( testMap );
213             print_stat( propout(), testMap );
214             additional_cleanup( testMap );
215         }
216
217         template <class Map>
218         void run_test()
219         {
220             Map testMap( *this );
221             test( testMap );
222         }
223     };
224
225     class Map_find_int_LF: public Map_find_int
226         , public ::testing::WithParamInterface<size_t>
227     {
228     public:
229         template <class Map>
230         void run_test()
231         {
232             s_nLoadFactor = GetParam();
233             propout() << std::make_pair( "load_factor", s_nLoadFactor );
234             Map_find_int::run_test<Map>();
235         }
236
237         static std::vector<size_t> get_load_factors();
238     };
239
240 } // namespace map