Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame^] | 1 | /*----------------------------------------------------------------------------*/ |
| 2 | /* Copyright (c) 2020 FIRST. 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 "wpi/static_circular_buffer.h" // NOLINT(build/include_order) |
| 9 | |
| 10 | #include <array> |
| 11 | |
| 12 | #include "gtest/gtest.h" |
| 13 | |
| 14 | static 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 | |
| 18 | static const std::array<double, 8> pushFrontOut = { |
| 19 | {799.913, 421.125, 22.727, 445.697, 132.344, 716.126, 234.252, 342.657}}; |
| 20 | |
| 21 | static const std::array<double, 8> pushBackOut = { |
| 22 | {342.657, 234.252, 716.126, 132.344, 445.697, 22.727, 421.125, 799.913}}; |
| 23 | |
| 24 | TEST(StaticCircularBufferTest, PushFrontTest) { |
| 25 | wpi::static_circular_buffer<double, 8> queue; |
| 26 | |
| 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 | |
| 36 | TEST(StaticCircularBufferTest, PushBackTest) { |
| 37 | wpi::static_circular_buffer<double, 8> queue; |
| 38 | |
| 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 | |
| 48 | TEST(StaticCircularBufferTest, EmplaceFrontTest) { |
| 49 | wpi::static_circular_buffer<double, 8> queue; |
| 50 | |
| 51 | for (auto& value : values) { |
| 52 | queue.emplace_front(value); |
| 53 | } |
| 54 | |
| 55 | for (size_t i = 0; i < pushFrontOut.size(); ++i) { |
| 56 | EXPECT_EQ(pushFrontOut[i], queue[i]); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | TEST(StaticCircularBufferTest, EmplaceBackTest) { |
| 61 | wpi::static_circular_buffer<double, 8> queue; |
| 62 | |
| 63 | for (auto& value : values) { |
| 64 | queue.emplace_back(value); |
| 65 | } |
| 66 | |
| 67 | for (size_t i = 0; i < pushBackOut.size(); ++i) { |
| 68 | EXPECT_EQ(pushBackOut[i], queue[i]); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | TEST(StaticCircularBufferTest, PushPopTest) { |
| 73 | wpi::static_circular_buffer<double, 3> queue; |
| 74 | |
| 75 | // Insert three elements into the buffer |
| 76 | queue.push_back(1.0); |
| 77 | queue.push_back(2.0); |
| 78 | queue.push_back(3.0); |
| 79 | |
| 80 | EXPECT_EQ(1.0, queue[0]); |
| 81 | EXPECT_EQ(2.0, queue[1]); |
| 82 | EXPECT_EQ(3.0, queue[2]); |
| 83 | |
| 84 | /* |
| 85 | * The buffer is full now, so pushing subsequent elements will overwrite the |
| 86 | * front-most elements. |
| 87 | */ |
| 88 | |
| 89 | queue.push_back(4.0); // Overwrite 1 with 4 |
| 90 | |
| 91 | // The buffer now contains 2, 3 and 4 |
| 92 | EXPECT_EQ(2.0, queue[0]); |
| 93 | EXPECT_EQ(3.0, queue[1]); |
| 94 | EXPECT_EQ(4.0, queue[2]); |
| 95 | |
| 96 | queue.push_back(5.0); // Overwrite 2 with 5 |
| 97 | |
| 98 | // The buffer now contains 3, 4 and 5 |
| 99 | EXPECT_EQ(3.0, queue[0]); |
| 100 | EXPECT_EQ(4.0, queue[1]); |
| 101 | EXPECT_EQ(5.0, queue[2]); |
| 102 | |
| 103 | EXPECT_EQ(5.0, queue.pop_back()); // 5 is removed |
| 104 | |
| 105 | // The buffer now contains 3 and 4 |
| 106 | EXPECT_EQ(3.0, queue[0]); |
| 107 | EXPECT_EQ(4.0, queue[1]); |
| 108 | |
| 109 | EXPECT_EQ(3.0, queue.pop_front()); // 3 is removed |
| 110 | |
| 111 | // Leaving only one element with value == 4 |
| 112 | EXPECT_EQ(4.0, queue[0]); |
| 113 | } |
| 114 | |
| 115 | TEST(StaticCircularBufferTest, ResetTest) { |
| 116 | wpi::static_circular_buffer<double, 5> queue; |
| 117 | |
| 118 | for (size_t i = 1; i < 6; ++i) { |
| 119 | queue.push_back(i); |
| 120 | } |
| 121 | |
| 122 | queue.reset(); |
| 123 | |
| 124 | EXPECT_EQ(queue.size(), size_t{0}); |
| 125 | } |
| 126 | |
| 127 | TEST(StaticCircularBufferTest, IteratorTest) { |
| 128 | wpi::static_circular_buffer<double, 3> queue; |
| 129 | |
| 130 | queue.push_back(1.0); |
| 131 | queue.push_back(2.0); |
| 132 | queue.push_back(3.0); |
| 133 | queue.push_back(4.0); // Overwrite 1 with 4 |
| 134 | |
| 135 | // The buffer now contains 2, 3 and 4 |
| 136 | const std::array<double, 3> values = {2.0, 3.0, 4.0}; |
| 137 | |
| 138 | // iterator |
| 139 | int i = 0; |
| 140 | for (auto& elem : queue) { |
| 141 | EXPECT_EQ(values[i], elem); |
| 142 | ++i; |
| 143 | } |
| 144 | |
| 145 | // const_iterator |
| 146 | i = 0; |
| 147 | for (const auto& elem : queue) { |
| 148 | EXPECT_EQ(values[i], elem); |
| 149 | ++i; |
| 150 | } |
| 151 | } |