1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
|
// Copyright (c) 2006-2018 Maxim Khizhinsky
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt)
#ifndef CDSUNIT_QUEUE_TEST_INTRUSIVE_BOUNDED_QUEUE_H
#define CDSUNIT_QUEUE_TEST_INTRUSIVE_BOUNDED_QUEUE_H
#include <cds_test/check_size.h>
#include <vector>
namespace cds_test {
class intrusive_bounded_queue : public ::testing::Test
{
protected:
struct item {
int nVal;
int nDisposeCount;
item()
: nDisposeCount( 0 )
{}
};
protected:
template <typename Queue>
void test( Queue& q )
{
typedef typename Queue::value_type value_type;
value_type it;
const size_t nSize = q.capacity();
ASSERT_TRUE( q.empty());
ASSERT_CONTAINER_SIZE( q, 0 );
std::vector< value_type > arr;
arr.resize( nSize );
for ( size_t i = 0; i < nSize; ++i )
arr[i].nVal = static_cast<int>(i);
// push
for ( auto& i : arr ) {
if ( i.nVal & 1 ) {
ASSERT_TRUE( q.push( i ));
}
else {
ASSERT_TRUE( q.enqueue( i ));
}
ASSERT_CONTAINER_SIZE( q, i.nVal + 1 );
ASSERT_FALSE( q.empty());
}
ASSERT_CONTAINER_SIZE( q, q.capacity());
// pop
int val = 0;
while ( !q.empty()) {
value_type * v;
if ( val & 1 )
v = q.pop();
else
v = q.dequeue();
ASSERT_TRUE( v != nullptr );
ASSERT_EQ( v->nVal, val );
++val;
ASSERT_CONTAINER_SIZE( q, nSize - static_cast<size_t>( val ));
}
ASSERT_EQ( val, static_cast<int>( nSize ));
ASSERT_TRUE( q.empty());
ASSERT_CONTAINER_SIZE( q, 0 );
// pop from empty queue
{
value_type * v = q.pop();
ASSERT_TRUE( v == nullptr );
ASSERT_TRUE( q.empty());
ASSERT_CONTAINER_SIZE( q, 0 );
}
// clear
for ( auto& i : arr ) {
ASSERT_TRUE( q.push( i ));
}
ASSERT_FALSE( q.empty());
ASSERT_CONTAINER_SIZE( q, q.capacity());
q.clear();
ASSERT_TRUE( q.empty());
ASSERT_CONTAINER_SIZE( q, 0 );
if ( std::is_same<typename Queue::disposer, cds::intrusive::opt::v::empty_disposer>::value ) {
// no disposer
for ( auto& i : arr ) {
ASSERT_EQ( i.nDisposeCount, 0 );
}
}
else {
// check the disposer has been called
for ( auto& i : arr ) {
ASSERT_EQ( i.nDisposeCount, 1 );
}
}
// clear with disposer
for ( auto& i : arr ) {
ASSERT_TRUE( q.push( i ));
i.nDisposeCount = 0;
}
ASSERT_FALSE( q.empty());
ASSERT_CONTAINER_SIZE( q, q.capacity());
q.clear( []( value_type * p ) { p->nDisposeCount = p->nVal + 1; } );
ASSERT_TRUE( q.empty());
ASSERT_CONTAINER_SIZE( q, 0 );
// check the disposer has not been called
for ( auto& i : arr ) {
ASSERT_EQ( i.nDisposeCount, i.nVal + 1 );
}
}
};
} // namespace cds_test
#endif // CDSUNIT_QUEUE_TEST_INTRUSIVE_BOUNDED_QUEUE_H
|