blob: 01e057f96be3448a509f7a01aafb1c6a463bbb3a [file] [log] [blame]
James Kuszmaul97f750d2019-01-20 20:08:03 -08001#include "aos/containers/ring_buffer.h"
Parker Schuhecd057f2017-03-11 20:03:01 -08002
3#include "gtest/gtest.h"
4
5namespace aos {
6namespace testing {
7
8class RingBufferTest : public ::testing::Test {
9 public:
10 RingBufferTest() {}
11
12 protected:
13 RingBuffer<int, 10> buffer_;
14};
15
16// Test if the RingBuffer is empty when initialized properly
17TEST_F(RingBufferTest, DefaultIsEmpty) {
18 // The RingBuffer should have a size of 0, a capacity of 10 (note that it was
19 // initialized as 10), have no items, and not be full
20 ASSERT_EQ(0u, buffer_.size());
21 ASSERT_EQ(10u, buffer_.capacity());
22 ASSERT_TRUE(buffer_.empty());
23 ASSERT_FALSE(buffer_.full());
24}
25
26// Test that the RingBuffer can fill it's entire capacity and read back the data
27TEST_F(RingBufferTest, CanAddData) {
28 ASSERT_TRUE(buffer_.empty());
29
30 // Add sequential numbers to the RingBuffer
31 // (the value of each item is it's index #)
32 for (size_t i = 0; i < buffer_.capacity(); ++i) {
33 // The buffer shouldn't be full yet, and it's size should be how many items
34 // we've added so far. Once that happens, we add another item
35 ASSERT_FALSE(buffer_.full());
36 ASSERT_EQ(i, buffer_.size());
37 buffer_.Push(i);
38
39 // The buffer shouldn't be empty and it's size should be 1 more since we
40 // just
41 // added an item. Also, the last item in the buffer should equal the one we
42 // just added
43 ASSERT_FALSE(buffer_.empty());
44 ASSERT_EQ(i + 1, buffer_.size());
45 ASSERT_EQ(i, buffer_[i]);
46 }
47
48 ASSERT_TRUE(buffer_.full());
49}
50
51// Tests that the RingBuffer properly loops back and starts overwriting from the
52// first element after being filled up
53TEST_F(RingBufferTest, OverfillData) {
54 // Add numbers 0-24 to the RingBuffer
55 for (int i = 0; i < 25; ++i) {
56 buffer_.Push(i);
57 }
58
59 // It should now be full
60 ASSERT_TRUE(buffer_.full());
61
62 // Since the buffer is a size of 10 and has been filled up 2.5 times, it
63 // should
64 // now contain the numbers 15-24
65 for (size_t i = 0; i < buffer_.size(); ++i) {
66 ASSERT_EQ(15 + i, buffer_[i]);
67 }
68}
69
Parker Schuh208a58d2017-04-12 20:51:38 -070070// Tests shifting from the front of the ringbuffer.
71TEST_F(RingBufferTest, RingBufferShift) {
72 // Add numbers 0-24 to the RingBuffer
73 for (int i = 0; i < 25; ++i) {
74 buffer_.Push(i);
75 }
76
77 // It should now be full
78 ASSERT_TRUE(buffer_.full());
79
80 buffer_.Shift();
81 buffer_.Shift();
82 buffer_.Shift();
83
84 ASSERT_EQ(buffer_.size(), 7);
85
86 // The buffer should now contain the numbers 18-24
87 for (size_t i = 0; i < buffer_.size(); ++i) {
88 ASSERT_EQ(18 + i, buffer_[i]);
89 }
90}
91
Austin Schuhd85c66e2017-04-16 10:50:33 -070092// Test that the buffer works after Reset.
93TEST_F(RingBufferTest, ResetWorks) {
94 // Over fill it, and then clear it out.
95 ASSERT_TRUE(buffer_.empty());
96
97 for (size_t i = 0; i < 53; ++i) {
98 buffer_.Push(i);
99 }
100 ASSERT_TRUE(buffer_.full());
101
102 buffer_.Reset();
103
104 ASSERT_TRUE(buffer_.empty());
105
106 // Now, add numbers 0-9 to the RingBuffer.
107 for (int i = 0; i < 10; ++i) {
108 buffer_.Push(i);
109 }
110
111 // It should now be full.
112 ASSERT_TRUE(buffer_.full());
113
114 // The last 10 numbers were added 0-9, so verify that is what is in the
115 // buffer.
116 for (size_t i = 0; i < buffer_.size(); ++i) {
117 ASSERT_EQ(i, buffer_[i]);
118 }
119}
120
Austin Schuh39f7ae92019-04-14 17:14:50 -0700121// Test that an iterator over the buffer works.
122TEST_F(RingBufferTest, Iterator) {
123 // Over fill it, and then clear it out.
124 ASSERT_TRUE(buffer_.empty());
125
126 for (int i = 0; i < 12; ++i) {
127 buffer_.Push(i);
128 }
129
130 int i = 0;
131 for (int element : buffer_) {
132 EXPECT_EQ(i + 2, element);
133 ++i;
134 }
135 EXPECT_EQ(i, buffer_.size());
136}
137
138// Test that a const iterator over the buffer works.
139TEST_F(RingBufferTest, CIterator) {
140 // Over fill it, and then clear it out.
141 ASSERT_TRUE(buffer_.empty());
142
143 for (int i = 0; i < 12; ++i) {
144 buffer_.Push(i);
145 }
146
147 const RingBuffer<int, 10> &cbuffer = buffer_;
148
149 int i = 0;
150 for (const int element : cbuffer) {
151 EXPECT_EQ(i + 2, element);
152 ++i;
153 }
154 EXPECT_EQ(i, buffer_.size());
155}
156
Parker Schuhecd057f2017-03-11 20:03:01 -0800157} // namespace testing
158} // namespace aos