3 #ifndef CDSTEST_HDR_INTRUSIVE_PQUEUE_H
4 #define CDSTEST_HDR_INTRUSIVE_PQUEUE_H
6 #include "cppunit/cppunit_proxy.h"
7 #include "size_check.h"
9 #include <functional> // ref
11 namespace priority_queue {
13 namespace intrusive_pqueue {
14 static size_t const c_nCapacity = 1024 * 16;
16 struct another_disposer {
29 template <typename PQueue>
31 static size_t const nCapacity = c_nCapacity;
33 } // namespace intrusive_pqueue
35 class IntrusivePQueueHdrTest: public CppUnitMini::TestCase
38 static size_t const c_nCapacity = intrusive_pqueue::c_nCapacity;
41 static key_type const c_nMinValue = -123;
44 int operator()( key_type k1, key_type k2 ) const
57 data_array( size_t nSize )
58 : pFirst( new T[nSize] )
59 , pLast( pFirst + nSize )
62 for ( T * p = pFirst; p != pLast; ++p, ++i )
65 std::random_shuffle( pFirst, pLast );
73 T * begin() { return pFirst; }
74 T * end() { return pLast ; }
77 return pLast - pFirst;
82 template <class PQueue>
83 void test_bounded_with( PQueue& pq )
85 data_array<key_type> arr( pq.capacity() );
86 key_type * pFirst = arr.begin();
87 key_type * pLast = pFirst + pq.capacity();
89 CPPUNIT_ASSERT( pq.empty() );
90 CPPUNIT_ASSERT( pq.size() == 0 );
91 CPPUNIT_ASSERT( pq.capacity() == intrusive_pqueue::constants<PQueue>::nCapacity );
96 for ( key_type * p = pFirst; p < pLast; ++p ) {
97 CPPUNIT_ASSERT( pq.push( *p ));
98 CPPUNIT_ASSERT( !pq.empty() );
99 CPPUNIT_ASSERT( pq.size() == ++nSize );
102 CPPUNIT_ASSERT( pq.full() );
103 CPPUNIT_ASSERT( pq.size() == pq.capacity() );
106 key_type k = c_nMinValue + key_type(c_nCapacity);
107 CPPUNIT_ASSERT( !pq.push( k ));
108 CPPUNIT_ASSERT( pq.full() );
109 CPPUNIT_ASSERT( pq.size() == pq.capacity() );
112 key_type nPrev = c_nMinValue + key_type(pq.capacity()) - 1;
113 key_type * p = pq.pop();
114 CPPUNIT_ASSERT( p != nullptr );
115 CPPUNIT_CHECK_EX( *p == nPrev, "Expected=" << nPrev << ", current=" << *p );
117 CPPUNIT_ASSERT( pq.size() == pq.capacity() - 1 );
118 CPPUNIT_ASSERT( !pq.full() );
119 CPPUNIT_ASSERT( !pq.empty() );
122 while ( pq.size() > 1 ) {
124 CPPUNIT_ASSERT( p != nullptr );
125 CPPUNIT_CHECK_EX( *p == nPrev - 1, "Expected=" << nPrev - 1 << ", current=" << *p );
128 CPPUNIT_ASSERT( pq.size() == nSize );
131 CPPUNIT_ASSERT( !pq.full() );
132 CPPUNIT_ASSERT( !pq.empty() );
133 CPPUNIT_ASSERT( pq.size() == 1 );
136 CPPUNIT_ASSERT( p != nullptr );
137 CPPUNIT_CHECK_EX( *p == c_nMinValue, "Expected=" << c_nMinValue << ", current=" << *p );
139 CPPUNIT_ASSERT( !pq.full() );
140 CPPUNIT_ASSERT( pq.empty() );
141 CPPUNIT_ASSERT( pq.size() == 0 );
144 for ( key_type * p = pFirst; p < pLast; ++p ) {
145 CPPUNIT_ASSERT( pq.push( *p ));
147 CPPUNIT_CHECK( !pq.empty() );
148 CPPUNIT_CHECK( pq.full() );
149 CPPUNIT_CHECK( pq.size() == pq.capacity() );
151 CPPUNIT_CHECK( pq.empty() );
152 CPPUNIT_CHECK( !pq.full() );
153 CPPUNIT_CHECK( pq.size() == 0 );
156 for ( key_type * p = pFirst; p < pLast; ++p ) {
157 CPPUNIT_ASSERT( pq.push( *p ));
159 CPPUNIT_ASSERT( !pq.empty() );
160 CPPUNIT_ASSERT( pq.full() );
161 CPPUNIT_ASSERT( pq.size() == pq.capacity() );
164 intrusive_pqueue::another_disposer disp;
165 pq.clear_with( std::ref(disp) );
166 CPPUNIT_ASSERT( pq.empty() );
167 CPPUNIT_ASSERT( !pq.full() );
168 CPPUNIT_ASSERT( pq.size() == 0 );
169 CPPUNIT_ASSERT( disp.m_nCallCount == pq.capacity() );
173 template <class PQueue>
176 PQueue pq( 0 ); // argument should be ignored for static buffer
177 test_bounded_with( pq );
179 template <class PQueue>
182 PQueue pq( c_nCapacity );
183 test_bounded_with( pq );
188 void MSPQueue_st_cmp();
189 void MSPQueue_st_less();
190 void MSPQueue_st_cmpless();
191 void MSPQueue_st_cmp_mtx();
193 void MSPQueue_dyn_cmp();
194 void MSPQueue_dyn_less();
195 void MSPQueue_dyn_cmpless();
196 void MSPQueue_dyn_cmp_mtx();
198 CPPUNIT_TEST_SUITE(IntrusivePQueueHdrTest)
199 CPPUNIT_TEST(MSPQueue_st)
200 CPPUNIT_TEST(MSPQueue_st_cmp)
201 CPPUNIT_TEST(MSPQueue_st_less)
202 CPPUNIT_TEST(MSPQueue_st_cmpless)
203 CPPUNIT_TEST(MSPQueue_st_cmp_mtx)
204 CPPUNIT_TEST(MSPQueue_dyn)
205 CPPUNIT_TEST(MSPQueue_dyn_cmp)
206 CPPUNIT_TEST(MSPQueue_dyn_less)
207 CPPUNIT_TEST(MSPQueue_dyn_cmpless)
208 CPPUNIT_TEST(MSPQueue_dyn_cmp_mtx)
209 CPPUNIT_TEST_SUITE_END()
212 } // namespace priority_queue
214 #endif // #ifndef CDSTEST_HDR_INTRUSIVE_PQUEUE_H