Skip to content

Commit 3d827e8

Browse files
unit tests for circular buffer
1 parent d4eaa48 commit 3d827e8

File tree

1 file changed

+95
-2
lines changed

1 file changed

+95
-2
lines changed

platform/tests/UNITTESTS/CircularBuffer/test_CircularBuffer.cpp

Lines changed: 95 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,17 @@
1717

1818
#include "gtest/gtest.h"
1919
#include "platform/CircularBuffer.h"
20+
#include "mbed_critical_stub.c"
21+
22+
#define TEST_BUFFER_SIZE (10)
2023

2124
class TestCircularBuffer : public testing::Test {
2225
protected:
23-
mbed::CircularBuffer<int, 10> *buf;
26+
mbed::CircularBuffer<int, TEST_BUFFER_SIZE> *buf;
2427

2528
virtual void SetUp()
2629
{
27-
buf = new mbed::CircularBuffer<int, 10>;
30+
buf = new mbed::CircularBuffer<int, TEST_BUFFER_SIZE>;
2831
}
2932

3033
virtual void TearDown()
@@ -37,3 +40,93 @@ TEST_F(TestCircularBuffer, constructor)
3740
{
3841
EXPECT_TRUE(buf);
3942
}
43+
44+
TEST_F(TestCircularBuffer, push_pop)
45+
{
46+
int item = 0;
47+
buf->push(1);
48+
bool ret = buf->pop(item);
49+
EXPECT_TRUE(ret);
50+
EXPECT_EQ(item, 1);
51+
}
52+
53+
TEST_F(TestCircularBuffer, reset)
54+
{
55+
buf->push(1);
56+
EXPECT_EQ(buf->size(), 1);
57+
buf->reset();
58+
EXPECT_EQ(buf->size(), 0);
59+
}
60+
61+
TEST_F(TestCircularBuffer, pop_empty)
62+
{
63+
int item = 0;
64+
bool ret = buf->pop(item);
65+
EXPECT_FALSE(ret);
66+
}
67+
68+
TEST_F(TestCircularBuffer, push_pop_multiple)
69+
{
70+
const int test_numbers[TEST_BUFFER_SIZE] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
71+
72+
/* this will check pushing across the buffer end */
73+
for (int i = 0; i < TEST_BUFFER_SIZE; i++) {
74+
int test_numbers_popped[TEST_BUFFER_SIZE] = { 0 };
75+
buf->push(test_numbers, i);
76+
EXPECT_EQ(buf->size(), i);
77+
int number_of_items = buf->pop(test_numbers_popped, i);
78+
EXPECT_EQ(buf->size(), 0);
79+
EXPECT_EQ(number_of_items, i);
80+
EXPECT_TRUE(0 == memcmp(test_numbers, test_numbers_popped, i));
81+
}
82+
}
83+
84+
TEST_F(TestCircularBuffer, overflow)
85+
{
86+
const int test_numbers[TEST_BUFFER_SIZE] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
87+
int test_numbers_popped[TEST_BUFFER_SIZE] = { 0 };
88+
89+
buf->push(-1);
90+
91+
/* there is now not enough space for all the elements, old ones should be overwritten */
92+
93+
buf->push(test_numbers, TEST_BUFFER_SIZE);
94+
95+
int number_of_items = buf->pop(test_numbers_popped, TEST_BUFFER_SIZE);
96+
EXPECT_EQ(number_of_items, TEST_BUFFER_SIZE);
97+
EXPECT_TRUE(0 == memcmp(test_numbers, test_numbers_popped, TEST_BUFFER_SIZE));
98+
99+
/* there is a difference where the overflow is caused by a smaller write
100+
* and the buffer should retain part of old values */
101+
102+
buf->push(-1);
103+
buf->push(-2);
104+
buf->push(test_numbers, TEST_BUFFER_SIZE-1); /* -1 is overwritten but -2 is kept */
105+
106+
int popped_number;
107+
buf->pop(popped_number);
108+
EXPECT_EQ(popped_number, -2);
109+
110+
buf->pop(test_numbers_popped, TEST_BUFFER_SIZE - 1);
111+
EXPECT_TRUE(0 == memcmp(test_numbers, test_numbers_popped, TEST_BUFFER_SIZE - 1));
112+
}
113+
114+
TEST_F(TestCircularBuffer, writing_over_max_capacity)
115+
{
116+
const int test_numbers[TEST_BUFFER_SIZE + 1] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
117+
int test_numbers_popped[TEST_BUFFER_SIZE] = { 0 };
118+
119+
/* the loop creates different amounts of existing elements prior to write over capacity */
120+
for (int i = 0; i < TEST_BUFFER_SIZE; i++) {
121+
for (int j = 0; j < i; j++) {
122+
buf->push(-1);
123+
}
124+
/* first element should be dropped */
125+
buf->push(test_numbers, TEST_BUFFER_SIZE + 1);
126+
127+
int number_of_items = buf->pop(test_numbers_popped, TEST_BUFFER_SIZE + 1);
128+
EXPECT_EQ(number_of_items, TEST_BUFFER_SIZE);
129+
EXPECT_EQ(buf->size(), 0);
130+
EXPECT_TRUE(0 == memcmp(test_numbers + 1, test_numbers_popped, TEST_BUFFER_SIZE));
131+
}
132+
}

0 commit comments

Comments
 (0)