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 "test_bounded_queue.h"
33 #include <cds/container/vyukov_mpmc_cycle_queue.h>
36 namespace cc = cds::container;
38 class VyukovMPMCCycleQueue: public cds_test::bounded_queue
41 template <typename Queue>
42 void test_single_consumer( Queue& q )
44 typedef typename Queue::value_type value_type;
46 const size_t nSize = q.capacity();
48 ASSERT_TRUE( q.empty());
49 ASSERT_CONTAINER_SIZE( q, 0 );
52 for ( unsigned pass = 0; pass < 3; ++pass ) {
53 for ( size_t i = 0; i < nSize; ++i ) {
54 ASSERT_TRUE( q.enqueue( static_cast<value_type>( i )));
55 ASSERT_CONTAINER_SIZE( q, i + 1 );
57 ASSERT_FALSE( q.empty());
58 ASSERT_CONTAINER_SIZE( q, nSize );
59 ASSERT_FALSE( q.enqueue( static_cast<value_type>( nSize ) * 2 ));
61 for ( size_t i = 0; i < nSize; ++i ) {
62 value_type* fr = q.front();
63 ASSERT_TRUE( fr != nullptr );
64 ASSERT_EQ( *fr, static_cast<value_type>( i ));
65 ASSERT_TRUE( q.pop_front());
66 ASSERT_CONTAINER_SIZE( q, nSize - i - 1 );
68 ASSERT_TRUE( q.empty());
69 ASSERT_CONTAINER_SIZE( q, 0 );
71 ASSERT_TRUE( q.front() == nullptr );
72 ASSERT_FALSE( q.pop_front());
77 TEST_F( VyukovMPMCCycleQueue, defaulted )
79 typedef cds::container::VyukovMPMCCycleQueue< int > test_queue;
85 TEST_F( VyukovMPMCCycleQueue, stat )
87 struct traits: public cds::container::vyukov_queue::traits
89 typedef cds::opt::v::uninitialized_static_buffer<int, 128> buffer;
91 typedef cds::container::VyukovMPMCCycleQueue< int, traits > test_queue;
97 TEST_F( VyukovMPMCCycleQueue, stat_item_counting )
100 typedef cds::container::VyukovMPMCCycleQueue< int,
101 cds::container::vyukov_queue::make_traits<
102 cds::opt::buffer< cds::opt::v::uninitialized_static_buffer<int, 128>>
103 , cds::opt::item_counter< cds::atomicity::item_counter>
111 TEST_F( VyukovMPMCCycleQueue, dynamic )
113 struct traits : public cds::container::vyukov_queue::traits
115 typedef cds::opt::v::uninitialized_dynamic_buffer<int> buffer;
117 typedef cds::container::VyukovMPMCCycleQueue< int, traits > test_queue;
123 TEST_F( VyukovMPMCCycleQueue, dynamic_item_counting )
125 typedef cds::container::VyukovMPMCCycleQueue< int,
126 cds::container::vyukov_queue::make_traits<
127 cds::opt::buffer< cds::opt::v::uninitialized_dynamic_buffer<int>>
128 , cds::opt::item_counter< cds::atomicity::item_counter>
136 TEST_F( VyukovMPMCCycleQueue, dynamic_padding )
138 struct traits : public cds::container::vyukov_queue::traits
140 typedef cds::opt::v::uninitialized_dynamic_buffer<int> buffer;
141 enum { padding = 64 };
143 typedef cds::container::VyukovMPMCCycleQueue< int, traits > test_queue;
149 TEST_F( VyukovMPMCCycleQueue, move )
151 typedef cds::container::VyukovMPMCCycleQueue< std::string > test_queue;
157 TEST_F( VyukovMPMCCycleQueue, move_item_counting )
159 struct traits : public cds::container::vyukov_queue::traits
161 typedef cds::atomicity::item_counter item_counter;
163 typedef cds::container::VyukovMPMCCycleQueue< std::string, traits > test_queue;
169 TEST_F( VyukovMPMCCycleQueue, single_consumer )
171 struct traits: public cds::container::vyukov_queue::traits
173 enum: bool { single_consumer = true };
175 typedef cds::container::VyukovMPMCCycleQueue< int, traits > test_queue;
179 test_single_consumer( q );