blob: f95f9cbb7ce9bfee29d191bd2d2cf0f29d397861 [file] [log] [blame]
Austin Schuh812d0d12021-11-04 20:16:48 -07001// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
Austin Schuh1e69f942020-11-14 15:06:14 -08004
5#include "wpi/static_circular_buffer.h" // NOLINT(build/include_order)
6
7#include <array>
8
9#include "gtest/gtest.h"
10
11static const std::array<double, 10> values = {
12 {751.848, 766.366, 342.657, 234.252, 716.126, 132.344, 445.697, 22.727,
13 421.125, 799.913}};
14
15static const std::array<double, 8> pushFrontOut = {
16 {799.913, 421.125, 22.727, 445.697, 132.344, 716.126, 234.252, 342.657}};
17
18static const std::array<double, 8> pushBackOut = {
19 {342.657, 234.252, 716.126, 132.344, 445.697, 22.727, 421.125, 799.913}};
20
Austin Schuh812d0d12021-11-04 20:16:48 -070021TEST(StaticCircularBufferTest, PushFront) {
Austin Schuh1e69f942020-11-14 15:06:14 -080022 wpi::static_circular_buffer<double, 8> queue;
23
24 for (auto& value : values) {
25 queue.push_front(value);
26 }
27
28 for (size_t i = 0; i < pushFrontOut.size(); ++i) {
29 EXPECT_EQ(pushFrontOut[i], queue[i]);
30 }
31}
32
Austin Schuh812d0d12021-11-04 20:16:48 -070033TEST(StaticCircularBufferTest, PushBack) {
Austin Schuh1e69f942020-11-14 15:06:14 -080034 wpi::static_circular_buffer<double, 8> queue;
35
36 for (auto& value : values) {
37 queue.push_back(value);
38 }
39
40 for (size_t i = 0; i < pushBackOut.size(); ++i) {
41 EXPECT_EQ(pushBackOut[i], queue[i]);
42 }
43}
44
Austin Schuh812d0d12021-11-04 20:16:48 -070045TEST(StaticCircularBufferTest, EmplaceFront) {
Austin Schuh1e69f942020-11-14 15:06:14 -080046 wpi::static_circular_buffer<double, 8> queue;
47
48 for (auto& value : values) {
49 queue.emplace_front(value);
50 }
51
52 for (size_t i = 0; i < pushFrontOut.size(); ++i) {
53 EXPECT_EQ(pushFrontOut[i], queue[i]);
54 }
55}
56
Austin Schuh812d0d12021-11-04 20:16:48 -070057TEST(StaticCircularBufferTest, EmplaceBack) {
Austin Schuh1e69f942020-11-14 15:06:14 -080058 wpi::static_circular_buffer<double, 8> queue;
59
60 for (auto& value : values) {
61 queue.emplace_back(value);
62 }
63
64 for (size_t i = 0; i < pushBackOut.size(); ++i) {
65 EXPECT_EQ(pushBackOut[i], queue[i]);
66 }
67}
68
Austin Schuh812d0d12021-11-04 20:16:48 -070069TEST(StaticCircularBufferTest, PushPop) {
Austin Schuh1e69f942020-11-14 15:06:14 -080070 wpi::static_circular_buffer<double, 3> queue;
71
72 // Insert three elements into the buffer
73 queue.push_back(1.0);
74 queue.push_back(2.0);
75 queue.push_back(3.0);
76
77 EXPECT_EQ(1.0, queue[0]);
78 EXPECT_EQ(2.0, queue[1]);
79 EXPECT_EQ(3.0, queue[2]);
80
81 /*
82 * The buffer is full now, so pushing subsequent elements will overwrite the
83 * front-most elements.
84 */
85
86 queue.push_back(4.0); // Overwrite 1 with 4
87
88 // The buffer now contains 2, 3 and 4
89 EXPECT_EQ(2.0, queue[0]);
90 EXPECT_EQ(3.0, queue[1]);
91 EXPECT_EQ(4.0, queue[2]);
92
93 queue.push_back(5.0); // Overwrite 2 with 5
94
95 // The buffer now contains 3, 4 and 5
96 EXPECT_EQ(3.0, queue[0]);
97 EXPECT_EQ(4.0, queue[1]);
98 EXPECT_EQ(5.0, queue[2]);
99
100 EXPECT_EQ(5.0, queue.pop_back()); // 5 is removed
101
102 // The buffer now contains 3 and 4
103 EXPECT_EQ(3.0, queue[0]);
104 EXPECT_EQ(4.0, queue[1]);
105
106 EXPECT_EQ(3.0, queue.pop_front()); // 3 is removed
107
108 // Leaving only one element with value == 4
109 EXPECT_EQ(4.0, queue[0]);
110}
111
Austin Schuh812d0d12021-11-04 20:16:48 -0700112TEST(StaticCircularBufferTest, Reset) {
Austin Schuh1e69f942020-11-14 15:06:14 -0800113 wpi::static_circular_buffer<double, 5> queue;
114
115 for (size_t i = 1; i < 6; ++i) {
116 queue.push_back(i);
117 }
118
119 queue.reset();
120
121 EXPECT_EQ(queue.size(), size_t{0});
122}
123
Austin Schuh812d0d12021-11-04 20:16:48 -0700124TEST(StaticCircularBufferTest, Iterator) {
Austin Schuh1e69f942020-11-14 15:06:14 -0800125 wpi::static_circular_buffer<double, 3> queue;
126
127 queue.push_back(1.0);
128 queue.push_back(2.0);
129 queue.push_back(3.0);
130 queue.push_back(4.0); // Overwrite 1 with 4
131
132 // The buffer now contains 2, 3 and 4
133 const std::array<double, 3> values = {2.0, 3.0, 4.0};
134
135 // iterator
136 int i = 0;
137 for (auto& elem : queue) {
138 EXPECT_EQ(values[i], elem);
139 ++i;
140 }
141
142 // const_iterator
143 i = 0;
144 for (const auto& elem : queue) {
145 EXPECT_EQ(values[i], elem);
146 ++i;
147 }
148}