#include "ring_buffer.h" #include #include #include using namespace ::testing; TEST(RingBuffer, BasicStoreLoad) { std::array arr; RingBuffer rb{.buffer = std::as_writable_bytes(std::span{arr})}; std::array a0 = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; std::array a1 = {}; ASSERT_TRUE(rb.Store(std::as_bytes(std::span{a0}))); ASSERT_TRUE(rb.Load(std::as_writable_bytes(std::span{a1}))); EXPECT_THAT(a1, ElementsAre(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)); } TEST(RingBuffer, StoreOverflow) { std::array arr; RingBuffer rb{.buffer = std::as_writable_bytes(std::span{arr})}; std::array a0 = {0, 1, 2, 3, 4, 5, 6, 7}; std::array a1 = {}; ASSERT_TRUE(rb.Store(std::as_bytes(std::span{a0}))); ASSERT_FALSE(rb.Store(std::as_bytes(std::span{a0}))); EXPECT_EQ(rb.AvailableData(), 8); ASSERT_TRUE(rb.Pop(7)); EXPECT_EQ(rb.AvailableData(), 1); EXPECT_TRUE(rb.Store(std::as_bytes(std::span{a0}))); ASSERT_TRUE(rb.Load(std::as_writable_bytes(std::span{a1}))); // one byte leftover from first run, then 7 more EXPECT_THAT(a1, ElementsAre(7, 0, 1, 2, 3, 4, 5, 6)); }