Main MRPT website > C++ reference for MRPT 1.5.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 <gtest/gtest.h>
13 
14 typedef int cb_t;
15 
16 TEST(circular_buffer_tests, EmptyPop)
17 {
19  try {
20  cb_t ret;
21  cb.pop(ret);
22  GTEST_FAIL() << "Exception was expected but didn't happen!";
23  } catch (std::exception &) {
24  // OK
25  }
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  try {
38  cb.pop(ret);
39  GTEST_FAIL() << "Exception was expected but didn't happen!";
40  } catch (std::exception &) {
41  // OK
42  }
43  }
44 }
45 
46 TEST(circular_buffer_tests, RandomWriteAndPeek)
47 {
48  const size_t LEN = 20;
50 
51  for (size_t iter=0;iter<1000;iter++)
52  {
53  const size_t nWr = mrpt::random::randomGenerator.drawUniform32bit() % LEN;
54  for (size_t i=0;i<nWr;i++) cb.push(i);
55  cb_t ret;
56  for (size_t i=0;i<nWr;i++) {
57  ret=cb.peek(i);
58  EXPECT_EQ(ret,cb_t(i));
59  }
60  for (size_t i=0;i<nWr;i++) {
61  cb.pop(ret);
62  EXPECT_EQ(ret,cb_t(i));
63  }
64  }
65 }
66 TEST(circular_buffer_tests, RandomWriteManyAndPeek)
67 {
68  const size_t LEN = 20;
70  std::vector<cb_t> dum_buf;
71 
72  for (size_t iter=0;iter<1000;iter++)
73  {
74  const size_t nWr = 1+mrpt::random::randomGenerator.drawUniform32bit() % (LEN-1);
75  dum_buf.resize(nWr);
76  cb.push_many(&dum_buf[0],nWr);
77  cb_t ret;
78  if (iter%1) {
79  for (size_t i=0;i<nWr;i++) ret=cb.peek(i);
80  MRPT_UNUSED_PARAM(ret);
81  } else {
82  cb.peek_many(&dum_buf[0],nWr);
83  }
84  cb.pop_many(&dum_buf[0],nWr);
85  }
86 }
87 TEST(circular_buffer_tests, RandomWriteAndPeekOverrun)
88 {
89  const size_t LEN = 20;
91 
92  for (size_t iter=0;iter<100;iter++)
93  {
94  const size_t nWr = mrpt::random::randomGenerator.drawUniform32bit() % LEN;
95  for (size_t i=0;i<nWr;i++) cb.push(i);
96  cb_t ret;
97  for (unsigned k=0;k<5;k++) {
98  try {
99  ret=cb.peek(nWr+k);
100  GTEST_FAIL() << "Exception was expected but didn't happen!";
101  } catch (std::exception &) {
102  // OK
103  }
104  }
105  for (size_t i=0;i<nWr;i++) cb.pop(ret);
106  }
107 }
108 
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...
BASE_IMPEXP CRandomGenerator randomGenerator
A static instance of a CRandomGenerator class, for use in single-thread applications.
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.



Page generated by Doxygen 1.8.14 for MRPT 1.5.9 Git: 690a4699f Wed Apr 15 19:29:53 2020 +0200 at miƩ abr 15 19:30:12 CEST 2020