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_TREE_TEST_INTRUSIVE_TREE_RCU_H
32 #define CDSUNIT_TREE_TEST_INTRUSIVE_TREE_RCU_H
34 #include "test_intrusive_tree.h"
37 // forward declaration
38 namespace cds { namespace intrusive {}}
42 namespace ci = cds::intrusive;
44 class intrusive_tree_rcu: public intrusive_tree
46 typedef intrusive_tree base_class;
54 // Precondition: tree is empty
55 // Postcondition: tree is empty
57 base_class::test( t );
59 ASSERT_TRUE( t.empty() );
60 ASSERT_CONTAINER_SIZE( t, 0 );
62 typedef typename Tree::value_type value_type;
63 typedef typename Tree::rcu_lock rcu_lock;
65 std::vector< value_type > data;
66 std::vector< size_t> indices;
67 data.reserve( kSize );
68 indices.reserve( kSize );
69 for ( size_t key = 0; key < kSize; ++key ) {
70 data.push_back( value_type( static_cast<int>(key) ) );
71 indices.push_back( key );
73 shuffle( indices.begin(), indices.end() );
75 typename Tree::exempt_ptr xp;
77 // get/extract from empty tree
78 for ( auto idx : indices ) {
83 value_type * p = t.get( i );
84 ASSERT_TRUE( p == nullptr );
87 p = t.get_with( other_item( i.key() ), other_less() );
88 ASSERT_TRUE( p == nullptr );
93 xp = t.extract( i.key());
95 xp = t.extract_with( other_item( i.key()), other_less());
100 xp = t.extract_max();
105 for ( auto idx : indices ) {
108 ASSERT_TRUE( t.insert( i ) );
112 for ( auto idx : indices ) {
119 EXPECT_EQ( i.nFindCount, 0u );
123 EXPECT_EQ( i.nFindCount, 1u );
125 p = t.get( i.key() );
128 EXPECT_EQ( i.nFindCount, 2u );
130 p = t.get_with( other_item( i.key() ), other_less() );
133 EXPECT_EQ( i.nFindCount, 3u );
136 EXPECT_EQ( i.nEraseCount, 0u );
137 switch ( i.key() % 3 ) {
139 xp = t.extract( i.key());
145 xp = t.extract_with( other_item( i.key() ), other_less() );
150 EXPECT_EQ( i.nEraseCount, 1u );
154 xp = t.extract( i.key() );
156 xp = t.extract_with( other_item( i.key() ), other_less() );
160 ASSERT_TRUE( t.empty() );
161 ASSERT_CONTAINER_SIZE( t, 0u );
163 // Force retiring cycle
164 Tree::gc::force_dispose();
165 for ( auto& i : data ) {
166 EXPECT_EQ( i.nDisposeCount, 1u );
170 for ( auto idx : indices ) {
173 ASSERT_TRUE( t.insert( i ) );
178 while ( !t.empty() ) {
179 xp = t.extract_min();
181 EXPECT_EQ( xp->key(), nKey + 1 );
186 ASSERT_TRUE( t.empty() );
187 ASSERT_CONTAINER_SIZE( t, 0u );
188 EXPECT_EQ( nCount, data.size() );
190 // Force retiring cycle
191 Tree::gc::force_dispose();
192 for ( auto& i : data ) {
193 EXPECT_EQ( i.nDisposeCount, 1u );
197 for ( auto idx : indices ) {
200 ASSERT_TRUE( t.insert( i ) );
204 nKey = static_cast<int>( data.size());
205 while ( !t.empty() ) {
206 xp = t.extract_max();
208 EXPECT_EQ( xp->key(), nKey - 1 );
213 ASSERT_TRUE( t.empty() );
214 ASSERT_CONTAINER_SIZE( t, 0u );
215 EXPECT_EQ( nCount, data.size() );
217 // Force retiring cycle
218 Tree::gc::force_dispose();
219 for ( auto& i : data ) {
220 EXPECT_EQ( i.nDisposeCount, 1u );
225 } // namespace cds_test
227 #endif // #ifndef CDSUNIT_TREE_TEST_INTRUSIVE_TREE_RCU_H