fixed adding file problem
[c11concurrency-benchmarks.git] / gdax-orderbook-hpp / demo / dependencies / libcds-2.3.2 / test / unit / deque / fcdeque.cpp
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 #include <cds_test/ext_gtest.h>
32 #include <cds/container/fcdeque.h>
33 #include <boost/container/deque.hpp>
34
35 namespace {
36
37     class FCDeque: public ::testing::Test
38     {
39     protected:
40         template <class Deque>
41         void test( Deque& dq )
42         {
43             size_t const c_nSize = 100;
44             int total_sum = 0;
45
46             // push_front/pop_front
47             for ( int i = 0; i < static_cast<int>( c_nSize ); ++i ) {
48                 EXPECT_TRUE( dq.push_front( i ) );
49                 total_sum += i;
50             }
51             EXPECT_EQ( dq.size(), c_nSize );
52
53             int sum = 0;
54             dq.apply( [&sum]( typename Deque::deque_type const& d )
55                 {
56                     for ( auto const& el : d )
57                         sum += el;
58                 }
59             );
60             EXPECT_EQ( sum, total_sum );
61
62             size_t nCount = 0;
63             int val;
64             while ( !dq.empty()) {
65                 EXPECT_TRUE( dq.pop_front( val ));
66                 ++nCount;
67                 EXPECT_EQ( static_cast<int>(c_nSize - nCount), val );
68             }
69             EXPECT_EQ( nCount, c_nSize );
70
71             // push_back/pop_back
72             for ( int i = 0; i < static_cast<int>( c_nSize ); ++i )
73                 EXPECT_TRUE( dq.push_back( i ));
74             EXPECT_EQ( dq.size(), c_nSize );
75
76             nCount = 0;
77             while ( !dq.empty()) {
78                 EXPECT_TRUE( dq.pop_back( val ));
79                 ++nCount;
80                 EXPECT_EQ( static_cast<int>(c_nSize - nCount), val );
81             }
82             EXPECT_EQ( nCount, c_nSize );
83
84             // push_back/pop_front
85             for ( int i = 0; i < static_cast<int>( c_nSize ); ++i )
86                 EXPECT_TRUE( dq.push_back( i ));
87             EXPECT_EQ( dq.size(), c_nSize );
88
89             nCount = 0;
90             while ( !dq.empty()) {
91                 EXPECT_TRUE( dq.pop_front( val ));
92                 EXPECT_EQ( static_cast<int>( nCount ), val );
93                 ++nCount;
94             }
95             EXPECT_EQ( nCount, c_nSize );
96
97             // push_front/pop_back
98             for ( int i = 0; i < static_cast<int>( c_nSize ); ++i )
99                 EXPECT_TRUE( dq.push_front( i ));
100             EXPECT_EQ( dq.size(), c_nSize );
101
102             nCount = 0;
103             while ( !dq.empty()) {
104                 EXPECT_TRUE( dq.pop_back( val ));
105                 EXPECT_EQ( static_cast<int>( nCount ), val );
106                 ++nCount;
107             }
108             EXPECT_EQ( nCount, c_nSize );
109
110             // clear
111             for ( int i = 0; i < static_cast<int>( c_nSize ); ++i )
112                 EXPECT_TRUE( dq.push_front( i ));
113             EXPECT_EQ( dq.size(), c_nSize );
114
115             EXPECT_FALSE( dq.empty());
116             dq.clear();
117             EXPECT_TRUE( dq.empty());
118         }
119     };
120
121     TEST_F( FCDeque, std )
122     {
123         typedef cds::container::FCDeque<int> deque_type;
124
125         deque_type dq;
126         test( dq );
127     }
128
129     TEST_F( FCDeque, std_empty_wait_strategy )
130     {
131         typedef cds::container::FCDeque<int, std::deque<int>,
132             cds::container::fcdeque::make_traits<
133                 cds::opt::wait_strategy< cds::algo::flat_combining::wait_strategy::empty >
134             >::type
135         > deque_type;
136
137         deque_type dq;
138         test( dq );
139     }
140
141     TEST_F( FCDeque, std_multi_mutex_multi_condvar )
142     {
143         typedef cds::container::FCDeque<int, std::deque<int>,
144             cds::container::fcdeque::make_traits<
145                 cds::opt::wait_strategy< cds::algo::flat_combining::wait_strategy::multi_mutex_multi_condvar<>>
146             >::type
147         > deque_type;
148
149         deque_type dq;
150         test( dq );
151     }
152
153     TEST_F( FCDeque, std_elimination )
154     {
155         typedef cds::container::FCDeque<int, std::deque<int>,
156             cds::container::fcdeque::make_traits<
157                 cds::opt::enable_elimination< true >
158             >::type
159         > deque_type;
160
161         deque_type dq;
162         test( dq );
163     }
164
165     TEST_F( FCDeque, std_elimination_single_mutex_single_condvar )
166     {
167         typedef cds::container::FCDeque<int, std::deque<int>,
168             cds::container::fcdeque::make_traits<
169                 cds::opt::enable_elimination< true >
170                 , cds::opt::wait_strategy< cds::algo::flat_combining::wait_strategy::single_mutex_single_condvar<3>>
171             >::type
172         > deque_type;
173
174         deque_type dq;
175         test( dq );
176     }
177
178     TEST_F( FCDeque, std_statistics )
179     {
180         typedef cds::container::FCDeque<int, std::deque<int>,
181             cds::container::fcdeque::make_traits<
182                 cds::opt::stat< cds::container::fcdeque::stat<> >
183             >::type
184         > deque_type;
185
186         deque_type dq;
187         test( dq );
188     }
189
190     TEST_F( FCDeque, std_stat_single_mutex_multi_condvar )
191     {
192         typedef cds::container::FCDeque<int, std::deque<int>,
193             cds::container::fcdeque::make_traits<
194                 cds::opt::stat< cds::container::fcdeque::stat<> >
195                 , cds::opt::wait_strategy< cds::algo::flat_combining::wait_strategy::single_mutex_multi_condvar<2>>
196             >::type
197         > deque_type;
198
199         deque_type dq;
200         test( dq );
201     }
202
203     TEST_F( FCDeque, std_mutex )
204     {
205         struct deque_traits : public
206             cds::container::fcdeque::make_traits<
207                 cds::opt::enable_elimination< true >
208             >::type
209         {
210             typedef std::mutex lock_type;
211         };
212         typedef cds::container::FCDeque<int, std::deque<int>, deque_traits > deque_type;
213
214         deque_type dq;
215         test( dq );
216     }
217
218     TEST_F( FCDeque, boost )
219     {
220         typedef cds::container::FCDeque<int, boost::container::deque<int> > deque_type;
221
222         deque_type dq;
223         test( dq );
224     }
225
226     TEST_F( FCDeque, boost_empty_wait_strategy )
227     {
228         typedef cds::container::FCDeque<int, boost::container::deque<int>,
229             cds::container::fcdeque::make_traits<
230                 cds::opt::wait_strategy< cds::algo::flat_combining::wait_strategy::empty >
231             >::type
232         > deque_type;
233
234         deque_type dq;
235         test( dq );
236     }
237
238     TEST_F( FCDeque, boost_single_mutex_single_condvar )
239     {
240         typedef cds::container::FCDeque<int, boost::container::deque<int>,
241             cds::container::fcdeque::make_traits<
242                 cds::opt::wait_strategy< cds::algo::flat_combining::wait_strategy::single_mutex_single_condvar<>>
243             >::type
244         > deque_type;
245
246         deque_type dq;
247         test( dq );
248     }
249
250     TEST_F( FCDeque, boost_elimination )
251     {
252         typedef cds::container::FCDeque<int, boost::container::deque<int>,
253             cds::container::fcdeque::make_traits<
254                 cds::opt::enable_elimination< true >
255             >::type
256         > deque_type;
257
258         deque_type dq;
259         test( dq );
260     }
261
262     TEST_F( FCDeque, boost_elimination_single_mutex_multi_condvar )
263     {
264         typedef cds::container::FCDeque<int, boost::container::deque<int>,
265             cds::container::fcdeque::make_traits<
266                 cds::opt::enable_elimination< true >
267                 ,cds::opt::wait_strategy< cds::algo::flat_combining::wait_strategy::single_mutex_multi_condvar<5>>
268             >::type
269         > deque_type;
270
271         deque_type dq;
272         test( dq );
273     }
274
275     TEST_F( FCDeque, boost_statistics )
276     {
277         typedef cds::container::FCDeque<int, boost::container::deque<int>,
278             cds::container::fcdeque::make_traits<
279                 cds::opt::stat< cds::container::fcdeque::stat<> >
280             >::type
281         > deque_type;
282
283         deque_type dq;
284         test( dq );
285     }
286
287     TEST_F( FCDeque, boost_mutex )
288     {
289         typedef cds::container::FCDeque<int, boost::container::deque<int>,
290             cds::container::fcdeque::make_traits<
291                 cds::opt::enable_elimination< true >
292                 ,cds::opt::lock_type< std::mutex >
293             >::type
294         > deque_type;
295
296         deque_type dq;
297         test( dq );
298     }
299
300     TEST_F( FCDeque, boost_mutex_multi_mutex_multi_condvar )
301     {
302         typedef cds::container::FCDeque<int, boost::container::deque<int>,
303             cds::container::fcdeque::make_traits<
304                 cds::opt::enable_elimination< true >
305                 , cds::opt::lock_type< std::mutex >
306                 , cds::opt::wait_strategy< cds::algo::flat_combining::wait_strategy::multi_mutex_multi_condvar<>>
307             >::type
308         > deque_type;
309
310         deque_type dq;
311         test( dq );
312     }
313
314 } // namespace