MRPT  2.0.4
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-2020, 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 #include <array>
15 
16 #include <gtest/gtest.h>
17 
18 template class mrpt::CTraitsTest<mrpt::containers::circular_buffer<char>>;
19 
20 using cb_t = int;
21 
22 TEST(circular_buffer_tests, EmptyPop)
23 {
25  cb_t ret;
26  EXPECT_THROW(cb.pop(ret), std::exception);
27 }
28 TEST(circular_buffer_tests, EmptyPopAfterPushes)
29 {
30  constexpr size_t LEN = 20;
32  for (size_t nWr = 0; nWr < LEN; nWr++)
33  {
34  for (size_t i = 0; i < nWr; i++) cb.push(12);
35  cb_t ret;
36  for (size_t i = 0; i < nWr; i++) cb.pop(ret);
37  // The next one must fail:
38  EXPECT_THROW(cb.pop(ret), std::exception);
39  }
40 }
41 
42 TEST(circular_buffer_tests, RandomWriteAndPeek)
43 {
44  constexpr size_t LEN = 20;
46 
47  for (size_t iter = 0; iter < 1000; iter++)
48  {
49  const size_t nWr =
51  for (size_t i = 0; i < nWr; i++) cb.push(i);
52  cb_t ret;
53  for (size_t i = 0; i < nWr; i++)
54  {
55  ret = cb.peek(i);
56  EXPECT_EQ(ret, cb_t(i));
57  }
58  for (size_t i = 0; i < nWr; i++)
59  {
60  cb.pop(ret);
61  EXPECT_EQ(ret, cb_t(i));
62  }
63  }
64 }
65 TEST(circular_buffer_tests, RandomWriteManyAndPeek)
66 {
67  constexpr size_t LEN = 20;
69  std::vector<cb_t> dum_buf;
70 
71  for (size_t iter = 0; iter < 1000; iter++)
72  {
73  const size_t nWr =
74  1 +
76  dum_buf.resize(nWr);
77  cb.push_many(&dum_buf[0], nWr);
78  cb_t ret;
79  if (iter % 2)
80  {
81  for (size_t i = 0; i < nWr; i++) ret = cb.peek(i);
82  (void)ret;
83  }
84  else
85  {
86  cb.peek_many(&dum_buf[0], nWr);
87  }
88  if (iter % 3)
89  {
90  for (size_t i = 0; i < nWr; i++) cb.pop(ret);
91  (void)ret;
92  }
93  else
94  {
95  cb.pop_many(&dum_buf[0], nWr);
96  }
97  }
98 }
99 TEST(circular_buffer_tests, RandomWriteAndPeekOverrun)
100 {
101  constexpr size_t LEN = 20;
103 
104  for (size_t iter = 0; iter < 100; iter++)
105  {
106  const size_t nWr =
108  for (size_t i = 0; i < nWr; i++) cb.push(i);
109  cb_t ret;
110  for (unsigned k = 0; k < 5; k++)
111  {
112  EXPECT_ANY_THROW(ret = cb.peek(nWr + k););
113  }
114  for (size_t i = 0; i < nWr; i++) cb.pop(ret);
115  }
116 }
117 
118 TEST(circular_buffer_tests, Size)
119 {
121  for (size_t i = 0; i < cb.capacity() - 1; i++)
122  {
123  cb.push(0);
124  EXPECT_EQ(cb.size(), i + 1);
125  }
126  EXPECT_ANY_THROW(cb.push(0));
127  for (size_t i = 0; i < cb.capacity() - 1; i++)
128  {
129  cb.pop();
130  EXPECT_EQ(cb.size(), cb.capacity() - 2 - i);
131  }
132 }
133 
134 template <typename T>
136 {
137  constexpr T LEN = 20;
139 
140  for (T i = 0; i < LEN; i++) cb.push(i);
141 
142  std::array<T, LEN> peek_vals;
143  cb.peek_many(&peek_vals[0], LEN);
144 
145  for (T i = 0; i < LEN; i++)
146  EXPECT_EQ(static_cast<int>(peek_vals[i]), static_cast<int>(i));
147 }
148 
149 TEST(circular_buffer_tests, WritePeekCheck_uint8_t)
150 {
151  impl_WritePeekCheck<uint8_t>();
152 }
153 TEST(circular_buffer_tests, WritePeekCheck_uint16_t)
154 {
155  impl_WritePeekCheck<uint16_t>();
156 }
157 TEST(circular_buffer_tests, WritePeekCheck_uint32_t)
158 {
159  impl_WritePeekCheck<uint32_t>();
160 }
161 TEST(circular_buffer_tests, WritePeekCheck_uint64_t)
162 {
163  impl_WritePeekCheck<uint64_t>();
164 }
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)
void impl_WritePeekCheck()
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...
EXPECT_EQ(out.image_pair_was_used.size(), NUM_IMGS)
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.



Page generated by Doxygen 1.8.14 for MRPT 2.0.4 Git: 33de1d0ad Sat Jun 20 11:02:42 2020 +0200 at sáb jun 20 17:35:17 CEST 2020