Fixed Clang build
[libcds.git] / test / unit / map / test_map_nogc.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 #ifndef CDSUNIT_MAP_TEST_MAP_NOGC_H
32 #define CDSUNIT_MAP_TEST_MAP_NOGC_H
33
34 #include "test_map_data.h"
35
36 // forward declaration
37 namespace cds { namespace container {} }
38
39 namespace cds_test {
40
41     class container_map_nogc: public map_fixture
42     {
43     public:
44         static size_t const kSize = 1000;
45
46     protected:
47         template <class Map>
48         void test( Map& m )
49         {
50             // Precondition: map is empty
51             // Postcondition: map is empty
52
53             ASSERT_TRUE( m.empty());
54             ASSERT_CONTAINER_SIZE( m, 0 );
55
56             typedef typename Map::value_type map_pair;
57             typedef typename Map::iterator   iterator;
58             size_t const kkSize = kSize;
59
60             std::vector<key_type> arrKeys;
61             for ( int i = 0; i < static_cast<int>(kkSize); ++i )
62                 arrKeys.push_back( key_type( i ));
63             shuffle( arrKeys.begin(), arrKeys.end());
64
65             std::vector< value_type > arrVals;
66             for ( size_t i = 0; i < kkSize; ++i ) {
67                 value_type val;
68                 val.nVal = static_cast<int>( i );
69                 val.strVal = std::to_string( i );
70                 arrVals.push_back( val );
71             }
72
73             // insert/find
74             for ( auto const& i : arrKeys ) {
75                 value_type const& val( arrVals.at( i.nKey ));
76
77                 ASSERT_FALSE( m.contains( i.nKey ) != m.end());
78                 ASSERT_FALSE( m.contains( i ) != m.end());
79                 ASSERT_FALSE( m.contains( other_item( i.nKey ), other_less()) != m.end());
80
81                 std::pair< iterator, bool > updResult;
82                 iterator it;
83                 switch ( i.nKey % 16 ) {
84                 case 0:
85                     it = m.insert( i );
86                     ASSERT_FALSE( it == m.end());
87                     ASSERT_TRUE( m.insert( i ) == m.end());
88                     it->second.nVal = it->first.nKey;
89                     it->second.strVal = std::to_string( it->first.nKey );
90                     break;
91                 case 1:
92                     it = m.insert( i.nKey );
93                     ASSERT_FALSE( it == m.end() );
94                     ASSERT_TRUE( m.insert( i.nKey ) == m.end());
95                     it->second.nVal = it->first.nKey;
96                     it->second.strVal = std::to_string( it->first.nKey );
97                     break;
98                 case 2:
99                     it = m.insert( std::to_string( i.nKey ));
100                     ASSERT_FALSE( it == m.end() );
101                     ASSERT_TRUE( m.insert( std::to_string( i.nKey )) == m.end());
102                     it->second.nVal = it->first.nKey;
103                     it->second.strVal = std::to_string( it->first.nKey );
104                     break;
105                 case 3:
106                     it = m.insert( i, val );
107                     ASSERT_FALSE( it == m.end() );
108                     ASSERT_TRUE( m.insert( i, val ) == m.end());
109                     break;
110                 case 4:
111                     it = m.insert( i.nKey, val.strVal );
112                     ASSERT_FALSE( it == m.end() );
113                     ASSERT_TRUE( m.insert( i.nKey, val.strVal ) == m.end());
114                     break;
115                 case 5:
116                     it = m.insert( val.strVal, i.nKey );
117                     ASSERT_FALSE( it == m.end() );
118                     ASSERT_TRUE( m.insert( val.strVal, i.nKey ) == m.end());
119                     break;
120                 case 6:
121                     it = m.insert_with( i, []( map_pair& v ) {
122                         v.second.nVal = v.first.nKey;
123                         v.second.strVal = std::to_string( v.first.nKey );
124                     } );
125                     ASSERT_FALSE( it == m.end() );
126                     ASSERT_TRUE( m.insert_with( i, []( map_pair& ) {
127                         EXPECT_TRUE( false );
128                     } ) == m.end());
129                     break;
130                 case 7:
131                     it = m.insert_with( i.nKey, []( map_pair& v ) {
132                         v.second.nVal = v.first.nKey;
133                         v.second.strVal = std::to_string( v.first.nKey );
134                     } );
135                     ASSERT_FALSE( it == m.end() );
136                     ASSERT_TRUE( m.insert_with( i.nKey, []( map_pair& ) {
137                         EXPECT_TRUE( false );
138                     } ) == m.end());
139                     break;
140                 case 8:
141                     it = m.insert_with( val.strVal, []( map_pair& v ) {
142                         v.second.nVal = v.first.nKey;
143                         v.second.strVal = std::to_string( v.first.nKey );
144                     } );
145                     ASSERT_FALSE( it == m.end() );
146                     ASSERT_TRUE( m.insert_with( val.strVal, []( map_pair& ) {
147                         EXPECT_TRUE( false );
148                     } ) == m.end());
149                     break;
150                 case 9:
151                     updResult = m.update( i.nKey, false );
152                     ASSERT_TRUE( updResult.first == m.end() );
153                     ASSERT_FALSE( updResult.second );
154
155                     updResult = m.update( i.nKey );
156                     ASSERT_TRUE( updResult.first != m.end());
157                     ASSERT_TRUE( updResult.second );
158                     updResult.first->second.nVal = updResult.first->first.nKey;
159
160                     updResult = m.update( i.nKey, false );
161                     ASSERT_TRUE( updResult.first != m.end() );
162                     ASSERT_FALSE( updResult.second );
163                     EXPECT_EQ( updResult.first->first.nKey, updResult.first->second.nVal );
164                     updResult.first->second.strVal = std::to_string( updResult.first->second.nVal );
165                     break;
166                 case 10:
167                     updResult = m.update( i, false );
168                     ASSERT_TRUE( updResult.first == m.end() );
169                     ASSERT_FALSE( updResult.second );
170
171                     updResult = m.update( i );
172                     ASSERT_TRUE( updResult.first != m.end());
173                     ASSERT_TRUE( updResult.second );
174                     updResult.first->second.nVal = updResult.first->first.nKey;
175
176                     updResult = m.update( i );
177                     ASSERT_TRUE( updResult.first != m.end());
178                     ASSERT_FALSE( updResult.second );
179                     EXPECT_EQ( updResult.first->first.nKey, updResult.first->second.nVal );
180                     updResult.first->second.strVal = std::to_string( updResult.first->second.nVal );
181                     break;
182                 case 11:
183                     updResult = m.update( val.strVal, false );
184                     ASSERT_TRUE( updResult.first == m.end());
185                     ASSERT_FALSE( updResult.second );
186
187                     updResult = m.update( val.strVal );
188                     ASSERT_TRUE( updResult.first != m.end());
189                     ASSERT_TRUE( updResult.second );
190                     updResult.first->second.nVal = updResult.first->first.nKey;
191
192                     updResult = m.update( val.strVal, false );
193                     ASSERT_TRUE( updResult.first != m.end());
194                     ASSERT_FALSE( updResult.second );
195                     EXPECT_EQ( updResult.first->first.nKey, updResult.first->second.nVal );
196                     updResult.first->second.strVal = std::to_string( updResult.first->second.nVal );
197                     break;
198                 case 12:
199                     it = m.emplace( i.nKey );
200                     ASSERT_TRUE( it != m.end());
201                     ASSERT_FALSE( m.emplace( i.nKey ) != m.end());
202                     it->second.nVal = it->first.nKey;
203                     it->second.strVal = std::to_string( it->first.nKey );
204                     break;
205                 case 13:
206                     it = m.emplace( i, i.nKey );
207                     ASSERT_TRUE( it != m.end() );
208                     ASSERT_FALSE( m.emplace( i, i.nKey ) != m.end());
209                     break;
210                 case 14:
211                     {
212                         std::string str = val.strVal;
213                         it = m.emplace( i, std::move( str ));
214                         ASSERT_TRUE( it != m.end() );
215                         ASSERT_TRUE( str.empty());
216                         str = val.strVal;
217                         ASSERT_FALSE( m.emplace( i, std::move( str )) != m.end());
218                         ASSERT_TRUE( str.empty());
219                     }
220                     break;
221                 case 15:
222                     {
223                         std::string str = val.strVal;
224                         it = m.emplace( i, i.nKey, std::move( str ));
225                         ASSERT_TRUE( it != m.end() );
226                         ASSERT_TRUE( str.empty());
227                         str = val.strVal;
228                         ASSERT_FALSE( m.emplace( i, i.nKey, std::move( str )) != m.end());
229                         ASSERT_TRUE( str.empty());
230                     }
231                     break;
232                 }
233
234                 it = m.contains( i.nKey );
235                 ASSERT_TRUE( it != m.end());
236                 ASSERT_TRUE( m.contains( i ) == it );
237                 ASSERT_TRUE( m.contains( other_item( i.nKey ), other_less()) == it );
238                 EXPECT_EQ( it->first.nKey, it->second.nVal );
239                 EXPECT_EQ( std::to_string( it->first.nKey ), it->second.strVal );
240             }
241             ASSERT_FALSE( m.empty() );
242             ASSERT_CONTAINER_SIZE( m, kkSize );
243             ASSERT_FALSE( m.begin() == m.end() );
244             ASSERT_FALSE( m.cbegin() == m.cend() );
245
246             // clear
247
248             m.clear();
249
250             ASSERT_TRUE( m.empty() );
251             ASSERT_CONTAINER_SIZE( m, 0 );
252             ASSERT_TRUE( m.begin() == m.end() );
253             ASSERT_TRUE( m.cbegin() == m.cend() );
254         }
255     };
256
257 } // namespace cds_test
258
259 #endif // #ifndef CDSUNIT_MAP_TEST_MAP_NOGC_H