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_NOGC_H
32 #define CDSUNIT_SET_TEST_SET_NOGC_H
34 #include <cds_test/check_size.h>
35 #include <cds_test/fixture.h>
37 #include <cds/opt/hash.h>
39 // forward declaration
40 namespace cds { namespace container {}}
43 namespace co = cds::opt;
45 class container_set_nogc : public fixture
48 static size_t const kSize = 100;
52 unsigned int nFindCount;
53 unsigned int nUpdateNewCount;
54 unsigned int nUpdateCount;
63 memset( this, 0, sizeof( *this ));
70 explicit other_item( int k )
80 struct int_item: public stat
91 explicit int_item( int k )
97 explicit int_item( Q const& src )
102 int_item( int_item const& src )
105 , strVal( src.strVal )
108 int_item( int_item&& src )
111 , strVal( std::move( src.strVal ))
114 int_item( int k, std::string&& s )
117 , strVal( std::move( s ))
120 explicit int_item( other_item const& s )
122 , nVal( s.key() * 2 )
132 size_t operator()( int i ) const
134 return co::v::hash<int>()(i);
136 template <typename Item>
137 size_t operator()( const Item& i ) const
139 return (*this)(i.key());
143 struct simple_item_counter {
146 simple_item_counter()
165 operator size_t() const
174 bool operator ()( int_item const& v1, int_item const& v2 ) const
176 return v1.key() < v2.key();
179 template <typename Q>
180 bool operator ()( int_item const& v1, const Q& v2 ) const
182 return v1.key() < v2;
185 template <typename Q>
186 bool operator ()( const Q& v1, int_item const& v2 ) const
188 return v1 < v2.key();
193 int operator ()( int_item const& v1, int_item const& v2 ) const
195 if ( v1.key() < v2.key())
197 return v1.key() > v2.key() ? 1 : 0;
200 template <typename T>
201 int operator ()( T const& v1, int v2 ) const
205 return v1.key() > v2 ? 1 : 0;
208 template <typename T>
209 int operator ()( int v1, T const& v2 ) const
213 return v1 > v2.key() ? 1 : 0;
218 template <typename Q, typename T>
219 bool operator()( Q const& lhs, T const& rhs ) const
221 return lhs.key() < rhs.key();
226 template <typename Set>
229 // Precondition: set is empty
230 // Postcondition: set is empty
232 ASSERT_TRUE( s.empty());
233 ASSERT_CONTAINER_SIZE( s, 0 );
234 size_t const nSetSize = kSize;
236 typedef typename Set::value_type value_type;
238 std::vector< value_type > data;
239 std::vector< size_t> indices;
240 data.reserve( kSize );
241 indices.reserve( kSize );
242 for ( size_t key = 0; key < kSize; ++key ) {
243 data.push_back( value_type( static_cast<int>(key)));
244 indices.push_back( key );
246 shuffle( indices.begin(), indices.end());
249 for ( auto idx : indices ) {
252 ASSERT_FALSE( s.contains( i.nKey ) != s.end());
253 ASSERT_FALSE( s.contains( i ) != s.end());
254 ASSERT_FALSE( s.contains( other_item( i.key()), other_less()) != s.end());
256 std::pair<typename Set::iterator, bool> updResult;
259 updResult = s.update( i.key(), false );
260 EXPECT_TRUE( updResult.first == s.end());
261 EXPECT_FALSE( updResult.second );
263 typename Set::iterator it;
267 ASSERT_TRUE( it != s.end());
268 it->nFindCount = it->nKey;
269 ASSERT_TRUE( s.insert( i ) == s.end());
270 updResult = s.update( i, false );
271 EXPECT_TRUE( updResult.first == it );
272 EXPECT_FALSE( updResult.second );
275 it = s.insert( i.key());
276 ASSERT_TRUE( it != s.end());
277 ASSERT_TRUE( s.insert( i.key()) == s.end());
278 it->nFindCount = it->nKey;
279 updResult = s.update( i.key(), false );
280 EXPECT_TRUE( updResult.first == it );
281 EXPECT_FALSE( updResult.second );
284 updResult = s.update( i );
285 EXPECT_TRUE( updResult.first != s.end());
286 EXPECT_TRUE( updResult.second );
287 updResult.first->nFindCount = updResult.first->nKey;
290 updResult = s.update( i.key());
291 EXPECT_TRUE( updResult.first != s.end());
292 EXPECT_TRUE( updResult.second );
293 updResult.first->nFindCount = updResult.first->nKey;
296 it = s.emplace( i.key());
297 ASSERT_TRUE( it != s.end());
298 it->nFindCount = it->nKey;
299 ASSERT_FALSE( s.emplace( i.key()) != s.end());
303 it = s.emplace( i.key(), std::move( str ));
304 ASSERT_TRUE( it != s.end());
305 EXPECT_TRUE( str.empty());
306 it->nFindCount = it->nKey;
308 ASSERT_TRUE( s.emplace( i.key(), std::move( str )) == s.end());
309 EXPECT_TRUE( str.empty()); // yes, that's is :(
312 // forgot anything?..
313 ASSERT_TRUE( false );
316 it = s.contains( i.nKey );
317 ASSERT_TRUE( it != s.end());
318 EXPECT_EQ( it->nFindCount, static_cast<unsigned>( it->nKey ));
319 ASSERT_TRUE( s.contains( i ) != s.end());
320 ASSERT_TRUE( s.contains( other_item( i.key()), other_less()) != s.end());
323 ASSERT_FALSE( s.empty());
324 ASSERT_CONTAINER_SIZE( s, nSetSize );
327 for ( auto it = s.begin(); it != s.end(); ++it ) {
328 it->nFindCount = it->key() * 3;
331 for ( auto it = s.cbegin(); it != s.cend(); ++it ) {
332 EXPECT_EQ( it->nFindCount, static_cast<unsigned>( it->key() * 3 ));
339 ASSERT_TRUE( s.empty());
340 ASSERT_CONTAINER_SIZE( s, 0 );
342 ASSERT_TRUE( s.begin() == s.end());
343 ASSERT_TRUE( s.cbegin() == s.cend());
347 } // namespace cds_test
349 #endif // CDSUNIT_SET_TEST_SET_NOGC_H