Removed redundant spaces
[libcds.git] / test / unit / intrusive-set / test_intrusive_feldman_hashset_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-2016
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_FELDMAN_HASHSET_HP_H
32 #define CDSUNIT_SET_TEST_INTRUSIVE_FELDMAN_HASHSET_HP_H
33
34 #include "test_intrusive_feldman_hashset.h"
35
36 namespace cds_test {
37
38     class intrusive_feldman_hashset_hp: public intrusive_feldman_hashset
39     {
40         typedef intrusive_feldman_hashset base_class;
41
42     protected:
43
44         template <class Set>
45         void test( Set& s )
46         {
47             // Precondition: set is empty
48             // Postcondition: set is empty
49
50             base_class::test( s );
51
52             ASSERT_TRUE( s.empty());
53             ASSERT_CONTAINER_SIZE( s, 0 );
54
55             typedef typename Set::value_type value_type;
56             size_t const nSetSize = std::max( s.head_size() * 2, static_cast<size_t>( 100 ));
57
58             std::vector< value_type > data;
59             std::vector< size_t> indices;
60             data.reserve( nSetSize );
61             indices.reserve( nSetSize );
62             for ( size_t key = 0; key < nSetSize; ++key ) {
63                 data.push_back( value_type( static_cast<int>(key)) );
64                 indices.push_back( key );
65             }
66             shuffle( indices.begin(), indices.end());
67
68             typename Set::guarded_ptr gp;
69
70             // get/extract from empty set
71             for ( auto idx : indices ) {
72                 auto& i = data[idx];
73
74                 gp = s.get( i.key());
75                 ASSERT_TRUE( !gp );
76
77                 gp = s.extract( i.key());
78                 ASSERT_TRUE( !gp );
79             }
80
81             // fill set
82             for ( auto& i : data ) {
83                 i.nDisposeCount = 0;
84                 ASSERT_TRUE( s.insert( i ));
85             }
86
87             // get/extract
88             for ( auto idx : indices ) {
89                 auto& i = data[idx];
90
91                 EXPECT_EQ( i.nFindCount, 0u );
92                 gp = s.get( i.key());
93                 ASSERT_FALSE( !gp );
94                 ++gp->nFindCount;
95                 EXPECT_EQ( i.nFindCount, 1u );
96
97                 gp = s.extract( i.key());
98                 ASSERT_FALSE( !gp );
99                 ++gp->nEraseCount;
100                 EXPECT_EQ( i.nEraseCount, 1u );
101
102                 gp = s.extract( i.key());
103                 ASSERT_TRUE( !gp );
104
105                 gp = s.get( i.key());
106                 ASSERT_TRUE( !gp );
107             }
108             gp.release();
109
110             ASSERT_TRUE( s.empty());
111             ASSERT_CONTAINER_SIZE( s, 0 );
112
113             // Force retiring cycle
114             Set::gc::force_dispose();
115             for ( auto& i : data ) {
116                 EXPECT_EQ( i.nDisposeCount, 1u );
117             }
118
119             // erase_at( iterator )
120             for ( auto& i : data ) {
121                 i.clear_stat();
122                 ASSERT_TRUE( s.insert( i ));
123             }
124             ASSERT_FALSE( s.empty());
125             ASSERT_CONTAINER_SIZE( s, nSetSize );
126
127             for ( auto it = s.begin(); it != s.end(); ++it ) {
128                 ASSERT_TRUE( s.erase_at( it ));
129                 ASSERT_FALSE( s.erase_at( it ));
130             }
131             ASSERT_TRUE( s.empty());
132             ASSERT_CONTAINER_SIZE( s, 0 );
133
134             // Force retiring cycle
135             Set::gc::force_dispose();
136             for ( auto& i : data ) {
137                 EXPECT_EQ( i.nDisposeCount, 1u );
138             }
139
140             // erase_at( reverse_iterator )
141             for ( auto& i : data ) {
142                 i.clear_stat();
143                 ASSERT_TRUE( s.insert( i ));
144             }
145             ASSERT_FALSE( s.empty());
146             ASSERT_CONTAINER_SIZE( s, nSetSize );
147
148             for ( auto it = s.rbegin(); it != s.rend(); ++it ) {
149                 ASSERT_TRUE( s.erase_at( it ));
150                 ASSERT_FALSE( s.erase_at( it ));
151             }
152             ASSERT_TRUE( s.empty());
153             ASSERT_CONTAINER_SIZE( s, 0 );
154
155             // Force retiring cycle
156             Set::gc::force_dispose();
157             for ( auto& i : data ) {
158                 EXPECT_EQ( i.nDisposeCount, 1u );
159             }
160         }
161     };
162
163 } // namespace cds_test
164
165 #endif // #ifndef CDSUNIT_SET_TEST_INTRUSIVE_FELDMAN_HASHSET_HP_H