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 #include "test_data.h"
32 #include <cds/container/mspriority_queue.h>
50 class MSPQueue : public cds_test::PQueueTest
52 typedef cds_test::PQueueTest base_class;
54 template <class PQueue>
55 void test( PQueue& pq )
57 data_array<value_type> arr( pq.capacity());
58 value_type * pFirst = arr.begin();
59 value_type * pLast = pFirst + pq.capacity();
61 ASSERT_TRUE( pq.empty());
62 ASSERT_EQ( pq.size(), 0u );
63 ASSERT_EQ( pq.capacity(), size_t( base_class::c_nCapacity - 1 ));
68 for ( value_type * p = pFirst; p < pLast; ++p ) {
69 switch ( pq.size() & 3 ) {
71 ASSERT_TRUE( pq.push_with( [p]( value_type& dest ) { dest = *p; } ));
74 ASSERT_TRUE( pq.emplace( p->k, p->v ));
77 ASSERT_TRUE( pq.emplace( std::make_pair( p->k, p->v )));
80 ASSERT_TRUE( pq.push( *p ));
82 ASSERT_TRUE( !pq.empty());
83 ASSERT_TRUE( pq.size() == ++nSize );
86 ASSERT_TRUE( pq.full());
87 ASSERT_EQ( pq.size(), pq.capacity());
90 key_type k = base_class::c_nMinValue + key_type( base_class::c_nCapacity );
91 ASSERT_TRUE( !pq.push( k ));
92 ASSERT_TRUE( pq.full());
93 ASSERT_EQ( pq.size(), pq.capacity());
96 key_type nPrev = base_class::c_nMinValue + key_type( pq.capacity()) - 1;
99 ASSERT_TRUE( pq.pop( kv ));
100 EXPECT_EQ( kv.k, nPrev );
102 ASSERT_EQ( pq.size(), pq.capacity() - 1 );
103 ASSERT_TRUE( !pq.full());
104 ASSERT_TRUE( !pq.empty());
107 while ( pq.size() > 1 ) {
108 if ( pq.size() & 1 ) {
109 ASSERT_TRUE( pq.pop( kv ));
110 EXPECT_EQ( kv.k, nPrev - 1 );
114 ASSERT_TRUE( pq.pop_with( [&key]( value_type& src ) { key = src.k; } ));
115 EXPECT_EQ( key, nPrev - 1 );
120 ASSERT_EQ( pq.size(), nSize );
123 ASSERT_TRUE( !pq.full());
124 ASSERT_TRUE( !pq.empty());
125 ASSERT_EQ( pq.size(), 1u );
127 ASSERT_TRUE( pq.pop( kv ));
128 EXPECT_EQ( kv.k, base_class::c_nMinValue );
130 ASSERT_TRUE( !pq.full());
131 ASSERT_TRUE( pq.empty());
132 ASSERT_EQ( pq.size(), 0u );
135 for ( value_type * p = pFirst; p < pLast; ++p ) {
136 ASSERT_TRUE( pq.push( *p ));
138 ASSERT_TRUE( !pq.empty());
139 ASSERT_TRUE( pq.full());
140 ASSERT_EQ( pq.size(), pq.capacity());
142 ASSERT_TRUE( pq.empty());
143 ASSERT_TRUE( !pq.full());
144 ASSERT_EQ( pq.size(), 0u );
147 for ( value_type * p = pFirst; p < pLast; ++p ) {
148 ASSERT_TRUE( pq.push( *p ));
150 ASSERT_TRUE( !pq.empty());
151 ASSERT_TRUE( pq.full());
152 ASSERT_EQ( pq.size(), pq.capacity());
156 pq.clear_with( std::ref( disp ));
157 ASSERT_TRUE( pq.empty());
158 ASSERT_TRUE( !pq.full());
159 ASSERT_EQ( pq.size(), 0u );
160 ASSERT_EQ( disp.m_nCallCount, pq.capacity());
165 typedef cds::opt::v::initialized_dynamic_buffer< char > dyn_buffer_type;
166 typedef cds::opt::v::initialized_static_buffer< char, MSPQueue::c_nCapacity > static_buffer_type;
168 TEST_F( MSPQueue, dynamic )
170 typedef cds::container::MSPriorityQueue< MSPQueue::value_type,
171 cds::container::mspriority_queue::make_traits<
172 cds::opt::buffer< dyn_buffer_type >
176 pqueue pq( c_nCapacity );
180 TEST_F( MSPQueue, dynamic_cmp )
182 typedef cds::container::MSPriorityQueue< value_type,
183 cds::container::mspriority_queue::make_traits<
184 cds::opt::buffer< dyn_buffer_type >
185 , cds::opt::compare< compare >
189 pqueue pq( c_nCapacity );
193 TEST_F( MSPQueue, dynamic_less )
195 typedef cds::container::MSPriorityQueue< value_type,
196 cds::container::mspriority_queue::make_traits<
197 cds::opt::buffer< dyn_buffer_type >
198 ,cds::opt::less< less >
202 pqueue pq( c_nCapacity );
205 TEST_F( MSPQueue, dynamic_cmp_less )
207 struct pqueue_traits : public cds::container::mspriority_queue::traits
209 typedef dyn_buffer_type buffer;
210 typedef MSPQueue::less less;
211 typedef MSPQueue::compare compare;
213 typedef cds::container::MSPriorityQueue< value_type, pqueue_traits > pqueue;
215 pqueue pq( c_nCapacity );
219 TEST_F( MSPQueue, dynamic_mutex )
221 typedef cds::container::MSPriorityQueue< value_type,
222 cds::container::mspriority_queue::make_traits<
223 cds::opt::buffer< dyn_buffer_type >
224 ,cds::opt::compare< compare >
225 ,cds::opt::lock_type<std::mutex>
229 pqueue pq( c_nCapacity );
233 TEST_F( MSPQueue, stat )
235 typedef cds::container::MSPriorityQueue< MSPQueue::value_type,
236 cds::container::mspriority_queue::make_traits<
237 cds::opt::buffer< static_buffer_type >
241 std::unique_ptr< pqueue > pq( new pqueue(0));
245 TEST_F( MSPQueue, stat_cmp )
247 typedef cds::container::MSPriorityQueue< value_type,
248 cds::container::mspriority_queue::make_traits<
249 cds::opt::buffer< static_buffer_type >
250 ,cds::opt::compare< compare >
254 std::unique_ptr< pqueue > pq( new pqueue(0));
258 TEST_F( MSPQueue, stat_less )
260 typedef cds::container::MSPriorityQueue< value_type,
261 cds::container::mspriority_queue::make_traits<
262 cds::opt::buffer< static_buffer_type >
263 ,cds::opt::less< less >
267 std::unique_ptr< pqueue > pq( new pqueue(0));
271 TEST_F( MSPQueue, stat_mutex )
273 typedef cds::container::MSPriorityQueue< value_type,
274 cds::container::mspriority_queue::make_traits<
275 cds::opt::buffer< static_buffer_type >
276 ,cds::opt::less< less >
277 ,cds::opt::lock_type<std::mutex>
281 std::unique_ptr< pqueue > pq( new pqueue(0));