2 This file is a part of libcds - Concurrent Data Structures library
4 (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2017
6 Source code repo: http://github.com/khizmax/libcds/
7 Download: http://sourceforge.net/projects/libcds/files/
9 Redistribution and use in source and binary forms, with or without
10 modification, are permitted provided that the following conditions are met:
12 * Redistributions of source code must retain the above copyright notice, this
13 list of conditions and the following disclaimer.
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.
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.
31 #include <cds_test/ext_gtest.h>
32 #include <cds/container/fcdeque.h>
33 #include <boost/container/deque.hpp>
37 class FCDeque: public ::testing::Test
40 template <class Deque>
41 void test( Deque& dq )
43 size_t const c_nSize = 100;
46 // push_front/pop_front
47 for ( int i = 0; i < static_cast<int>( c_nSize ); ++i ) {
48 EXPECT_TRUE( dq.push_front( i ) );
51 EXPECT_EQ( dq.size(), c_nSize );
54 dq.apply( [&sum]( typename Deque::deque_type const& d )
56 for ( auto const& el : d )
60 EXPECT_EQ( sum, total_sum );
64 while ( !dq.empty()) {
65 EXPECT_TRUE( dq.pop_front( val ));
67 EXPECT_EQ( static_cast<int>(c_nSize - nCount), val );
69 EXPECT_EQ( nCount, c_nSize );
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 );
77 while ( !dq.empty()) {
78 EXPECT_TRUE( dq.pop_back( val ));
80 EXPECT_EQ( static_cast<int>(c_nSize - nCount), val );
82 EXPECT_EQ( nCount, c_nSize );
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 );
90 while ( !dq.empty()) {
91 EXPECT_TRUE( dq.pop_front( val ));
92 EXPECT_EQ( static_cast<int>( nCount ), val );
95 EXPECT_EQ( nCount, c_nSize );
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 );
103 while ( !dq.empty()) {
104 EXPECT_TRUE( dq.pop_back( val ));
105 EXPECT_EQ( static_cast<int>( nCount ), val );
108 EXPECT_EQ( nCount, c_nSize );
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 );
115 EXPECT_FALSE( dq.empty());
117 EXPECT_TRUE( dq.empty());
121 TEST_F( FCDeque, std )
123 typedef cds::container::FCDeque<int> deque_type;
129 TEST_F( FCDeque, std_empty_wait_strategy )
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 >
141 TEST_F( FCDeque, std_multi_mutex_multi_condvar )
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<>>
153 TEST_F( FCDeque, std_elimination )
155 typedef cds::container::FCDeque<int, std::deque<int>,
156 cds::container::fcdeque::make_traits<
157 cds::opt::enable_elimination< true >
165 TEST_F( FCDeque, std_elimination_single_mutex_single_condvar )
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>>
178 TEST_F( FCDeque, std_statistics )
180 typedef cds::container::FCDeque<int, std::deque<int>,
181 cds::container::fcdeque::make_traits<
182 cds::opt::stat< cds::container::fcdeque::stat<> >
190 TEST_F( FCDeque, std_stat_single_mutex_multi_condvar )
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>>
203 TEST_F( FCDeque, std_mutex )
205 struct deque_traits : public
206 cds::container::fcdeque::make_traits<
207 cds::opt::enable_elimination< true >
210 typedef std::mutex lock_type;
212 typedef cds::container::FCDeque<int, std::deque<int>, deque_traits > deque_type;
218 TEST_F( FCDeque, boost )
220 typedef cds::container::FCDeque<int, boost::container::deque<int> > deque_type;
226 TEST_F( FCDeque, boost_empty_wait_strategy )
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 >
238 TEST_F( FCDeque, boost_single_mutex_single_condvar )
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<>>
250 TEST_F( FCDeque, boost_elimination )
252 typedef cds::container::FCDeque<int, boost::container::deque<int>,
253 cds::container::fcdeque::make_traits<
254 cds::opt::enable_elimination< true >
262 TEST_F( FCDeque, boost_elimination_single_mutex_multi_condvar )
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>>
275 TEST_F( FCDeque, boost_statistics )
277 typedef cds::container::FCDeque<int, boost::container::deque<int>,
278 cds::container::fcdeque::make_traits<
279 cds::opt::stat< cds::container::fcdeque::stat<> >
287 TEST_F( FCDeque, boost_mutex )
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 >
300 TEST_F( FCDeque, boost_mutex_multi_mutex_multi_condvar )
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<>>