Added erase_at( iterator ) function to MichaelHashSet/Map and SplitListSet/Map based...
[libcds.git] / test / unit / intrusive-set / test_intrusive_michael_iterable_hp.h
1 /*
2     This file is a part of libcds - Concurrent Data Structures library
3
4     (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2017
5
6     Source code repo: http://github.com/khizmax/libcds/
7     Download: http://sourceforge.net/projects/libcds/files/
8
9     Redistribution and use in source and binary forms, with or without
10     modification, are permitted provided that the following conditions are met:
11
12     * Redistributions of source code must retain the above copyright notice, this
13       list of conditions and the following disclaimer.
14
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.
18
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.
29 */
30
31 #ifndef CDSUNIT_SET_TEST_INTRUSIVE_MICHAEL_ITERABLE_HP_H
32 #define CDSUNIT_SET_TEST_INTRUSIVE_MICHAEL_ITERABLE_HP_H
33
34 #include "test_intrusive_michael_iterable.h"
35
36 // forward declaration
37 namespace cds { namespace intrusive {}}
38
39 namespace cds_test {
40
41     namespace ci = cds::intrusive;
42     namespace co = cds::opt;
43
44     class intrusive_set_hp: public intrusive_set
45     {
46         typedef intrusive_set base_class;
47
48     protected:
49
50         template <class Set>
51         void test( Set& s )
52         {
53             // Precondition: set is empty
54             // Postcondition: set is empty
55
56             base_class::test( s );
57
58             ASSERT_TRUE( s.empty());
59             ASSERT_CONTAINER_SIZE( s, 0 );
60
61             typedef typename Set::value_type value_type;
62
63             std::vector< value_type > data;
64             std::vector< size_t> indices;
65             data.reserve( kSize );
66             indices.reserve( kSize );
67             for ( size_t key = 0; key < kSize; ++key ) {
68                 data.push_back( value_type( static_cast<int>(key)));
69                 indices.push_back( key );
70             }
71             shuffle( indices.begin(), indices.end());
72
73             typename Set::guarded_ptr gp;
74
75             // get/extract from empty set
76             for ( auto idx : indices ) {
77                 auto& i = data[idx];
78
79                 gp = s.get( i );
80                 ASSERT_TRUE( !gp );
81                 gp = s.get( i.key());
82                 ASSERT_TRUE( !gp );
83                 gp = s.get_with( other_item( i.key()), other_less());
84                 ASSERT_TRUE( !gp );
85
86                 gp = s.extract( i );
87                 ASSERT_TRUE( !gp );
88                 gp = s.extract( i.key());
89                 ASSERT_TRUE( !gp );
90                 gp = s.extract_with( other_item( i.key()), other_less());
91                 ASSERT_TRUE( !gp );
92             }
93
94             // fill set
95             for ( auto& i : data ) {
96                 i.nDisposeCount = 0;
97                 ASSERT_TRUE( s.insert( i ));
98             }
99
100             // get/extract
101             for ( auto idx : indices ) {
102                 auto& i = data[idx];
103
104                 EXPECT_EQ( i.nFindCount, 0u );
105                 gp = s.get( i );
106                 ASSERT_FALSE( !gp );
107                 ++gp->nFindCount;
108                 EXPECT_EQ( i.nFindCount, 1u );
109
110                 gp = s.get( i.key());
111                 ASSERT_FALSE( !gp );
112                 ++gp->nFindCount;
113                 EXPECT_EQ( i.nFindCount, 2u );
114
115                 gp = s.get_with( other_item( i.key()), other_less());
116                 ASSERT_FALSE( !gp );
117                 ++gp->nFindCount;
118                 EXPECT_EQ( i.nFindCount, 3u );
119
120                 EXPECT_EQ( i.nEraseCount, 0u );
121                 switch ( i.key() % 3 ) {
122                 case 0:
123                     gp = s.extract( i.key());
124                     break;
125                 case 1:
126                     gp = s.extract( i );
127                     break;
128                 case 2:
129                     gp = s.extract_with( other_item( i.key()), other_less());
130                     break;
131                 }
132                 ASSERT_FALSE( !gp );
133                 ++gp->nEraseCount;
134                 EXPECT_EQ( i.nEraseCount, 1u );
135
136                 gp = s.extract( i );
137                 ASSERT_TRUE( !gp );
138                 gp = s.extract( i.key());
139                 ASSERT_TRUE( !gp );
140                 gp = s.extract_with( other_item( i.key()), other_less());
141                 ASSERT_TRUE( !gp );
142             }
143
144             gp.release();
145
146             ASSERT_TRUE( s.empty());
147             ASSERT_CONTAINER_SIZE( s, 0 );
148
149             // Force retiring cycle
150             Set::gc::force_dispose();
151             for ( auto& i : data ) {
152                 EXPECT_EQ( i.nDisposeCount, 1u );
153             }
154
155             // erase_at() test
156             for ( auto& i : data ) {
157                 i.nDisposeCount = 0;
158                 ASSERT_TRUE( s.insert( i ) );
159             }
160
161             for ( auto it = s.begin(); it != s.end(); ++it ) {
162                 EXPECT_TRUE( s.erase_at( it ));
163                 EXPECT_FALSE( s.erase_at( it ));
164             }
165             ASSERT_TRUE( s.empty() );
166             ASSERT_CONTAINER_SIZE( s, 0 );
167
168             // Force retiring cycle
169             Set::gc::force_dispose();
170             for ( auto& i : data ) {
171                 EXPECT_EQ( i.nDisposeCount, 1u );
172             }
173         }
174     };
175
176 } // namespace cds_test
177
178 #endif // #ifndef CDSUNIT_SET_TEST_INTRUSIVE_MICHAEL_ITERABLE_HP_H