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 idx : indices ) {
102 ASSERT_TRUE( t.insert( i ) );
106 for ( auto idx : indices ) {
109 EXPECT_EQ( i.nFindCount, 0u );
113 EXPECT_EQ( i.nFindCount, 1u );
115 gp = t.get( i.key() );
118 EXPECT_EQ( i.nFindCount, 2u );
120 gp = t.get_with( other_item( i.key()), other_less());
123 EXPECT_EQ( i.nFindCount, 3u );
125 EXPECT_EQ( i.nEraseCount, 0u );
126 switch ( i.key() % 3 ) {
128 gp = t.extract( i.key());
134 gp = t.extract_with( other_item( i.key() ), other_less() );
139 EXPECT_EQ( i.nEraseCount, 1u );
143 gp = t.extract( i.key() );
145 gp = t.extract_with( other_item( i.key() ), other_less() );
151 ASSERT_TRUE( t.empty() );
152 ASSERT_CONTAINER_SIZE( t, 0u );
154 // Force retiring cycle
155 Tree::gc::force_dispose();
156 for ( auto& i : data ) {
157 EXPECT_EQ( i.nDisposeCount, 1u );
161 for ( auto idx : indices ) {
164 ASSERT_TRUE( t.insert( i ) );
169 while ( !t.empty() ) {
170 gp = t.extract_min();
172 EXPECT_EQ( gp->key(), nKey + 1 );
177 ASSERT_TRUE( t.empty() );
178 ASSERT_CONTAINER_SIZE( t, 0u );
179 EXPECT_EQ( nCount, data.size() );
181 // Force retiring cycle
182 Tree::gc::force_dispose();
183 for ( auto& i : data ) {
184 EXPECT_EQ( i.nDisposeCount, 1u );
188 for ( auto idx : indices ) {
191 ASSERT_TRUE( t.insert( i ) );
195 nKey = static_cast<int>( data.size());
196 while ( !t.empty() ) {
197 gp = t.extract_max();
199 EXPECT_EQ( gp->key(), nKey - 1 );
204 ASSERT_TRUE( t.empty() );
205 ASSERT_CONTAINER_SIZE( t, 0u );
206 EXPECT_EQ( nCount, data.size() );
208 // Force retiring cycle
209 Tree::gc::force_dispose();
210 for ( auto& i : data ) {
211 EXPECT_EQ( i.nDisposeCount, 1u );
217 } // namespace cds_test
219 #endif // #ifndef CDSUNIT_TREE_TEST_INTRUSIVE_TREE_HP_H