mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-26 01:51:41 +00:00
CircularBuffer now uses an idiomatic interface in C++ and Java (#421)
This commit is contained in:
committed by
Peter Johnson
parent
029246ed28
commit
85157a56c3
@@ -95,8 +95,8 @@ double LinearDigitalFilter::Get() const {
|
||||
}
|
||||
|
||||
void LinearDigitalFilter::Reset() {
|
||||
m_inputs.Reset();
|
||||
m_outputs.Reset();
|
||||
m_inputs.reset();
|
||||
m_outputs.reset();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -108,7 +108,7 @@ double LinearDigitalFilter::PIDGet() {
|
||||
double retVal = 0.0;
|
||||
|
||||
// Rotate the inputs
|
||||
m_inputs.PushFront(PIDGetSource());
|
||||
m_inputs.push_front(PIDGetSource());
|
||||
|
||||
// Calculate the new value
|
||||
for (size_t i = 0; i < m_inputGains.size(); i++) {
|
||||
@@ -119,7 +119,7 @@ double LinearDigitalFilter::PIDGet() {
|
||||
}
|
||||
|
||||
// Rotate the outputs
|
||||
m_outputs.PushFront(retVal);
|
||||
m_outputs.push_front(retVal);
|
||||
|
||||
return retVal;
|
||||
}
|
||||
|
||||
@@ -12,8 +12,8 @@
|
||||
|
||||
#include <llvm/ArrayRef.h>
|
||||
|
||||
#include "CircularBuffer.h"
|
||||
#include "Filter.h"
|
||||
#include "circular_buffer.h"
|
||||
|
||||
namespace frc {
|
||||
|
||||
@@ -89,8 +89,8 @@ class LinearDigitalFilter : public Filter {
|
||||
double PIDGet() override;
|
||||
|
||||
private:
|
||||
CircularBuffer<double> m_inputs;
|
||||
CircularBuffer<double> m_outputs;
|
||||
circular_buffer<double> m_inputs;
|
||||
circular_buffer<double> m_outputs;
|
||||
std::vector<double> m_inputGains;
|
||||
std::vector<double> m_outputGains;
|
||||
};
|
||||
|
||||
@@ -17,16 +17,29 @@ namespace frc {
|
||||
* old values.
|
||||
*/
|
||||
template <class T>
|
||||
class CircularBuffer {
|
||||
class circular_buffer {
|
||||
public:
|
||||
explicit CircularBuffer(size_t size);
|
||||
explicit circular_buffer(size_t size);
|
||||
|
||||
void PushFront(T value);
|
||||
void PushBack(T value);
|
||||
T PopFront();
|
||||
T PopBack();
|
||||
void Resize(size_t size);
|
||||
void Reset();
|
||||
typedef T value_type;
|
||||
typedef value_type& reference;
|
||||
typedef const value_type& const_reference;
|
||||
typedef value_type* pointer;
|
||||
typedef size_t size_type;
|
||||
typedef std::forward_iterator_tag iterator_category;
|
||||
typedef std::ptrdiff_t difference_type;
|
||||
|
||||
size_type size() const;
|
||||
T& front();
|
||||
const T& front() const;
|
||||
T& back();
|
||||
const T& back() const;
|
||||
void push_front(T value);
|
||||
void push_back(T value);
|
||||
T pop_front();
|
||||
T pop_back();
|
||||
void resize(size_t size);
|
||||
void reset();
|
||||
|
||||
T& operator[](size_t index);
|
||||
const T& operator[](size_t index) const;
|
||||
@@ -46,4 +59,4 @@ class CircularBuffer {
|
||||
|
||||
} // namespace frc
|
||||
|
||||
#include "CircularBuffer.inc"
|
||||
#include "circular_buffer.inc"
|
||||
@@ -12,14 +12,64 @@
|
||||
namespace frc {
|
||||
|
||||
template <class T>
|
||||
CircularBuffer<T>::CircularBuffer(size_t size) : m_data(size, 0) {}
|
||||
circular_buffer<T>::circular_buffer(size_t size) : m_data(size, 0) {}
|
||||
|
||||
/**
|
||||
* Returns number of elements in buffer
|
||||
*/
|
||||
template <class T>
|
||||
typename circular_buffer<T>::size_type circular_buffer<T>::size() const {
|
||||
return m_length;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns value at front of buffer
|
||||
*/
|
||||
template <class T>
|
||||
T& circular_buffer<T>::front() {
|
||||
return (*this)[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns value at front of buffer
|
||||
*/
|
||||
template <class T>
|
||||
const T& circular_buffer<T>::front() const {
|
||||
return (*this)[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns value at back of buffer
|
||||
*/
|
||||
template <class T>
|
||||
T& circular_buffer<T>::back() {
|
||||
// If there are no elements in the buffer, do nothing
|
||||
if (m_length == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return m_data[(m_front + m_length - 1) % m_data.size()];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns value at back of buffer
|
||||
*/
|
||||
template <class T>
|
||||
const T& circular_buffer<T>::back() const {
|
||||
// If there are no elements in the buffer, do nothing
|
||||
if (m_length == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return m_data[(m_front + m_length - 1) % m_data.size()];
|
||||
}
|
||||
|
||||
/**
|
||||
* Push new value onto front of the buffer. The value at the back is overwritten
|
||||
* if the buffer is full.
|
||||
*/
|
||||
template <class T>
|
||||
void CircularBuffer<T>::PushFront(T value) {
|
||||
void circular_buffer<T>::push_front(T value) {
|
||||
if (m_data.size() == 0) {
|
||||
return;
|
||||
}
|
||||
@@ -38,7 +88,7 @@ void CircularBuffer<T>::PushFront(T value) {
|
||||
* if the buffer is full.
|
||||
*/
|
||||
template <class T>
|
||||
void CircularBuffer<T>::PushBack(T value) {
|
||||
void circular_buffer<T>::push_back(T value) {
|
||||
if (m_data.size() == 0) {
|
||||
return;
|
||||
}
|
||||
@@ -57,7 +107,7 @@ void CircularBuffer<T>::PushBack(T value) {
|
||||
* Pop value at front of buffer.
|
||||
*/
|
||||
template <class T>
|
||||
T CircularBuffer<T>::PopFront() {
|
||||
T circular_buffer<T>::pop_front() {
|
||||
// If there are no elements in the buffer, do nothing
|
||||
if (m_length == 0) {
|
||||
return 0;
|
||||
@@ -73,7 +123,7 @@ T CircularBuffer<T>::PopFront() {
|
||||
* Pop value at back of buffer.
|
||||
*/
|
||||
template <class T>
|
||||
T CircularBuffer<T>::PopBack() {
|
||||
T circular_buffer<T>::pop_back() {
|
||||
// If there are no elements in the buffer, do nothing
|
||||
if (m_length == 0) {
|
||||
return 0;
|
||||
@@ -87,7 +137,7 @@ T CircularBuffer<T>::PopBack() {
|
||||
* Resizes internal buffer to given size.
|
||||
*/
|
||||
template <class T>
|
||||
void CircularBuffer<T>::Resize(size_t size) {
|
||||
void circular_buffer<T>::resize(size_t size) {
|
||||
if (size > m_data.size()) {
|
||||
// Find end of buffer
|
||||
size_t insertLocation = (m_front + m_length) % m_data.size();
|
||||
@@ -140,7 +190,7 @@ void CircularBuffer<T>::Resize(size_t size) {
|
||||
* Sets internal buffer contents to zero.
|
||||
*/
|
||||
template <class T>
|
||||
void CircularBuffer<T>::Reset() {
|
||||
void circular_buffer<T>::reset() {
|
||||
std::fill(m_data.begin(), m_data.end(), 0);
|
||||
m_front = 0;
|
||||
m_length = 0;
|
||||
@@ -150,7 +200,7 @@ void CircularBuffer<T>::Reset() {
|
||||
* @return Element at index starting from front of buffer.
|
||||
*/
|
||||
template <class T>
|
||||
T& CircularBuffer<T>::operator[](size_t index) {
|
||||
T& circular_buffer<T>::operator[](size_t index) {
|
||||
return m_data[(m_front + index) % m_data.size()];
|
||||
}
|
||||
|
||||
@@ -158,7 +208,7 @@ T& CircularBuffer<T>::operator[](size_t index) {
|
||||
* @return Element at index starting from front of buffer.
|
||||
*/
|
||||
template <class T>
|
||||
const T& CircularBuffer<T>::operator[](size_t index) const {
|
||||
const T& circular_buffer<T>::operator[](size_t index) const {
|
||||
return m_data[(m_front + index) % m_data.size()];
|
||||
}
|
||||
|
||||
@@ -168,7 +218,7 @@ const T& CircularBuffer<T>::operator[](size_t index) const {
|
||||
* @return The result of the modulo operation.
|
||||
*/
|
||||
template <class T>
|
||||
size_t CircularBuffer<T>::ModuloInc(size_t index) {
|
||||
size_t circular_buffer<T>::ModuloInc(size_t index) {
|
||||
return (index + 1) % m_data.size();
|
||||
}
|
||||
|
||||
@@ -178,7 +228,7 @@ size_t CircularBuffer<T>::ModuloInc(size_t index) {
|
||||
* @return The result of the modulo operation.
|
||||
*/
|
||||
template <class T>
|
||||
size_t CircularBuffer<T>::ModuloDec(size_t index) {
|
||||
size_t circular_buffer<T>::ModuloDec(size_t index) {
|
||||
if (index == 0) {
|
||||
return m_data.size() - 1;
|
||||
} else {
|
||||
@@ -5,7 +5,7 @@
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#include "CircularBuffer.h" // NOLINT(build/include_order)
|
||||
#include "circular_buffer.h" // NOLINT(build/include_order)
|
||||
|
||||
#include <array>
|
||||
|
||||
@@ -24,10 +24,10 @@ static const std::array<double, 8> pushBackOut = {
|
||||
342.657, 234.252, 716.126, 132.344, 445.697, 22.727, 421.125, 799.913};
|
||||
|
||||
TEST(CircularBufferTest, PushFrontTest) {
|
||||
CircularBuffer<double> queue(8);
|
||||
circular_buffer<double> queue(8);
|
||||
|
||||
for (auto& value : values) {
|
||||
queue.PushFront(value);
|
||||
queue.push_front(value);
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < pushFrontOut.size(); i++) {
|
||||
@@ -36,10 +36,10 @@ TEST(CircularBufferTest, PushFrontTest) {
|
||||
}
|
||||
|
||||
TEST(CircularBufferTest, PushBackTest) {
|
||||
CircularBuffer<double> queue(8);
|
||||
circular_buffer<double> queue(8);
|
||||
|
||||
for (auto& value : values) {
|
||||
queue.PushBack(value);
|
||||
queue.push_back(value);
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < pushBackOut.size(); i++) {
|
||||
@@ -48,12 +48,12 @@ TEST(CircularBufferTest, PushBackTest) {
|
||||
}
|
||||
|
||||
TEST(CircularBufferTest, PushPopTest) {
|
||||
CircularBuffer<double> queue(3);
|
||||
circular_buffer<double> queue(3);
|
||||
|
||||
// Insert three elements into the buffer
|
||||
queue.PushBack(1.0);
|
||||
queue.PushBack(2.0);
|
||||
queue.PushBack(3.0);
|
||||
queue.push_back(1.0);
|
||||
queue.push_back(2.0);
|
||||
queue.push_back(3.0);
|
||||
|
||||
EXPECT_EQ(1.0, queue[0]);
|
||||
EXPECT_EQ(2.0, queue[1]);
|
||||
@@ -64,40 +64,40 @@ TEST(CircularBufferTest, PushPopTest) {
|
||||
* front-most elements.
|
||||
*/
|
||||
|
||||
queue.PushBack(4.0); // Overwrite 1 with 4
|
||||
queue.push_back(4.0); // Overwrite 1 with 4
|
||||
|
||||
// The buffer now contains 2, 3 and 4
|
||||
EXPECT_EQ(2.0, queue[0]);
|
||||
EXPECT_EQ(3.0, queue[1]);
|
||||
EXPECT_EQ(4.0, queue[2]);
|
||||
|
||||
queue.PushBack(5.0); // Overwrite 2 with 5
|
||||
queue.push_back(5.0); // Overwrite 2 with 5
|
||||
|
||||
// The buffer now contains 3, 4 and 5
|
||||
EXPECT_EQ(3.0, queue[0]);
|
||||
EXPECT_EQ(4.0, queue[1]);
|
||||
EXPECT_EQ(5.0, queue[2]);
|
||||
|
||||
EXPECT_EQ(5.0, queue.PopBack()); // 5 is removed
|
||||
EXPECT_EQ(5.0, queue.pop_back()); // 5 is removed
|
||||
|
||||
// The buffer now contains 3 and 4
|
||||
EXPECT_EQ(3.0, queue[0]);
|
||||
EXPECT_EQ(4.0, queue[1]);
|
||||
|
||||
EXPECT_EQ(3.0, queue.PopFront()); // 3 is removed
|
||||
EXPECT_EQ(3.0, queue.pop_front()); // 3 is removed
|
||||
|
||||
// Leaving only one element with value == 4
|
||||
EXPECT_EQ(4.0, queue[0]);
|
||||
}
|
||||
|
||||
TEST(CircularBufferTest, ResetTest) {
|
||||
CircularBuffer<double> queue(5);
|
||||
circular_buffer<double> queue(5);
|
||||
|
||||
for (size_t i = 1; i < 6; i++) {
|
||||
queue.PushBack(i);
|
||||
queue.push_back(i);
|
||||
}
|
||||
|
||||
queue.Reset();
|
||||
queue.reset();
|
||||
|
||||
for (size_t i = 0; i < 5; i++) {
|
||||
EXPECT_EQ(0.0, queue[i]);
|
||||
@@ -105,105 +105,105 @@ TEST(CircularBufferTest, ResetTest) {
|
||||
}
|
||||
|
||||
TEST(CircularBufferTest, ResizeTest) {
|
||||
CircularBuffer<double> queue(5);
|
||||
circular_buffer<double> queue(5);
|
||||
|
||||
/* Buffer contains {1, 2, 3, _, _}
|
||||
* ^ front
|
||||
*/
|
||||
queue.PushBack(1.0);
|
||||
queue.PushBack(2.0);
|
||||
queue.PushBack(3.0);
|
||||
queue.push_back(1.0);
|
||||
queue.push_back(2.0);
|
||||
queue.push_back(3.0);
|
||||
|
||||
queue.Resize(2);
|
||||
queue.resize(2);
|
||||
EXPECT_EQ(1.0, queue[0]);
|
||||
EXPECT_EQ(2.0, queue[1]);
|
||||
|
||||
queue.Resize(5);
|
||||
queue.resize(5);
|
||||
EXPECT_EQ(1.0, queue[0]);
|
||||
EXPECT_EQ(2.0, queue[1]);
|
||||
|
||||
queue.Reset();
|
||||
queue.reset();
|
||||
|
||||
/* Buffer contains {_, 1, 2, 3, _}
|
||||
* ^ front
|
||||
*/
|
||||
queue.PushBack(0.0);
|
||||
queue.PushBack(1.0);
|
||||
queue.PushBack(2.0);
|
||||
queue.PushBack(3.0);
|
||||
queue.PopFront();
|
||||
queue.push_back(0.0);
|
||||
queue.push_back(1.0);
|
||||
queue.push_back(2.0);
|
||||
queue.push_back(3.0);
|
||||
queue.pop_front();
|
||||
|
||||
queue.Resize(2);
|
||||
queue.resize(2);
|
||||
EXPECT_EQ(1.0, queue[0]);
|
||||
EXPECT_EQ(2.0, queue[1]);
|
||||
|
||||
queue.Resize(5);
|
||||
queue.resize(5);
|
||||
EXPECT_EQ(1.0, queue[0]);
|
||||
EXPECT_EQ(2.0, queue[1]);
|
||||
|
||||
queue.Reset();
|
||||
queue.reset();
|
||||
|
||||
/* Buffer contains {_, _, 1, 2, 3}
|
||||
* ^ front
|
||||
*/
|
||||
queue.PushBack(0.0);
|
||||
queue.PushBack(0.0);
|
||||
queue.PushBack(1.0);
|
||||
queue.PushBack(2.0);
|
||||
queue.PushBack(3.0);
|
||||
queue.PopFront();
|
||||
queue.PopFront();
|
||||
queue.push_back(0.0);
|
||||
queue.push_back(0.0);
|
||||
queue.push_back(1.0);
|
||||
queue.push_back(2.0);
|
||||
queue.push_back(3.0);
|
||||
queue.pop_front();
|
||||
queue.pop_front();
|
||||
|
||||
queue.Resize(2);
|
||||
queue.resize(2);
|
||||
EXPECT_EQ(1.0, queue[0]);
|
||||
EXPECT_EQ(2.0, queue[1]);
|
||||
|
||||
queue.Resize(5);
|
||||
queue.resize(5);
|
||||
EXPECT_EQ(1.0, queue[0]);
|
||||
EXPECT_EQ(2.0, queue[1]);
|
||||
|
||||
queue.Reset();
|
||||
queue.reset();
|
||||
|
||||
/* Buffer contains {3, _, _, 1, 2}
|
||||
* ^ front
|
||||
*/
|
||||
queue.PushBack(3.0);
|
||||
queue.PushFront(2.0);
|
||||
queue.PushFront(1.0);
|
||||
queue.push_back(3.0);
|
||||
queue.push_front(2.0);
|
||||
queue.push_front(1.0);
|
||||
|
||||
queue.Resize(2);
|
||||
queue.resize(2);
|
||||
EXPECT_EQ(1.0, queue[0]);
|
||||
EXPECT_EQ(2.0, queue[1]);
|
||||
|
||||
queue.Resize(5);
|
||||
queue.resize(5);
|
||||
EXPECT_EQ(1.0, queue[0]);
|
||||
EXPECT_EQ(2.0, queue[1]);
|
||||
|
||||
queue.Reset();
|
||||
queue.reset();
|
||||
|
||||
/* Buffer contains {2, 3, _, _, 1}
|
||||
* ^ front
|
||||
*/
|
||||
queue.PushBack(2.0);
|
||||
queue.PushBack(3.0);
|
||||
queue.PushFront(1.0);
|
||||
queue.push_back(2.0);
|
||||
queue.push_back(3.0);
|
||||
queue.push_front(1.0);
|
||||
|
||||
queue.Resize(2);
|
||||
queue.resize(2);
|
||||
EXPECT_EQ(1.0, queue[0]);
|
||||
EXPECT_EQ(2.0, queue[1]);
|
||||
|
||||
queue.Resize(5);
|
||||
queue.resize(5);
|
||||
EXPECT_EQ(1.0, queue[0]);
|
||||
EXPECT_EQ(2.0, queue[1]);
|
||||
|
||||
// Test PushBack() after resize
|
||||
queue.PushBack(3.0);
|
||||
// Test push_back() after resize
|
||||
queue.push_back(3.0);
|
||||
EXPECT_EQ(1.0, queue[0]);
|
||||
EXPECT_EQ(2.0, queue[1]);
|
||||
EXPECT_EQ(3.0, queue[2]);
|
||||
|
||||
// Test PushFront() after resize
|
||||
queue.PushFront(4.0);
|
||||
// Test push_front() after resize
|
||||
queue.push_front(4.0);
|
||||
EXPECT_EQ(4.0, queue[0]);
|
||||
EXPECT_EQ(1.0, queue[1]);
|
||||
EXPECT_EQ(2.0, queue[2]);
|
||||
|
||||
@@ -31,11 +31,43 @@ public class CircularBuffer {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns number of elements in buffer.
|
||||
*
|
||||
* @return number of elements in buffer
|
||||
*/
|
||||
double size() {
|
||||
return m_length;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get value at front of buffer.
|
||||
*
|
||||
* @return value at front of buffer
|
||||
*/
|
||||
double getFirst() {
|
||||
return m_data[m_front];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get value at back of buffer.
|
||||
*
|
||||
* @return value at back of buffer
|
||||
*/
|
||||
double getLast() {
|
||||
// If there are no elements in the buffer, do nothing
|
||||
if (m_length == 0) {
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
return m_data[(m_front + m_length - 1) % m_data.length];
|
||||
}
|
||||
|
||||
/**
|
||||
* Push new value onto front of the buffer. The value at the back is overwritten if the buffer is
|
||||
* full.
|
||||
*/
|
||||
public void pushFront(double value) {
|
||||
public void addFirst(double value) {
|
||||
if (m_data.length == 0) {
|
||||
return;
|
||||
}
|
||||
@@ -53,7 +85,7 @@ public class CircularBuffer {
|
||||
* Push new value onto back of the buffer. The value at the front is overwritten if the buffer is
|
||||
* full.
|
||||
*/
|
||||
public void pushBack(double value) {
|
||||
public void addLast(double value) {
|
||||
if (m_data.length == 0) {
|
||||
return;
|
||||
}
|
||||
@@ -73,7 +105,7 @@ public class CircularBuffer {
|
||||
*
|
||||
* @return value at front of buffer
|
||||
*/
|
||||
public double popFront() {
|
||||
public double removeFirst() {
|
||||
// If there are no elements in the buffer, do nothing
|
||||
if (m_length == 0) {
|
||||
return 0.0;
|
||||
@@ -89,7 +121,7 @@ public class CircularBuffer {
|
||||
/**
|
||||
* Pop value at back of buffer.
|
||||
*/
|
||||
public double popBack() {
|
||||
public double removeLast() {
|
||||
// If there are no elements in the buffer, do nothing
|
||||
if (m_length == 0) {
|
||||
return 0.0;
|
||||
@@ -117,7 +149,7 @@ public class CircularBuffer {
|
||||
/**
|
||||
* Sets internal buffer contents to zero.
|
||||
*/
|
||||
public void reset() {
|
||||
public void clear() {
|
||||
for (int i = 0; i < m_data.length; i++) {
|
||||
m_data[i] = 0.0;
|
||||
}
|
||||
|
||||
@@ -151,8 +151,8 @@ public class LinearDigitalFilter extends Filter {
|
||||
|
||||
@Override
|
||||
public void reset() {
|
||||
m_inputs.reset();
|
||||
m_outputs.reset();
|
||||
m_inputs.clear();
|
||||
m_outputs.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -165,7 +165,7 @@ public class LinearDigitalFilter extends Filter {
|
||||
double retVal = 0.0;
|
||||
|
||||
// Rotate the inputs
|
||||
m_inputs.pushFront(pidGetSource());
|
||||
m_inputs.addFirst(pidGetSource());
|
||||
|
||||
// Calculate the new value
|
||||
for (int i = 0; i < m_inputGains.length; i++) {
|
||||
@@ -176,7 +176,7 @@ public class LinearDigitalFilter extends Filter {
|
||||
}
|
||||
|
||||
// Rotate the outputs
|
||||
m_outputs.pushFront(retVal);
|
||||
m_outputs.addFirst(retVal);
|
||||
|
||||
return retVal;
|
||||
}
|
||||
|
||||
@@ -15,9 +15,9 @@ import static org.junit.Assert.assertEquals;
|
||||
public class CircularBufferTest {
|
||||
private double[] m_values = {751.848, 766.366, 342.657, 234.252, 716.126,
|
||||
132.344, 445.697, 22.727, 421.125, 799.913};
|
||||
private double[] m_pushFrontOut = {799.913, 421.125, 22.727, 445.697, 132.344,
|
||||
private double[] m_addFirstOut = {799.913, 421.125, 22.727, 445.697, 132.344,
|
||||
716.126, 234.252, 342.657};
|
||||
private double[] m_pushBackOut = {342.657, 234.252, 716.126, 132.344, 445.697,
|
||||
private double[] m_addLastOut = {342.657, 234.252, 716.126, 132.344, 445.697,
|
||||
22.727, 421.125, 799.913};
|
||||
|
||||
@BeforeClass
|
||||
@@ -26,28 +26,28 @@ public class CircularBufferTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void pushFrontTest() {
|
||||
public void addFirstTest() {
|
||||
CircularBuffer queue = new CircularBuffer(8);
|
||||
|
||||
for (double value : m_values) {
|
||||
queue.pushFront(value);
|
||||
queue.addFirst(value);
|
||||
}
|
||||
|
||||
for (int i = 0; i < m_pushFrontOut.length; i++) {
|
||||
assertEquals(m_pushFrontOut[i], queue.get(i), 0.00005);
|
||||
for (int i = 0; i < m_addFirstOut.length; i++) {
|
||||
assertEquals(m_addFirstOut[i], queue.get(i), 0.00005);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void pushBackTest() {
|
||||
public void addLastTest() {
|
||||
CircularBuffer queue = new CircularBuffer(8);
|
||||
|
||||
for (double value : m_values) {
|
||||
queue.pushBack(value);
|
||||
queue.addLast(value);
|
||||
}
|
||||
|
||||
for (int i = 0; i < m_pushBackOut.length; i++) {
|
||||
assertEquals(m_pushBackOut[i], queue.get(i), 0.00005);
|
||||
for (int i = 0; i < m_addLastOut.length; i++) {
|
||||
assertEquals(m_addLastOut[i], queue.get(i), 0.00005);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,9 +56,9 @@ public class CircularBufferTest {
|
||||
CircularBuffer queue = new CircularBuffer(3);
|
||||
|
||||
// Insert three elements into the buffer
|
||||
queue.pushBack(1.0);
|
||||
queue.pushBack(2.0);
|
||||
queue.pushBack(3.0);
|
||||
queue.addLast(1.0);
|
||||
queue.addLast(2.0);
|
||||
queue.addLast(3.0);
|
||||
|
||||
assertEquals(1.0, queue.get(0), 0.00005);
|
||||
assertEquals(2.0, queue.get(1), 0.00005);
|
||||
@@ -69,27 +69,27 @@ public class CircularBufferTest {
|
||||
* front-most elements.
|
||||
*/
|
||||
|
||||
queue.pushBack(4.0); // Overwrite 1 with 4
|
||||
queue.addLast(4.0); // Overwrite 1 with 4
|
||||
|
||||
// The buffer now contains 2, 3, and 4
|
||||
assertEquals(2.0, queue.get(0), 0.00005);
|
||||
assertEquals(3.0, queue.get(1), 0.00005);
|
||||
assertEquals(4.0, queue.get(2), 0.00005);
|
||||
|
||||
queue.pushBack(5.0); // Overwrite 2 with 5
|
||||
queue.addLast(5.0); // Overwrite 2 with 5
|
||||
|
||||
// The buffer now contains 3, 4, and 5
|
||||
assertEquals(3.0, queue.get(0), 0.00005);
|
||||
assertEquals(4.0, queue.get(1), 0.00005);
|
||||
assertEquals(5.0, queue.get(2), 0.00005);
|
||||
|
||||
assertEquals(5.0, queue.popBack(), 0.00005); // 5 is removed
|
||||
assertEquals(5.0, queue.removeLast(), 0.00005); // 5 is removed
|
||||
|
||||
// The buffer now contains 3 and 4
|
||||
assertEquals(3.0, queue.get(0), 0.00005);
|
||||
assertEquals(4.0, queue.get(1), 0.00005);
|
||||
|
||||
assertEquals(3.0, queue.popFront(), 0.00005); // 3 is removed
|
||||
assertEquals(3.0, queue.removeFirst(), 0.00005); // 3 is removed
|
||||
|
||||
// Leaving only one element with value == 4
|
||||
assertEquals(4.0, queue.get(0), 0.00005);
|
||||
@@ -100,10 +100,10 @@ public class CircularBufferTest {
|
||||
CircularBuffer queue = new CircularBuffer(5);
|
||||
|
||||
for (int i = 0; i < 6; i++) {
|
||||
queue.pushBack(i);
|
||||
queue.addLast(i);
|
||||
}
|
||||
|
||||
queue.reset();
|
||||
queue.clear();
|
||||
|
||||
for (int i = 0; i < 5; i++) {
|
||||
assertEquals(0.0, queue.get(i), 0.00005);
|
||||
@@ -117,9 +117,9 @@ public class CircularBufferTest {
|
||||
/* Buffer contains {1, 2, 3, _, _}
|
||||
* ^ front
|
||||
*/
|
||||
queue.pushBack(1.0);
|
||||
queue.pushBack(2.0);
|
||||
queue.pushBack(3.0);
|
||||
queue.addLast(1.0);
|
||||
queue.addLast(2.0);
|
||||
queue.addLast(3.0);
|
||||
|
||||
queue.resize(2);
|
||||
assertEquals(1.0, queue.get(0), 0.00005);
|
||||
@@ -129,16 +129,16 @@ public class CircularBufferTest {
|
||||
assertEquals(1.0, queue.get(0), 0.00005);
|
||||
assertEquals(2.0, queue.get(1), 0.00005);
|
||||
|
||||
queue.reset();
|
||||
queue.clear();
|
||||
|
||||
/* Buffer contains {_, 1, 2, 3, _}
|
||||
* ^ front
|
||||
*/
|
||||
queue.pushBack(0.0);
|
||||
queue.pushBack(1.0);
|
||||
queue.pushBack(2.0);
|
||||
queue.pushBack(3.0);
|
||||
queue.popFront();
|
||||
queue.addLast(0.0);
|
||||
queue.addLast(1.0);
|
||||
queue.addLast(2.0);
|
||||
queue.addLast(3.0);
|
||||
queue.removeFirst();
|
||||
|
||||
queue.resize(2);
|
||||
assertEquals(1.0, queue.get(0), 0.00005);
|
||||
@@ -148,18 +148,18 @@ public class CircularBufferTest {
|
||||
assertEquals(1.0, queue.get(0), 0.00005);
|
||||
assertEquals(2.0, queue.get(1), 0.00005);
|
||||
|
||||
queue.reset();
|
||||
queue.clear();
|
||||
|
||||
/* Buffer contains {_, _, 1, 2, 3}
|
||||
* ^ front
|
||||
*/
|
||||
queue.pushBack(0.0);
|
||||
queue.pushBack(0.0);
|
||||
queue.pushBack(1.0);
|
||||
queue.pushBack(2.0);
|
||||
queue.pushBack(3.0);
|
||||
queue.popFront();
|
||||
queue.popFront();
|
||||
queue.addLast(0.0);
|
||||
queue.addLast(0.0);
|
||||
queue.addLast(1.0);
|
||||
queue.addLast(2.0);
|
||||
queue.addLast(3.0);
|
||||
queue.removeFirst();
|
||||
queue.removeFirst();
|
||||
|
||||
queue.resize(2);
|
||||
assertEquals(1.0, queue.get(0), 0.00005);
|
||||
@@ -169,14 +169,14 @@ public class CircularBufferTest {
|
||||
assertEquals(1.0, queue.get(0), 0.00005);
|
||||
assertEquals(2.0, queue.get(1), 0.00005);
|
||||
|
||||
queue.reset();
|
||||
queue.clear();
|
||||
|
||||
/* Buffer contains {3, _, _, 1, 2}
|
||||
* ^ front
|
||||
*/
|
||||
queue.pushBack(3.0);
|
||||
queue.pushFront(2.0);
|
||||
queue.pushFront(1.0);
|
||||
queue.addLast(3.0);
|
||||
queue.addFirst(2.0);
|
||||
queue.addFirst(1.0);
|
||||
|
||||
queue.resize(2);
|
||||
assertEquals(1.0, queue.get(0), 0.00005);
|
||||
@@ -186,14 +186,14 @@ public class CircularBufferTest {
|
||||
assertEquals(1.0, queue.get(0), 0.00005);
|
||||
assertEquals(2.0, queue.get(1), 0.00005);
|
||||
|
||||
queue.reset();
|
||||
queue.clear();
|
||||
|
||||
/* Buffer contains {2, 3, _, _, 1}
|
||||
* ^ front
|
||||
*/
|
||||
queue.pushBack(2.0);
|
||||
queue.pushBack(3.0);
|
||||
queue.pushFront(1.0);
|
||||
queue.addLast(2.0);
|
||||
queue.addLast(3.0);
|
||||
queue.addFirst(1.0);
|
||||
|
||||
queue.resize(2);
|
||||
assertEquals(1.0, queue.get(0), 0.00005);
|
||||
@@ -203,14 +203,14 @@ public class CircularBufferTest {
|
||||
assertEquals(1.0, queue.get(0), 0.00005);
|
||||
assertEquals(2.0, queue.get(1), 0.00005);
|
||||
|
||||
// Test pushBack() after resize
|
||||
queue.pushBack(3.0);
|
||||
// Test addLast() after resize
|
||||
queue.addLast(3.0);
|
||||
assertEquals(1.0, queue.get(0), 0.00005);
|
||||
assertEquals(2.0, queue.get(1), 0.00005);
|
||||
assertEquals(3.0, queue.get(2), 0.00005);
|
||||
|
||||
// Test pushFront() after resize
|
||||
queue.pushFront(4.0);
|
||||
// Test addFirst() after resize
|
||||
queue.addFirst(4.0);
|
||||
assertEquals(4.0, queue.get(0), 0.00005);
|
||||
assertEquals(1.0, queue.get(1), 0.00005);
|
||||
assertEquals(2.0, queue.get(2), 0.00005);
|
||||
|
||||
Reference in New Issue
Block a user