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