2 This file is a part of libcds - Concurrent Data Structures library
4 (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2016
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_INTRUSIVE_SET_NOGC_H
32 #define CDSUNIT_SET_TEST_INTRUSIVE_SET_NOGC_H
34 #include <cds_test/check_size.h>
35 #include <cds_test/fixture.h>
37 #include <cds/opt/hash.h>
38 #include <functional> // ref
40 // forward declaration
41 namespace cds { namespace intrusive {}}
45 namespace ci = cds::intrusive;
46 namespace co = cds::opt;
48 class intrusive_set_nogc: public fixture
51 static size_t const kSize = 100;
55 unsigned int nFindCount ; // count of find-functor calling
56 unsigned int nUpdateNewCount;
57 unsigned int nUpdateCount;
58 unsigned int nDisposeCount;
67 memset( this, 0, sizeof( *this ) );
71 template <typename Node>
83 explicit base_int_item( int key )
88 base_int_item(int key, int val)
93 base_int_item( base_int_item const& v )
106 template <typename Node>
107 struct member_int_item: public stat
119 explicit member_int_item( int key )
124 member_int_item(int key, int val)
129 member_int_item(member_int_item const& v )
142 size_t operator()( int i ) const
144 return co::v::hash<int>()( i );
146 template <typename Item>
147 size_t operator()( const Item& i ) const
149 return (*this)( i.key() );
153 struct simple_item_counter {
156 simple_item_counter()
175 operator size_t() const
183 template <typename T>
186 bool operator ()(const T& v1, const T& v2 ) const
188 return v1.key() < v2.key();
191 template <typename Q>
192 bool operator ()(const T& v1, const Q& v2 ) const
194 return v1.key() < v2;
197 template <typename Q>
198 bool operator ()(const Q& v1, const T& v2 ) const
200 return v1 < v2.key();
204 template <typename T>
206 int operator ()(const T& v1, const T& v2 ) const
208 if ( v1.key() < v2.key() )
210 return v1.key() > v2.key() ? 1 : 0;
213 template <typename Q>
214 int operator ()(const T& v1, const Q& v2 ) const
218 return v1.key() > v2 ? 1 : 0;
221 template <typename Q>
222 int operator ()(const Q& v1, const T& v2 ) const
226 return v1 > v2.key() ? 1 : 0;
233 explicit other_item( int k )
244 template <typename Q, typename T>
245 bool operator()( Q const& lhs, T const& rhs ) const
247 return lhs.key() < rhs.key();
253 template <typename T>
254 void operator ()( T * p )
264 // Precondition: set is empty
265 // Postcondition: set is empty
267 ASSERT_TRUE( s.empty() );
268 ASSERT_CONTAINER_SIZE( s, 0 );
269 size_t const nSetSize = kSize;
271 typedef typename Set::value_type value_type;
273 std::vector< value_type > data;
274 std::vector< size_t> indices;
275 data.reserve( kSize );
276 indices.reserve( kSize );
277 for ( size_t key = 0; key < kSize; ++key ) {
278 data.push_back( value_type( static_cast<int>( key )));
279 indices.push_back( key );
281 shuffle( indices.begin(), indices.end() );
284 for ( auto idx : indices ) {
285 auto& i = data[ idx ];
287 ASSERT_TRUE( s.contains( i.nKey ) == nullptr );
288 ASSERT_TRUE( s.contains( i ) == nullptr );
289 ASSERT_TRUE( s.contains( other_item( i.key()), other_less()) == nullptr );
290 ASSERT_FALSE( s.find( i.nKey, []( value_type&, int ) {} ));
291 ASSERT_FALSE( s.find_with( other_item( i.key()), other_less(), []( value_type&, other_item const& ) {} ));
293 std::pair<bool, bool> updResult;
295 updResult = s.update( i, []( bool, value_type&, value_type& )
297 ASSERT_TRUE( false );
299 EXPECT_FALSE( updResult.first );
300 EXPECT_FALSE( updResult.second );
302 switch ( i.key() % 2 ) {
304 ASSERT_TRUE( s.insert( i ));
305 ASSERT_FALSE( s.insert( i ));
306 EXPECT_EQ( i.nUpdateCount, 0 );
307 updResult = s.update( i, []( bool bNew, value_type& val, value_type& arg)
309 EXPECT_FALSE( bNew );
310 EXPECT_EQ( &val, &arg );
313 EXPECT_TRUE( updResult.first );
314 EXPECT_FALSE( updResult.second );
315 EXPECT_EQ( i.nUpdateCount, 1 );
318 EXPECT_EQ( i.nUpdateNewCount, 0 );
319 updResult = s.update( i, []( bool bNew, value_type& val, value_type& arg )
322 EXPECT_EQ( &val, &arg );
323 ++val.nUpdateNewCount;
325 EXPECT_TRUE( updResult.first );
326 EXPECT_TRUE( updResult.second );
327 EXPECT_EQ( i.nUpdateNewCount, 1 );
331 ASSERT_TRUE( s.contains( i.nKey ) == &i );
332 ASSERT_TRUE( s.contains( i ) == &i );
333 ASSERT_TRUE( s.contains( other_item( i.key() ), other_less()) == &i );
334 EXPECT_EQ( i.nFindCount, 0 );
335 ASSERT_TRUE( s.find( i.nKey, []( value_type& v, int ) { ++v.nFindCount; } ));
336 EXPECT_EQ( i.nFindCount, 1 );
337 ASSERT_TRUE( s.find_with( other_item( i.key() ), other_less(), []( value_type& v, other_item const& ) { ++v.nFindCount; } ));
338 EXPECT_EQ( i.nFindCount, 2 );
340 ASSERT_FALSE( s.empty() );
341 ASSERT_CONTAINER_SIZE( s, nSetSize );
343 std::for_each( data.begin(), data.end(), []( value_type& v ) { v.clear_stat(); });
346 for ( auto it = s.begin(); it != s.end(); ++it ) {
349 for ( auto it = s.cbegin(); it != s.cend(); ++it ) {
350 EXPECT_EQ( it->nFindCount, 1 );
352 for ( auto& i : data ) {
353 EXPECT_EQ( i.nFindCount, 1 );
357 for ( auto& i : data ) {
358 EXPECT_EQ( i.nDisposeCount, 0 );
361 for ( auto& i : data ) {
362 EXPECT_EQ( i.nDisposeCount, 1 );
365 ASSERT_TRUE( s.empty());
366 ASSERT_CONTAINER_SIZE( s, 0 );
367 ASSERT_TRUE( s.begin() == s.end() );
368 ASSERT_TRUE( s.cbegin() == s.cend() );
372 } // namespace cds_test
374 #endif // #ifndef CDSUNIT_SET_TEST_INTRUSIVE_SET_NOGC_H