aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--libs/kstd/kstd/ring_buffer.hpp9
-rw-r--r--libs/kstd/kstd/ring_buffer.tests.cpp27
2 files changed, 36 insertions, 0 deletions
diff --git a/libs/kstd/kstd/ring_buffer.hpp b/libs/kstd/kstd/ring_buffer.hpp
index aa0b3a5c..2b97e0a9 100644
--- a/libs/kstd/kstd/ring_buffer.hpp
+++ b/libs/kstd/kstd/ring_buffer.hpp
@@ -175,6 +175,15 @@ namespace kstd
//! Construct an empty ring buffer.
constexpr ring_buffer() noexcept = default;
+ //! Construct a ring buffer by copying from an existing one.
+ constexpr ring_buffer(ring_buffer const & other)
+ : m_size{other.m_size}
+ , m_read_index{}
+ {
+ std::ranges::for_each(std::views::iota(0uz, other.size()),
+ [&](auto const i) { m_storage.construct(i, other[i]); });
+ }
+
//! Destroy this ring buffer.
constexpr ~ring_buffer() noexcept
{
diff --git a/libs/kstd/kstd/ring_buffer.tests.cpp b/libs/kstd/kstd/ring_buffer.tests.cpp
index 2a2d23b0..4e34249b 100644
--- a/libs/kstd/kstd/ring_buffer.tests.cpp
+++ b/libs/kstd/kstd/ring_buffer.tests.cpp
@@ -6,9 +6,12 @@
#include <catch2/matchers/catch_matchers.hpp>
#include <catch2/matchers/catch_matchers_exception.hpp>
+#include <algorithm>
#include <cstddef>
+#include <functional>
#include <iterator>
#include <memory>
+#include <string>
#include <type_traits>
#include <utility>
@@ -303,6 +306,30 @@ SCENARIO("Ring Buffer initialization and construction", "[kstd][ring_buffer]")
REQUIRE(buffer.size() == buffer.capacity());
}
}
+
+ GIVEN("A buffer with a capacity of 5 containing 3 elements")
+ {
+ auto buffer = kstd::ring_buffer<std::string, 5>{3, "test"};
+
+ WHEN("constructing a copy")
+ {
+ auto copy = buffer;
+
+ THEN("the contents of both are identical")
+ {
+ REQUIRE(std::ranges::equal(buffer, copy));
+ }
+
+ THEN("their element addresses differ")
+ {
+ auto address_of = [](auto const & obj) {
+ return std::addressof(obj);
+ };
+
+ REQUIRE_FALSE(std::ranges::equal(buffer, copy, std::equal_to{}, address_of, address_of));
+ }
+ }
+ }
}
SCENARIO("Ring Buffer destruction", "[kstd][ring_buffer]")