hack/tests/patterns/ring_buffer.cpp
2025-01-04 11:41:10 +03:00

61 lines
1.5 KiB
C++

#include "catch2/catch_test_macros.hpp"
#include "hack/patterns/ring_buffer.hpp"
TEST_CASE("patterns")
{
SECTION("ring buffer")
{
// single value
hack::patterns::ring_buffer<int> rb1(10);
REQUIRE(rb1.empty() == true);
for (int i = 1; i < 13; ++i) rb1.put(i);
REQUIRE(rb1.empty() == false);
REQUIRE(rb1.size() == 10);
while(!rb1.empty()) REQUIRE(rb1.get().has_value() == true);
REQUIRE(rb1.empty() == true);
REQUIRE(rb1.size() == 0);
rb1.put(123);
rb1.put(23);
rb1.put(3);
REQUIRE(rb1.get().value() == 123);
REQUIRE(rb1.empty() == false);
REQUIRE(rb1.size() == 2);
REQUIRE(rb1.get().value() == 23);
REQUIRE(rb1.empty() == false);
REQUIRE(rb1.size() == 1);
rb1.reset();
REQUIRE(rb1.empty() == true);
REQUIRE(rb1.size() == 0);
REQUIRE(rb1.get().has_value() == false);
// vector value
std::vector<int> v { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
hack::patterns::ring_buffer<int> rb2(10);
REQUIRE(rb2.empty() == true);
rb2.put(v);
REQUIRE(rb2.get().value() == 1);
REQUIRE(rb2.get().value() == 2);
v.emplace_back(17);
rb2.put(v);
REQUIRE(rb2.get().value() == 2);
REQUIRE(rb2.get().value() == 3);
REQUIRE(rb2.get().value() == 4);
REQUIRE(rb2.get().value() == 5);
REQUIRE(rb2.get().value() == 6);
REQUIRE(rb2.get().value() == 7);
REQUIRE(rb2.get().value() == 8);
REQUIRE(rb2.get().value() == 9);
REQUIRE(rb2.get().value() == 10);
REQUIRE(rb2.get().value() == 17);
}
}