MRPT  1.9.9
circularbuffer_unittest.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2019, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include <CTraitsTest.h>
12 #include <mrpt/core/common.h>
13 #include <mrpt/random.h>
14 
15 #include <gtest/gtest.h>
16 
17 template class mrpt::CTraitsTest<mrpt::containers::circular_buffer<char>>;
18 
19 using cb_t = int;
20 
21 TEST(circular_buffer_tests, EmptyPop)
22 {
24  cb_t ret;
25  EXPECT_THROW(cb.pop(ret), std::exception);
26 }
27 TEST(circular_buffer_tests, EmptyPopAfterPushes)
28 {
29  const size_t LEN = 20;
31  for (size_t nWr = 0; nWr < LEN; nWr++)
32  {
33  for (size_t i = 0; i < nWr; i++) cb.push(12);
34  cb_t ret;
35  for (size_t i = 0; i < nWr; i++) cb.pop(ret);
36  // The next one must fail:
37  EXPECT_THROW(cb.pop(ret), std::exception);
38  }
39 }
40 
41 TEST(circular_buffer_tests, RandomWriteAndPeek)
42 {
43  const size_t LEN = 20;
45 
46  for (size_t iter = 0; iter < 1000; iter++)
47  {
48  const size_t nWr =
50  for (size_t i = 0; i < nWr; i++) cb.push(i);
51  cb_t ret;
52  for (size_t i = 0; i < nWr; i++)
53  {
54  ret = cb.peek(i);
55  EXPECT_EQ(ret, cb_t(i));
56  }
57  for (size_t i = 0; i < nWr; i++)
58  {
59  cb.pop(ret);
60  EXPECT_EQ(ret, cb_t(i));
61  }
62  }
63 }
64 TEST(circular_buffer_tests, RandomWriteManyAndPeek)
65 {
66  const size_t LEN = 20;
68  std::vector<cb_t> dum_buf;
69 
70  for (size_t iter = 0; iter < 1000; iter++)
71  {
72  const size_t nWr =
73  1 +
75  dum_buf.resize(nWr);
76  cb.push_many(&dum_buf[0], nWr);
77  cb_t ret;
78  if (iter % 2)
79  {
80  for (size_t i = 0; i < nWr; i++) ret = cb.peek(i);
81  MRPT_UNUSED_PARAM(ret);
82  }
83  else
84  {
85  cb.peek_many(&dum_buf[0], nWr);
86  }
87  if (iter % 3)
88  {
89  for (size_t i = 0; i < nWr; i++) cb.pop(ret);
90  MRPT_UNUSED_PARAM(ret);
91  }
92  else
93  {
94  cb.pop_many(&dum_buf[0], nWr);
95  }
96  }
97 }
98 TEST(circular_buffer_tests, RandomWriteAndPeekOverrun)
99 {
100  const size_t LEN = 20;
102 
103  for (size_t iter = 0; iter < 100; iter++)
104  {
105  const size_t nWr =
107  for (size_t i = 0; i < nWr; i++) cb.push(i);
108  cb_t ret;
109  for (unsigned k = 0; k < 5; k++)
110  {
111  try
112  {
113  ret = cb.peek(nWr + k);
114  GTEST_FAIL() << "Exception was expected but didn't happen!";
115  }
116  catch (std::exception&)
117  {
118  // OK
119  }
120  }
121  for (size_t i = 0; i < nWr; i++) cb.pop(ret);
122  }
123 }
124 
125 TEST(circular_buffer_tests, Size)
126 {
128  for (size_t i = 0; i < cb.capacity() - 1; i++)
129  {
130  cb.push(0);
131  EXPECT_EQ(cb.size(), i + 1);
132  }
133  EXPECT_ANY_THROW(cb.push(0));
134  for (size_t i = 0; i < cb.capacity() - 1; i++)
135  {
136  cb.pop();
137  EXPECT_EQ(cb.size(), cb.capacity() - 2 - i);
138  }
139 }
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)
Definition: inflate.h:44
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.
Definition: common.h:186



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 8fe78517f Sun Jul 14 19:43:28 2019 +0200 at lun oct 28 02:10:00 CET 2019