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_H
32 #define CDSUNIT_SET_TEST_INTRUSIVE_SET_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: public fixture
53 unsigned int nDisposeCount ; // count of disposer calling
54 unsigned int nFindCount ; // count of find-functor calling
55 unsigned int nUpdateNewCount;
56 unsigned int nUpdateCount;
57 mutable unsigned int nEraseCount;
61 memset( this, 0, sizeof(*this));
65 template <typename Node>
77 explicit base_int_item( int key )
82 base_int_item(int key, int val)
87 base_int_item( base_int_item const& v )
100 template <typename Node>
101 struct member_int_item: public stat
113 explicit member_int_item( int key )
118 member_int_item(int key, int val)
123 member_int_item(member_int_item const& v )
136 size_t operator()( int i ) const
138 return co::v::hash<int>()( i );
140 template <typename Item>
141 size_t operator()( const Item& i ) const
143 return (*this)( i.key() );
147 struct simple_item_counter {
150 simple_item_counter()
169 operator size_t() const
177 template <typename T>
180 bool operator ()(const T& v1, const T& v2 ) const
182 return v1.key() < v2.key();
185 template <typename Q>
186 bool operator ()(const T& v1, const Q& v2 ) const
188 return v1.key() < v2;
191 template <typename Q>
192 bool operator ()(const Q& v1, const T& v2 ) const
194 return v1 < v2.key();
198 template <typename T>
200 int operator ()(const T& v1, const T& v2 ) const
202 if ( v1.key() < v2.key() )
204 return v1.key() > v2.key() ? 1 : 0;
207 template <typename Q>
208 int operator ()(const T& v1, const Q& v2 ) const
212 return v1.key() > v2 ? 1 : 0;
215 template <typename Q>
216 int operator ()(const Q& v1, const T& v2 ) const
220 return v1 > v2.key() ? 1 : 0;
227 explicit other_item( int k )
238 template <typename Q, typename T>
239 bool operator()( Q const& lhs, T const& rhs ) const
241 return lhs.key() < rhs.key();
247 template <typename T>
248 void operator ()( T * p )
256 template <typename Item, typename T>
257 void operator()( Item& item, T& /*val*/ )
263 struct insert_functor
265 template <typename Item>
266 void operator()(Item& item )
268 item.nVal = item.nKey * 100;
272 struct update_functor
274 template <typename Item>
275 void operator()( bool bNew, Item& item, Item& /*val*/ )
278 ++item.nUpdateNewCount;
286 template <typename Item>
287 void operator()( Item const& item )
296 // Precondition: set is empty
297 // Postcondition: set is empty
299 ASSERT_TRUE( s.empty() );
300 ASSERT_CONTAINER_SIZE( s, 0 );
302 typedef typename Set::value_type value_type;
304 std::vector< value_type > data;
305 size_t const kSize = 100;
306 data.reserve( kSize );
307 for ( size_t key = 0; key < kSize; ++key ) {
308 data.push_back( value_type( static_cast<int>( key )));
310 shuffle( data.begin(), data.end() );
313 for ( auto& i : data ) {
314 ASSERT_FALSE( s.contains( i.nKey ));
315 ASSERT_FALSE( s.contains( i ));
316 ASSERT_FALSE( s.contains( other_item( i.key()), other_less());
317 ASSERT_FALSE( s.find( i.nKey, []( value_type&, int ) {} );
318 ASSERT_FALSE( s.find_with( other_item( i.key()), other_less(), []( value_type&, other_item const& ) {} ));
323 } // namespace cds_test
325 #endif // #ifndef CDSUNIT_SET_TEST_INTRUSIVE_SET_H