Main MRPT website > C++ reference for MRPT 1.9.9
circularbuffer_unittest.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
11 #include <mrpt/random.h>
12 #include <mrpt/utils/CTraitsTest.h>
13 
14 #include <gtest/gtest.h>
15 
17 
18 typedef int cb_t;
19 
20 TEST(circular_buffer_tests, EmptyPop)
21 {
23  try
24  {
25  cb_t ret;
26  cb.pop(ret);
27  GTEST_FAIL() << "Exception was expected but didn't happen!";
28  }
29  catch (std::exception&)
30  {
31  // OK
32  }
33 }
34 TEST(circular_buffer_tests, EmptyPopAfterPushes)
35 {
36  const size_t LEN = 20;
38  for (size_t nWr = 0; nWr < LEN; nWr++)
39  {
40  for (size_t i = 0; i < nWr; i++) cb.push(12);
41  cb_t ret;
42  for (size_t i = 0; i < nWr; i++) cb.pop(ret);
43  // The next one must fail:
44  try
45  {
46  cb.pop(ret);
47  GTEST_FAIL() << "Exception was expected but didn't happen!";
48  }
49  catch (std::exception&)
50  {
51  // OK
52  }
53  }
54 }
55 
56 TEST(circular_buffer_tests, RandomWriteAndPeek)
57 {
58  const size_t LEN = 20;
60 
61  for (size_t iter = 0; iter < 1000; iter++)
62  {
63  const size_t nWr =
65  for (size_t i = 0; i < nWr; i++) cb.push(i);
66  cb_t ret;
67  for (size_t i = 0; i < nWr; i++)
68  {
69  ret = cb.peek(i);
70  EXPECT_EQ(ret, cb_t(i));
71  }
72  for (size_t i = 0; i < nWr; i++)
73  {
74  cb.pop(ret);
75  EXPECT_EQ(ret, cb_t(i));
76  }
77  }
78 }
79 TEST(circular_buffer_tests, RandomWriteManyAndPeek)
80 {
81  const size_t LEN = 20;
83  std::vector<cb_t> dum_buf;
84 
85  for (size_t iter = 0; iter < 1000; iter++)
86  {
87  const size_t nWr =
89  dum_buf.resize(nWr);
90  cb.push_many(&dum_buf[0], nWr);
91  cb_t ret;
92  if (iter % 1)
93  {
94  for (size_t i = 0; i < nWr; i++) ret = cb.peek(i);
95  MRPT_UNUSED_PARAM(ret);
96  }
97  else
98  {
99  cb.peek_many(&dum_buf[0], nWr);
100  }
101  cb.pop_many(&dum_buf[0], nWr);
102  }
103 }
104 TEST(circular_buffer_tests, RandomWriteAndPeekOverrun)
105 {
106  const size_t LEN = 20;
108 
109  for (size_t iter = 0; iter < 100; iter++)
110  {
111  const size_t nWr =
113  for (size_t i = 0; i < nWr; i++) cb.push(i);
114  cb_t ret;
115  for (unsigned k = 0; k < 5; k++)
116  {
117  try
118  {
119  ret = cb.peek(nWr + k);
120  GTEST_FAIL() << "Exception was expected but didn't happen!";
121  }
122  catch (std::exception&)
123  {
124  // OK
125  }
126  }
127  for (size_t i = 0; i < nWr; i++) cb.pop(ret);
128  }
129 }
uint32_t drawUniform32bit()
Generate a uniformly distributed pseudo-random number using the MT19937 algorithm, in the whole range of 32-bit integers.
T peek() const
Peek (see without modifying) what is to be read from the buffer if pop() was to be called...
TEST(circular_buffer_tests, EmptyPop)
Definition: inflate.h:43
void pop_many(T *out_array, size_t count)
Pop a number of elements into a user-provided array.
#define MRPT_UNUSED_PARAM(a)
Can be used to avoid "not used parameters" warnings from the compiler.
T pop()
Retrieve an element from the buffer.
A circular buffer of fixed size (defined at construction-time), implemented with a std::vector as the...
void push(T d)
Insert a copy of the given element in the buffer.
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...
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.



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019