aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--libs/kstd/kstd/ring_buffer.hpp15
-rw-r--r--libs/kstd/kstd/ring_buffer.tests.cpp38
2 files changed, 53 insertions, 0 deletions
diff --git a/libs/kstd/kstd/ring_buffer.hpp b/libs/kstd/kstd/ring_buffer.hpp
index af70e991..5039c7b4 100644
--- a/libs/kstd/kstd/ring_buffer.hpp
+++ b/libs/kstd/kstd/ring_buffer.hpp
@@ -534,6 +534,21 @@ namespace kstd
return true;
}
+ //! Remove the first element in the buffer.
+ //!
+ //! @warning This function will panic if the buffer is empty.
+ constexpr auto pop_front() -> void
+ {
+ if (empty())
+ {
+ os::panic("[KSTD] Tried to pop an element from an empty ring_buffer!");
+ }
+
+ std::destroy_at(element_at(0));
+ m_read_index = (m_read_index + 1) % capacity();
+ --m_size;
+ }
+
//! @}
private:
diff --git a/libs/kstd/kstd/ring_buffer.tests.cpp b/libs/kstd/kstd/ring_buffer.tests.cpp
index 0b332494..479283ed 100644
--- a/libs/kstd/kstd/ring_buffer.tests.cpp
+++ b/libs/kstd/kstd/ring_buffer.tests.cpp
@@ -1105,6 +1105,12 @@ SCENARIO("Ring Buffer modifiers", "[kstd][ring_buffer]")
{
REQUIRE(buffer.try_push_back(1));
}
+
+ THEN("popping an element panics")
+ {
+ REQUIRE_THROWS_MATCHES(buffer.pop_front(), kstd::tests::os_panic,
+ Catch::Matchers::Message("[KSTD] Tried to pop an element from an empty ring_buffer!"));
+ }
}
GIVEN("A ring buffer with a capacity of 5 containing 3 elements")
@@ -1242,6 +1248,38 @@ SCENARIO("Ring Buffer modifiers", "[kstd][ring_buffer]")
}
}
}
+
+ WHEN("popping an element")
+ {
+ buffer.pop_front();
+
+ THEN("the size is 2")
+ {
+ REQUIRE(buffer.size() == 2);
+ }
+
+ THEN("the buffer is not empty")
+ {
+ REQUIRE_FALSE(buffer.empty());
+ }
+ }
+
+ WHEN("popping 3 elements")
+ {
+ buffer.pop_front();
+ buffer.pop_front();
+ buffer.pop_front();
+
+ THEN("the size is 0")
+ {
+ REQUIRE(buffer.size() == 0);
+ }
+
+ THEN("the buffer is empty")
+ {
+ REQUIRE(buffer.empty());
+ }
+ }
}
GIVEN("A full ring buffer of static operation trackers")