9186771db8264652555c2ae1217282fa96eb251c
[libcds.git] / test / unit / queue / test_bounded_queue.h
1 /*
2     This file is a part of libcds - Concurrent Data Structures library
3
4     (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2017
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 #ifndef CDSUNIT_QUEUE_TEST_BOUNDED_QUEUE_H
32 #define CDSUNIT_QUEUE_TEST_BOUNDED_QUEUE_H
33
34 #include <cds_test/check_size.h>
35
36 namespace cds_test {
37
38     class bounded_queue : public ::testing::Test
39     {
40     protected:
41         template <typename Queue>
42         void test( Queue& q )
43         {
44             typedef typename Queue::value_type value_type;
45             value_type it;
46
47             const size_t nSize = q.capacity();
48
49             ASSERT_TRUE( q.empty());
50             ASSERT_CONTAINER_SIZE( q, 0 );
51
52             // enqueue/dequeue
53             for ( unsigned pass = 0; pass < 3; ++pass ) {
54                 for ( size_t i = 0; i < nSize; ++i ) {
55                     it = static_cast<value_type>( i );
56                     ASSERT_TRUE( q.enqueue( it ) );
57                     ASSERT_CONTAINER_SIZE( q, i + 1 );
58                 }
59                 ASSERT_FALSE( q.empty() );
60                 ASSERT_CONTAINER_SIZE( q, nSize );
61                 ASSERT_FALSE( q.enqueue( static_cast<value_type>( nSize ) * 2 ) );
62
63                 for ( size_t i = 0; i < nSize; ++i ) {
64                     it = -1;
65                     ASSERT_TRUE( q.dequeue( it ) );
66                     ASSERT_EQ( it, static_cast<value_type>( i ) );
67                     ASSERT_CONTAINER_SIZE( q, nSize - i - 1 );
68                 }
69                 ASSERT_TRUE( q.empty() );
70                 ASSERT_CONTAINER_SIZE( q, 0 );
71             }
72
73             // push/pop
74             for ( unsigned pass = 0; pass < 3; ++pass ) {
75                 for ( size_t i = 0; i < nSize; ++i ) {
76                     it = static_cast<value_type>( i );
77                     ASSERT_TRUE( q.push( it ) );
78                     ASSERT_CONTAINER_SIZE( q, i + 1 );
79                 }
80                 ASSERT_FALSE( q.empty() );
81                 ASSERT_CONTAINER_SIZE( q, nSize );
82
83                 for ( size_t i = 0; i < nSize; ++i ) {
84                     it = -1;
85                     ASSERT_TRUE( q.pop( it ) );
86                     ASSERT_EQ( it, static_cast<value_type>( i ) );
87                     ASSERT_CONTAINER_SIZE( q, nSize - i - 1 );
88                 }
89                 ASSERT_TRUE( q.empty() );
90                 ASSERT_CONTAINER_SIZE( q, 0 );
91             }
92
93             // push/pop with lambda
94             for ( unsigned pass = 0; pass < 3; ++pass ) {
95                 for ( size_t i = 0; i < nSize; ++i ) {
96                     it = static_cast<value_type>( i );
97                     ASSERT_NE( it, -1 );
98                     auto f = [&it]( value_type& dest ) { dest = it; it = -1; };
99                     if ( i & 1 )
100                         ASSERT_TRUE( q.enqueue_with( f ) );
101                     else
102                         ASSERT_TRUE( q.push_with( f ) );
103                     ASSERT_EQ( it, -1 );
104                     ASSERT_CONTAINER_SIZE( q, i + 1 );
105                 }
106                 ASSERT_FALSE( q.empty() );
107                 ASSERT_CONTAINER_SIZE( q, nSize );
108
109                 for ( size_t i = 0; i < nSize; ++i ) {
110                     it = -1;
111                     auto f = [&it]( value_type& src ) { it = src; src = -1; };
112                     if ( i & 1 )
113                         ASSERT_TRUE( q.pop_with( f ) );
114                     else
115                         ASSERT_TRUE( q.dequeue_with( f ) );
116                     ASSERT_EQ( it, static_cast<value_type>( i ) );
117                     ASSERT_CONTAINER_SIZE( q, nSize - i - 1 );
118                 }
119                 ASSERT_TRUE( q.empty() );
120                 ASSERT_CONTAINER_SIZE( q, 0u );
121             }
122
123             for ( size_t i = 0; i < nSize; ++i ) {
124                 ASSERT_TRUE( q.push( static_cast<value_type>(i)));
125             }
126             ASSERT_FALSE( q.empty());
127             ASSERT_CONTAINER_SIZE( q, nSize );
128
129             // push in full queue
130             ASSERT_FALSE( q.push( static_cast<int>(nSize * 2 )));
131             ASSERT_FALSE( q.empty());
132             ASSERT_CONTAINER_SIZE( q, nSize );
133             it = static_cast<int>( nSize * 2 );
134             ASSERT_FALSE( q.enqueue( it ));
135             ASSERT_FALSE( q.empty());
136             ASSERT_CONTAINER_SIZE( q, nSize );
137
138             // clear
139             q.clear();
140             ASSERT_TRUE( q.empty());
141             ASSERT_CONTAINER_SIZE( q, 0u );
142
143             // pop from empty queue
144             it = static_cast<int>(nSize * 2);
145             ASSERT_FALSE( q.pop( it ));
146             ASSERT_EQ( it, static_cast<value_type>( nSize * 2 ));
147             ASSERT_TRUE( q.empty());
148             ASSERT_CONTAINER_SIZE( q, 0u );
149
150             ASSERT_FALSE( q.dequeue( it ));
151             ASSERT_EQ( it, static_cast<value_type>( nSize * 2 ));
152             ASSERT_TRUE( q.empty());
153             ASSERT_CONTAINER_SIZE( q, 0u );
154         }
155
156         template <class Queue>
157         void test_string( Queue& q )
158         {
159             std::string str[3];
160             str[0] = "one";
161             str[1] = "two";
162             str[2] = "three";
163             const size_t nSize = sizeof( str ) / sizeof( str[0] );
164
165             // emplace
166             for ( size_t i = 0; i < nSize; ++i ) {
167                 ASSERT_TRUE( q.emplace( str[i].c_str()));
168                 ASSERT_CONTAINER_SIZE( q, i + 1 );
169             }
170             ASSERT_FALSE( q.empty());
171             ASSERT_CONTAINER_SIZE( q, nSize );
172
173             {
174                 std::string s;
175                 auto f = [&s]( std::string& src ) {
176                     ASSERT_FALSE( src.empty());
177                     s = std::move( src );
178                     ASSERT_NE( s, src );
179                 };
180                 for ( size_t i = 0; i < nSize; ++i ) {
181                     if ( i & 1 )
182                         ASSERT_TRUE( q.pop_with( f ));
183                     else
184                         ASSERT_TRUE( q.dequeue_with( f ));
185
186                     ASSERT_CONTAINER_SIZE( q, nSize - i - 1 );
187                     ASSERT_EQ( s, str[i] );
188                 }
189             }
190             ASSERT_TRUE( q.empty());
191             ASSERT_CONTAINER_SIZE( q, 0 );
192
193
194             // move push
195             for ( size_t i = 0; i < nSize; ++i ) {
196                 std::string s = str[i];
197                 ASSERT_FALSE( s.empty());
198                 if ( i & 1 )
199                     ASSERT_TRUE( q.enqueue( std::move( s )));
200                 else
201                     ASSERT_TRUE( q.push( std::move( s )));
202                 ASSERT_TRUE( s.empty());
203                 ASSERT_CONTAINER_SIZE( q, i + 1 );
204             }
205             ASSERT_FALSE( q.empty());
206             ASSERT_CONTAINER_SIZE( q, nSize );
207
208             for ( size_t i = 0; i < nSize; ++i ) {
209                 std::string s;
210                 ASSERT_TRUE( q.pop( s ));
211                 ASSERT_CONTAINER_SIZE( q, nSize - i - 1 );
212                 ASSERT_EQ( s, str[i] );
213             }
214             ASSERT_TRUE( q.empty());
215             ASSERT_CONTAINER_SIZE( q, 0 );
216         }
217
218     };
219
220 } // namespace cds_test
221
222 #endif // CDSUNIT_QUEUE_TEST_BOUNDED_QUEUE_H