2 This file is a part of libcds - Concurrent Data Structures library
4 (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2017
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_TREE_MAP_RCU_H
32 #define CDSUNIT_TREE_TEST_TREE_MAP_RCU_H
34 #include "test_tree_map.h"
38 class container_tree_map_rcu: public container_tree_map
40 typedef container_tree_map base_class;
46 // Precondition: map is empty
47 // Postcondition: map is empty
49 base_class::test( m );
51 ASSERT_TRUE( m.empty());
52 ASSERT_CONTAINER_SIZE( m, 0 );
54 size_t const kkSize = base_class::kSize;
56 std::vector<key_type> arrKeys;
57 for ( int i = 0; i < static_cast<int>(kkSize); ++i )
58 arrKeys.push_back( key_type( i ));
59 shuffle( arrKeys.begin(), arrKeys.end());
61 std::vector< value_type > arrVals;
62 for ( size_t i = 0; i < kkSize; ++i ) {
64 val.nVal = static_cast<int>( i );
65 val.strVal = std::to_string( i );
66 arrVals.push_back( val );
69 for ( auto const& i : arrKeys )
70 ASSERT_TRUE( m.insert( i ));
71 ASSERT_FALSE( m.empty());
72 ASSERT_CONTAINER_SIZE( m, kkSize );
74 typedef typename Map::exempt_ptr exempt_ptr;
75 typedef typename Map::value_type * raw_ptr;
76 typedef typename Map::rcu_lock rcu_lock;
79 shuffle( arrKeys.begin(), arrKeys.end());
82 for ( auto const& i : arrKeys ) {
83 value_type const& val = arrVals.at( i.nKey );
91 EXPECT_EQ( rp->first.nKey, i.nKey );
92 rp->second.nVal = rp->first.nKey * 2;
96 EXPECT_EQ( rp->first.nKey, i.nKey );
97 EXPECT_EQ( rp->second.nVal, rp->first.nKey * 2 );
98 rp->second.nVal = rp->first.nKey * 3;
100 rp = m.get_with( other_item( i.nKey ), other_less());
102 EXPECT_EQ( rp->first.nKey, i.nKey );
103 EXPECT_EQ( rp->second.nVal, rp->first.nKey * 3 );
104 rp->second.nVal = rp->first.nKey;
107 if ( Map::c_bExtractLockExternal ) {
111 switch ( i.nKey % 4 ) {
113 xp = m.extract( i.nKey );
119 xp = m.extract( val.strVal );
122 xp = m.extract_with( other_item( i.nKey ), other_less());
126 EXPECT_EQ( xp->first.nKey, i.nKey );
127 EXPECT_EQ( xp->second.nVal, xp->first.nKey );
134 switch ( i.nKey % 4 ) {
136 xp = m.extract( i.nKey );
142 xp = m.extract( val.strVal );
145 xp = m.extract_with( other_item( i.nKey ), other_less());
152 switch ( i.nKey % 4 ) {
154 xp = m.extract( i.nKey );
160 xp = m.extract( val.strVal );
163 xp = m.extract_with( other_item( i.nKey ), other_less());
167 EXPECT_EQ( xp->first.nKey, i.nKey );
168 EXPECT_EQ( xp->second.nVal, xp->first.nKey );
170 switch ( i.nKey % 4 ) {
172 xp = m.extract( i.nKey );
178 xp = m.extract( val.strVal );
181 xp = m.extract_with( other_item( i.nKey ), other_less());
191 rp = m.get( i.nKey );
195 rp = m.get_with( other_item( i.nKey ), other_less());
200 ASSERT_TRUE( m.empty());
201 ASSERT_CONTAINER_SIZE( m, 0 );
205 for ( auto const& i : arrKeys )
206 ASSERT_TRUE( m.insert( i ));
210 while ( !m.empty()) {
211 xp = m.extract_min();
213 EXPECT_EQ( xp->first.nKey, nKey + 1 );
214 nKey = xp->first.nKey;
217 xp = m.extract_min();
219 xp = m.extract_max();
221 EXPECT_EQ( kkSize, nCount );
222 ASSERT_TRUE( m.empty());
223 ASSERT_CONTAINER_SIZE( m, 0 );
226 xp = m.extract_min();
228 xp = m.extract_max();
231 for ( auto const& i : arrKeys )
232 ASSERT_TRUE( m.insert( i ));
236 while ( !m.empty()) {
237 xp = m.extract_max();
239 EXPECT_EQ( xp->first.nKey, nKey - 1 );
240 nKey = xp->first.nKey;
244 xp = m.extract_min();
246 xp = m.extract_max();
248 EXPECT_EQ( kkSize, nCount );
249 ASSERT_TRUE( m.empty());
250 ASSERT_CONTAINER_SIZE( m, 0 );
254 } // namespace cds_test
256 #endif // #ifndef CDSUNIT_TREE_TEST_TREE_MAP_RCU_H