aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--libs/kstd/kstd/ring_buffer.hpp17
-rw-r--r--libs/kstd/kstd/ring_buffer.tests.cpp35
2 files changed, 52 insertions, 0 deletions
diff --git a/libs/kstd/kstd/ring_buffer.hpp b/libs/kstd/kstd/ring_buffer.hpp
index 44510e3e..3e85baae 100644
--- a/libs/kstd/kstd/ring_buffer.hpp
+++ b/libs/kstd/kstd/ring_buffer.hpp
@@ -31,7 +31,24 @@ namespace kstd
//! Construct an empty ring buffer.
constexpr ring_buffer() noexcept = default;
+ //! Construct a ring buffer with a given number of value initialized elements.
+ //!
+ //! @param count The number of value initialized elements to construct.
+ constexpr ring_buffer(size_type count)
+ {
+ if (count > capacity())
+ {
+ os::panic("[KSTD] Tried to construct a ring buffer with more elements than it can support.");
+ }
+
+ std::ranges::for_each(std::views::iota(0uz, count), [&](auto i) { m_storage.construct(i); });
+ m_write_index = count % capacity();
+ }
+
//! Construct a ring buffer with a given number of copies of a given value.
+ //!
+ //! @param count The number of copies to construct.
+ //! @param value The value to copy.
constexpr ring_buffer(size_type count, ValueType const & value)
{
if (count > capacity())
diff --git a/libs/kstd/kstd/ring_buffer.tests.cpp b/libs/kstd/kstd/ring_buffer.tests.cpp
index 2157b6c3..43ddafae 100644
--- a/libs/kstd/kstd/ring_buffer.tests.cpp
+++ b/libs/kstd/kstd/ring_buffer.tests.cpp
@@ -152,6 +152,41 @@ SCENARIO("Ring Buffer initialization and construction", "[kstd][ring_buffer]")
}
}
}
+
+ WHEN("constructing by n-value-initialized constructor using a capacity of 5")
+ {
+ auto buffer = kstd::ring_buffer<int, 5>{3};
+
+ THEN("the capacity is 5")
+ {
+ REQUIRE(buffer.capacity() == 5);
+ }
+
+ THEN("the buffer is not empty")
+ {
+ REQUIRE_FALSE(buffer.empty());
+ }
+
+ THEN("the size is equal to the number of copies")
+ {
+ REQUIRE(buffer.size() == 3);
+ }
+
+ THEN("the maximum size is equal to the capacity")
+ {
+ REQUIRE(buffer.max_size() == buffer.capacity());
+ }
+ }
+
+ WHEN("constructing by n-value-initialized constructor using a capacity of 5 and 6 copies")
+ {
+ THEN("the constructor panics")
+ {
+ REQUIRE_THROWS_MATCHES(
+ (kstd::ring_buffer<int, 5>{6}), kstd::tests::os_panic,
+ Catch::Matchers::Message("[KSTD] Tried to construct a ring buffer with more elements than it can support."));
+ }
+ }
}
SCENARIO("Ring Buffer element access", "[kstd][ring_buffer]")