test_ring_buffer.cpp
Go to the documentation of this file.
1 #include <gtest/gtest.h>
2 #include <deque>
3 #include "../ring_buffer.h"
4 #include "../ISComm.h"
5 
6 #define BUFFER_SIZE 1024
7 
8 TEST(RingBuffer, U8_1for1_Test)
9 {
10  uint8_t buffer[BUFFER_SIZE] = { 0 };
11  ring_buf_t rb;
12  std::deque<uint8_t> myDeque;
13 
14  ringBufInit(&rb, buffer, sizeof(buffer), 1);
15 
16  uint8_t val, ref;
17  for (uint8_t i = 0; i < 255; i++)
18  {
19  EXPECT_FALSE(ringBufWrite(&rb, &i, 1));
20  myDeque.push_back(i);
21 
22  ringBufRead(&rb, &val, 1);
23  ref = myDeque.front();
24  myDeque.pop_front();
25  EXPECT_EQ(val, ref);
26  }
27 }
28 
29 
30 TEST(RingBuffer, U32_1for1_Test)
31 {
32  uint8_t buffer[BUFFER_SIZE];
33  ring_buf_t rb;
34  std::deque<uint32_t> myDeque;
35 
36  ringBufInit(&rb, buffer, sizeof(buffer), 1);
37 
38  uint32_t val, ref;
39  for (uint32_t i = 0; i < 1000000; i++)
40  {
41  EXPECT_FALSE(ringBufWrite(&rb, (uint8_t*)&i, 4));
42  myDeque.push_back(i);
43 
44  ringBufRead(&rb, (uint8_t*)&val, 4);
45  ref = myDeque.front();
46  myDeque.pop_front();
47  EXPECT_EQ(val, ref);
48  }
49 
50  // Ensure buffer is empty
51  EXPECT_TRUE((bool)(ringBufEmpty(&rb)) == true && myDeque.empty() == true);
52 }
53 
54 
55 TEST(RingBuffer, WriteReadWriteReadTest)
56 {
57  uint8_t buffer[BUFFER_SIZE];
58  ring_buf_t rb;
59  std::deque<uint8_t> myDeque;
60 
61  ringBufInit(&rb, buffer, BUFFER_SIZE, 1);
62 
63  uint8_t val, ref;
64 
65  // Write 201
66  for (uint8_t i = 0; i < 201; i++)
67  {
68  EXPECT_FALSE(ringBufWrite(&rb, &i, 1));
69  myDeque.push_back(i);
70  }
71 
72  // Read 50 (Leaving 151)
73  for (uint8_t i = 0; i < 50; i++)
74  {
75  ringBufRead(&rb, &val, 1);
76  ref = myDeque.front();
77  myDeque.pop_front();
78  EXPECT_EQ(val, ref);
79  }
80 
81  // Write 104 causing 255 total in buffer
82  for (uint8_t i = 0; i < 104; i++)
83  {
84  EXPECT_FALSE(ringBufWrite(&rb, &i, 1));
85  myDeque.push_back(i);
86  }
87 
88  // Read 254, leaving one in the buffer
89  for (uint8_t i = 0; i < 254; i++)
90  {
91  ringBufRead(&rb, &val, 1);
92  ref = myDeque.front();
93  myDeque.pop_front();
94  EXPECT_EQ(val, ref);
95  }
96 
97  // Ensure buffer has 1
98  EXPECT_TRUE(ringBufUsed(&rb) == 1 && myDeque.size() == 1);
99 
100  // Remove last element
101  ringBufRead(&rb, &val, 1);
102  ref = myDeque.front();
103  myDeque.pop_front();
104 
105  // Ensure buffer is empty
106  EXPECT_TRUE((bool)(ringBufEmpty(&rb))==true && myDeque.empty()==true);
107 }
108 
109 
110 TEST(RingBuffer, U32TestWriteThenReadTest)
111 {
112  uint32_t buffer[BUFFER_SIZE];
113  ring_buf_t rb;
114  std::deque<uint32_t> myDeque;
115 
116  ringBufInit(&rb, (uint8_t*)buffer, sizeof(buffer), 1);
117 
118  uint32_t val, ref;
119  for (uint32_t i = 0; i < BUFFER_SIZE-1; i++)
120  {
121  EXPECT_FALSE(ringBufWrite(&rb, (uint8_t*)&i, 4));
122  myDeque.push_back(i);
123  }
124 
125  for (uint32_t i = 0; i < BUFFER_SIZE-1; i++)
126  {
127  ringBufRead(&rb, (uint8_t*)&val, 4);
128  ref = myDeque.front();
129  myDeque.pop_front();
130 
131  EXPECT_EQ(val, ref);
132  }
133 }
134 
135 
136 TEST(RingBuffer, PacketTest)
137 {
138  static is_comm_instance_t comm;
139  static uint8_t comm_buffer[2048];
140  is_comm_init(&comm, comm_buffer, sizeof(comm_buffer));
141 
142  ins_1_t ins1 = { 0 };
143  int n;
144 
145  uint8_t buffer[BUFFER_SIZE];
146  ring_buf_t rb;
147  std::deque<uint8_t> myDeque;
148 
149  ringBufInit(&rb, buffer, sizeof(buffer), 1);
150 
151  // Write until buffer is full
152  for (int i = 0;; i++)
153  {
154  ins1.timeOfWeek = (double)i;
155  ins1.week = i;
156  ins1.insStatus = i;
157  ins1.hdwStatus = i;
158  ins1.theta[0] = i * 2.0f;
159  ins1.theta[1] = i * 3.0f;
160  ins1.theta[2] = i * 4.0f;
161  ins1.uvw[0] = i * 5.0f;
162  ins1.uvw[1] = i * 6.0f;
163  ins1.uvw[2] = i * 7.0f;
164  ins1.lla[0] = 40.330565516;
165  ins1.lla[1] = -111.725787806;
166  ins1.lla[2] = 1408.565264;
167  ins1.ned[0] = i * 1.234f;
168  ins1.ned[1] = i * 2.345f;
169  ins1.ned[2] = i * 3.456f;
170 
171  n = is_comm_data(&comm, DID_INS_1, 0, sizeof(ins_1_t), (void*)&(ins1));
172 
173  if (n > ringBufFree(&rb))
174  { // Buffer is full
175 
176  // Check that ring buffer and deque has save amount
177  EXPECT_EQ(ringBufUsed(&rb), myDeque.size());
178  break;
179  }
180 
181  EXPECT_FALSE(ringBufWrite(&rb, comm.buf.start, n));
182  for (int j = 0; j < n; j++)
183  {
184  myDeque.push_back(comm.buf.start[j]);
185  }
186  }
187 
188  // Read until buffer is empty
189  uint8_t val;
190  while (1)
191  {
192  if (ringBufEmpty(&rb) || myDeque.empty())
193  { // Buffer is empty
194 
195  // Ensure buffer is empty
196  EXPECT_TRUE((bool)(ringBufEmpty(&rb)) == true && myDeque.empty() == true);
197  break;
198  }
199 
200  ringBufRead(&rb, (uint8_t*)&val, 1);
201  EXPECT_EQ(val, myDeque.front());
202  myDeque.pop_front();
203  }
204 
205 }
206 
207 
208 TEST(RingBuffer, WriteOverflowTest)
209 {
210 #define BUFFERX_SIZE 1000
211 #define OVERLAP_SIZE 450
212  uint8_t buffer[BUFFERX_SIZE];
213  ring_buf_t rb;
214 
215  ringBufInit(&rb, buffer, sizeof(buffer), 1);
216 
217  // Buffer should one less than was allocated
218  EXPECT_TRUE(ringBufFree(&rb) == sizeof(buffer)-1);
219 
220  // Write BUFFER_X_SIZE + OVERLAP_SIZE
221  for (uint32_t i = 0; i < sizeof(buffer)+OVERLAP_SIZE; i++)
222  {
223  ringBufWrite(&rb, (uint8_t*)&i, 1);
224  }
225 
226  // Ensure buffer should have BUFFER_X_SIZE-1 bytes
227  EXPECT_TRUE(ringBufUsed(&rb) == sizeof(buffer)-1);
228 
229  // Check that overlap happened correctly
230  uint8_t val;
231  for (uint32_t i = OVERLAP_SIZE+1; ringBufUsed(&rb); i++)
232  {
233  ringBufRead(&rb, &val, 1);
234  uint8_t ii = i;
235  EXPECT_EQ(ii, val);
236  }
237 
238  // Ensure buffer is empty
239  EXPECT_TRUE((bool)(ringBufEmpty(&rb)) == true);
240 }
241 
242 
243 TEST(RingBuffer, FloatArrayWithOverflow)
244 {
245 #define BUFFERA_SIZE 4
246 #define OVRLAPA_SIZE 3
247  float buffer[BUFFERA_SIZE] = {};
248  ring_buf_t rb;
249 
250  ringBufInit(&rb, (uint8_t*)buffer, sizeof(buffer), sizeof(float));
251 
252  // Buffer should wordSize less than was allocated
253  EXPECT_TRUE(ringBufFree(&rb) == sizeof(buffer) - sizeof(float));
254 
255  // Write BUFFER_X_SIZE + OVERLAP_SIZE
256  for (float f = 0; f < BUFFERA_SIZE + OVRLAPA_SIZE; f++)
257  {
258  ringBufWrite(&rb, (uint8_t*)&f, sizeof(float));
259  }
260 
261  // Ensure buffer should have BUFFER_X_SIZE-wordSize bytes
262  EXPECT_TRUE(ringBufUsed(&rb) == sizeof(buffer) - sizeof(float));
263 
264  // Check that overlap happened correctly
265  float val=0;
266  for (float f = OVRLAPA_SIZE + 1; ringBufUsed(&rb); f++)
267  {
268  ringBufRead(&rb, (uint8_t*)&val, sizeof(float));
269  EXPECT_EQ(f, val);
270  }
271 
272  // Ensure buffer is empty
273  EXPECT_TRUE(ringBufEmpty(&rb) == 1);
274 }
int ringBufUsed(const ring_buf_t *rbuf)
This function returns the number of bytes currently in ring buffer.
Definition: ring_buffer.c:43
f_t f[DEBUG_F_ARRAY_SIZE]
Definition: data_sets.h:2106
is_can_uvw uvw
Definition: CAN_comm.h:257
#define DID_INS_1
Definition: data_sets.h:38
#define BUFFER_SIZE
#define BUFFERX_SIZE
TEST(RingBuffer, U8_1for1_Test)
uint32_t week
Definition: CAN_comm.h:26
void ringBufInit(ring_buf_t *rbuf, unsigned char *buf, int bufSize, int wordByteSize)
Initialize ring buffer pointers.
Definition: ring_buffer.c:29
double timeOfWeek
Definition: data_sets.h:417
is_comm_buffer_t buf
Definition: ISComm.h:489
uint32_t hdwStatus
Definition: CAN_comm.h:40
#define OVRLAPA_SIZE
uint8_t * start
Definition: ISComm.h:448
#define OVERLAP_SIZE
double lla[3]
Definition: data_sets.h:511
#define BUFFERA_SIZE
int ringBufRead(ring_buf_t *rbuf, unsigned char *buf, int len)
This function reads data from the ring buffer.
Definition: ring_buffer.c:136
int ringBufWrite(ring_buf_t *rb, unsigned char *buf, int numBytes)
This function writes data to the ring buffer.
Definition: ring_buffer.c:76
void is_comm_init(is_comm_instance_t *instance, uint8_t *buffer, int bufferSize)
Definition: ISComm.c:185
uint32_t insStatus
Definition: CAN_comm.h:37
float theta[3]
Definition: data_sets.h:505
int ringBufEmpty(const ring_buf_t *rbuf)
This function returns 1 if the buffer is empty, 0 if not empty.
Definition: ring_buffer.c:481
float ned[3]
Definition: data_sets.h:514
int is_comm_data(is_comm_instance_t *instance, uint32_t dataId, uint32_t offset, uint32_t size, void *data)
Definition: ISComm.c:639
int ringBufFree(const ring_buf_t *rbuf)
This function returns the number of bytes free in UART Rx buffer. Important: Buffer size is one less ...
Definition: ring_buffer.c:61


inertial_sense_ros
Author(s):
autogenerated on Sun Feb 28 2021 03:17:58