38#include <gmock/gmock.h>
39#include <gtest/gtest.h>
45using testing::ElementsAreArray;
49 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
50 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf,
63TEST(CircleBufTest, BasicReadWriteNoOverflow)
70 EXPECT_EQ(buf.
size(), 8);
72 EXPECT_THAT(
subArr(foo, 8), ElementsAreArray(
data, 8));
76 EXPECT_THAT(
subArr(foo, 2), ElementsAreArray(
data, 2));
77 EXPECT_EQ(buf.
size(), 6);
79 EXPECT_THAT(
subArr(foo, 6), ElementsAreArray(
data + 2, 6));
80 EXPECT_EQ(buf.
size(), 0);
84TEST(CircleBufTest, SingleWriteOverflow)
90 EXPECT_EQ(buf.
size(), 8);
92 EXPECT_THAT(
subArr(foo, 8), ElementsAreArray(
data + 8, 8));
97TEST(CircleBufTest, MultiWriteOverflow)
105 EXPECT_EQ(buf.
size(), 8);
107 EXPECT_THAT(
subArr(foo, 2), ElementsAreArray(
data + 4, 2));
108 EXPECT_THAT(
subArr(foo, 6, 2), ElementsAreArray(
data + 8, 6));
112TEST(CircleBufTest, PointerWrapAround)
123 EXPECT_EQ(buf.
size(), 8);
126 buf.
read(foo + 4, 6);
127 EXPECT_EQ(buf.
size(), 2);
128 EXPECT_THAT(
subArr(foo, 10), ElementsAreArray(
data, 10));
130 buf.
read(foo + 10, 2);
131 EXPECT_EQ(buf.
size(), 0);
132 EXPECT_THAT(
subArr(foo, 12), ElementsAreArray(
data, 12));
136TEST(CircleBufTest, ProduceConsumeEmpty)
142 EXPECT_TRUE(buf.
empty());
145 EXPECT_FALSE(buf.
empty());
151 EXPECT_TRUE(buf.
empty());
TEST(CircleBufTest, BasicReadWriteNoOverflow)
std::vector< T > subArr(T *arr, int size, int offset=0)
Circular buffer backed by a vector.
void read(OutputIterator out, size_t len)
Copy buffer contents and advance the read pointer.
void write(InputIterator in, size_t len)
Add elements to the end of the ring buffers and advance.
void peek(OutputIterator out, size_t len) const
Copy buffer contents without advancing the read pointer.
Copyright (c) 2024 - Pranith Kumar Copyright (c) 2020 Inria All rights reserved.