Removed redundant spaces
[libcds.git] / test / stress / map / insfind_int / map_insfind_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 namespace map {
34
35     class Map_InsFind_int: public cds_test::stress_fixture
36     {
37     public:
38         static size_t s_nMapSize;          // initial map size
39         static size_t s_nThreadCount;      // thread count
40         static size_t s_nMaxLoadFactor;    // maximum load factor
41
42         static size_t s_nCuckooInitialSize;         // initial size for CuckooMap
43         static size_t s_nCuckooProbesetSize;        // CuckooMap probeset size (only for list-based probeset)
44         static size_t s_nCuckooProbesetThreshold;   // CuckooMap probeset threshold (o - use default)
45
46         static size_t s_nFeldmanMap_HeadBits;
47         static size_t s_nFeldmanMap_ArrayBits;
48
49         static size_t  s_nLoadFactor;  // current load factor
50
51         static void SetUpTestCase();
52         //static void TearDownTestCase();
53
54     private:
55         typedef size_t  key_type;
56         typedef size_t  value_type;
57
58         template <typename Iterator, typename Map>
59         static bool check_result( Iterator const& it, Map const& map )
60         {
61             return it != map.end();
62         }
63         template <typename Map>
64         static bool check_result( bool b, Map const& )
65         {
66             return b;
67         }
68
69         template <class Map>
70         class Inserter: public cds_test::thread
71         {
72             typedef cds_test::thread base_class;
73
74             Map&     m_Map;
75             std::vector<size_t> m_arrVal;
76
77             void make_array()
78             {
79                 size_t const nThreadCount = s_nThreadCount;
80                 size_t const nSize = s_nMapSize / nThreadCount + 1;
81                 m_arrVal.resize( nSize );
82                 size_t nItem = id();
83                 for ( size_t i = 0; i < nSize; nItem += nThreadCount, ++i )
84                     m_arrVal[i] = nItem;
85                 shuffle( m_arrVal.begin(), m_arrVal.end());
86             }
87         public:
88             size_t  m_nInsertSuccess = 0;
89             size_t  m_nInsertFailed = 0;
90             size_t  m_nFindSuccess = 0;
91             size_t  m_nFindFail = 0;
92
93         public:
94             Inserter( cds_test::thread_pool& pool, Map& map )
95                 : base_class( pool )
96                 , m_Map( map )
97             {
98                 make_array();
99             }
100
101             Inserter( Inserter& src )
102                 : base_class( src )
103                 , m_Map( src.m_Map )
104             {
105                 make_array();
106             }
107
108             virtual thread * clone()
109             {
110                 return new Inserter( *this );
111             }
112
113             virtual void test()
114             {
115                 Map& rMap = m_Map;
116
117                 size_t const nArrSize = m_arrVal.size();
118                 for ( size_t i = 0; i < nArrSize; ++i ) {
119                     size_t const nItem = m_arrVal[i];
120                     if ( check_result( rMap.insert( nItem, nItem * 8 ), rMap ))
121                         ++m_nInsertSuccess;
122                     else
123                         ++m_nInsertFailed;
124
125                     for ( size_t k = 0; k <= i; ++k ) {
126                         if ( check_result( rMap.contains( m_arrVal[k] ), rMap ))
127                             ++m_nFindSuccess;
128                         else
129                             ++m_nFindFail;
130                     }
131                 }
132             }
133         };
134
135     protected:
136
137         template <class Map>
138         void do_test( Map& testMap )
139         {
140             typedef Inserter<Map> inserter;
141
142             cds_test::thread_pool& pool = get_pool();
143             pool.add( new inserter( pool, testMap ), s_nThreadCount );
144
145             propout()
146                 << std::make_pair( "thread_count", s_nThreadCount )
147                 << std::make_pair( "insert_per_thread", s_nMapSize );
148
149             std::chrono::milliseconds duration = pool.run();
150
151             propout() << std::make_pair( "duration", duration );
152
153             size_t nInsertSuccess = 0;
154             size_t nInsertFailed = 0;
155             size_t nFindSuccess = 0;
156             size_t nFindFailed = 0;
157             for ( size_t i = 0; i < pool.size(); ++i ) {
158                 inserter& thr = static_cast<inserter&>(pool.get( i ));
159
160                 EXPECT_EQ( thr.m_nInsertFailed, 0u ) << "thread " << thr.id();
161                 EXPECT_EQ( thr.m_nFindFail, 0u ) << "thread " << thr.id();
162
163                 nInsertSuccess += thr.m_nInsertSuccess;
164                 nInsertFailed += thr.m_nInsertFailed;
165                 nFindSuccess += thr.m_nFindSuccess;
166                 nFindFailed += thr.m_nFindFail;
167             }
168
169             propout()
170                 << std::make_pair( "insert_success", nInsertSuccess )
171                 << std::make_pair( "insert_failed", nInsertFailed )
172                 << std::make_pair( "find_success", nFindSuccess )
173                 << std::make_pair( "find_failed", nFindFailed )
174                 << std::make_pair( "finish_map_size", testMap.size());
175
176             EXPECT_EQ( nInsertFailed, 0u );
177             EXPECT_EQ( nFindFailed, 0u );
178
179             check_before_cleanup( testMap );
180
181             testMap.clear();
182             additional_check( testMap );
183             print_stat( propout(), testMap );
184             additional_cleanup( testMap );
185         }
186
187         template <class Map>
188         void run_test()
189         {
190             Map testMap( *this );
191             do_test( testMap );
192         }
193     };
194
195     class Map_InsFind_int_LF: public Map_InsFind_int
196         , public ::testing::WithParamInterface<size_t>
197     {
198     public:
199         template <class Map>
200         void run_test()
201         {
202             s_nLoadFactor = GetParam();
203             propout() << std::make_pair( "load_factor", s_nLoadFactor );
204             Map_InsFind_int::run_test<Map>();
205         }
206
207         static std::vector<size_t> get_load_factors();
208     };
209
210 } // namespace map