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>
33 #include <test/include/cds_test/fc_hevy_value.h>
39 class FCQueue: public ::testing::Test
42 template <class Queue>
45 typedef typename Queue::value_type value_type;
48 const size_t nSize = 100;
50 ASSERT_TRUE( q.empty());
51 ASSERT_EQ( q.size(), 0u );
54 for ( size_t i = 0; i < nSize; ++i ) {
55 ASSERT_TRUE( q.enqueue( static_cast<value_type>(i)));
56 ASSERT_EQ( q.size(), i + 1 );
58 ASSERT_FALSE( q.empty());
59 ASSERT_EQ( q.size(), nSize );
61 for ( size_t i = 0; i < nSize; ++i ) {
63 ASSERT_TRUE( q.dequeue( it ));
64 ASSERT_EQ( it, static_cast<value_type>( i ));
65 ASSERT_EQ( q.size(), nSize - i - 1 );
67 ASSERT_TRUE( q.empty());
68 ASSERT_EQ( q.size(), 0u );
71 for ( size_t i = 0; i < nSize; ++i ) {
72 ASSERT_TRUE( q.push( static_cast<value_type>(i)));
73 ASSERT_EQ( q.size(), i + 1 );
75 ASSERT_FALSE( q.empty());
76 ASSERT_EQ( q.size(), nSize );
78 for ( size_t i = 0; i < nSize; ++i ) {
80 ASSERT_TRUE( q.pop( it ));
81 ASSERT_EQ( it, static_cast<value_type>( i ));
82 ASSERT_EQ( q.size(), nSize - i - 1 );
84 ASSERT_TRUE( q.empty());
85 ASSERT_EQ( q.size(), 0u );
88 for ( size_t i = 0; i < nSize; ++i ) {
89 ASSERT_TRUE( q.push( static_cast<value_type>( i )));
91 ASSERT_FALSE( q.empty());
92 ASSERT_EQ( q.size(), nSize );
95 ASSERT_TRUE( q.empty());
96 ASSERT_EQ( q.size(), 0u );
98 // pop from empty queue
100 ASSERT_FALSE( q.pop( it ));
101 ASSERT_EQ( it, static_cast<value_type>( nSize * 2 ));
102 ASSERT_TRUE( q.empty());
103 ASSERT_EQ( q.size(), 0u );
105 ASSERT_FALSE( q.dequeue( it ));
106 ASSERT_EQ( it, static_cast<value_type>( nSize * 2 ));
107 ASSERT_TRUE( q.empty());
108 ASSERT_EQ( q.size(), 0u );
111 template <class Queue>
112 void test_string( Queue& q )
118 const size_t nSize = sizeof( str ) / sizeof( str[0] );
121 for ( size_t i = 0; i < nSize; ++i ) {
122 std::string s = str[i];
123 ASSERT_FALSE( s.empty());
124 ASSERT_TRUE( q.enqueue( std::move( s )));
125 ASSERT_FALSE( s.empty());
126 ASSERT_EQ( q.size(), i + 1 );
128 ASSERT_FALSE( q.empty());
129 ASSERT_EQ( q.size(), nSize );
131 for ( size_t i = 0; i < nSize; ++i ) {
133 ASSERT_TRUE( q.pop( s ));
134 ASSERT_EQ( q.size(), nSize - i - 1 );
135 ASSERT_EQ( s, str[i] );
137 ASSERT_TRUE( q.empty());
138 ASSERT_EQ( q.size(), 0u );
142 TEST_F( FCQueue, std_deque )
144 typedef cds::container::FCQueue<int> queue_type;
150 TEST_F( FCQueue, std_deque_move )
152 typedef cds::container::FCQueue<std::string> queue_type;
158 TEST_F( FCQueue, std_empty_wait_strategy )
160 typedef cds::container::FCQueue<int, std::queue< int, std::deque<int>>,
161 cds::container::fcqueue::make_traits<
162 cds::opt::wait_strategy< cds::algo::flat_combining::wait_strategy::empty >
170 TEST_F( FCQueue, std_deque_heavy_value )
172 typedef fc_test::HeavyValue<> ValueType;
173 typedef cds::container::FCQueue<ValueType> queue_type;
179 TEST_F( FCQueue, std_empty_wait_strategy_heavy_value )
181 typedef fc_test::HeavyValue<> ValueType;
182 typedef cds::container::FCQueue<ValueType, std::queue< ValueType, std::deque<ValueType>>,
183 cds::container::fcqueue::make_traits<
184 cds::opt::wait_strategy< cds::algo::flat_combining::wait_strategy::empty >
192 TEST_F( FCQueue, std_single_mutex_single_condvar_heavy_value )
194 typedef fc_test::HeavyValue<> ValueType;
195 typedef cds::container::FCQueue<ValueType, std::queue< ValueType, std::deque<ValueType>>,
196 cds::container::fcqueue::make_traits<
197 cds::opt::wait_strategy< cds::algo::flat_combining::wait_strategy::single_mutex_single_condvar<> >
205 TEST_F( FCQueue, std_single_mutex_multi_condvar_heavy_value )
207 typedef fc_test::HeavyValue<> ValueType;
208 typedef cds::container::FCQueue<ValueType, std::queue< ValueType, std::deque<ValueType>>,
209 cds::container::fcqueue::make_traits<
210 cds::opt::wait_strategy< cds::algo::flat_combining::wait_strategy::single_mutex_multi_condvar<> >
218 TEST_F( FCQueue, std_multi_mutex_multi_condvar_heavy_value )
220 typedef fc_test::HeavyValue<> ValueType;
221 typedef cds::container::FCQueue<ValueType, std::queue< ValueType, std::deque<ValueType>>,
222 cds::container::fcqueue::make_traits<
223 cds::opt::wait_strategy< cds::algo::flat_combining::wait_strategy::multi_mutex_multi_condvar<> >
231 TEST_F( FCQueue, std_single_mutex_single_condvar )
233 typedef cds::container::FCQueue<int, std::queue< int, std::deque<int>>,
234 cds::container::fcqueue::make_traits<
235 cds::opt::wait_strategy< cds::algo::flat_combining::wait_strategy::single_mutex_single_condvar<>>
243 TEST_F( FCQueue, std_deque_elimination )
245 typedef cds::container::FCQueue<int, std::queue< int, std::deque<int>>,
246 cds::container::fcqueue::make_traits<
247 cds::opt::enable_elimination< true >
255 TEST_F( FCQueue, std_deque_elimination_single_mutex_multi_condvar )
257 typedef cds::container::FCQueue<int, std::queue< int, std::deque<int>>,
258 cds::container::fcqueue::make_traits<
259 cds::opt::enable_elimination< true >
260 , cds::opt::wait_strategy< cds::algo::flat_combining::wait_strategy::single_mutex_multi_condvar<2>>
268 TEST_F( FCQueue, std_deque_elimination_move )
270 typedef cds::container::FCQueue<std::string, std::queue< std::string, std::deque<std::string>>,
271 cds::container::fcqueue::make_traits<
272 cds::opt::enable_elimination< true >
280 TEST_F( FCQueue, std_deque_elimination_move_multi_mutex_multi_condvar )
282 typedef cds::container::FCQueue<std::string, std::queue< std::string, std::deque<std::string>>,
283 cds::container::fcqueue::make_traits<
284 cds::opt::enable_elimination< true >
285 , cds::opt::wait_strategy< cds::algo::flat_combining::wait_strategy::multi_mutex_multi_condvar<>>
293 TEST_F( FCQueue, std_deque_mutex )
295 typedef cds::container::FCQueue<int, std::queue< int, std::deque<int>>,
296 cds::container::fcqueue::make_traits<
297 cds::opt::lock_type< std::mutex >
305 TEST_F( FCQueue, std_list )
307 typedef cds::container::FCQueue<int, std::queue< int, std::list<int>>> queue_type;
313 TEST_F( FCQueue, std_list_move )
315 typedef cds::container::FCQueue<std::string, std::queue< std::string, std::list<std::string>>> queue_type;
321 TEST_F( FCQueue, std_list_empty_wait_strategy )
323 typedef cds::container::FCQueue<int, std::queue< int, std::list<int> >,
324 cds::container::fcqueue::make_traits<
325 cds::opt::wait_strategy< cds::algo::flat_combining::wait_strategy::empty >
333 TEST_F( FCQueue, std_list_single_mutex_single_condvar )
335 typedef cds::container::FCQueue<int, std::queue< int, std::list<int> >,
336 cds::container::fcqueue::make_traits<
337 cds::opt::wait_strategy< cds::algo::flat_combining::wait_strategy::single_mutex_single_condvar<5>>
345 TEST_F( FCQueue, std_list_elimination )
347 typedef cds::container::FCQueue<int, std::queue< int, std::list<int> >,
348 cds::container::fcqueue::make_traits<
349 cds::opt::enable_elimination< true >
357 TEST_F( FCQueue, std_list_elimination_multi_mutex_multi_condvar )
359 typedef cds::container::FCQueue<int, std::queue< int, std::list<int> >,
360 cds::container::fcqueue::make_traits<
361 cds::opt::enable_elimination< true >
362 ,cds::opt::wait_strategy< cds::algo::flat_combining::wait_strategy::multi_mutex_multi_condvar<5>>
370 TEST_F( FCQueue, std_list_elimination_move )
372 typedef cds::container::FCQueue<std::string, std::queue< std::string, std::list<std::string> >,
373 cds::container::fcqueue::make_traits<
374 cds::opt::enable_elimination< true >
382 TEST_F( FCQueue, std_list_mutex )
384 typedef cds::container::FCQueue<int, std::queue<int, std::list<int> >,
385 cds::container::fcqueue::make_traits<
386 cds::opt::lock_type< std::mutex >