Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 1 | /*----------------------------------------------------------------------------*/ |
| 2 | /* Copyright (c) FIRST 2015-2016. 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> |
| 9 | #include "gtest/gtest.h" |
| 10 | #include <array> |
| 11 | |
| 12 | static const std::array<double, 10> values = {751.848, 766.366, 342.657, |
| 13 | 234.252, 716.126, 132.344, |
| 14 | 445.697, 22.727, 421.125, |
| 15 | 799.913}; |
| 16 | |
| 17 | static const std::array<double, 8> pushFrontOut = {799.913, 421.125, 22.727, |
| 18 | 445.697, 132.344, 716.126, |
| 19 | 234.252, 342.657}; |
| 20 | |
| 21 | static const std::array<double, 8> pushBackOut = {342.657, 234.252, 716.126, |
| 22 | 132.344, 445.697, 22.727, |
| 23 | 421.125, 799.913}; |
| 24 | |
| 25 | TEST(CircularBufferTest, PushFrontTest) { |
| 26 | CircularBuffer<double> queue(8); |
| 27 | |
| 28 | for (auto& value : values) { |
| 29 | queue.PushFront(value); |
| 30 | } |
| 31 | |
| 32 | for (unsigned int i = 0; i < pushFrontOut.size(); i++) { |
| 33 | EXPECT_EQ(pushFrontOut[i], queue[i]); |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | TEST(CircularBufferTest, PushBackTest) { |
| 38 | CircularBuffer<double> queue(8); |
| 39 | |
| 40 | for (auto& value : values) { |
| 41 | queue.PushBack(value); |
| 42 | } |
| 43 | |
| 44 | for (unsigned int i = 0; i < pushBackOut.size(); i++) { |
| 45 | EXPECT_EQ(pushBackOut[i], queue[i]); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | TEST(CircularBufferTest, PushPopTest) { |
| 50 | CircularBuffer<double> queue(3); |
| 51 | |
| 52 | // Insert three elements into the buffer |
| 53 | queue.PushBack(1.0); |
| 54 | queue.PushBack(2.0); |
| 55 | queue.PushBack(3.0); |
| 56 | |
| 57 | EXPECT_EQ(1.0, queue[0]); |
| 58 | EXPECT_EQ(2.0, queue[1]); |
| 59 | EXPECT_EQ(3.0, queue[2]); |
| 60 | |
| 61 | /* |
| 62 | * The buffer is full now, so pushing subsequent elements will overwrite the |
| 63 | * front-most elements. |
| 64 | */ |
| 65 | |
| 66 | queue.PushBack(4.0); // Overwrite 1 with 4 |
| 67 | |
| 68 | // The buffer now contains 2, 3 and 4 |
| 69 | EXPECT_EQ(2.0, queue[0]); |
| 70 | EXPECT_EQ(3.0, queue[1]); |
| 71 | EXPECT_EQ(4.0, queue[2]); |
| 72 | |
| 73 | queue.PushBack(5.0); // Overwrite 2 with 5 |
| 74 | |
| 75 | // The buffer now contains 3, 4 and 5 |
| 76 | EXPECT_EQ(3.0, queue[0]); |
| 77 | EXPECT_EQ(4.0, queue[1]); |
| 78 | EXPECT_EQ(5.0, queue[2]); |
| 79 | |
| 80 | EXPECT_EQ(5.0, queue.PopBack()); // 5 is removed |
| 81 | |
| 82 | // The buffer now contains 3 and 4 |
| 83 | EXPECT_EQ(3.0, queue[0]); |
| 84 | EXPECT_EQ(4.0, queue[1]); |
| 85 | |
| 86 | EXPECT_EQ(3.0, queue.PopFront()); // 3 is removed |
| 87 | |
| 88 | // Leaving only one element with value == 4 |
| 89 | EXPECT_EQ(4.0, queue[0]); |
| 90 | } |