Moved queue unit test to gtest framework
[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(), 0 );
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(), 0 );
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(), 0 );
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(), 0 );
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(), 0 );
103
104             ASSERT_FALSE( q.dequeue( it ) );
105             ASSERT_EQ( it, nSize * 2 );
106             ASSERT_TRUE( q.empty() );
107             ASSERT_EQ( q.size(), 0 );
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(), 0 );
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_deque_elimination )
158     {
159         typedef cds::container::FCQueue<int, std::queue< int, std::deque<int>>,
160             cds::container::fcqueue::make_traits<
161                 cds::opt::enable_elimination< true >
162             >::type
163         > queue_type;
164
165         queue_type q;
166         test( q );
167     }
168
169     TEST_F( FCQueue, std_deque_elimination_move )
170     {
171         typedef cds::container::FCQueue<std::string, std::queue< std::string, std::deque<std::string>>,
172             cds::container::fcqueue::make_traits<
173                 cds::opt::enable_elimination< true >
174             >::type
175         > queue_type;
176
177         queue_type q;
178         test_string( q );
179     }
180
181     TEST_F( FCQueue, std_deque_mutex )
182     {
183         typedef cds::container::FCQueue<int, std::queue< int, std::deque<int>>,
184             cds::container::fcqueue::make_traits<
185                 cds::opt::lock_type< std::mutex >
186             >::type
187         > queue_type;
188
189         queue_type q;
190         test( q );
191     }
192
193     TEST_F( FCQueue, std_list )
194     {
195         typedef cds::container::FCQueue<int, std::queue< int, std::list<int>>> queue_type;
196
197         queue_type q;
198         test( q );
199     }
200
201     TEST_F( FCQueue, std_list_move )
202     {
203         typedef cds::container::FCQueue<std::string, std::queue< std::string, std::list<std::string>>> queue_type;
204
205         queue_type q;
206         test_string( q );
207     }
208
209     TEST_F( FCQueue, std_list_elimination )
210     {
211         typedef cds::container::FCQueue<int, std::queue< int, std::list<int> >,
212             cds::container::fcqueue::make_traits<
213                 cds::opt::enable_elimination< true >
214             >::type
215         > queue_type;
216
217         queue_type q;
218         test( q );
219     }
220
221     TEST_F( FCQueue, std_list_elimination_move )
222     {
223         typedef cds::container::FCQueue<std::string, std::queue< std::string, std::list<std::string> >,
224             cds::container::fcqueue::make_traits<
225             cds::opt::enable_elimination< true >
226             >::type
227         > queue_type;
228
229         queue_type q;
230         test_string( q );
231     }
232
233     TEST_F( FCQueue, std_list_mutex )
234     {
235         typedef cds::container::FCQueue<int, std::queue<int, std::list<int> >,
236             cds::container::fcqueue::make_traits<
237                 cds::opt::lock_type< std::mutex >
238             >::type
239         > queue_type;
240
241         queue_type q;
242         test( q );
243     }
244
245 } // namespace