Fixed minor gcc warnings
[libcds.git] / test / unit / stack / fcstack.cpp
1 /*
2     This file is a part of libcds - Concurrent Data Structures library
3
4     (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2016
5
6     Source code repo: http://github.com/khizmax/libcds/
7     Download: http://sourceforge.net/projects/libcds/files/
8
9     Redistribution and use in source and binary forms, with or without
10     modification, are permitted provided that the following conditions are met:
11
12     * Redistributions of source code must retain the above copyright notice, this
13       list of conditions and the following disclaimer.
14
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.
18
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.
29 */
30
31 #include <gtest/gtest.h>
32 #include <cds/container/fcstack.h>
33
34 #include <vector>
35 #include <list>
36
37 namespace {
38     class FCStack : public ::testing::Test
39     {
40     protected:
41         template <class Stack>
42         void test()
43         {
44             typedef typename Stack::value_type  value_type;
45             Stack stack;
46             value_type v;
47
48             ASSERT_TRUE( stack.empty() );
49             ASSERT_EQ( stack.size(), 0u );
50
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 );
60
61             ASSERT_TRUE( stack.pop( v ) );
62             EXPECT_EQ( v, 3 );
63             ASSERT_TRUE( !stack.empty() );
64             ASSERT_EQ( stack.size(), 2u );
65             ASSERT_TRUE( stack.pop( v ) );
66             EXPECT_EQ( v, 2 );
67             ASSERT_TRUE( !stack.empty() );
68             ASSERT_EQ( stack.size(), 1u );
69             ASSERT_TRUE( stack.pop( v ) );
70             EXPECT_EQ( v, 1 );
71             ASSERT_TRUE( stack.empty() );
72             ASSERT_EQ( stack.size(), 0u );
73             v = 1000;
74             ASSERT_TRUE( !stack.pop( v ) );
75             EXPECT_EQ( v, 1000 );
76             ASSERT_TRUE( stack.empty() );
77             ASSERT_EQ( stack.size(), 0u );
78
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 );
84
85             while ( stack.pop( v ) );
86
87             ASSERT_TRUE( stack.empty() );
88             ASSERT_EQ( stack.size(), 0u );
89         }
90     };
91
92     TEST_F( FCStack, default_stack )
93     {
94         typedef cds::container::FCStack< unsigned int > stack_type;
95         test<stack_type>();
96     }
97
98     TEST_F( FCStack, deque_based )
99     {
100         typedef cds::container::FCStack< unsigned int, std::stack<unsigned int, std::deque<unsigned int>>> stack_type;
101         test<stack_type>();
102     }
103
104     TEST_F( FCStack, deque_empty_wait_strategy )
105     {
106         struct stack_traits: public
107             cds::container::fcstack::make_traits <
108                 cds::opt::wait_strategy<cds::algo::flat_combining::wait_strategy::empty>
109             > ::type
110         {};
111         typedef cds::container::FCStack< unsigned int, std::stack<unsigned int, std::deque<unsigned int>>, stack_traits > stack_type;
112         test<stack_type>();
113     }
114
115     TEST_F( FCStack, deque_single_mutex_single_condvar )
116     {
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<>>
120             > ::type
121         {};
122         typedef cds::container::FCStack< unsigned int, std::stack<unsigned int, std::deque<unsigned int>>, stack_traits > stack_type;
123         test<stack_type>();
124     }
125
126     TEST_F( FCStack, deque_single_mutex_multi_condvar )
127     {
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<>>
131             > ::type
132         {};
133         typedef cds::container::FCStack< unsigned int, std::stack<unsigned int, std::deque<unsigned int>>, stack_traits > stack_type;
134         test<stack_type>();
135     }
136
137     TEST_F( FCStack, deque_multi_mutex_multi_condvar )
138     {
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<>>
142             > ::type
143         {};
144         typedef cds::container::FCStack< unsigned int, std::stack<unsigned int, std::deque<unsigned int>>, stack_traits > stack_type;
145         test<stack_type>();
146     }
147
148     TEST_F( FCStack, deque_single_mutex_single_condvar_2ms )
149     {
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>>
153             > ::type
154         {};
155         typedef cds::container::FCStack< unsigned int, std::stack<unsigned int, std::deque<unsigned int>>, stack_traits > stack_type;
156         test<stack_type>();
157     }
158
159     TEST_F( FCStack, deque_single_mutex_multi_condvar_2ms )
160     {
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>>
164             > ::type
165         {};
166         typedef cds::container::FCStack< unsigned int, std::stack<unsigned int, std::deque<unsigned int>>, stack_traits > stack_type;
167         test<stack_type>();
168     }
169
170     TEST_F( FCStack, deque_multi_mutex_multi_condvar_3ms )
171     {
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>>
175             > ::type
176         {};
177         typedef cds::container::FCStack< unsigned int, std::stack<unsigned int, std::deque<unsigned int>>, stack_traits > stack_type;
178         test<stack_type>();
179     }
180
181     TEST_F( FCStack, deque_elimination )
182     {
183         struct stack_traits : public
184             cds::container::fcstack::make_traits <
185             cds::opt::enable_elimination < true >
186             > ::type
187         {};
188         typedef cds::container::FCStack< unsigned int, std::stack<unsigned int, std::deque<unsigned int>>, stack_traits > stack_type;
189         test<stack_type>();
190     }
191
192     TEST_F( FCStack, vector_based )
193     {
194         typedef cds::container::FCStack< unsigned int, std::stack<unsigned int, std::vector<unsigned int>>> stack_type;
195         test<stack_type>();
196     }
197
198     TEST_F( FCStack, vector_empty_wait_strategy )
199     {
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 >
203             >::type
204         > stack_type;
205         test<stack_type>();
206     }
207
208     TEST_F( FCStack, vector_multi_mutex_multi_condvar )
209     {
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<>>
213             >::type
214         > stack_type;
215         test<stack_type>();
216     }
217
218     TEST_F( FCStack, vector_single_mutex_multi_condvar )
219     {
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<>>
223             >::type
224         > stack_type;
225         test<stack_type>();
226     }
227
228     TEST_F( FCStack, vector_single_mutex_single_condvar )
229     {
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<>>
233             >::type
234         > stack_type;
235         test<stack_type>();
236     }
237
238     TEST_F( FCStack, vector_elimination )
239     {
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 >
243             >::type
244         > stack_type;
245         test<stack_type>();
246     }
247
248     TEST_F( FCStack, list_based )
249     {
250         typedef cds::container::FCStack< unsigned int, std::stack<unsigned int, std::list<unsigned int>>> stack_type;
251         test<stack_type>();
252     }
253
254     TEST_F( FCStack, list_empty_wait_strategy )
255     {
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 >
259             >::type
260         > stack_type;
261         test<stack_type>();
262     }
263
264     TEST_F( FCStack, list_single_mutex_single_condvar )
265     {
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<>>
269             >::type
270         > stack_type;
271         test<stack_type>();
272     }
273
274     TEST_F( FCStack, list_single_mutex_multi_condvar )
275     {
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<>>
279             >::type
280         > stack_type;
281         test<stack_type>();
282     }
283
284     TEST_F( FCStack, list_multi_mutex_multi_condvar )
285     {
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<>>
289             >::type
290         > stack_type;
291         test<stack_type>();
292     }
293
294     TEST_F( FCStack, list_elimination )
295     {
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>>
300             >::type
301         > stack_type;
302         test<stack_type>();
303     }
304 } // namespace