2 This file is a part of libcds - Concurrent Data Structures library
4 (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2016
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 <gtest/gtest.h>
32 #include <cds/container/fcqueue.h>
38 class FCQueue: public ::testing::Test
41 template <class Queue>
44 typedef typename Queue::value_type value_type;
47 const size_t nSize = 100;
49 ASSERT_TRUE( q.empty() );
50 ASSERT_EQ( q.size(), 0 );
53 for ( size_t i = 0; i < nSize; ++i ) {
54 ASSERT_TRUE( q.enqueue( static_cast<value_type>(i)));
55 ASSERT_EQ( q.size(), i + 1 );
57 ASSERT_FALSE( q.empty() );
58 ASSERT_EQ( q.size(), nSize );
60 for ( size_t i = 0; i < nSize; ++i ) {
62 ASSERT_TRUE( q.dequeue( it ));
64 ASSERT_EQ( q.size(), nSize - i - 1 );
66 ASSERT_TRUE( q.empty() );
67 ASSERT_EQ( q.size(), 0 );
70 for ( size_t i = 0; i < nSize; ++i ) {
71 ASSERT_TRUE( q.push( static_cast<value_type>(i)));
72 ASSERT_EQ( q.size(), i + 1 );
74 ASSERT_FALSE( q.empty() );
75 ASSERT_EQ( q.size(), nSize );
77 for ( size_t i = 0; i < nSize; ++i ) {
79 ASSERT_TRUE( q.pop( it ) );
81 ASSERT_EQ( q.size(), nSize - i - 1 );
83 ASSERT_TRUE( q.empty() );
84 ASSERT_EQ( q.size(), 0 );
87 for ( size_t i = 0; i < nSize; ++i ) {
88 ASSERT_TRUE( q.push( static_cast<value_type>( i )));
90 ASSERT_FALSE( q.empty() );
91 ASSERT_EQ( q.size(), nSize );
94 ASSERT_TRUE( q.empty() );
95 ASSERT_EQ( q.size(), 0 );
97 // pop from empty queue
99 ASSERT_FALSE( q.pop( it ));
100 ASSERT_EQ( it, nSize * 2 );
101 ASSERT_TRUE( q.empty() );
102 ASSERT_EQ( q.size(), 0 );
104 ASSERT_FALSE( q.dequeue( it ) );
105 ASSERT_EQ( it, nSize * 2 );
106 ASSERT_TRUE( q.empty() );
107 ASSERT_EQ( q.size(), 0 );
110 template <class Queue>
111 void test_string( Queue& q )
117 const size_t nSize = sizeof( str ) / sizeof( str[0] );
120 for ( size_t i = 0; i < nSize; ++i ) {
121 std::string s = str[i];
122 ASSERT_FALSE( s.empty());
123 ASSERT_TRUE( q.enqueue( std::move( s )));
124 ASSERT_FALSE( s.empty());
125 ASSERT_EQ( q.size(), i + 1 );
127 ASSERT_FALSE( q.empty() );
128 ASSERT_EQ( q.size(), nSize );
130 for ( size_t i = 0; i < nSize; ++i ) {
132 ASSERT_TRUE( q.pop( s ));
133 ASSERT_EQ( q.size(), nSize - i - 1 );
134 ASSERT_EQ( s, str[i] );
136 ASSERT_TRUE( q.empty() );
137 ASSERT_EQ( q.size(), 0 );
141 TEST_F( FCQueue, std_deque )
143 typedef cds::container::FCQueue<int> queue_type;
149 TEST_F( FCQueue, std_deque_move )
151 typedef cds::container::FCQueue<std::string> queue_type;
157 TEST_F( FCQueue, std_empty_wait_strategy )
159 typedef cds::container::FCQueue<int, std::queue< int, std::deque<int>>,
160 cds::container::fcqueue::make_traits<
161 cds::opt::wait_strategy< cds::algo::flat_combining::wait_strategy::empty >
169 TEST_F( FCQueue, std_single_mutex_single_condvar )
171 typedef cds::container::FCQueue<int, std::queue< int, std::deque<int>>,
172 cds::container::fcqueue::make_traits<
173 cds::opt::wait_strategy< cds::algo::flat_combining::wait_strategy::single_mutex_single_condvar<>>
181 TEST_F( FCQueue, std_deque_elimination )
183 typedef cds::container::FCQueue<int, std::queue< int, std::deque<int>>,
184 cds::container::fcqueue::make_traits<
185 cds::opt::enable_elimination< true >
193 TEST_F( FCQueue, std_deque_elimination_single_mutex_multi_condvar )
195 typedef cds::container::FCQueue<int, std::queue< int, std::deque<int>>,
196 cds::container::fcqueue::make_traits<
197 cds::opt::enable_elimination< true >
198 , cds::opt::wait_strategy< cds::algo::flat_combining::wait_strategy::single_mutex_multi_condvar<2>>
206 TEST_F( FCQueue, std_deque_elimination_move )
208 typedef cds::container::FCQueue<std::string, std::queue< std::string, std::deque<std::string>>,
209 cds::container::fcqueue::make_traits<
210 cds::opt::enable_elimination< true >
218 TEST_F( FCQueue, std_deque_elimination_move_multi_mutex_multi_condvar )
220 typedef cds::container::FCQueue<std::string, std::queue< std::string, std::deque<std::string>>,
221 cds::container::fcqueue::make_traits<
222 cds::opt::enable_elimination< true >
223 , cds::opt::wait_strategy< cds::algo::flat_combining::wait_strategy::multi_mutex_multi_condvar<>>
231 TEST_F( FCQueue, std_deque_mutex )
233 typedef cds::container::FCQueue<int, std::queue< int, std::deque<int>>,
234 cds::container::fcqueue::make_traits<
235 cds::opt::lock_type< std::mutex >
243 TEST_F( FCQueue, std_list )
245 typedef cds::container::FCQueue<int, std::queue< int, std::list<int>>> queue_type;
251 TEST_F( FCQueue, std_list_move )
253 typedef cds::container::FCQueue<std::string, std::queue< std::string, std::list<std::string>>> queue_type;
259 TEST_F( FCQueue, std_list_empty_wait_strategy )
261 typedef cds::container::FCQueue<int, std::queue< int, std::list<int> >,
262 cds::container::fcqueue::make_traits<
263 cds::opt::wait_strategy< cds::algo::flat_combining::wait_strategy::empty >
271 TEST_F( FCQueue, std_list_single_mutex_single_condvar )
273 typedef cds::container::FCQueue<int, std::queue< int, std::list<int> >,
274 cds::container::fcqueue::make_traits<
275 cds::opt::wait_strategy< cds::algo::flat_combining::wait_strategy::single_mutex_single_condvar<5>>
283 TEST_F( FCQueue, std_list_elimination )
285 typedef cds::container::FCQueue<int, std::queue< int, std::list<int> >,
286 cds::container::fcqueue::make_traits<
287 cds::opt::enable_elimination< true >
295 TEST_F( FCQueue, std_list_elimination_multi_mutex_multi_condvar )
297 typedef cds::container::FCQueue<int, std::queue< int, std::list<int> >,
298 cds::container::fcqueue::make_traits<
299 cds::opt::enable_elimination< true >
300 ,cds::opt::wait_strategy< cds::algo::flat_combining::wait_strategy::multi_mutex_multi_condvar<5>>
308 TEST_F( FCQueue, std_list_elimination_move )
310 typedef cds::container::FCQueue<std::string, std::queue< std::string, std::list<std::string> >,
311 cds::container::fcqueue::make_traits<
312 cds::opt::enable_elimination< true >
320 TEST_F( FCQueue, std_list_mutex )
322 typedef cds::container::FCQueue<int, std::queue<int, std::list<int> >,
323 cds::container::fcqueue::make_traits<
324 cds::opt::lock_type< std::mutex >