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 <cds_test/ext_gtest.h>
32 #include <cds/intrusive/fcstack.h>
34 #include <boost/intrusive/list.hpp>
37 class IntrusiveFCStack : public ::testing::Test
40 template <typename Hook>
41 struct base_hook_item : public Hook
51 template <typename Hook>
52 struct member_hook_item
66 void operator ()( T * p )
72 template <class Stack>
75 typedef typename Stack::value_type value_type;
76 typedef typename Stack::container_type underlying_stack_type;
79 ASSERT_TRUE( stack.empty());
81 value_type v1, v2, v3;
85 ASSERT_TRUE( stack.push( v1 ));
86 ASSERT_TRUE( !stack.empty());
87 ASSERT_TRUE( stack.push( v2 ));
88 ASSERT_TRUE( !stack.empty());
89 ASSERT_TRUE( stack.push( v3 ));
90 ASSERT_TRUE( !stack.empty());
93 stack.apply( [&sum]( underlying_stack_type& stack )
95 for ( auto const& el : stack )
102 ASSERT_TRUE( pv != nullptr );
103 ASSERT_EQ( pv->nVal, 3 );
104 ASSERT_TRUE( !stack.empty());
106 ASSERT_TRUE( pv != nullptr );
107 ASSERT_EQ( pv->nVal, 2 );
108 ASSERT_TRUE( !stack.empty());
110 ASSERT_TRUE( pv != nullptr );
111 ASSERT_EQ( pv->nVal, 1 );
112 ASSERT_TRUE( stack.empty());
114 ASSERT_TRUE( pv == nullptr );
115 ASSERT_TRUE( stack.empty());
117 if ( !std::is_same<typename Stack::disposer, cds::intrusive::opt::v::empty_disposer>::value ) {
118 int v1disp = v1.nDisposeCount;
119 int v2disp = v2.nDisposeCount;
120 int v3disp = v3.nDisposeCount;
122 ASSERT_TRUE( stack.push( v1 ));
123 ASSERT_TRUE( stack.push( v2 ));
124 ASSERT_TRUE( stack.push( v3 ));
127 ASSERT_TRUE( stack.empty());
129 EXPECT_EQ( v1.nDisposeCount, v1disp);
130 EXPECT_EQ( v2.nDisposeCount, v2disp);
131 EXPECT_EQ( v3.nDisposeCount, v3disp);
133 ASSERT_TRUE( stack.push( v1 ));
134 ASSERT_TRUE( stack.push( v2 ));
135 ASSERT_TRUE( stack.push( v3 ));
136 ASSERT_TRUE( !stack.empty());
139 ASSERT_TRUE( stack.empty());
141 EXPECT_EQ( v1.nDisposeCount, v1disp + 1 );
142 EXPECT_EQ( v2.nDisposeCount, v2disp + 1 );
143 EXPECT_EQ( v3.nDisposeCount, v3disp + 1 );
148 TEST_F( IntrusiveFCStack, slist )
150 typedef base_hook_item< boost::intrusive::slist_base_hook<> > value_type;
151 typedef cds::intrusive::FCStack< value_type, boost::intrusive::slist< value_type > > stack_type;
155 TEST_F( IntrusiveFCStack, slist_empty_wait_strategy )
157 typedef base_hook_item< boost::intrusive::slist_base_hook<> > value_type;
158 struct stack_traits: public cds::intrusive::fcstack::traits
160 typedef cds::algo::flat_combining::wait_strategy::empty wait_strategy;
162 typedef cds::intrusive::FCStack< value_type, boost::intrusive::slist< value_type >, stack_traits > stack_type;
166 TEST_F( IntrusiveFCStack, slist_single_mutex_single_condvar )
168 typedef base_hook_item< boost::intrusive::slist_base_hook<> > value_type;
169 struct stack_traits: public cds::intrusive::fcstack::traits
171 typedef cds::algo::flat_combining::wait_strategy::single_mutex_single_condvar<> wait_strategy;
173 typedef cds::intrusive::FCStack< value_type, boost::intrusive::slist< value_type >, stack_traits > stack_type;
177 TEST_F( IntrusiveFCStack, slist_single_mutex_multi_condvar )
179 typedef base_hook_item< boost::intrusive::slist_base_hook<> > value_type;
180 struct stack_traits: public cds::intrusive::fcstack::traits
182 typedef cds::algo::flat_combining::wait_strategy::single_mutex_multi_condvar<> wait_strategy;
184 typedef cds::intrusive::FCStack< value_type, boost::intrusive::slist< value_type >, stack_traits > stack_type;
188 TEST_F( IntrusiveFCStack, slist_multi_mutex_multi_condvar )
190 typedef base_hook_item< boost::intrusive::slist_base_hook<> > value_type;
191 struct stack_traits: public cds::intrusive::fcstack::traits
193 typedef cds::algo::flat_combining::wait_strategy::multi_mutex_multi_condvar<> wait_strategy;
195 typedef cds::intrusive::FCStack< value_type, boost::intrusive::slist< value_type >, stack_traits > stack_type;
199 TEST_F( IntrusiveFCStack, slist_single_mutex_single_condvar_2ms )
201 typedef base_hook_item< boost::intrusive::slist_base_hook<> > value_type;
202 struct stack_traits: public cds::intrusive::fcstack::traits
204 typedef cds::algo::flat_combining::wait_strategy::single_mutex_single_condvar<2> wait_strategy;
206 typedef cds::intrusive::FCStack< value_type, boost::intrusive::slist< value_type >, stack_traits > stack_type;
210 TEST_F( IntrusiveFCStack, slist_single_mutex_multi_condvar_3ms )
212 typedef base_hook_item< boost::intrusive::slist_base_hook<> > value_type;
213 struct stack_traits: public cds::intrusive::fcstack::traits
215 typedef cds::algo::flat_combining::wait_strategy::single_mutex_multi_condvar<3> wait_strategy;
217 typedef cds::intrusive::FCStack< value_type, boost::intrusive::slist< value_type >, stack_traits > stack_type;
221 TEST_F( IntrusiveFCStack, slist_multi_mutex_multi_condvar_2ms )
223 typedef base_hook_item< boost::intrusive::slist_base_hook<> > value_type;
224 struct stack_traits: public cds::intrusive::fcstack::traits
226 typedef cds::algo::flat_combining::wait_strategy::multi_mutex_multi_condvar<2> wait_strategy;
228 typedef cds::intrusive::FCStack< value_type, boost::intrusive::slist< value_type >, stack_traits > stack_type;
232 TEST_F( IntrusiveFCStack, slist_disposer )
234 typedef base_hook_item< boost::intrusive::slist_base_hook<> > value_type;
235 struct stack_traits : public cds::intrusive::fcstack::traits
237 typedef mock_disposer disposer;
239 typedef cds::intrusive::FCStack< value_type, boost::intrusive::slist< value_type >, stack_traits > stack_type;
243 TEST_F( IntrusiveFCStack, slist_mutex )
245 typedef base_hook_item< boost::intrusive::slist_base_hook<> > value_type;
246 struct stack_traits : public cds::intrusive::fcstack::traits
248 typedef std::mutex lock_type;
250 typedef cds::intrusive::FCStack< value_type, boost::intrusive::slist< value_type >, stack_traits > stack_type;
254 TEST_F( IntrusiveFCStack, slist_elimination )
256 typedef base_hook_item< boost::intrusive::slist_base_hook<> > value_type;
257 struct stack_traits : public
258 cds::intrusive::fcstack::make_traits <
259 cds::opt::enable_elimination < true >
262 typedef cds::intrusive::FCStack< value_type, boost::intrusive::slist< value_type >, stack_traits > stack_type;
266 TEST_F( IntrusiveFCStack, slist_elimination_disposer )
268 typedef base_hook_item< boost::intrusive::slist_base_hook<> > value_type;
269 struct stack_traits : public
270 cds::intrusive::fcstack::make_traits <
271 cds::opt::enable_elimination < true >,
272 cds::intrusive::opt::disposer< mock_disposer >,
273 cds::opt::wait_strategy< cds::algo::flat_combining::wait_strategy::multi_mutex_multi_condvar<>>
276 typedef cds::intrusive::FCStack< value_type, boost::intrusive::slist< value_type >, stack_traits > stack_type;
280 TEST_F( IntrusiveFCStack, slist_elimination_stat )
282 typedef base_hook_item< boost::intrusive::slist_base_hook<> > value_type;
283 typedef cds::intrusive::FCStack< value_type, boost::intrusive::slist< value_type >,
284 cds::intrusive::fcstack::make_traits<
285 cds::opt::enable_elimination< true >
286 , cds::opt::stat< cds::intrusive::fcstack::stat<> >
287 , cds::opt::wait_strategy< cds::algo::flat_combining::wait_strategy::single_mutex_multi_condvar<>>
293 TEST_F( IntrusiveFCStack, slist_member )
295 typedef member_hook_item< boost::intrusive::slist_member_hook<> > value_type;
296 typedef boost::intrusive::member_hook<value_type, boost::intrusive::slist_member_hook<>, &value_type::hMember> member_option;
298 typedef cds::intrusive::FCStack< value_type, boost::intrusive::slist< value_type, member_option > > stack_type;
302 TEST_F( IntrusiveFCStack, slist_member_empty_wait_strategy )
304 typedef member_hook_item< boost::intrusive::slist_member_hook<> > value_type;
305 typedef boost::intrusive::member_hook<value_type, boost::intrusive::slist_member_hook<>, &value_type::hMember> member_option;
306 struct stack_traits: public cds::intrusive::fcstack::traits
308 typedef cds::algo::flat_combining::wait_strategy::empty wait_strategy;
311 typedef cds::intrusive::FCStack< value_type, boost::intrusive::slist< value_type, member_option >, stack_traits > stack_type;
315 TEST_F( IntrusiveFCStack, slist_member_single_mutex_single_condvar )
317 typedef member_hook_item< boost::intrusive::slist_member_hook<> > value_type;
318 typedef boost::intrusive::member_hook<value_type, boost::intrusive::slist_member_hook<>, &value_type::hMember> member_option;
319 struct stack_traits: public cds::intrusive::fcstack::traits
321 typedef cds::algo::flat_combining::wait_strategy::single_mutex_single_condvar<> wait_strategy;
324 typedef cds::intrusive::FCStack< value_type, boost::intrusive::slist< value_type, member_option >, stack_traits > stack_type;
328 TEST_F( IntrusiveFCStack, slist_member_single_mutex_multi_condvar )
330 typedef member_hook_item< boost::intrusive::slist_member_hook<> > value_type;
331 typedef boost::intrusive::member_hook<value_type, boost::intrusive::slist_member_hook<>, &value_type::hMember> member_option;
332 struct stack_traits: public cds::intrusive::fcstack::traits
334 typedef cds::algo::flat_combining::wait_strategy::single_mutex_multi_condvar<> wait_strategy;
337 typedef cds::intrusive::FCStack< value_type, boost::intrusive::slist< value_type, member_option >, stack_traits > stack_type;
341 TEST_F( IntrusiveFCStack, slist_member_multi_mutex_multi_condvar )
343 typedef member_hook_item< boost::intrusive::slist_member_hook<> > value_type;
344 typedef boost::intrusive::member_hook<value_type, boost::intrusive::slist_member_hook<>, &value_type::hMember> member_option;
345 struct stack_traits: public cds::intrusive::fcstack::traits
347 typedef cds::algo::flat_combining::wait_strategy::multi_mutex_multi_condvar<> wait_strategy;
350 typedef cds::intrusive::FCStack< value_type, boost::intrusive::slist< value_type, member_option >, stack_traits > stack_type;
354 TEST_F( IntrusiveFCStack, slist_member_disposer )
356 typedef member_hook_item< boost::intrusive::slist_member_hook<> > value_type;
357 typedef boost::intrusive::member_hook<value_type, boost::intrusive::slist_member_hook<>, &value_type::hMember> member_option;
358 struct stack_traits : public cds::intrusive::fcstack::traits
360 typedef mock_disposer disposer;
363 typedef cds::intrusive::FCStack< value_type, boost::intrusive::slist< value_type, member_option >, stack_traits > stack_type;
367 TEST_F( IntrusiveFCStack, slist_member_elimination )
369 typedef member_hook_item< boost::intrusive::slist_member_hook<> > value_type;
370 typedef boost::intrusive::member_hook<value_type, boost::intrusive::slist_member_hook<>, &value_type::hMember> member_option;
372 typedef cds::intrusive::FCStack< value_type, boost::intrusive::slist< value_type, member_option >,
373 cds::intrusive::fcstack::make_traits<
374 cds::opt::enable_elimination< true >
380 TEST_F( IntrusiveFCStack, slist_member_elimination_stat )
382 typedef member_hook_item< boost::intrusive::slist_member_hook<> > value_type;
383 typedef boost::intrusive::member_hook<value_type, boost::intrusive::slist_member_hook<>, &value_type::hMember> member_option;
385 typedef cds::intrusive::FCStack< value_type, boost::intrusive::slist< value_type, member_option >,
386 cds::intrusive::fcstack::make_traits<
387 cds::opt::enable_elimination< true >
388 , cds::opt::stat< cds::intrusive::fcstack::stat<> >
389 , cds::opt::wait_strategy< cds::algo::flat_combining::wait_strategy::single_mutex_multi_condvar<>>
395 TEST_F( IntrusiveFCStack, list )
397 typedef base_hook_item< boost::intrusive::list_base_hook<> > value_type;
398 typedef cds::intrusive::FCStack< value_type, boost::intrusive::list< value_type > > stack_type;
402 TEST_F( IntrusiveFCStack, list_mutex )
404 typedef base_hook_item< boost::intrusive::list_base_hook<> > value_type;
405 typedef cds::intrusive::FCStack< value_type, boost::intrusive::list< value_type >,
406 cds::intrusive::fcstack::make_traits<
407 cds::opt::lock_type< std::mutex >
414 TEST_F( IntrusiveFCStack, list_elimination )
416 typedef base_hook_item< boost::intrusive::list_base_hook<> > value_type;
417 typedef cds::intrusive::FCStack< value_type, boost::intrusive::list< value_type >,
418 cds::intrusive::fcstack::make_traits<
419 cds::opt::enable_elimination< true >
425 TEST_F( IntrusiveFCStack, list_elimination_stat )
427 typedef base_hook_item< boost::intrusive::list_base_hook<> > value_type;
428 typedef cds::intrusive::FCStack< value_type, boost::intrusive::list< value_type >,
429 cds::intrusive::fcstack::make_traits<
430 cds::opt::enable_elimination< true >
431 , cds::opt::stat< cds::intrusive::fcstack::stat<> >
432 , cds::opt::wait_strategy< cds::algo::flat_combining::wait_strategy::multi_mutex_multi_condvar<>>
438 TEST_F( IntrusiveFCStack, list_member )
440 typedef member_hook_item< boost::intrusive::list_member_hook<> > value_type;
441 typedef boost::intrusive::member_hook<value_type, boost::intrusive::list_member_hook<>, &value_type::hMember> member_option;
443 typedef cds::intrusive::FCStack< value_type, boost::intrusive::list< value_type, member_option > > stack_type;
447 TEST_F( IntrusiveFCStack, list_member_elimination )
449 typedef member_hook_item< boost::intrusive::list_member_hook<> > value_type;
450 typedef boost::intrusive::member_hook<value_type, boost::intrusive::list_member_hook<>, &value_type::hMember> member_option;
452 typedef cds::intrusive::FCStack< value_type, boost::intrusive::list< value_type, member_option >,
453 cds::intrusive::fcstack::make_traits<
454 cds::opt::enable_elimination< true >
460 TEST_F( IntrusiveFCStack, list_member_elimination_stat )
462 typedef member_hook_item< boost::intrusive::list_member_hook<> > value_type;
463 typedef boost::intrusive::member_hook<value_type, boost::intrusive::list_member_hook<>, &value_type::hMember> member_option;
465 typedef cds::intrusive::FCStack< value_type, boost::intrusive::list< value_type, member_option >,
466 cds::intrusive::fcstack::make_traits<
467 cds::opt::enable_elimination< true >
468 , cds::opt::stat< cds::intrusive::fcstack::stat<> >
469 , cds::opt::wait_strategy< cds::algo::flat_combining::wait_strategy::single_mutex_single_condvar<>>