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_deque_elimination )
159 typedef cds::container::FCQueue<int, std::queue< int, std::deque<int>>,
160 cds::container::fcqueue::make_traits<
161 cds::opt::enable_elimination< true >
169 TEST_F( FCQueue, std_deque_elimination_move )
171 typedef cds::container::FCQueue<std::string, std::queue< std::string, std::deque<std::string>>,
172 cds::container::fcqueue::make_traits<
173 cds::opt::enable_elimination< true >
181 TEST_F( FCQueue, std_deque_mutex )
183 typedef cds::container::FCQueue<int, std::queue< int, std::deque<int>>,
184 cds::container::fcqueue::make_traits<
185 cds::opt::lock_type< std::mutex >
193 TEST_F( FCQueue, std_list )
195 typedef cds::container::FCQueue<int, std::queue< int, std::list<int>>> queue_type;
201 TEST_F( FCQueue, std_list_move )
203 typedef cds::container::FCQueue<std::string, std::queue< std::string, std::list<std::string>>> queue_type;
209 TEST_F( FCQueue, std_list_elimination )
211 typedef cds::container::FCQueue<int, std::queue< int, std::list<int> >,
212 cds::container::fcqueue::make_traits<
213 cds::opt::enable_elimination< true >
221 TEST_F( FCQueue, std_list_elimination_move )
223 typedef cds::container::FCQueue<std::string, std::queue< std::string, std::list<std::string> >,
224 cds::container::fcqueue::make_traits<
225 cds::opt::enable_elimination< true >
233 TEST_F( FCQueue, std_list_mutex )
235 typedef cds::container::FCQueue<int, std::queue<int, std::list<int> >,
236 cds::container::fcqueue::make_traits<
237 cds::opt::lock_type< std::mutex >