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 #ifndef CDSUNIT_QUEUE_TEST_GENERIC_QUEUE_H
32 #define CDSUNIT_QUEUE_TEST_GENERIC_QUEUE_H
34 #include <cds_test/check_size.h>
38 class generic_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_EQ( it, static_cast<value_type>( i ));
65 ASSERT_CONTAINER_SIZE( q, nSize - i - 1 );
67 ASSERT_TRUE( q.empty());
68 ASSERT_CONTAINER_SIZE( q, 0 );
71 for ( size_t i = 0; i < nSize; ++i ) {
72 it = static_cast<value_type>(i);
73 ASSERT_TRUE( q.push( it ));
74 ASSERT_CONTAINER_SIZE( q, i + 1 );
76 ASSERT_FALSE( q.empty());
77 ASSERT_CONTAINER_SIZE( q, nSize );
79 for ( size_t i = 0; i < nSize; ++i ) {
81 ASSERT_TRUE( q.pop( it ));
82 ASSERT_EQ( it, static_cast<value_type>( i ));
83 ASSERT_CONTAINER_SIZE( q, nSize - i - 1 );
85 ASSERT_TRUE( q.empty());
86 ASSERT_CONTAINER_SIZE( q, 0 );
88 // push/pop with lambda
89 for ( size_t i = 0; i < nSize; ++i ) {
90 it = static_cast<value_type>(i);
92 auto f = [&it]( value_type& dest ) { dest = it; it = -1; };
94 ASSERT_TRUE( q.enqueue_with( f ));
96 ASSERT_TRUE( q.push_with( f ));
98 ASSERT_CONTAINER_SIZE( q, i + 1 );
100 ASSERT_FALSE( q.empty());
101 ASSERT_CONTAINER_SIZE( q, nSize );
103 for ( size_t i = 0; i < nSize; ++i ) {
105 auto f = [&it]( value_type& src ) { it = src; src = -1; };
107 ASSERT_TRUE( q.pop_with( f ));
109 ASSERT_TRUE( q.dequeue_with( f ));
110 ASSERT_EQ( it, static_cast<value_type>( i ));
111 ASSERT_CONTAINER_SIZE( q, nSize - i - 1 );
113 ASSERT_TRUE( q.empty());
114 ASSERT_CONTAINER_SIZE( q, 0 );
117 for ( size_t i = 0; i < nSize; ++i ) {
118 ASSERT_TRUE( q.push( static_cast<value_type>(i)));
120 ASSERT_FALSE( q.empty());
121 ASSERT_CONTAINER_SIZE( q, nSize );
124 ASSERT_TRUE( q.empty());
125 ASSERT_CONTAINER_SIZE( q, 0 );
127 // pop from empty queue
129 ASSERT_FALSE( q.pop( it ));
130 ASSERT_EQ( it, static_cast<value_type>( nSize * 2 ));
131 ASSERT_TRUE( q.empty());
132 ASSERT_CONTAINER_SIZE( q, 0 );
134 ASSERT_FALSE( q.dequeue( it ));
135 ASSERT_EQ( it, static_cast<value_type>( nSize * 2 ));
136 ASSERT_TRUE( q.empty());
137 ASSERT_CONTAINER_SIZE( q, 0 );
140 template <class Queue>
141 void test_string( Queue& q )
147 const size_t nSize = sizeof( str ) / sizeof( str[0] );
150 for ( size_t i = 0; i < nSize; ++i ) {
151 ASSERT_TRUE( q.emplace( str[i].c_str()));
152 ASSERT_CONTAINER_SIZE( q, i + 1 );
154 ASSERT_FALSE( q.empty());
155 ASSERT_CONTAINER_SIZE( q, nSize );
159 auto f = [&s]( std::string& src ) {
160 ASSERT_FALSE( src.empty());
161 s = std::move( src );
164 for ( size_t i = 0; i < nSize; ++i ) {
166 ASSERT_TRUE( q.pop_with( f ));
168 ASSERT_TRUE( q.dequeue_with( f ));
170 ASSERT_CONTAINER_SIZE( q, nSize - i - 1 );
171 ASSERT_EQ( s, str[i] );
174 ASSERT_TRUE( q.empty());
175 ASSERT_CONTAINER_SIZE( q, 0 );
179 for ( size_t i = 0; i < nSize; ++i ) {
180 std::string s = str[i];
181 ASSERT_FALSE( s.empty());
183 ASSERT_TRUE( q.enqueue( std::move( s )));
185 ASSERT_TRUE( q.push( std::move( s )));
186 ASSERT_TRUE( s.empty());
187 ASSERT_CONTAINER_SIZE( q, i + 1 );
189 ASSERT_FALSE( q.empty());
190 ASSERT_CONTAINER_SIZE( q, nSize );
192 for ( size_t i = 0; i < nSize; ++i ) {
194 ASSERT_TRUE( q.pop( s ));
195 ASSERT_CONTAINER_SIZE( q, nSize - i - 1 );
196 ASSERT_EQ( s, str[i] );
198 ASSERT_TRUE( q.empty());
199 ASSERT_CONTAINER_SIZE( q, 0 );
204 } // namespace cds_test
206 #endif // CDSUNIT_QUEUE_TEST_GENERIC_QUEUE_H