blob: 7fe9e03346b17bbd4bc919ccf1681b1e1b44e260 [file] [log] [blame]
Brian Silverman41cdd3e2019-01-19 19:48:58 -08001/*----------------------------------------------------------------------------*/
James Kuszmaul4f3ad3c2019-12-01 16:35:21 -08002/* Copyright (c) 2015-2019 FIRST. All Rights Reserved. */
Brian Silverman41cdd3e2019-01-19 19:48:58 -08003/* Open Source Software - may be modified and shared by FRC teams. The code */
4/* must be accompanied by the FIRST BSD license file in the root directory of */
5/* the project. */
6/*----------------------------------------------------------------------------*/
7
James Kuszmaul4f3ad3c2019-12-01 16:35:21 -08008#include "wpi/circular_buffer.h" // NOLINT(build/include_order)
Brian Silverman41cdd3e2019-01-19 19:48:58 -08009
10#include <array>
11
12#include "gtest/gtest.h"
13
Brian Silverman41cdd3e2019-01-19 19:48:58 -080014static const std::array<double, 10> values = {
15 {751.848, 766.366, 342.657, 234.252, 716.126, 132.344, 445.697, 22.727,
16 421.125, 799.913}};
17
18static const std::array<double, 8> pushFrontOut = {
19 {799.913, 421.125, 22.727, 445.697, 132.344, 716.126, 234.252, 342.657}};
20
21static const std::array<double, 8> pushBackOut = {
22 {342.657, 234.252, 716.126, 132.344, 445.697, 22.727, 421.125, 799.913}};
23
24TEST(CircularBufferTest, PushFrontTest) {
James Kuszmaul4f3ad3c2019-12-01 16:35:21 -080025 wpi::circular_buffer<double> queue(8);
Brian Silverman41cdd3e2019-01-19 19:48:58 -080026
27 for (auto& value : values) {
28 queue.push_front(value);
29 }
30
31 for (size_t i = 0; i < pushFrontOut.size(); i++) {
32 EXPECT_EQ(pushFrontOut[i], queue[i]);
33 }
34}
35
36TEST(CircularBufferTest, PushBackTest) {
James Kuszmaul4f3ad3c2019-12-01 16:35:21 -080037 wpi::circular_buffer<double> queue(8);
Brian Silverman41cdd3e2019-01-19 19:48:58 -080038
39 for (auto& value : values) {
40 queue.push_back(value);
41 }
42
43 for (size_t i = 0; i < pushBackOut.size(); i++) {
44 EXPECT_EQ(pushBackOut[i], queue[i]);
45 }
46}
47
48TEST(CircularBufferTest, PushPopTest) {
James Kuszmaul4f3ad3c2019-12-01 16:35:21 -080049 wpi::circular_buffer<double> queue(3);
Brian Silverman41cdd3e2019-01-19 19:48:58 -080050
51 // Insert three elements into the buffer
52 queue.push_back(1.0);
53 queue.push_back(2.0);
54 queue.push_back(3.0);
55
56 EXPECT_EQ(1.0, queue[0]);
57 EXPECT_EQ(2.0, queue[1]);
58 EXPECT_EQ(3.0, queue[2]);
59
60 /*
61 * The buffer is full now, so pushing subsequent elements will overwrite the
62 * front-most elements.
63 */
64
65 queue.push_back(4.0); // Overwrite 1 with 4
66
67 // The buffer now contains 2, 3 and 4
68 EXPECT_EQ(2.0, queue[0]);
69 EXPECT_EQ(3.0, queue[1]);
70 EXPECT_EQ(4.0, queue[2]);
71
72 queue.push_back(5.0); // Overwrite 2 with 5
73
74 // The buffer now contains 3, 4 and 5
75 EXPECT_EQ(3.0, queue[0]);
76 EXPECT_EQ(4.0, queue[1]);
77 EXPECT_EQ(5.0, queue[2]);
78
79 EXPECT_EQ(5.0, queue.pop_back()); // 5 is removed
80
81 // The buffer now contains 3 and 4
82 EXPECT_EQ(3.0, queue[0]);
83 EXPECT_EQ(4.0, queue[1]);
84
85 EXPECT_EQ(3.0, queue.pop_front()); // 3 is removed
86
87 // Leaving only one element with value == 4
88 EXPECT_EQ(4.0, queue[0]);
89}
90
91TEST(CircularBufferTest, ResetTest) {
James Kuszmaul4f3ad3c2019-12-01 16:35:21 -080092 wpi::circular_buffer<double> queue(5);
Brian Silverman41cdd3e2019-01-19 19:48:58 -080093
94 for (size_t i = 1; i < 6; i++) {
95 queue.push_back(i);
96 }
97
98 queue.reset();
99
100 for (size_t i = 0; i < 5; i++) {
101 EXPECT_EQ(0.0, queue[i]);
102 }
103}
104
105TEST(CircularBufferTest, ResizeTest) {
James Kuszmaul4f3ad3c2019-12-01 16:35:21 -0800106 wpi::circular_buffer<double> queue(5);
Brian Silverman41cdd3e2019-01-19 19:48:58 -0800107
108 /* Buffer contains {1, 2, 3, _, _}
109 * ^ front
110 */
111 queue.push_back(1.0);
112 queue.push_back(2.0);
113 queue.push_back(3.0);
114
115 queue.resize(2);
116 EXPECT_EQ(1.0, queue[0]);
117 EXPECT_EQ(2.0, queue[1]);
118
119 queue.resize(5);
120 EXPECT_EQ(1.0, queue[0]);
121 EXPECT_EQ(2.0, queue[1]);
122
123 queue.reset();
124
125 /* Buffer contains {_, 1, 2, 3, _}
126 * ^ front
127 */
128 queue.push_back(0.0);
129 queue.push_back(1.0);
130 queue.push_back(2.0);
131 queue.push_back(3.0);
132 queue.pop_front();
133
134 queue.resize(2);
135 EXPECT_EQ(1.0, queue[0]);
136 EXPECT_EQ(2.0, queue[1]);
137
138 queue.resize(5);
139 EXPECT_EQ(1.0, queue[0]);
140 EXPECT_EQ(2.0, queue[1]);
141
142 queue.reset();
143
144 /* Buffer contains {_, _, 1, 2, 3}
145 * ^ front
146 */
147 queue.push_back(0.0);
148 queue.push_back(0.0);
149 queue.push_back(1.0);
150 queue.push_back(2.0);
151 queue.push_back(3.0);
152 queue.pop_front();
153 queue.pop_front();
154
155 queue.resize(2);
156 EXPECT_EQ(1.0, queue[0]);
157 EXPECT_EQ(2.0, queue[1]);
158
159 queue.resize(5);
160 EXPECT_EQ(1.0, queue[0]);
161 EXPECT_EQ(2.0, queue[1]);
162
163 queue.reset();
164
165 /* Buffer contains {3, _, _, 1, 2}
166 * ^ front
167 */
168 queue.push_back(3.0);
169 queue.push_front(2.0);
170 queue.push_front(1.0);
171
172 queue.resize(2);
173 EXPECT_EQ(1.0, queue[0]);
174 EXPECT_EQ(2.0, queue[1]);
175
176 queue.resize(5);
177 EXPECT_EQ(1.0, queue[0]);
178 EXPECT_EQ(2.0, queue[1]);
179
180 queue.reset();
181
182 /* Buffer contains {2, 3, _, _, 1}
183 * ^ front
184 */
185 queue.push_back(2.0);
186 queue.push_back(3.0);
187 queue.push_front(1.0);
188
189 queue.resize(2);
190 EXPECT_EQ(1.0, queue[0]);
191 EXPECT_EQ(2.0, queue[1]);
192
193 queue.resize(5);
194 EXPECT_EQ(1.0, queue[0]);
195 EXPECT_EQ(2.0, queue[1]);
196
197 // Test push_back() after resize
198 queue.push_back(3.0);
199 EXPECT_EQ(1.0, queue[0]);
200 EXPECT_EQ(2.0, queue[1]);
201 EXPECT_EQ(3.0, queue[2]);
202
203 // Test push_front() after resize
204 queue.push_front(4.0);
205 EXPECT_EQ(4.0, queue[0]);
206 EXPECT_EQ(1.0, queue[1]);
207 EXPECT_EQ(2.0, queue[2]);
208 EXPECT_EQ(3.0, queue[3]);
209}