2 This file is a part of libcds - Concurrent Data Structures library
4 (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2017
6 Source code repo: http://github.com/khizmax/libcds/
7 Download: http://sourceforge.net/projects/libcds/files/
9 Redistribution and use in source and binary forms, with or without
10 modification, are permitted provided that the following conditions are met:
12 * Redistributions of source code must retain the above copyright notice, this
13 list of conditions and the following disclaimer.
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.
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.
31 #ifndef CDSUNIT_SET_TEST_SET_H
32 #define CDSUNIT_SET_TEST_SET_H
34 #include "test_set_data.h"
36 #include <cds/opt/hash.h>
40 class container_set : public container_set_data
43 template <typename Set>
46 // Precondition: set is empty
47 // Postcondition: set is empty
49 ASSERT_TRUE( s.empty());
50 ASSERT_CONTAINER_SIZE( s, 0 );
51 size_t const nSetSize = kSize;
53 typedef typename Set::value_type value_type;
55 std::vector< value_type > data;
56 std::vector< size_t> indices;
57 data.reserve( kSize );
58 indices.reserve( kSize );
59 for ( size_t key = 0; key < kSize; ++key ) {
60 data.push_back( value_type( static_cast<int>(key)));
61 indices.push_back( key );
63 shuffle( indices.begin(), indices.end());
66 for ( auto idx : indices ) {
69 ASSERT_FALSE( s.contains( i.nKey ));
70 ASSERT_FALSE( s.contains( i ));
71 ASSERT_FALSE( s.contains( other_item( i.key()), other_less()));
72 ASSERT_FALSE( s.find( i.nKey, []( value_type&, int ) {} ));
73 ASSERT_FALSE( s.find( i, []( value_type&, value_type const& ) {} ));
74 ASSERT_FALSE( s.find_with( other_item( i.key()), other_less(), []( value_type&, other_item const& ) {} ));
76 std::pair<bool, bool> updResult;
79 updResult = s.update( i.key(), []( bool, value_type&, int )
83 EXPECT_FALSE( updResult.first );
84 EXPECT_FALSE( updResult.second );
88 ASSERT_TRUE( s.insert( i ));
89 ASSERT_FALSE( s.insert( i ));
90 updResult = s.update( i, []( bool bNew, value_type& val, value_type const& arg)
93 EXPECT_EQ( val.key(), arg.key());
95 EXPECT_TRUE( updResult.first );
96 EXPECT_FALSE( updResult.second );
99 ASSERT_TRUE( s.insert( i.key()));
100 ASSERT_FALSE( s.insert( i.key()));
101 updResult = s.update( i.key(), []( bool bNew, value_type& val, int arg)
103 EXPECT_FALSE( bNew );
104 EXPECT_EQ( val.key(), arg );
106 EXPECT_TRUE( updResult.first );
107 EXPECT_FALSE( updResult.second );
110 ASSERT_TRUE( s.insert( i, []( value_type& v ) { ++v.nFindCount; } ));
111 ASSERT_FALSE( s.insert( i, []( value_type& v ) { ++v.nFindCount; } ));
112 ASSERT_TRUE( s.find( i.nKey, []( value_type const& v, int key )
114 EXPECT_EQ( v.key(), key );
115 EXPECT_EQ( v.nFindCount, 1u );
119 ASSERT_TRUE( s.insert( i.key(), []( value_type& v ) { ++v.nFindCount; } ));
120 ASSERT_FALSE( s.insert( i.key(), []( value_type& v ) { ++v.nFindCount; } ));
121 ASSERT_TRUE( s.find( i.nKey, []( value_type const& v, int key )
123 EXPECT_EQ( v.key(), key );
124 EXPECT_EQ( v.nFindCount, 1u );
128 updResult = s.update( i, []( bool bNew, value_type& v, value_type const& arg )
131 EXPECT_EQ( v.key(), arg.key());
134 EXPECT_TRUE( updResult.first );
135 EXPECT_TRUE( updResult.second );
137 updResult = s.update( i, []( bool bNew, value_type& v, value_type const& arg )
139 EXPECT_FALSE( bNew );
140 EXPECT_EQ( v.key(), arg.key());
143 EXPECT_TRUE( updResult.first );
144 EXPECT_FALSE( updResult.second );
146 ASSERT_TRUE( s.find( i.nKey, []( value_type const& v, int key )
148 EXPECT_EQ( v.key(), key );
149 EXPECT_EQ( v.nUpdateNewCount, 2u );
153 updResult = s.update( i.key(), []( bool bNew, value_type& v, int arg )
156 EXPECT_EQ( v.key(), arg );
159 EXPECT_TRUE( updResult.first );
160 EXPECT_TRUE( updResult.second );
162 updResult = s.update( i.key(), []( bool bNew, value_type& v, int arg )
164 EXPECT_FALSE( bNew );
165 EXPECT_EQ( v.key(), arg );
168 EXPECT_TRUE( updResult.first );
169 EXPECT_FALSE( updResult.second );
171 ASSERT_TRUE( s.find( i, []( value_type const& v, value_type const& arg )
173 EXPECT_EQ( v.key(), arg.key());
174 EXPECT_EQ( v.nUpdateNewCount, 2u );
178 ASSERT_TRUE( s.emplace( i.key()));
179 ASSERT_TRUE( s.find( i, []( value_type const& v, value_type const& arg )
181 EXPECT_EQ( v.key(), arg.key());
182 EXPECT_EQ( v.nVal, arg.nVal );
187 ASSERT_TRUE( s.emplace( i.key(), std::move( str )));
188 EXPECT_TRUE( str.empty());
189 ASSERT_TRUE( s.find( i, []( value_type const& v, value_type const& arg )
191 EXPECT_EQ( v.key(), arg.key());
192 EXPECT_EQ( v.nVal, arg.nVal );
193 EXPECT_EQ( v.strVal, std::string( "Hello!" ));
197 // forgot anything?..
198 ASSERT_TRUE( false );
201 ASSERT_TRUE( s.contains( i.nKey ));
202 ASSERT_TRUE( s.contains( i ));
203 ASSERT_TRUE( s.contains( other_item( i.key()), other_less()));
204 ASSERT_TRUE( s.find( i.nKey, []( value_type&, int ) {} ));
205 ASSERT_TRUE( s.find( i, []( value_type&, value_type const& ) {} ));
206 ASSERT_TRUE( s.find_with( other_item( i.key()), other_less(), []( value_type&, other_item const& ) {} ));
209 ASSERT_FALSE( s.empty());
210 ASSERT_CONTAINER_SIZE( s, nSetSize );
213 shuffle( indices.begin(), indices.end());
214 for ( auto idx : indices ) {
217 ASSERT_TRUE( s.contains( i.nKey ));
218 ASSERT_TRUE( s.contains( i ));
219 ASSERT_TRUE( s.contains( other_item( i.key()), other_less()));
220 ASSERT_TRUE( s.find( i.nKey, []( value_type& v, int )
224 ASSERT_TRUE( s.find( i, []( value_type& v, value_type const& )
226 EXPECT_EQ( ++v.nFindCount, 2u );
228 ASSERT_TRUE( s.find_with( other_item( i.key()), other_less(), []( value_type& v, other_item const& )
230 EXPECT_EQ( ++v.nFindCount, 3u );
233 int nKey = i.key() - 1;
236 ASSERT_TRUE( s.erase( i.key()));
237 ASSERT_FALSE( s.erase( i.key()));
240 ASSERT_TRUE( s.erase( i ));
241 ASSERT_FALSE( s.erase( i ));
244 ASSERT_TRUE( s.erase_with( other_item( i.key()), other_less()));
245 ASSERT_FALSE( s.erase_with( other_item( i.key()), other_less()));
248 ASSERT_TRUE( s.erase( i.key(), [&nKey]( value_type const& v )
252 EXPECT_EQ( i.key(), nKey );
255 ASSERT_FALSE( s.erase( i.key(), [&nKey]( value_type const& v )
259 EXPECT_EQ( i.key(), nKey + 1 );
262 ASSERT_TRUE( s.erase( i, [&nKey]( value_type const& v )
266 EXPECT_EQ( i.key(), nKey );
269 ASSERT_FALSE( s.erase( i, [&nKey]( value_type const& v )
273 EXPECT_EQ( i.key(), nKey + 1 );
276 ASSERT_TRUE( s.erase_with( other_item( i.key()), other_less(), [&nKey]( value_type const& v )
280 EXPECT_EQ( i.key(), nKey );
283 ASSERT_FALSE( s.erase_with( other_item( i.key()), other_less(), [&nKey]( value_type const& v )
287 EXPECT_EQ( i.key(), nKey + 1 );
291 ASSERT_FALSE( s.contains( i.nKey ));
292 ASSERT_FALSE( s.contains( i ));
293 ASSERT_FALSE( s.contains( other_item( i.key()), other_less()));
294 ASSERT_FALSE( s.find( i.nKey, []( value_type&, int ) {} ));
295 ASSERT_FALSE( s.find( i, []( value_type&, value_type const& ) {} ));
296 ASSERT_FALSE( s.find_with( other_item( i.key()), other_less(), []( value_type&, other_item const& ) {} ));
298 ASSERT_TRUE( s.empty());
299 ASSERT_CONTAINER_SIZE( s, 0u );
303 for ( auto& i : data ) {
304 ASSERT_TRUE( s.insert( i ));
307 ASSERT_FALSE( s.empty());
308 ASSERT_CONTAINER_SIZE( s, nSetSize );
312 ASSERT_TRUE( s.empty());
313 ASSERT_CONTAINER_SIZE( s, 0u );
315 ASSERT_TRUE( s.begin() == s.end());
316 ASSERT_TRUE( s.cbegin() == s.cend());
320 } // namespace cds_test
322 #endif // CDSUNIT_SET_TEST_SET_H