2 This file is a part of libcds - Concurrent Data Structures library
4 (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2016
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/intrusive/mspriority_queue.h>
50 class IntrusiveMSPQueue : 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 = arr.end();
61 ASSERT_TRUE( pq.empty() );
62 ASSERT_FALSE( pq.full() );
63 ASSERT_EQ( pq.size(), 0 );
64 ASSERT_EQ( pq.capacity(), base_class::c_nCapacity - 1 );
69 for ( value_type * p = pFirst; p < pLast; ++p ) {
70 ASSERT_TRUE( pq.push( *p ) );
71 ASSERT_FALSE( pq.empty() );
72 ASSERT_EQ( pq.size(), ++nSize );
75 ASSERT_TRUE( pq.full() );
76 ASSERT_EQ( pq.size(), pq.capacity() );
80 value_type k( base_class::c_nMinValue + key_type( base_class::c_nCapacity ));
81 ASSERT_FALSE( pq.push( k ) );
82 ASSERT_TRUE( pq.full() );
83 ASSERT_EQ( pq.size(), pq.capacity() );
87 key_type nPrev = base_class::c_nMinValue + key_type( pq.capacity() ) - 1;
88 value_type * p = pq.pop();
89 ASSERT_TRUE( p != nullptr );
90 EXPECT_EQ( p->k, nPrev );
92 ASSERT_EQ( pq.size(), pq.capacity() - 1 );
93 ASSERT_FALSE( pq.full() );
94 ASSERT_FALSE( pq.empty() );
97 while ( pq.size() > 1 ) {
99 ASSERT_TRUE( p != nullptr );
100 EXPECT_EQ( p->k, nPrev - 1 );
103 ASSERT_EQ( pq.size(), nSize );
106 ASSERT_FALSE( pq.full() );
107 ASSERT_FALSE( pq.empty() );
108 ASSERT_EQ( pq.size(), 1 );
111 ASSERT_TRUE( p != nullptr );
112 EXPECT_EQ( p->k, base_class::c_nMinValue );
114 ASSERT_FALSE( pq.full() );
115 ASSERT_TRUE( pq.empty() );
116 ASSERT_EQ( pq.size(), 0 );
119 for ( value_type * p = pFirst; p < pLast; ++p ) {
120 ASSERT_TRUE( pq.push( *p ) );
122 EXPECT_FALSE( pq.empty() );
123 EXPECT_TRUE( pq.full() );
124 EXPECT_EQ( pq.size(), pq.capacity() );
126 EXPECT_TRUE( pq.empty() );
127 EXPECT_FALSE( pq.full() );
128 EXPECT_EQ( pq.size(), 0 );
131 for ( value_type * p = pFirst; p < pLast; ++p ) {
132 ASSERT_TRUE( pq.push( *p ) );
134 ASSERT_FALSE( pq.empty() );
135 ASSERT_TRUE( pq.full() );
136 ASSERT_EQ( pq.size(), pq.capacity() );
140 pq.clear_with( std::ref( disp ) );
141 ASSERT_TRUE( pq.empty() );
142 ASSERT_FALSE( pq.full() );
143 ASSERT_EQ( pq.size(), 0 );
144 ASSERT_EQ( disp.m_nCallCount, pq.capacity() );
149 typedef cds::opt::v::dynamic_buffer< char > dyn_buffer_type;
150 typedef cds::opt::v::static_buffer< char, IntrusiveMSPQueue::c_nCapacity > static_buffer_type;
152 TEST_F( IntrusiveMSPQueue, dynamic )
154 struct traits : public cds::intrusive::mspriority_queue::traits
156 typedef dyn_buffer_type buffer;
158 typedef cds::intrusive::MSPriorityQueue< value_type, traits > pqueue;
160 pqueue pq( c_nCapacity );
164 TEST_F( IntrusiveMSPQueue, dynamic_cmp )
166 struct traits : public cds::intrusive::mspriority_queue::traits
168 typedef dyn_buffer_type buffer;
169 typedef IntrusiveMSPQueue::compare compare;
171 typedef cds::intrusive::MSPriorityQueue< value_type, traits > pqueue;
173 pqueue pq( c_nCapacity );
177 TEST_F( IntrusiveMSPQueue, dynamic_less )
179 typedef cds::intrusive::MSPriorityQueue< value_type,
180 cds::intrusive::mspriority_queue::make_traits<
181 cds::opt::buffer< dyn_buffer_type >
182 ,cds::opt::less< std::less<value_type> >
186 pqueue pq( c_nCapacity );
190 TEST_F( IntrusiveMSPQueue, dynamic_cmp_less )
192 typedef cds::intrusive::MSPriorityQueue< value_type,
193 cds::intrusive::mspriority_queue::make_traits<
194 cds::opt::buffer< dyn_buffer_type >
195 ,cds::opt::less< std::less<value_type> >
196 ,cds::opt::compare< IntrusiveMSPQueue::compare >
200 pqueue pq( c_nCapacity );
204 TEST_F( IntrusiveMSPQueue, dynamic_mutex )
206 struct traits : public cds::intrusive::mspriority_queue::traits
208 typedef dyn_buffer_type buffer;
209 typedef IntrusiveMSPQueue::compare compare;
210 typedef std::mutex lock_type;
212 typedef cds::intrusive::MSPriorityQueue< value_type, traits > pqueue;
214 pqueue pq( c_nCapacity );
218 TEST_F( IntrusiveMSPQueue, stat )
220 struct traits : public cds::intrusive::mspriority_queue::traits
222 typedef static_buffer_type buffer;
224 typedef cds::intrusive::MSPriorityQueue< value_type, traits > pqueue;
226 std::unique_ptr<pqueue> pq( new pqueue(0));
230 TEST_F( IntrusiveMSPQueue, stat_cmp )
232 typedef cds::intrusive::MSPriorityQueue< value_type,
233 cds::intrusive::mspriority_queue::make_traits<
234 cds::opt::buffer< static_buffer_type >
235 ,cds::opt::compare< compare >
239 std::unique_ptr<pqueue> pq( new pqueue( 0 ) );
243 TEST_F( IntrusiveMSPQueue, stat_less )
245 typedef cds::intrusive::MSPriorityQueue< value_type,
246 cds::intrusive::mspriority_queue::make_traits<
247 cds::opt::buffer< static_buffer_type >
248 ,cds::opt::less< less >
252 std::unique_ptr<pqueue> pq( new pqueue( 0 ) );
256 TEST_F( IntrusiveMSPQueue, stat_mutex )
258 struct traits : public cds::intrusive::mspriority_queue::traits
260 typedef static_buffer_type buffer;
261 typedef IntrusiveMSPQueue::compare compare;
262 typedef std::mutex lock_type;
264 typedef cds::intrusive::MSPriorityQueue< value_type, traits > pqueue;
266 std::unique_ptr<pqueue> pq( new pqueue( 0 ) );