17
17
18
18
#include " gtest/gtest.h"
19
19
#include " platform/CircularBuffer.h"
20
+ #include " mbed_critical_stub.c"
21
+
22
+ #define TEST_BUFFER_SIZE (10 )
20
23
21
24
class TestCircularBuffer : public testing ::Test {
22
25
protected:
23
- mbed::CircularBuffer<int , 10 > *buf;
26
+ mbed::CircularBuffer<int , TEST_BUFFER_SIZE > *buf;
24
27
25
28
virtual void SetUp ()
26
29
{
27
- buf = new mbed::CircularBuffer<int , 10 >;
30
+ buf = new mbed::CircularBuffer<int , TEST_BUFFER_SIZE >;
28
31
}
29
32
30
33
virtual void TearDown ()
@@ -37,3 +40,93 @@ TEST_F(TestCircularBuffer, constructor)
37
40
{
38
41
EXPECT_TRUE (buf);
39
42
}
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