Skip to content
Snippets Groups Projects
Commit 18838bcf authored by Cody Heiner's avatar Cody Heiner
Browse files

Add `front` and `back` methods to RingBuffer

Test: Corresponding added RingBuffer tests pass
(`atest libinput_tests`)

Bug: 268245099
Change-Id: If330940e67e70d809748b2aaa6156bed835c6c9d
parent 3704cb3b
No related branches found
No related tags found
No related merge requests found
......@@ -103,6 +103,11 @@ public:
iterator end() { return {*this, mSize}; }
const_iterator end() const { return {*this, mSize}; }
reference front() { return mBuffer[mBegin]; }
const_reference front() const { return mBuffer[mBegin]; }
reference back() { return mBuffer[bufferIndex(mSize - 1)]; }
const_reference back() const { return mBuffer[bufferIndex(mSize - 1)]; }
reference operator[](size_type i) { return mBuffer[bufferIndex(i)]; }
const_reference operator[](size_type i) const { return mBuffer[bufferIndex(i)]; }
......
......@@ -118,6 +118,21 @@ TEST(RingBufferTest, Assignment) {
EXPECT_EQ(0u, d.capacity());
}
TEST(RingBufferTest, FrontBackAccess) {
RingBuffer<int> buffer(/*capacity=*/2);
buffer.pushBack(1);
EXPECT_EQ(1, buffer.front());
EXPECT_EQ(1, buffer.back());
buffer.pushFront(0);
EXPECT_EQ(0, buffer.front());
EXPECT_EQ(1, buffer.back());
buffer.pushFront(-1);
EXPECT_EQ(-1, buffer.front());
EXPECT_EQ(0, buffer.back());
}
TEST(RingBufferTest, Subscripting) {
RingBuffer<int> buffer(/*capacity=*/2);
buffer.pushBack(1);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment