Removes unnecessary test cases for maps
[libcds.git] / test / stress / map / insdel_item_int / map_insdel_item_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-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_InsDel_item_int: public cds_test::stress_fixture
36     {
37     public:
38         static size_t s_nMapSize;           // map size
39         static size_t s_nInsertThreadCount; // count of insertion thread
40         static size_t s_nDeleteThreadCount; // count of deletion thread
41         static size_t s_nMaxLoadFactor;     // maximum load factor
42
43         static size_t s_nAttemptCount;      // count of SUCCESS insert/delete for each thread
44         static size_t s_nGoalItem;
45
46         static size_t s_nCuckooInitialSize;         // initial size for CuckooMap
47         static size_t s_nCuckooProbesetSize;        // CuckooMap probeset size (only for list-based probeset)
48         static size_t s_nCuckooProbesetThreshold;   // CuckooMap probeset threshold (o - use default)
49
50         static size_t s_nFeldmanMap_HeadBits;
51         static size_t s_nFeldmanMap_ArrayBits;
52
53         static size_t  s_nLoadFactor;  // current load factor
54
55         static void SetUpTestCase();
56         //static void TearDownTestCase();
57
58         typedef size_t  key_type;
59         typedef size_t  value_type;
60
61     protected:
62         enum {
63             insert_thread,
64             delete_thread
65         };
66
67         template <class Map>
68         class Inserter: public cds_test::thread
69         {
70             typedef cds_test::thread base_class;
71             Map&     m_Map;
72
73             struct update_func
74             {
75                 void operator()( bool bNew, std::pair<key_type const, value_type>& item ) const
76                 {
77                     if ( bNew )
78                         item.second = item.first;
79                 }
80
81                 // for boost::container::flat_map
82                 void operator()( bool bNew, std::pair<key_type, value_type>& item ) const
83                 {
84                     if ( bNew )
85                         item.second = item.first;
86                 }
87
88                 // for BronsonAVLTreeMap
89                 void operator()( bool bNew, key_type key, value_type& val ) const
90                 {
91                     if ( bNew )
92                         val = key;
93                 }
94
95                 // for FeldmanHashMap
96                 void operator()( std::pair<key_type const, value_type>& item, std::pair<key_type const, value_type>* pOld ) const
97                 {
98                     if ( !pOld )
99                         item.second = item.first;
100                 }
101             };
102
103         public:
104             size_t  m_nInsertSuccess = 0;
105             size_t  m_nInsertFailed = 0;
106
107         public:
108             Inserter( cds_test::thread_pool& pool, Map& map )
109                 : base_class( pool, insert_thread )
110                 , m_Map( map )
111             {}
112
113             Inserter( Inserter& src )
114                 : base_class( src )
115                 , m_Map( src.m_Map )
116             {}
117
118             virtual thread * clone()
119             {
120                 return new Inserter( *this );
121             }
122
123             virtual void test()
124             {
125                 Map& rMap = m_Map;
126
127                 size_t const nGoalItem = s_nGoalItem;
128                 size_t const nAttemptCount = s_nAttemptCount;
129
130                 for ( size_t nAttempt = 0; nAttempt < nAttemptCount; ) {
131                     if ( nAttempt % 2  == 0 ) {
132                         if ( rMap.insert( nGoalItem, nGoalItem )) {
133                             ++m_nInsertSuccess;
134                             ++nAttempt;
135                         }
136                         else
137                             ++m_nInsertFailed;
138                     }
139                     else {
140                         std::pair<bool, bool> updateResult = rMap.update( nGoalItem, update_func(), true );
141                         if ( updateResult.second ) {
142                             ++m_nInsertSuccess;
143                             ++nAttempt;
144                         }
145                         else
146                             ++m_nInsertFailed;
147                     }
148                 }
149             }
150         };
151
152         template <class Map>
153         class Deleter: public cds_test::thread
154         {
155             typedef cds_test::thread base_class;
156             Map&     m_Map;
157
158         public:
159             size_t  m_nDeleteSuccess = 0;
160             size_t  m_nDeleteFailed = 0;
161
162         public:
163             Deleter( cds_test::thread_pool& pool, Map& map )
164                 : base_class( pool, delete_thread )
165                 , m_Map( map )
166             {}
167
168             Deleter( Deleter& src )
169                 : base_class( src )
170                 , m_Map( src.m_Map )
171             {}
172
173             virtual thread * clone()
174             {
175                 return new Deleter( *this );
176             }
177
178             virtual void test()
179             {
180                 Map& rMap = m_Map;
181
182                 size_t const nGoalItem = s_nGoalItem;
183                 size_t const nAttemptCount = s_nAttemptCount;
184                 for ( size_t nAttempt = 0; nAttempt < nAttemptCount; ) {
185                     if ( rMap.erase( nGoalItem )) {
186                         ++m_nDeleteSuccess;
187                         ++nAttempt;
188                     }
189                     else
190                         ++m_nDeleteFailed;
191                 }
192             }
193         };
194
195     protected:
196
197         template <class Map>
198         void do_test( Map& testMap )
199         {
200             typedef Inserter<Map>       inserter;
201             typedef Deleter<Map>        deleter;
202
203             // Fill the map
204             {
205                 std::vector<key_type>   v;
206                 v.reserve( s_nMapSize );
207                 for ( size_t i = 0; i < s_nMapSize; ++i )
208                     v.push_back( i );
209                 shuffle( v.begin(), v.end());
210                 for ( auto i: v )
211                     EXPECT_TRUE( testMap.insert( i, i ));
212             }
213
214             cds_test::thread_pool& pool = get_pool();
215             pool.add( new inserter( pool, testMap ), s_nInsertThreadCount );
216             pool.add( new deleter( pool, testMap ), s_nDeleteThreadCount );
217
218             propout() << std::make_pair( "insert_thread_count", s_nInsertThreadCount )
219                 << std::make_pair( "delete_thread_count", s_nDeleteThreadCount )
220                 << std::make_pair( "map_size", s_nMapSize )
221                 << std::make_pair( "goal_item", s_nGoalItem )
222                 << std::make_pair( "attempt_count", s_nAttemptCount );
223
224
225             std::chrono::milliseconds duration = pool.run();
226
227             propout() << std::make_pair( "duration", duration );
228
229             size_t nInsertSuccess = 0;
230             size_t nInsertFailed = 0;
231             size_t nDeleteSuccess = 0;
232             size_t nDeleteFailed = 0;
233
234             for ( size_t i = 0; i < pool.size(); ++i ) {
235                 cds_test::thread& thr = pool.get( i );
236                 switch ( thr.type()) {
237                 case insert_thread:
238                 {
239                     inserter& t = static_cast<inserter&>(thr);
240                     EXPECT_EQ( t.m_nInsertSuccess, s_nAttemptCount ) << "thread=" << t.id();
241                     nInsertSuccess += t.m_nInsertSuccess;
242                     nInsertFailed += t.m_nInsertFailed;
243                 }
244                 break;
245                 case delete_thread:
246                 {
247                     deleter& t = static_cast<deleter&>(thr);
248                     EXPECT_EQ( t.m_nDeleteSuccess, s_nAttemptCount ) << "thread=" << t.id();
249                     nDeleteSuccess += t.m_nDeleteSuccess;
250                     nDeleteFailed += t.m_nDeleteFailed;
251                 }
252                 break;
253                 default:
254                     assert( false );
255                 }
256             }
257
258             EXPECT_EQ( nInsertSuccess, nDeleteSuccess );
259             EXPECT_TRUE( testMap.contains( s_nGoalItem ));
260             EXPECT_CONTAINER_SIZE( testMap, s_nMapSize );
261
262             propout()
263                 << std::make_pair( "insert_success", nInsertSuccess )
264                 << std::make_pair( "insert_failed", nInsertFailed )
265                 << std::make_pair( "delete_success", nDeleteSuccess )
266                 << std::make_pair( "delete_failed", nDeleteFailed );
267
268             // Check if the map contains all items
269             for ( size_t i = 0; i < s_nMapSize; ++i )
270                 EXPECT_TRUE( testMap.contains( i )) << "key=" << i;
271
272             check_before_cleanup( testMap );
273
274             testMap.clear();
275             additional_check( testMap );
276             print_stat( propout(), testMap );
277             additional_cleanup( testMap );
278         }
279
280         template <class Map>
281         void run_test()
282         {
283             Map testMap( *this );
284             do_test<Map>( testMap );
285         }
286     };
287
288     class Map_InsDel_item_int_LF: public Map_InsDel_item_int
289         , public ::testing::WithParamInterface<size_t>
290     {
291     public:
292         template <class Set>
293         void run_test()
294         {
295             s_nLoadFactor = GetParam();
296             propout() << std::make_pair( "load_factor", s_nLoadFactor );
297             Map_InsDel_item_int::run_test<Set>();
298         }
299
300         static std::vector<size_t> get_load_factors();
301     };
302
303 } // namespace map