fixed adding file problem
[c11concurrency-benchmarks.git] / gdax-orderbook-hpp / demo / dependencies / libcds-2.3.2 / test / unit / map / test_map_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_MAP_TEST_MAP_RCU_H
32 #define CDSUNIT_MAP_TEST_MAP_RCU_H
33
34 #include "test_map.h"
35
36 namespace cds_test {
37
38     class container_map_rcu: public container_map
39     {
40         typedef container_map base_class;
41
42     protected:
43         template <class Map>
44         void test( Map& m )
45         {
46             // Precondition: map is empty
47             // Postcondition: map is empty
48
49             base_class::test( m );
50
51             ASSERT_TRUE( m.empty());
52             ASSERT_CONTAINER_SIZE( m, 0 );
53
54             size_t const kkSize = base_class::kSize;
55
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());
60
61             std::vector< value_type > arrVals;
62             for ( size_t i = 0; i < kkSize; ++i ) {
63                 value_type val;
64                 val.nVal = static_cast<int>( i );
65                 val.strVal = std::to_string( i );
66                 arrVals.push_back( val );
67             }
68
69             for ( auto const& i : arrKeys )
70                 ASSERT_TRUE( m.insert( i ));
71             ASSERT_FALSE( m.empty());
72             ASSERT_CONTAINER_SIZE( m, kkSize );
73
74             // iterators
75             size_t nCount = 0;
76             for ( auto it = m.begin(); it != m.end(); ++it ) {
77                 EXPECT_EQ( it->second.nVal, 0 );
78                 it->second.nVal = it->first.nKey * 2;
79                 ++nCount;
80             }
81             EXPECT_EQ( nCount, kkSize );
82
83             nCount = 0;
84             for ( auto it = m.cbegin(); it != m.cend(); ++it ) {
85                 EXPECT_EQ( it->second.nVal, it->first.nKey * 2 );
86                 ++nCount;
87             }
88             EXPECT_EQ( nCount, kkSize );
89
90             typedef typename Map::exempt_ptr exempt_ptr;
91             typedef typename Map::raw_ptr    raw_ptr;
92             typedef typename Map::rcu_lock   rcu_lock;
93
94             // get/extract
95             shuffle( arrKeys.begin(), arrKeys.end());
96
97             for ( auto const& i : arrKeys ) {
98                 value_type const& val = arrVals.at( i.nKey );
99
100                 {
101                     rcu_lock l;
102                     raw_ptr rp;
103
104                     rp = m.get( i.nKey );
105                     ASSERT_FALSE( !rp );
106                     EXPECT_EQ( rp->first.nKey, i.nKey );
107                     rp->second.nVal = rp->first.nKey * 2;
108
109                     rp = m.get( i );
110                     ASSERT_FALSE( !rp );
111                     EXPECT_EQ( rp->first.nKey, i.nKey );
112                     EXPECT_EQ( rp->second.nVal, rp->first.nKey * 2 );
113                     rp->second.nVal = rp->first.nKey * 3;
114
115                     rp = m.get_with( other_item( i.nKey ), other_less());
116                     ASSERT_FALSE( !rp );
117                     EXPECT_EQ( rp->first.nKey, i.nKey );
118                     EXPECT_EQ( rp->second.nVal, rp->first.nKey * 3 );
119                     rp->second.nVal = rp->first.nKey;
120                 }
121
122                 exempt_ptr xp;
123                 if ( Map::c_bExtractLockExternal ) {
124                     {
125                         rcu_lock l;
126
127                         switch ( i.nKey % 4 ) {
128                         case 0:
129                             xp = m.extract( i.nKey );
130                             break;
131                         case 1:
132                             xp = m.extract( i );
133                             break;
134                         case 2:
135                             xp = m.extract( val.strVal );
136                             break;
137                         case 3:
138                             xp = m.extract_with( other_item( i.nKey ), other_less());
139                             break;
140                         }
141                         ASSERT_FALSE( !xp );
142                         EXPECT_EQ( xp->first.nKey, i.nKey );
143                         EXPECT_EQ( xp->second.nVal, xp->first.nKey );
144                     }
145                     xp.release();
146
147                     {
148                         rcu_lock l;
149
150                         switch ( i.nKey % 4 ) {
151                         case 0:
152                             xp = m.extract( i.nKey );
153                             break;
154                         case 1:
155                             xp = m.extract( i );
156                             break;
157                         case 2:
158                             xp = m.extract( val.strVal );
159                             break;
160                         case 3:
161                             xp = m.extract_with( other_item( i.nKey ), other_less());
162                             break;
163                         }
164                         EXPECT_TRUE( !xp );
165                     }
166                 }
167                 else {
168                     switch ( i.nKey % 4 ) {
169                     case 0:
170                         xp = m.extract( i.nKey );
171                         break;
172                     case 1:
173                         xp = m.extract( i );
174                         break;
175                     case 2:
176                         xp = m.extract( val.strVal );
177                         break;
178                     case 3:
179                         xp = m.extract_with( other_item( i.nKey ), other_less());
180                         break;
181                     }
182                     ASSERT_FALSE( !xp );
183                     EXPECT_EQ( xp->first.nKey, i.nKey );
184                     EXPECT_EQ( xp->second.nVal, xp->first.nKey );
185
186                     switch ( i.nKey % 4 ) {
187                     case 0:
188                         xp = m.extract( i.nKey );
189                         break;
190                     case 1:
191                         xp = m.extract( i );
192                         break;
193                     case 2:
194                         xp = m.extract( val.strVal );
195                         break;
196                     case 3:
197                         xp = m.extract_with( other_item( i.nKey ), other_less());
198                         break;
199                     }
200                     EXPECT_TRUE( !xp );
201                 }
202
203                 {
204                     rcu_lock l;
205                     raw_ptr rp;
206
207                     rp = m.get( i.nKey );
208                     ASSERT_TRUE( !rp );
209                     rp = m.get( i );
210                     ASSERT_TRUE( !rp );
211                     rp = m.get_with( other_item( i.nKey ), other_less());
212                     ASSERT_TRUE( !rp );
213                 }
214             }
215
216             ASSERT_TRUE( m.empty());
217             ASSERT_CONTAINER_SIZE( m, 0 );
218         }
219     };
220
221 } // namespace cds_test
222
223 #endif // #ifndef CDSUNIT_MAP_TEST_MAP_H