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 <gtest/gtest.h>
32 #include <cds/container/fcstack.h>
38 class FCStack : public ::testing::Test
41 template <class Stack>
44 typedef typename Stack::value_type value_type;
48 ASSERT_TRUE( stack.empty());
49 ASSERT_EQ( stack.size(), 0u );
51 ASSERT_TRUE( stack.push( 1 ));
52 ASSERT_TRUE( !stack.empty());
53 ASSERT_EQ( stack.size(), 1u );
54 ASSERT_TRUE( stack.push( 2 ));
55 ASSERT_TRUE( !stack.empty());
56 ASSERT_EQ( stack.size(), 2u );
57 ASSERT_TRUE( stack.push( 3 ));
58 ASSERT_TRUE( !stack.empty());
59 ASSERT_EQ( stack.size(), 3u );
61 ASSERT_TRUE( stack.pop( v ));
62 EXPECT_EQ( v, value_type( 3 ));
63 ASSERT_TRUE( !stack.empty());
64 ASSERT_EQ( stack.size(), 2u );
65 ASSERT_TRUE( stack.pop( v ));
66 EXPECT_EQ( v, value_type( 2 ));
67 ASSERT_TRUE( !stack.empty());
68 ASSERT_EQ( stack.size(), 1u );
69 ASSERT_TRUE( stack.pop( v ));
70 EXPECT_EQ( v, value_type( 1 ));
71 ASSERT_TRUE( stack.empty());
72 ASSERT_EQ( stack.size(), 0u );
74 ASSERT_TRUE( !stack.pop( v ));
75 EXPECT_EQ( v, value_type( 1000 ));
76 ASSERT_TRUE( stack.empty());
77 ASSERT_EQ( stack.size(), 0u );
79 ASSERT_TRUE( stack.push( 10 ));
80 ASSERT_TRUE( stack.push( 20 ));
81 ASSERT_TRUE( stack.push( 30 ));
82 ASSERT_TRUE( !stack.empty());
83 ASSERT_EQ( stack.size(), 3u );
85 while ( stack.pop( v ));
87 ASSERT_TRUE( stack.empty());
88 ASSERT_EQ( stack.size(), 0u );
92 TEST_F( FCStack, default_stack )
94 typedef cds::container::FCStack< unsigned int > stack_type;
98 TEST_F( FCStack, deque_based )
100 typedef cds::container::FCStack< unsigned int, std::stack<unsigned int, std::deque<unsigned int>>> stack_type;
104 TEST_F( FCStack, deque_empty_wait_strategy )
106 struct stack_traits: public
107 cds::container::fcstack::make_traits <
108 cds::opt::wait_strategy<cds::algo::flat_combining::wait_strategy::empty>
111 typedef cds::container::FCStack< unsigned int, std::stack<unsigned int, std::deque<unsigned int>>, stack_traits > stack_type;
115 TEST_F( FCStack, deque_single_mutex_single_condvar )
117 struct stack_traits: public
118 cds::container::fcstack::make_traits <
119 cds::opt::wait_strategy<cds::algo::flat_combining::wait_strategy::single_mutex_single_condvar<>>
122 typedef cds::container::FCStack< unsigned int, std::stack<unsigned int, std::deque<unsigned int>>, stack_traits > stack_type;
126 TEST_F( FCStack, deque_single_mutex_multi_condvar )
128 struct stack_traits: public
129 cds::container::fcstack::make_traits <
130 cds::opt::wait_strategy<cds::algo::flat_combining::wait_strategy::single_mutex_multi_condvar<>>
133 typedef cds::container::FCStack< unsigned int, std::stack<unsigned int, std::deque<unsigned int>>, stack_traits > stack_type;
137 TEST_F( FCStack, deque_multi_mutex_multi_condvar )
139 struct stack_traits: public
140 cds::container::fcstack::make_traits <
141 cds::opt::wait_strategy<cds::algo::flat_combining::wait_strategy::multi_mutex_multi_condvar<>>
144 typedef cds::container::FCStack< unsigned int, std::stack<unsigned int, std::deque<unsigned int>>, stack_traits > stack_type;
148 TEST_F( FCStack, deque_single_mutex_single_condvar_2ms )
150 struct stack_traits: public
151 cds::container::fcstack::make_traits <
152 cds::opt::wait_strategy<cds::algo::flat_combining::wait_strategy::single_mutex_single_condvar<2>>
155 typedef cds::container::FCStack< unsigned int, std::stack<unsigned int, std::deque<unsigned int>>, stack_traits > stack_type;
159 TEST_F( FCStack, deque_single_mutex_multi_condvar_2ms )
161 struct stack_traits: public
162 cds::container::fcstack::make_traits <
163 cds::opt::wait_strategy<cds::algo::flat_combining::wait_strategy::single_mutex_multi_condvar<2>>
166 typedef cds::container::FCStack< unsigned int, std::stack<unsigned int, std::deque<unsigned int>>, stack_traits > stack_type;
170 TEST_F( FCStack, deque_multi_mutex_multi_condvar_3ms )
172 struct stack_traits: public
173 cds::container::fcstack::make_traits <
174 cds::opt::wait_strategy<cds::algo::flat_combining::wait_strategy::multi_mutex_multi_condvar<3>>
177 typedef cds::container::FCStack< unsigned int, std::stack<unsigned int, std::deque<unsigned int>>, stack_traits > stack_type;
181 TEST_F( FCStack, deque_elimination )
183 struct stack_traits : public
184 cds::container::fcstack::make_traits <
185 cds::opt::enable_elimination < true >
188 typedef cds::container::FCStack< unsigned int, std::stack<unsigned int, std::deque<unsigned int>>, stack_traits > stack_type;
192 TEST_F( FCStack, vector_based )
194 typedef cds::container::FCStack< unsigned int, std::stack<unsigned int, std::vector<unsigned int>>> stack_type;
198 TEST_F( FCStack, vector_empty_wait_strategy )
200 typedef cds::container::FCStack< unsigned int, std::stack<unsigned int, std::vector<unsigned int>>,
201 cds::container::fcstack::make_traits<
202 cds::opt::wait_strategy< cds::algo::flat_combining::wait_strategy::empty >
208 TEST_F( FCStack, vector_multi_mutex_multi_condvar )
210 typedef cds::container::FCStack< unsigned int, std::stack<unsigned int, std::vector<unsigned int>>,
211 cds::container::fcstack::make_traits<
212 cds::opt::wait_strategy< cds::algo::flat_combining::wait_strategy::multi_mutex_multi_condvar<>>
218 TEST_F( FCStack, vector_single_mutex_multi_condvar )
220 typedef cds::container::FCStack< unsigned int, std::stack<unsigned int, std::vector<unsigned int>>,
221 cds::container::fcstack::make_traits<
222 cds::opt::wait_strategy< cds::algo::flat_combining::wait_strategy::single_mutex_multi_condvar<>>
228 TEST_F( FCStack, vector_single_mutex_single_condvar )
230 typedef cds::container::FCStack< unsigned int, std::stack<unsigned int, std::vector<unsigned int>>,
231 cds::container::fcstack::make_traits<
232 cds::opt::wait_strategy< cds::algo::flat_combining::wait_strategy::single_mutex_single_condvar<>>
238 TEST_F( FCStack, vector_elimination )
240 typedef cds::container::FCStack< unsigned int, std::stack<unsigned int, std::vector<unsigned int>>,
241 cds::container::fcstack::make_traits<
242 cds::opt::enable_elimination< true >
248 TEST_F( FCStack, list_based )
250 typedef cds::container::FCStack< unsigned int, std::stack<unsigned int, std::list<unsigned int>>> stack_type;
254 TEST_F( FCStack, list_empty_wait_strategy )
256 typedef cds::container::FCStack< unsigned int, std::stack<unsigned int, std::list<unsigned int>>,
257 cds::container::fcstack::make_traits<
258 cds::opt::wait_strategy< cds::algo::flat_combining::wait_strategy::empty >
264 TEST_F( FCStack, list_single_mutex_single_condvar )
266 typedef cds::container::FCStack< unsigned int, std::stack<unsigned int, std::list<unsigned int>>,
267 cds::container::fcstack::make_traits<
268 cds::opt::wait_strategy< cds::algo::flat_combining::wait_strategy::single_mutex_single_condvar<>>
274 TEST_F( FCStack, list_single_mutex_multi_condvar )
276 typedef cds::container::FCStack< unsigned int, std::stack<unsigned int, std::list<unsigned int>>,
277 cds::container::fcstack::make_traits<
278 cds::opt::wait_strategy< cds::algo::flat_combining::wait_strategy::single_mutex_multi_condvar<>>
284 TEST_F( FCStack, list_multi_mutex_multi_condvar )
286 typedef cds::container::FCStack< unsigned int, std::stack<unsigned int, std::list<unsigned int>>,
287 cds::container::fcstack::make_traits<
288 cds::opt::wait_strategy< cds::algo::flat_combining::wait_strategy::multi_mutex_multi_condvar<>>
294 TEST_F( FCStack, list_elimination )
296 typedef cds::container::FCStack< unsigned int, std::stack<unsigned int, std::list<unsigned int>>,
297 cds::container::fcstack::make_traits<
298 cds::opt::enable_elimination< true >
299 , cds::opt::wait_strategy< cds::algo::flat_combining::wait_strategy::multi_mutex_multi_condvar<2>>