Added WeakRingBuffer - a single-producer/single-consumer queue based on ring buffer
[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 ( 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 );
57             }
58             ASSERT_FALSE( q.empty());
59             ASSERT_CONTAINER_SIZE( q, nSize );
60             ASSERT_FALSE( q.enqueue( static_cast<value_type>( nSize ) * 2 ) );
61
62             for ( size_t i = 0; i < nSize; ++i ) {
63                 it = -1;
64                 ASSERT_TRUE( q.dequeue( it ));
65                 ASSERT_EQ( it, static_cast<value_type>( i ));
66                 ASSERT_CONTAINER_SIZE( q, nSize - i - 1 );
67             }
68             ASSERT_TRUE( q.empty());
69             ASSERT_CONTAINER_SIZE( q, 0 );
70
71             // push/pop
72             for ( size_t i = 0; i < nSize; ++i ) {
73                 it = static_cast<value_type>(i);
74                 ASSERT_TRUE( q.push( it ));
75                 ASSERT_CONTAINER_SIZE( q, i + 1 );
76             }
77             ASSERT_FALSE( q.empty());
78             ASSERT_CONTAINER_SIZE( q, nSize );
79
80             for ( size_t i = 0; i < nSize; ++i ) {
81                 it = -1;
82                 ASSERT_TRUE( q.pop( it ));
83                 ASSERT_EQ( it, static_cast<value_type>( i ));
84                 ASSERT_CONTAINER_SIZE( q, nSize - i - 1 );
85             }
86             ASSERT_TRUE( q.empty());
87             ASSERT_CONTAINER_SIZE( q, 0 );
88
89             // push/pop with lambda
90             for ( size_t i = 0; i < nSize; ++i ) {
91                 it = static_cast<value_type>(i);
92                 ASSERT_NE( it, -1 );
93                 auto f = [&it]( value_type& dest ) { dest = it; it = -1; };
94                 if ( i & 1 )
95                     ASSERT_TRUE( q.enqueue_with( f ));
96                 else
97                     ASSERT_TRUE( q.push_with( f ));
98                 ASSERT_EQ( it, -1 );
99                 ASSERT_CONTAINER_SIZE( q, i + 1 );
100             }
101             ASSERT_FALSE( q.empty());
102             ASSERT_CONTAINER_SIZE( q, nSize );
103
104             for ( size_t i = 0; i < nSize; ++i ) {
105                 it = -1;
106                 auto f = [&it]( value_type& src ) { it = src; src = -1; };
107                 if ( i & 1 )
108                     ASSERT_TRUE( q.pop_with( f ));
109                 else
110                     ASSERT_TRUE( q.dequeue_with( f ));
111                 ASSERT_EQ( it, static_cast<value_type>( i ));
112                 ASSERT_CONTAINER_SIZE( q, nSize - i - 1 );
113             }
114             ASSERT_TRUE( q.empty());
115             ASSERT_CONTAINER_SIZE( q, 0u );
116
117             for ( size_t i = 0; i < nSize; ++i ) {
118                 ASSERT_TRUE( q.push( static_cast<value_type>(i)));
119             }
120             ASSERT_FALSE( q.empty());
121             ASSERT_CONTAINER_SIZE( q, nSize );
122
123             // push in full queue
124             ASSERT_FALSE( q.push( static_cast<int>(nSize * 2 )));
125             ASSERT_FALSE( q.empty());
126             ASSERT_CONTAINER_SIZE( q, nSize );
127             it = static_cast<int>( nSize * 2 );
128             ASSERT_FALSE( q.enqueue( it ));
129             ASSERT_FALSE( q.empty());
130             ASSERT_CONTAINER_SIZE( q, nSize );
131
132             // clear
133             q.clear();
134             ASSERT_TRUE( q.empty());
135             ASSERT_CONTAINER_SIZE( q, 0u );
136
137             // pop from empty queue
138             it = static_cast<int>(nSize * 2);
139             ASSERT_FALSE( q.pop( it ));
140             ASSERT_EQ( it, static_cast<value_type>( nSize * 2 ));
141             ASSERT_TRUE( q.empty());
142             ASSERT_CONTAINER_SIZE( q, 0u );
143
144             ASSERT_FALSE( q.dequeue( it ));
145             ASSERT_EQ( it, static_cast<value_type>( nSize * 2 ));
146             ASSERT_TRUE( q.empty());
147             ASSERT_CONTAINER_SIZE( q, 0u );
148         }
149
150         template <class Queue>
151         void test_string( Queue& q )
152         {
153             std::string str[3];
154             str[0] = "one";
155             str[1] = "two";
156             str[2] = "three";
157             const size_t nSize = sizeof( str ) / sizeof( str[0] );
158
159             // emplace
160             for ( size_t i = 0; i < nSize; ++i ) {
161                 ASSERT_TRUE( q.emplace( str[i].c_str()));
162                 ASSERT_CONTAINER_SIZE( q, i + 1 );
163             }
164             ASSERT_FALSE( q.empty());
165             ASSERT_CONTAINER_SIZE( q, nSize );
166
167             {
168                 std::string s;
169                 auto f = [&s]( std::string& src ) {
170                     ASSERT_FALSE( src.empty());
171                     s = std::move( src );
172                     ASSERT_NE( s, src );
173                 };
174                 for ( size_t i = 0; i < nSize; ++i ) {
175                     if ( i & 1 )
176                         ASSERT_TRUE( q.pop_with( f ));
177                     else
178                         ASSERT_TRUE( q.dequeue_with( f ));
179
180                     ASSERT_CONTAINER_SIZE( q, nSize - i - 1 );
181                     ASSERT_EQ( s, str[i] );
182                 }
183             }
184             ASSERT_TRUE( q.empty());
185             ASSERT_CONTAINER_SIZE( q, 0 );
186
187
188             // move push
189             for ( size_t i = 0; i < nSize; ++i ) {
190                 std::string s = str[i];
191                 ASSERT_FALSE( s.empty());
192                 if ( i & 1 )
193                     ASSERT_TRUE( q.enqueue( std::move( s )));
194                 else
195                     ASSERT_TRUE( q.push( std::move( s )));
196                 ASSERT_TRUE( s.empty());
197                 ASSERT_CONTAINER_SIZE( q, i + 1 );
198             }
199             ASSERT_FALSE( q.empty());
200             ASSERT_CONTAINER_SIZE( q, nSize );
201
202             for ( size_t i = 0; i < nSize; ++i ) {
203                 std::string s;
204                 ASSERT_TRUE( q.pop( s ));
205                 ASSERT_CONTAINER_SIZE( q, nSize - i - 1 );
206                 ASSERT_EQ( s, str[i] );
207             }
208             ASSERT_TRUE( q.empty());
209             ASSERT_CONTAINER_SIZE( q, 0 );
210         }
211
212     };
213
214 } // namespace cds_test
215
216 #endif // CDSUNIT_QUEUE_TEST_BOUNDED_QUEUE_H