Brian Silverman | f7f267a | 2017-02-04 16:16:08 -0800 | [diff] [blame^] | 1 | /*----------------------------------------------------------------------------*/ |
| 2 | /* Copyright (c) FIRST 2015-2017. All Rights Reserved. */ |
| 3 | /* 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 | |
| 8 | #include "CircularBuffer.h" // NOLINT(build/include_order) |
| 9 | |
| 10 | #include <array> |
| 11 | |
| 12 | #include "gtest/gtest.h" |
| 13 | |
| 14 | using namespace frc; |
| 15 | |
| 16 | static const std::array<double, 10> values = { |
| 17 | 751.848, 766.366, 342.657, 234.252, 716.126, |
| 18 | 132.344, 445.697, 22.727, 421.125, 799.913}; |
| 19 | |
| 20 | static const std::array<double, 8> pushFrontOut = { |
| 21 | 799.913, 421.125, 22.727, 445.697, 132.344, 716.126, 234.252, 342.657}; |
| 22 | |
| 23 | static const std::array<double, 8> pushBackOut = { |
| 24 | 342.657, 234.252, 716.126, 132.344, 445.697, 22.727, 421.125, 799.913}; |
| 25 | |
| 26 | TEST(CircularBufferTest, PushFrontTest) { |
| 27 | CircularBuffer<double> queue(8); |
| 28 | |
| 29 | for (auto& value : values) { |
| 30 | queue.PushFront(value); |
| 31 | } |
| 32 | |
| 33 | for (size_t i = 0; i < pushFrontOut.size(); i++) { |
| 34 | EXPECT_EQ(pushFrontOut[i], queue[i]); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | TEST(CircularBufferTest, PushBackTest) { |
| 39 | CircularBuffer<double> queue(8); |
| 40 | |
| 41 | for (auto& value : values) { |
| 42 | queue.PushBack(value); |
| 43 | } |
| 44 | |
| 45 | for (size_t i = 0; i < pushBackOut.size(); i++) { |
| 46 | EXPECT_EQ(pushBackOut[i], queue[i]); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | TEST(CircularBufferTest, PushPopTest) { |
| 51 | CircularBuffer<double> queue(3); |
| 52 | |
| 53 | // Insert three elements into the buffer |
| 54 | queue.PushBack(1.0); |
| 55 | queue.PushBack(2.0); |
| 56 | queue.PushBack(3.0); |
| 57 | |
| 58 | EXPECT_EQ(1.0, queue[0]); |
| 59 | EXPECT_EQ(2.0, queue[1]); |
| 60 | EXPECT_EQ(3.0, queue[2]); |
| 61 | |
| 62 | /* |
| 63 | * The buffer is full now, so pushing subsequent elements will overwrite the |
| 64 | * front-most elements. |
| 65 | */ |
| 66 | |
| 67 | queue.PushBack(4.0); // Overwrite 1 with 4 |
| 68 | |
| 69 | // The buffer now contains 2, 3 and 4 |
| 70 | EXPECT_EQ(2.0, queue[0]); |
| 71 | EXPECT_EQ(3.0, queue[1]); |
| 72 | EXPECT_EQ(4.0, queue[2]); |
| 73 | |
| 74 | queue.PushBack(5.0); // Overwrite 2 with 5 |
| 75 | |
| 76 | // The buffer now contains 3, 4 and 5 |
| 77 | EXPECT_EQ(3.0, queue[0]); |
| 78 | EXPECT_EQ(4.0, queue[1]); |
| 79 | EXPECT_EQ(5.0, queue[2]); |
| 80 | |
| 81 | EXPECT_EQ(5.0, queue.PopBack()); // 5 is removed |
| 82 | |
| 83 | // The buffer now contains 3 and 4 |
| 84 | EXPECT_EQ(3.0, queue[0]); |
| 85 | EXPECT_EQ(4.0, queue[1]); |
| 86 | |
| 87 | EXPECT_EQ(3.0, queue.PopFront()); // 3 is removed |
| 88 | |
| 89 | // Leaving only one element with value == 4 |
| 90 | EXPECT_EQ(4.0, queue[0]); |
| 91 | } |
| 92 | |
| 93 | TEST(CircularBufferTest, ResetTest) { |
| 94 | CircularBuffer<double> queue(5); |
| 95 | |
| 96 | for (size_t i = 1; i < 6; i++) { |
| 97 | queue.PushBack(i); |
| 98 | } |
| 99 | |
| 100 | queue.Reset(); |
| 101 | |
| 102 | for (size_t i = 0; i < 5; i++) { |
| 103 | EXPECT_EQ(0.0, queue[i]); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | TEST(CircularBufferTest, ResizeTest) { |
| 108 | CircularBuffer<double> queue(5); |
| 109 | |
| 110 | /* Buffer contains {1, 2, 3, _, _} |
| 111 | * ^ front |
| 112 | */ |
| 113 | queue.PushBack(1.0); |
| 114 | queue.PushBack(2.0); |
| 115 | queue.PushBack(3.0); |
| 116 | |
| 117 | queue.Resize(2); |
| 118 | EXPECT_EQ(1.0, queue[0]); |
| 119 | EXPECT_EQ(2.0, queue[1]); |
| 120 | |
| 121 | queue.Resize(5); |
| 122 | EXPECT_EQ(1.0, queue[0]); |
| 123 | EXPECT_EQ(2.0, queue[1]); |
| 124 | |
| 125 | queue.Reset(); |
| 126 | |
| 127 | /* Buffer contains {_, 1, 2, 3, _} |
| 128 | * ^ front |
| 129 | */ |
| 130 | queue.PushBack(0.0); |
| 131 | queue.PushBack(1.0); |
| 132 | queue.PushBack(2.0); |
| 133 | queue.PushBack(3.0); |
| 134 | queue.PopFront(); |
| 135 | |
| 136 | queue.Resize(2); |
| 137 | EXPECT_EQ(1.0, queue[0]); |
| 138 | EXPECT_EQ(2.0, queue[1]); |
| 139 | |
| 140 | queue.Resize(5); |
| 141 | EXPECT_EQ(1.0, queue[0]); |
| 142 | EXPECT_EQ(2.0, queue[1]); |
| 143 | |
| 144 | queue.Reset(); |
| 145 | |
| 146 | /* Buffer contains {_, _, 1, 2, 3} |
| 147 | * ^ front |
| 148 | */ |
| 149 | queue.PushBack(0.0); |
| 150 | queue.PushBack(0.0); |
| 151 | queue.PushBack(1.0); |
| 152 | queue.PushBack(2.0); |
| 153 | queue.PushBack(3.0); |
| 154 | queue.PopFront(); |
| 155 | queue.PopFront(); |
| 156 | |
| 157 | queue.Resize(2); |
| 158 | EXPECT_EQ(1.0, queue[0]); |
| 159 | EXPECT_EQ(2.0, queue[1]); |
| 160 | |
| 161 | queue.Resize(5); |
| 162 | EXPECT_EQ(1.0, queue[0]); |
| 163 | EXPECT_EQ(2.0, queue[1]); |
| 164 | |
| 165 | queue.Reset(); |
| 166 | |
| 167 | /* Buffer contains {3, _, _, 1, 2} |
| 168 | * ^ front |
| 169 | */ |
| 170 | queue.PushBack(3.0); |
| 171 | queue.PushFront(2.0); |
| 172 | queue.PushFront(1.0); |
| 173 | |
| 174 | queue.Resize(2); |
| 175 | EXPECT_EQ(1.0, queue[0]); |
| 176 | EXPECT_EQ(2.0, queue[1]); |
| 177 | |
| 178 | queue.Resize(5); |
| 179 | EXPECT_EQ(1.0, queue[0]); |
| 180 | EXPECT_EQ(2.0, queue[1]); |
| 181 | |
| 182 | queue.Reset(); |
| 183 | |
| 184 | /* Buffer contains {2, 3, _, _, 1} |
| 185 | * ^ front |
| 186 | */ |
| 187 | queue.PushBack(2.0); |
| 188 | queue.PushBack(3.0); |
| 189 | queue.PushFront(1.0); |
| 190 | |
| 191 | queue.Resize(2); |
| 192 | EXPECT_EQ(1.0, queue[0]); |
| 193 | EXPECT_EQ(2.0, queue[1]); |
| 194 | |
| 195 | queue.Resize(5); |
| 196 | EXPECT_EQ(1.0, queue[0]); |
| 197 | EXPECT_EQ(2.0, queue[1]); |
| 198 | |
| 199 | // Test PushBack() after resize |
| 200 | queue.PushBack(3.0); |
| 201 | EXPECT_EQ(1.0, queue[0]); |
| 202 | EXPECT_EQ(2.0, queue[1]); |
| 203 | EXPECT_EQ(3.0, queue[2]); |
| 204 | |
| 205 | // Test PushFront() after resize |
| 206 | queue.PushFront(4.0); |
| 207 | EXPECT_EQ(4.0, queue[0]); |
| 208 | EXPECT_EQ(1.0, queue[1]); |
| 209 | EXPECT_EQ(2.0, queue[2]); |
| 210 | EXPECT_EQ(3.0, queue[3]); |
| 211 | } |