aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--libs/kstd/kstd/ring_buffer.hpp14
-rw-r--r--libs/kstd/kstd/ring_buffer.tests.cpp49
2 files changed, 62 insertions, 1 deletions
diff --git a/libs/kstd/kstd/ring_buffer.hpp b/libs/kstd/kstd/ring_buffer.hpp
index c332d2e2..8741c6db 100644
--- a/libs/kstd/kstd/ring_buffer.hpp
+++ b/libs/kstd/kstd/ring_buffer.hpp
@@ -197,7 +197,7 @@ namespace kstd
//! Destroy this ring buffer.
constexpr ~ring_buffer() noexcept
{
- std::ranges::for_each(std::views::reverse(*this), [](auto & element) { std::destroy_at(&element); });
+ clear();
}
//! Construct a ring buffer with a given number of value initialized elements.
@@ -455,6 +455,18 @@ namespace kstd
//! @}
+ //! @name Modifiers
+ //! @{
+
+ constexpr auto clear() noexcept -> void
+ {
+ std::ranges::for_each(std::views::reverse(*this), [](auto & element) { std::destroy_at(&element); });
+ m_size = 0;
+ m_read_index = 0;
+ }
+
+ //! @}
+
private:
//! Get a pointer to the element at the given read-index relative position.
//!
diff --git a/libs/kstd/kstd/ring_buffer.tests.cpp b/libs/kstd/kstd/ring_buffer.tests.cpp
index 8cd50e2f..163d537c 100644
--- a/libs/kstd/kstd/ring_buffer.tests.cpp
+++ b/libs/kstd/kstd/ring_buffer.tests.cpp
@@ -997,3 +997,52 @@ SCENARIO("Ring Buffer iterators", "[kstd][ring_buffer]")
}
}
}
+
+SCENARIO("Ring Buffer modifiers", "[kstd][ring_buffer]")
+{
+ GIVEN("An empty ring buffer")
+ {
+ auto buffer = kstd::ring_buffer<int, 5>{};
+
+ WHEN("clearing the buffer")
+ {
+ buffer.clear();
+
+ THEN("the buffer is still empty")
+ {
+ REQUIRE(buffer.empty());
+ }
+
+ THEN("the size is still 0")
+ {
+ REQUIRE(buffer.size() == 0);
+ }
+ }
+ }
+
+ GIVEN("A ring buffer containing static operation trackers")
+ {
+ auto buffer = kstd::ring_buffer<kstd::tests::static_copy_move_tracker, 5>{3};
+
+ WHEN("clearing the buffer")
+ {
+ kstd::tests::static_copy_move_tracker::reset();
+ buffer.clear();
+
+ THEN("the buffer is empty")
+ {
+ REQUIRE(buffer.empty());
+ }
+
+ THEN("the size is 0")
+ {
+ REQUIRE(buffer.size() == 0);
+ }
+
+ THEN("3 dtors are invoked")
+ {
+ REQUIRE(kstd::tests::static_copy_move_tracker::dtor_call_count == 3);
+ }
+ }
+ }
+} \ No newline at end of file