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  EXPECT_ANY_THROW(ret = cb.peek(nWr + k););
112  }
113  for (size_t i = 0; i < nWr; i++) cb.pop(ret);
114  }
115 }
116 
117 TEST(circular_buffer_tests, Size)
118 {
120  for (size_t i = 0; i < cb.capacity() - 1; i++)
121  {
122  cb.push(0);
123  EXPECT_EQ(cb.size(), i + 1);
124  }
125  EXPECT_ANY_THROW(cb.push(0));
126  for (size_t i = 0; i < cb.capacity() - 1; i++)
127  {
128  cb.pop();
129  EXPECT_EQ(cb.size(), cb.capacity() - 2 - i);
130  }
131 }
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...
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.
#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: 6e6d28d09 Wed Nov 13 18:58:42 2019 +0100 at miƩ nov 13 19:00:09 CET 2019