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_HP_H
32 #define CDSUNIT_TREE_TEST_INTRUSIVE_TREE_HP_H
34 #include "test_intrusive_tree.h"
36 // forward declaration
37 namespace cds { namespace intrusive {}}
41 namespace ci = cds::intrusive;
43 class intrusive_tree_hp: public intrusive_tree
45 typedef intrusive_tree base_class;
52 // Precondition: tree is empty
53 // Postcondition: tree is empty
55 base_class::test( t );
57 ASSERT_TRUE( t.empty() );
58 ASSERT_CONTAINER_SIZE( t, 0 );
60 typedef typename Tree::value_type value_type;
62 std::vector< value_type > data;
63 std::vector< size_t> indices;
64 data.reserve( kSize );
65 indices.reserve( kSize );
66 for ( size_t key = 0; key < kSize; ++key ) {
67 data.push_back( value_type( static_cast<int>(key) ) );
68 indices.push_back( key );
70 shuffle( indices.begin(), indices.end() );
72 typename Tree::guarded_ptr gp;
74 // get/extract from empty tree
75 for ( auto idx : indices ) {
80 gp = t.get( i.key() );
82 gp = t.get_with( other_item( i.key()), other_less());
87 gp = t.extract( i.key());
89 gp = t.extract_with( other_item( i.key()), other_less());
99 for ( auto& i : data ) {
101 ASSERT_TRUE( t.insert( i ) );
105 for ( auto idx : indices ) {
108 EXPECT_EQ( i.nFindCount, 0 );
112 EXPECT_EQ( i.nFindCount, 1 );
114 gp = t.get( i.key() );
117 EXPECT_EQ( i.nFindCount, 2 );
119 gp = t.get_with( other_item( i.key()), other_less());
122 EXPECT_EQ( i.nFindCount, 3 );
124 EXPECT_EQ( i.nEraseCount, 0 );
125 switch ( i.key() % 3 ) {
127 gp = t.extract( i.key());
133 gp = t.extract_with( other_item( i.key() ), other_less() );
138 EXPECT_EQ( i.nEraseCount, 1 );
142 gp = t.extract( i.key() );
144 gp = t.extract_with( other_item( i.key() ), other_less() );
150 ASSERT_TRUE( t.empty() );
151 ASSERT_CONTAINER_SIZE( t, 0 );
153 // Force retiring cycle
154 Tree::gc::force_dispose();
155 for ( auto& i : data ) {
156 EXPECT_EQ( i.nDisposeCount, 1 );
160 for ( auto& i : data ) {
162 ASSERT_TRUE( t.insert( i ) );
167 while ( !t.empty() ) {
168 gp = t.extract_min();
170 EXPECT_EQ( gp->key(), nKey + 1 );
175 ASSERT_TRUE( t.empty() );
176 ASSERT_CONTAINER_SIZE( t, 0 );
177 EXPECT_EQ( nCount, data.size() );
179 // Force retiring cycle
180 Tree::gc::force_dispose();
181 for ( auto& i : data ) {
182 EXPECT_EQ( i.nDisposeCount, 1 );
186 for ( auto& i : data ) {
188 ASSERT_TRUE( t.insert( i ) );
192 nKey = static_cast<int>( data.size());
193 while ( !t.empty() ) {
194 gp = t.extract_max();
196 EXPECT_EQ( gp->key(), nKey - 1 );
201 ASSERT_TRUE( t.empty() );
202 ASSERT_CONTAINER_SIZE( t, 0 );
203 EXPECT_EQ( nCount, data.size() );
205 // Force retiring cycle
206 Tree::gc::force_dispose();
207 for ( auto& i : data ) {
208 EXPECT_EQ( i.nDisposeCount, 1 );
214 } // namespace cds_test
216 #endif // #ifndef CDSUNIT_TREE_TEST_INTRUSIVE_TREE_HP_H