c729edf2486d5bde2bcb58ea22cdc1261dfbfc13
[libcds.git] / test / unit / queue / fcqueue.cpp
1 /*
2     This file is a part of libcds - Concurrent Data Structures library
3
4     (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2016
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 #include <gtest/gtest.h>
32 #include <cds/container/fcqueue.h>
33
34 #include <list>
35
36 namespace {
37
38     class FCQueue: public ::testing::Test
39     {
40     protected:
41         template <class 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 = 100;
48
49             ASSERT_TRUE( q.empty() );
50             ASSERT_EQ( q.size(), 0u );
51
52             // enqueue/dequeue
53             for ( size_t i = 0; i < nSize; ++i ) {
54                 ASSERT_TRUE( q.enqueue( static_cast<value_type>(i)));
55                 ASSERT_EQ( q.size(), i + 1 );
56             }
57             ASSERT_FALSE( q.empty() );
58             ASSERT_EQ( q.size(), nSize );
59
60             for ( size_t i = 0; i < nSize; ++i ) {
61                 it = -1;
62                 ASSERT_TRUE( q.dequeue( it ));
63                 ASSERT_EQ( it, i );
64                 ASSERT_EQ( q.size(), nSize - i - 1 );
65             }
66             ASSERT_TRUE( q.empty() );
67             ASSERT_EQ( q.size(), 0u );
68
69             // push/pop
70             for ( size_t i = 0; i < nSize; ++i ) {
71                 ASSERT_TRUE( q.push( static_cast<value_type>(i)));
72                 ASSERT_EQ( q.size(), i + 1 );
73             }
74             ASSERT_FALSE( q.empty() );
75             ASSERT_EQ( q.size(), nSize );
76
77             for ( size_t i = 0; i < nSize; ++i ) {
78                 it = -1;
79                 ASSERT_TRUE( q.pop( it ) );
80                 ASSERT_EQ( it, i );
81                 ASSERT_EQ( q.size(), nSize - i - 1 );
82             }
83             ASSERT_TRUE( q.empty() );
84             ASSERT_EQ( q.size(), 0u );
85
86             // clear
87             for ( size_t i = 0; i < nSize; ++i ) {
88                 ASSERT_TRUE( q.push( static_cast<value_type>( i )));
89             }
90             ASSERT_FALSE( q.empty() );
91             ASSERT_EQ( q.size(), nSize );
92
93             q.clear();
94             ASSERT_TRUE( q.empty() );
95             ASSERT_EQ( q.size(), 0u );
96
97             // pop from empty queue
98             it = nSize * 2;
99             ASSERT_FALSE( q.pop( it ));
100             ASSERT_EQ( it, nSize * 2 );
101             ASSERT_TRUE( q.empty() );
102             ASSERT_EQ( q.size(), 0u );
103
104             ASSERT_FALSE( q.dequeue( it ) );
105             ASSERT_EQ( it, nSize * 2 );
106             ASSERT_TRUE( q.empty() );
107             ASSERT_EQ( q.size(), 0u );
108         }
109
110         template <class Queue>
111         void test_string( Queue& q )
112         {
113             std::string str[3];
114             str[0] = "one";
115             str[1] = "two";
116             str[2] = "three";
117             const size_t nSize = sizeof( str ) / sizeof( str[0] );
118
119             // move push
120             for ( size_t i = 0; i < nSize; ++i ) {
121                 std::string s = str[i];
122                 ASSERT_FALSE( s.empty());
123                 ASSERT_TRUE( q.enqueue( std::move( s )));
124                 ASSERT_FALSE( s.empty());
125                 ASSERT_EQ( q.size(), i + 1 );
126             }
127             ASSERT_FALSE( q.empty() );
128             ASSERT_EQ( q.size(), nSize );
129
130             for ( size_t i = 0; i < nSize; ++i ) {
131                 std::string s;
132                 ASSERT_TRUE( q.pop( s ));
133                 ASSERT_EQ( q.size(), nSize - i - 1 );
134                 ASSERT_EQ( s, str[i] );
135             }
136             ASSERT_TRUE( q.empty() );
137             ASSERT_EQ( q.size(), 0u );
138         }
139     };
140
141     TEST_F( FCQueue, std_deque )
142     {
143         typedef cds::container::FCQueue<int> queue_type;
144
145         queue_type q;
146         test( q );
147     }
148
149     TEST_F( FCQueue, std_deque_move )
150     {
151         typedef cds::container::FCQueue<std::string> queue_type;
152
153         queue_type q;
154         test_string( q );
155     }
156
157     TEST_F( FCQueue, std_empty_wait_strategy )
158     {
159         typedef cds::container::FCQueue<int, std::queue< int, std::deque<int>>,
160             cds::container::fcqueue::make_traits<
161                 cds::opt::wait_strategy< cds::algo::flat_combining::wait_strategy::empty >
162             >::type
163         > queue_type;
164
165         queue_type q;
166         test( q );
167     }
168
169     TEST_F( FCQueue, std_single_mutex_single_condvar )
170     {
171         typedef cds::container::FCQueue<int, std::queue< int, std::deque<int>>,
172             cds::container::fcqueue::make_traits<
173                 cds::opt::wait_strategy< cds::algo::flat_combining::wait_strategy::single_mutex_single_condvar<>>
174             >::type
175         > queue_type;
176
177         queue_type q;
178         test( q );
179     }
180
181     TEST_F( FCQueue, std_deque_elimination )
182     {
183         typedef cds::container::FCQueue<int, std::queue< int, std::deque<int>>,
184             cds::container::fcqueue::make_traits<
185                 cds::opt::enable_elimination< true >
186             >::type
187         > queue_type;
188
189         queue_type q;
190         test( q );
191     }
192
193     TEST_F( FCQueue, std_deque_elimination_single_mutex_multi_condvar )
194     {
195         typedef cds::container::FCQueue<int, std::queue< int, std::deque<int>>,
196             cds::container::fcqueue::make_traits<
197                 cds::opt::enable_elimination< true >
198                 , cds::opt::wait_strategy< cds::algo::flat_combining::wait_strategy::single_mutex_multi_condvar<2>>
199             >::type
200         > queue_type;
201
202         queue_type q;
203         test( q );
204     }
205
206     TEST_F( FCQueue, std_deque_elimination_move )
207     {
208         typedef cds::container::FCQueue<std::string, std::queue< std::string, std::deque<std::string>>,
209             cds::container::fcqueue::make_traits<
210                 cds::opt::enable_elimination< true >
211             >::type
212         > queue_type;
213
214         queue_type q;
215         test_string( q );
216     }
217
218     TEST_F( FCQueue, std_deque_elimination_move_multi_mutex_multi_condvar )
219     {
220         typedef cds::container::FCQueue<std::string, std::queue< std::string, std::deque<std::string>>,
221             cds::container::fcqueue::make_traits<
222                 cds::opt::enable_elimination< true >
223                 , cds::opt::wait_strategy< cds::algo::flat_combining::wait_strategy::multi_mutex_multi_condvar<>>
224             >::type
225         > queue_type;
226
227         queue_type q;
228         test_string( q );
229     }
230
231     TEST_F( FCQueue, std_deque_mutex )
232     {
233         typedef cds::container::FCQueue<int, std::queue< int, std::deque<int>>,
234             cds::container::fcqueue::make_traits<
235                 cds::opt::lock_type< std::mutex >
236             >::type
237         > queue_type;
238
239         queue_type q;
240         test( q );
241     }
242
243     TEST_F( FCQueue, std_list )
244     {
245         typedef cds::container::FCQueue<int, std::queue< int, std::list<int>>> queue_type;
246
247         queue_type q;
248         test( q );
249     }
250
251     TEST_F( FCQueue, std_list_move )
252     {
253         typedef cds::container::FCQueue<std::string, std::queue< std::string, std::list<std::string>>> queue_type;
254
255         queue_type q;
256         test_string( q );
257     }
258
259     TEST_F( FCQueue, std_list_empty_wait_strategy )
260     {
261         typedef cds::container::FCQueue<int, std::queue< int, std::list<int> >,
262             cds::container::fcqueue::make_traits<
263                 cds::opt::wait_strategy< cds::algo::flat_combining::wait_strategy::empty >
264             >::type
265         > queue_type;
266
267         queue_type q;
268         test( q );
269     }
270
271     TEST_F( FCQueue, std_list_single_mutex_single_condvar )
272     {
273         typedef cds::container::FCQueue<int, std::queue< int, std::list<int> >,
274             cds::container::fcqueue::make_traits<
275                 cds::opt::wait_strategy< cds::algo::flat_combining::wait_strategy::single_mutex_single_condvar<5>>
276             >::type
277         > queue_type;
278
279         queue_type q;
280         test( q );
281     }
282
283     TEST_F( FCQueue, std_list_elimination )
284     {
285         typedef cds::container::FCQueue<int, std::queue< int, std::list<int> >,
286             cds::container::fcqueue::make_traits<
287                 cds::opt::enable_elimination< true >
288             >::type
289         > queue_type;
290
291         queue_type q;
292         test( q );
293     }
294
295     TEST_F( FCQueue, std_list_elimination_multi_mutex_multi_condvar )
296     {
297         typedef cds::container::FCQueue<int, std::queue< int, std::list<int> >,
298             cds::container::fcqueue::make_traits<
299                 cds::opt::enable_elimination< true >
300                 ,cds::opt::wait_strategy< cds::algo::flat_combining::wait_strategy::multi_mutex_multi_condvar<5>>
301             >::type
302         > queue_type;
303
304         queue_type q;
305         test( q );
306     }
307
308     TEST_F( FCQueue, std_list_elimination_move )
309     {
310         typedef cds::container::FCQueue<std::string, std::queue< std::string, std::list<std::string> >,
311             cds::container::fcqueue::make_traits<
312             cds::opt::enable_elimination< true >
313             >::type
314         > queue_type;
315
316         queue_type q;
317         test_string( q );
318     }
319
320     TEST_F( FCQueue, std_list_mutex )
321     {
322         typedef cds::container::FCQueue<int, std::queue<int, std::list<int> >,
323             cds::container::fcqueue::make_traits<
324                 cds::opt::lock_type< std::mutex >
325             >::type
326         > queue_type;
327
328         queue_type q;
329         test( q );
330     }
331
332 } // namespace