fixed adding file problem
[c11concurrency-benchmarks.git] / gdax-orderbook-hpp / demo / dependencies / libcds-2.3.2 / test / unit / set / test_set_rcu.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_SET_RCU_H
32 #define CDSUNIT_SET_TEST_SET_RCU_H
33
34 #include "test_set.h"
35
36 namespace cds_test {
37
38     class container_set_rcu: public container_set
39     {
40         typedef container_set base_class;
41
42     protected:
43         template <typename Set>
44         void test( Set& s )
45         {
46             // Precondition: set is empty
47             // Postcondition: set is empty
48
49             ASSERT_TRUE( s.empty());
50             ASSERT_CONTAINER_SIZE( s, 0 );
51
52             base_class::test( s );
53
54             typedef typename Set::value_type value_type;
55
56             size_t const nSetSize = kSize;
57             std::vector< value_type > data;
58             std::vector< size_t> indices;
59             data.reserve( kSize );
60             indices.reserve( kSize );
61             for ( size_t key = 0; key < kSize; ++key ) {
62                 data.push_back( value_type( static_cast<int>(key)));
63                 indices.push_back( key );
64             }
65             shuffle( indices.begin(), indices.end());
66
67             for ( auto& i : data ) {
68                 ASSERT_TRUE( s.insert( i ));
69             }
70             ASSERT_FALSE( s.empty());
71             ASSERT_CONTAINER_SIZE( s, nSetSize );
72
73             // iterator test
74             for ( auto it = s.begin(); it != s.end(); ++it ) {
75                 it->nFindCount = it->key() * 3;
76             }
77
78             for ( auto it = s.cbegin(); it != s.cend(); ++it ) {
79                 EXPECT_EQ( it->nFindCount, static_cast<unsigned>( it->key() * 3 ));
80             }
81
82             typedef typename Set::exempt_ptr exempt_ptr;
83             typedef typename Set::raw_ptr    raw_ptr;
84             typedef typename Set::rcu_lock   rcu_lock;
85
86             // get()
87             for ( auto idx : indices ) {
88                 auto& i = data[idx];
89
90                 {
91                     rcu_lock l;
92                     raw_ptr  rp{};
93                     ASSERT_TRUE( !rp );
94                     switch ( idx % 3 ) {
95                     case 0:
96                         rp = s.get( i.key());
97                         ASSERT_FALSE( !rp );
98                         break;
99                     case 1:
100                         rp = s.get( i );
101                         ASSERT_FALSE( !rp );
102                         break;
103                     case 2:
104                         rp = s.get_with( other_item( i.key()), other_less());
105                         ASSERT_FALSE( !rp );
106                     }
107                     EXPECT_EQ( rp->key(), i.key());
108                     EXPECT_EQ( rp->nFindCount, static_cast<unsigned>( i.key() * 3 ));
109                     rp->nFindCount *= 2;
110                 }
111             }
112
113             // extract()
114             exempt_ptr xp;
115             for ( auto idx : indices ) {
116                 auto& i = data[idx];
117
118                 ASSERT_TRUE( !xp );
119                 if ( Set::c_bExtractLockExternal ) {
120                     {
121                         rcu_lock l;
122
123                         switch ( idx % 3 ) {
124                         case 0:
125                             xp = s.extract( i.key());
126                             ASSERT_FALSE( !xp );
127                             break;
128                         case 1:
129                             xp = s.extract( i );
130                             ASSERT_FALSE( !xp );
131                             break;
132                         case 2:
133                             xp = s.extract_with( other_item( i.key()), other_less());
134                             ASSERT_FALSE( !xp );
135                             break;
136                         }
137                         EXPECT_EQ( xp->key(), i.key());
138                         EXPECT_EQ( xp->nFindCount, static_cast<unsigned>( i.key() * 6 ));
139                     }
140                     xp.release();
141
142                     {
143                         rcu_lock l;
144
145                         switch ( idx % 3 ) {
146                         case 0:
147                             xp = s.extract( i.key());
148                             break;
149                         case 1:
150                             xp = s.extract( i );
151                             break;
152                         case 2:
153                             xp = s.extract_with( other_item( i.key()), other_less());
154                             break;
155                         }
156                         ASSERT_TRUE( !xp );
157                     }
158                 }
159                 else {
160                     switch ( idx % 3 ) {
161                     case 0:
162                         xp = s.extract( i.key());
163                         ASSERT_FALSE( !xp );
164                         break;
165                     case 1:
166                         xp = s.extract( i );
167                         ASSERT_FALSE( !xp );
168                         break;
169                     case 2:
170                         xp = s.extract_with( other_item( i.key()), other_less());
171                         ASSERT_FALSE( !xp );
172                         break;
173                     }
174                     EXPECT_EQ( xp->key(), i.key());
175                     EXPECT_EQ( xp->nFindCount, static_cast<unsigned>( i.key() * 6 ));
176
177                     switch ( idx % 3 ) {
178                     case 0:
179                         xp = s.extract( i.key());
180                         break;
181                     case 1:
182                         xp = s.extract( i );
183                         break;
184                     case 2:
185                         xp = s.extract_with( other_item( i.key()), other_less());
186                         break;
187                     }
188                     ASSERT_TRUE( !xp );
189                 }
190             }
191
192             ASSERT_TRUE( s.empty());
193             ASSERT_CONTAINER_SIZE( s, 0 );
194         }
195
196     };
197 } // namespace cds_test
198
199 #endif // CDSUNIT_SET_TEST_SET_RCU_H