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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
|
// 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_GENERIC_QUEUE_H
#define CDSUNIT_QUEUE_TEST_GENERIC_QUEUE_H
#include <cds_test/check_size.h>
namespace cds_test {
class generic_queue : public ::testing::Test
{
protected:
template <typename Queue>
void test( Queue& q )
{
typedef typename Queue::value_type value_type;
value_type it;
const size_t nSize = 100;
ASSERT_TRUE( q.empty());
ASSERT_CONTAINER_SIZE( q, 0 );
// enqueue/dequeue
for ( size_t i = 0; i < nSize; ++i ) {
it = static_cast<value_type>(i);
ASSERT_TRUE( q.enqueue( it ));
ASSERT_CONTAINER_SIZE( q, i + 1 );
}
ASSERT_FALSE( q.empty());
ASSERT_CONTAINER_SIZE( q, nSize );
for ( size_t i = 0; i < nSize; ++i ) {
it = -1;
ASSERT_TRUE( q.dequeue( it ));
ASSERT_EQ( it, static_cast<value_type>( i ));
ASSERT_CONTAINER_SIZE( q, nSize - i - 1 );
}
ASSERT_TRUE( q.empty());
ASSERT_CONTAINER_SIZE( q, 0 );
// push/pop
for ( size_t i = 0; i < nSize; ++i ) {
it = static_cast<value_type>(i);
ASSERT_TRUE( q.push( it ));
ASSERT_CONTAINER_SIZE( q, i + 1 );
}
ASSERT_FALSE( q.empty());
ASSERT_CONTAINER_SIZE( q, nSize );
for ( size_t i = 0; i < nSize; ++i ) {
it = -1;
ASSERT_TRUE( q.pop( it ));
ASSERT_EQ( it, static_cast<value_type>( i ));
ASSERT_CONTAINER_SIZE( q, nSize - i - 1 );
}
ASSERT_TRUE( q.empty());
ASSERT_CONTAINER_SIZE( q, 0 );
// push/pop with lambda
for ( size_t i = 0; i < nSize; ++i ) {
it = static_cast<value_type>(i);
ASSERT_NE( it, -1 );
auto f = [&it]( value_type& dest ) { dest = it; it = -1; };
if ( i & 1 )
ASSERT_TRUE( q.enqueue_with( f ));
else
ASSERT_TRUE( q.push_with( f ));
ASSERT_EQ( it, -1 );
ASSERT_CONTAINER_SIZE( q, i + 1 );
}
ASSERT_FALSE( q.empty());
ASSERT_CONTAINER_SIZE( q, nSize );
for ( size_t i = 0; i < nSize; ++i ) {
it = -1;
auto f = [&it]( value_type& src ) { it = src; src = -1; };
if ( i & 1 )
ASSERT_TRUE( q.pop_with( f ));
else
ASSERT_TRUE( q.dequeue_with( f ));
ASSERT_EQ( it, static_cast<value_type>( i ));
ASSERT_CONTAINER_SIZE( q, nSize - i - 1 );
}
ASSERT_TRUE( q.empty());
ASSERT_CONTAINER_SIZE( q, 0 );
// clear
for ( size_t i = 0; i < nSize; ++i ) {
ASSERT_TRUE( q.push( static_cast<value_type>(i)));
}
ASSERT_FALSE( q.empty());
ASSERT_CONTAINER_SIZE( q, nSize );
q.clear();
ASSERT_TRUE( q.empty());
ASSERT_CONTAINER_SIZE( q, 0 );
// pop from empty queue
it = nSize * 2;
ASSERT_FALSE( q.pop( it ));
ASSERT_EQ( it, static_cast<value_type>( nSize * 2 ));
ASSERT_TRUE( q.empty());
ASSERT_CONTAINER_SIZE( q, 0 );
ASSERT_FALSE( q.dequeue( it ));
ASSERT_EQ( it, static_cast<value_type>( nSize * 2 ));
ASSERT_TRUE( q.empty());
ASSERT_CONTAINER_SIZE( q, 0 );
}
template <class Queue>
void test_string( Queue& q )
{
std::string str[3];
str[0] = "one";
str[1] = "two";
str[2] = "three";
const size_t nSize = sizeof( str ) / sizeof( str[0] );
// emplace
for ( size_t i = 0; i < nSize; ++i ) {
ASSERT_TRUE( q.emplace( str[i].c_str()));
ASSERT_CONTAINER_SIZE( q, i + 1 );
}
ASSERT_FALSE( q.empty());
ASSERT_CONTAINER_SIZE( q, nSize );
{
std::string s;
auto f = [&s]( std::string& src ) {
ASSERT_FALSE( src.empty());
s = std::move( src );
ASSERT_NE( s, src );
};
for ( size_t i = 0; i < nSize; ++i ) {
if ( i & 1 )
ASSERT_TRUE( q.pop_with( f ));
else
ASSERT_TRUE( q.dequeue_with( f ));
ASSERT_CONTAINER_SIZE( q, nSize - i - 1 );
ASSERT_EQ( s, str[i] );
}
}
ASSERT_TRUE( q.empty());
ASSERT_CONTAINER_SIZE( q, 0 );
// move push
for ( size_t i = 0; i < nSize; ++i ) {
std::string s = str[i];
ASSERT_FALSE( s.empty());
if ( i & 1 )
ASSERT_TRUE( q.enqueue( std::move( s )));
else
ASSERT_TRUE( q.push( std::move( s )));
ASSERT_TRUE( s.empty());
ASSERT_CONTAINER_SIZE( q, i + 1 );
}
ASSERT_FALSE( q.empty());
ASSERT_CONTAINER_SIZE( q, nSize );
for ( size_t i = 0; i < nSize; ++i ) {
std::string s;
ASSERT_TRUE( q.pop( s ));
ASSERT_CONTAINER_SIZE( q, nSize - i - 1 );
ASSERT_EQ( s, str[i] );
}
ASSERT_TRUE( q.empty());
ASSERT_CONTAINER_SIZE( q, 0 );
}
};
} // namespace cds_test
#endif // CDSUNIT_QUEUE_TEST_GENERIC_QUEUE_H
|