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 #ifndef CDSUNIT_QUEUE_TEST_SEGMENTED_QUEUE_H
32 #define CDSUNIT_QUEUE_TEST_SEGMENTED_QUEUE_H
34 #include <cds_test/check_size.h>
38 class segmented_queue : public ::testing::Test
41 template <typename Queue>
44 typedef typename Queue::value_type value_type;
47 const size_t nSize = 100;
49 ASSERT_TRUE( q.empty() );
50 ASSERT_CONTAINER_SIZE( q, 0 );
53 for ( size_t i = 0; i < nSize; ++i ) {
54 it = static_cast<value_type>(i);
55 ASSERT_TRUE( q.enqueue( it ));
56 ASSERT_CONTAINER_SIZE( q, i + 1 );
58 ASSERT_FALSE( q.empty() );
59 ASSERT_CONTAINER_SIZE( q, nSize );
61 for ( size_t i = 0; i < nSize; ++i ) {
63 ASSERT_TRUE( q.dequeue( it ) );
64 ASSERT_CONTAINER_SIZE( q, nSize - i - 1 );
66 int nSegment = int( i / q.quasi_factor() );
67 int nMin = nSegment * int( q.quasi_factor() );
68 int nMax = nMin + int( q.quasi_factor() ) - 1;
69 EXPECT_LE( nMin, it );
70 EXPECT_LE( it, nMax );
72 ASSERT_TRUE( q.empty() );
73 ASSERT_CONTAINER_SIZE( q, 0 );
76 for ( size_t i = 0; i < nSize; ++i ) {
77 it = static_cast<value_type>(i);
78 ASSERT_TRUE( q.push( it ));
79 ASSERT_CONTAINER_SIZE( q, i + 1 );
81 ASSERT_FALSE( q.empty() );
82 ASSERT_CONTAINER_SIZE( q, nSize );
84 size_t nPushed = nSize;
85 size_t nStartSegment = nPushed / q.quasi_factor();
86 size_t nOffset = nPushed % q.quasi_factor();
87 for ( size_t i = 0; i < nSize; ++i ) {
89 ASSERT_TRUE( q.pop( it ) );
90 ASSERT_CONTAINER_SIZE( q, nSize - i - 1 );
92 int nSegment = static_cast<int>((i + nPushed) / q.quasi_factor() - nStartSegment );
93 int nMin = nSegment * static_cast<int>( q.quasi_factor());
95 nMin -= static_cast<int>( nOffset );
96 int nMax = nMin + static_cast<int>( q.quasi_factor()) - 1;
97 EXPECT_LE( nMin, it );
98 EXPECT_LE( it, nMax );
100 ASSERT_TRUE( q.empty() );
101 ASSERT_CONTAINER_SIZE( q, 0 );
103 // push/pop with lambda
104 for ( size_t i = 0; i < nSize; ++i ) {
105 it = static_cast<value_type>(i);
107 auto f = [&it]( value_type& dest ) { dest = it; it = -1; };
109 ASSERT_TRUE( q.enqueue_with( f ));
111 ASSERT_TRUE( q.push_with( f ));
113 ASSERT_CONTAINER_SIZE( q, i + 1 );
115 ASSERT_FALSE( q.empty() );
116 ASSERT_CONTAINER_SIZE( q, nSize );
119 nStartSegment = nPushed / q.quasi_factor();
120 nOffset = nPushed % q.quasi_factor();
121 for ( size_t i = 0; i < nSize; ++i ) {
123 auto f = [&it]( value_type& src ) { it = src; src = -1; };
125 ASSERT_TRUE( q.pop_with( f ));
127 ASSERT_TRUE( q.dequeue_with( f ));
128 ASSERT_CONTAINER_SIZE( q, nSize - i - 1 );
130 int nSegment = static_cast<int>((i + nPushed) / q.quasi_factor() - nStartSegment);
131 int nMin = nSegment * static_cast<int>(q.quasi_factor());
133 nMin -= static_cast<int>(nOffset);
134 int nMax = nMin + static_cast<int>(q.quasi_factor()) - 1;
135 EXPECT_LE( nMin, it );
136 EXPECT_LE( it, nMax );
138 ASSERT_TRUE( q.empty() );
139 ASSERT_CONTAINER_SIZE( q, 0 );
142 for ( size_t i = 0; i < nSize; ++i ) {
143 ASSERT_TRUE( q.push( static_cast<value_type>(i) ) );
145 ASSERT_FALSE( q.empty() );
146 ASSERT_CONTAINER_SIZE( q, nSize );
149 ASSERT_TRUE( q.empty() );
150 ASSERT_CONTAINER_SIZE( q, 0 );
152 // pop from empty queue
154 ASSERT_FALSE( q.pop( it ) );
155 ASSERT_EQ( it, static_cast<value_type>( nSize * 2 ));
156 ASSERT_TRUE( q.empty() );
157 ASSERT_CONTAINER_SIZE( q, 0 );
159 ASSERT_FALSE( q.dequeue( it ) );
160 ASSERT_EQ( it, static_cast<value_type>( nSize * 2 ));
161 ASSERT_TRUE( q.empty() );
162 ASSERT_CONTAINER_SIZE( q, 0 );
165 template <class Queue>
166 void test_string( Queue& q )
172 const size_t nSize = sizeof( str ) / sizeof( str[0] );
175 for ( size_t i = 0; i < nSize; ++i ) {
176 ASSERT_TRUE( q.emplace( str[i].c_str()));
177 ASSERT_CONTAINER_SIZE( q, i + 1 );
179 ASSERT_FALSE( q.empty() );
180 ASSERT_CONTAINER_SIZE( q, nSize );
184 auto f = [&s]( std::string& src ) {
185 ASSERT_FALSE( src.empty() );
186 s = std::move( src );
189 for ( size_t i = 0; i < nSize; ++i ) {
191 ASSERT_TRUE( q.pop_with( f ));
193 ASSERT_TRUE( q.dequeue_with( f ));
195 ASSERT_CONTAINER_SIZE( q, nSize - i - 1 );
196 ASSERT_TRUE( s == str[0] || s == str[1] || s == str[2] );
199 ASSERT_TRUE( q.empty() );
200 ASSERT_CONTAINER_SIZE( q, 0 );
204 for ( size_t i = 0; i < nSize; ++i ) {
205 std::string s = str[i];
206 ASSERT_FALSE( s.empty() );
208 ASSERT_TRUE( q.enqueue( std::move( s )));
210 ASSERT_TRUE( q.push( std::move( s )));
211 ASSERT_TRUE( s.empty() );
212 ASSERT_CONTAINER_SIZE( q, i + 1 );
214 ASSERT_FALSE( q.empty() );
215 ASSERT_CONTAINER_SIZE( q, nSize );
217 for ( size_t i = 0; i < nSize; ++i ) {
219 ASSERT_TRUE( q.pop( s ) );
220 ASSERT_CONTAINER_SIZE( q, nSize - i - 1 );
221 ASSERT_TRUE( s == str[0] || s == str[1] || s == str[2] );
223 ASSERT_TRUE( q.empty() );
224 ASSERT_CONTAINER_SIZE( q, 0 );
229 } // namespace cds_test
231 #endif // CDSUNIT_QUEUE_TEST_SEGMENTED_QUEUE_H