13 #include <CTraitsTest.h> 15 #include <gtest/gtest.h> 17 template class mrpt::CTraitsTest<mrpt::containers::circular_buffer<char>>;
21 TEST(circular_buffer_tests, EmptyPop)
25 EXPECT_THROW(cb.
pop(ret), std::exception);
27 TEST(circular_buffer_tests, EmptyPopAfterPushes)
29 const size_t LEN = 20;
31 for (
size_t nWr = 0; nWr <
LEN; nWr++)
33 for (
size_t i = 0; i < nWr; i++) cb.
push(12);
35 for (
size_t i = 0; i < nWr; i++) cb.
pop(ret);
37 EXPECT_THROW(cb.
pop(ret), std::exception);
41 TEST(circular_buffer_tests, RandomWriteAndPeek)
43 const size_t LEN = 20;
46 for (
size_t iter = 0; iter < 1000; iter++)
50 for (
size_t i = 0; i < nWr; i++) cb.
push(i);
52 for (
size_t i = 0; i < nWr; i++)
55 EXPECT_EQ(ret,
cb_t(i));
57 for (
size_t i = 0; i < nWr; i++)
60 EXPECT_EQ(ret,
cb_t(i));
64 TEST(circular_buffer_tests, RandomWriteManyAndPeek)
66 const size_t LEN = 20;
68 std::vector<cb_t> dum_buf;
70 for (
size_t iter = 0; iter < 1000; iter++)
80 for (
size_t i = 0; i < nWr; i++) ret = cb.
peek(i);
89 for (
size_t i = 0; i < nWr; i++) cb.
pop(ret);
98 TEST(circular_buffer_tests, RandomWriteAndPeekOverrun)
100 const size_t LEN = 20;
103 for (
size_t iter = 0; iter < 100; iter++)
107 for (
size_t i = 0; i < nWr; i++) cb.
push(i);
109 for (
unsigned k = 0; k < 5; k++)
113 ret = cb.
peek(nWr + k);
114 GTEST_FAIL() <<
"Exception was expected but didn't happen!";
116 catch (std::exception&)
121 for (
size_t i = 0; i < nWr; i++) cb.
pop(ret);
125 TEST(circular_buffer_tests, Size)
128 for (
size_t i = 0; i < cb.
capacity() - 1; i++)
131 EXPECT_EQ(cb.
size(), i + 1);
133 EXPECT_ANY_THROW(cb.
push(0));
134 for (
size_t i = 0; i < cb.
capacity() - 1; i++)
uint32_t drawUniform32bit()
Generate a uniformly distributed pseudo-random number using the MT19937 algorithm, in the whole range of 32-bit integers.
size_t capacity() const
Return the maximum capacity of the buffer.
TEST(circular_buffer_tests, EmptyPop)
T pop()
Retrieve an element from the buffer.
T peek() const
Peek (see without modifying) what is to be read from the buffer if pop() was to be called...
size_t size() const
Return the number of elements available for read ("pop") in the buffer (this is NOT the maximum size ...
void pop_many(T *out_array, size_t count)
Pop a number of elements into a user-provided array.
void peek_many(T *out_array, size_t count) const
Like peek(), for multiple elements, storing a number of elements into a user-provided array...
A circular buffer of fixed size (defined at construction-time), implemented with a std::vector as the...
void push_many(T *array_elements, size_t count)
Insert an array of elements in the buffer.
CRandomGenerator & getRandomGenerator()
A static instance of a CRandomGenerator class, for use in single-thread applications.
void push(T d)
Insert a copy of the given element in the buffer.
#define MRPT_UNUSED_PARAM(a)
Determines whether this is an X86 or AMD64 platform.