aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--libs/kstd/kstd/ring_buffer.hpp18
-rw-r--r--libs/kstd/kstd/ring_buffer.tests.cpp117
2 files changed, 134 insertions, 1 deletions
diff --git a/libs/kstd/kstd/ring_buffer.hpp b/libs/kstd/kstd/ring_buffer.hpp
index a848f3ce..92605fc2 100644
--- a/libs/kstd/kstd/ring_buffer.hpp
+++ b/libs/kstd/kstd/ring_buffer.hpp
@@ -278,6 +278,24 @@ namespace kstd
return const_iterator{this, 0};
}
+ //! Get an iterator to one past the last element.
+ [[nodiscard]] constexpr auto end() noexcept -> std::default_sentinel_t
+ {
+ return std::default_sentinel;
+ }
+
+ //! Get an iterator to one past the last element.
+ [[nodiscard]] constexpr auto end() const noexcept -> std::default_sentinel_t
+ {
+ return std::default_sentinel;
+ }
+
+ //! Get an iterator to one past the last element.
+ [[nodiscard]] constexpr auto cend() const noexcept -> std::default_sentinel_t
+ {
+ return std::default_sentinel;
+ }
+
//! @}
//! @name Capacity
diff --git a/libs/kstd/kstd/ring_buffer.tests.cpp b/libs/kstd/kstd/ring_buffer.tests.cpp
index a3881aa6..7a9c56be 100644
--- a/libs/kstd/kstd/ring_buffer.tests.cpp
+++ b/libs/kstd/kstd/ring_buffer.tests.cpp
@@ -121,6 +121,21 @@ SCENARIO("Ring Buffer interface types", "[kstd][ring_buffer]")
{
STATIC_REQUIRE(std::is_same_v<buffer::const_iterator, decltype(std::declval<buffer &>().cbegin())>);
}
+
+ THEN("the return type of end() is `std::default_sentinel_t`")
+ {
+ STATIC_REQUIRE(std::is_same_v<std::default_sentinel_t, decltype(std::declval<buffer &>().end())>);
+ }
+
+ THEN("the return type of end() is `std::default_sentinel_t` on a const buffer")
+ {
+ STATIC_REQUIRE(std::is_same_v<std::default_sentinel_t, decltype(std::declval<buffer const &>().end())>);
+ }
+
+ THEN("the return type of cend() is `std::default_sentinel_t`")
+ {
+ STATIC_REQUIRE(std::is_same_v<std::default_sentinel_t, decltype(std::declval<buffer &>().cend())>);
+ }
}
}
@@ -348,6 +363,26 @@ SCENARIO("Ring Buffer iterators", "[kstd][ring_buffer]")
REQUIRE(buffer.cbegin() == std::default_sentinel);
}
+ THEN("begin() returns an iterator equal to end()")
+ {
+ REQUIRE(buffer.begin() == buffer.end());
+ }
+
+ THEN("begin() returns an iterator equal to cend()")
+ {
+ REQUIRE(buffer.begin() == buffer.cend());
+ }
+
+ THEN("cbegin() returns an iterator equal to end()")
+ {
+ REQUIRE(buffer.cbegin() == buffer.end());
+ }
+
+ THEN("cbegin() returns an iterator equal to cend()")
+ {
+ REQUIRE(buffer.cbegin() == buffer.cend());
+ }
+
WHEN("working through a const reference")
{
auto const & ref = buffer;
@@ -356,6 +391,11 @@ SCENARIO("Ring Buffer iterators", "[kstd][ring_buffer]")
{
REQUIRE(ref.begin() == std::default_sentinel);
}
+
+ THEN("begin() returns an iterator equal to end()")
+ {
+ REQUIRE(buffer.begin() == buffer.end());
+ }
}
}
@@ -363,11 +403,21 @@ SCENARIO("Ring Buffer iterators", "[kstd][ring_buffer]")
{
auto buffer = kstd::ring_buffer<char, 5>{3, 'a'};
- THEN("begin() return an iterator that does not equal std::default_sentinel")
+ THEN("begin() returns an iterator that does not equal std::default_sentinel")
{
REQUIRE(buffer.begin() != std::default_sentinel);
}
+ THEN("begin() returns an iterator that does not equal end()")
+ {
+ REQUIRE(buffer.begin() != buffer.end());
+ }
+
+ THEN("begin() returns an iterator that does not equal cend()")
+ {
+ REQUIRE(buffer.begin() != buffer.cend());
+ }
+
THEN("begin() returns an iterator to the first element")
{
REQUIRE(*buffer.begin() == 'a');
@@ -378,6 +428,16 @@ SCENARIO("Ring Buffer iterators", "[kstd][ring_buffer]")
REQUIRE(buffer.cbegin() != std::default_sentinel);
}
+ THEN("cbegin() returns an iterator that does not equal end()")
+ {
+ REQUIRE(buffer.cbegin() != buffer.end());
+ }
+
+ THEN("cbegin() returns an iterator that does not equal cend()")
+ {
+ REQUIRE(buffer.cbegin() != buffer.cend());
+ }
+
THEN("cbegin() returns an iterator to the first element")
{
REQUIRE(*buffer.cbegin() == 'a');
@@ -394,6 +454,26 @@ SCENARIO("Ring Buffer iterators", "[kstd][ring_buffer]")
REQUIRE(it == std::default_sentinel);
}
+ THEN("incrementing the iterator returned by begin() past the end, yields an iterator equal to end()")
+ {
+ auto it = buffer.begin();
+ ++it;
+ ++it;
+ ++it;
+
+ REQUIRE(it == buffer.end());
+ }
+
+ THEN("incrementing the iterator returned by begin() past the end, yields an iterator equal to cend()")
+ {
+ auto it = buffer.begin();
+ ++it;
+ ++it;
+ ++it;
+
+ REQUIRE(it == buffer.cend());
+ }
+
THEN("incrementing the iterator returned by cbegin() past the end, yields an iterator equal to "
"std::default_sentinel")
{
@@ -405,6 +485,26 @@ SCENARIO("Ring Buffer iterators", "[kstd][ring_buffer]")
REQUIRE(it == std::default_sentinel);
}
+ THEN("incrementing the iterator returned by cbegin() past the end, yields an iterator equal to end()")
+ {
+ auto it = buffer.cbegin();
+ ++it;
+ ++it;
+ ++it;
+
+ REQUIRE(it == buffer.end());
+ }
+
+ THEN("incrementing the iterator returned by cbegin() past the end, yields an iterator equal to cend()")
+ {
+ auto it = buffer.cbegin();
+ ++it;
+ ++it;
+ ++it;
+
+ REQUIRE(it == buffer.cend());
+ }
+
WHEN("working through a const reference")
{
auto const & ref = buffer;
@@ -414,6 +514,11 @@ SCENARIO("Ring Buffer iterators", "[kstd][ring_buffer]")
REQUIRE(ref.begin() != std::default_sentinel);
}
+ THEN("begin() return an iterator that does not equal end()")
+ {
+ REQUIRE(ref.begin() != ref.end());
+ }
+
THEN("begin() returns an iterator to the first element")
{
REQUIRE(*ref.begin() == 'a');
@@ -429,6 +534,16 @@ SCENARIO("Ring Buffer iterators", "[kstd][ring_buffer]")
REQUIRE(it == std::default_sentinel);
}
+
+ THEN("incrementing the iterator returned by begin() past the end, yields an iterator equal to end()")
+ {
+ auto it = ref.begin();
+ ++it;
+ ++it;
+ ++it;
+
+ REQUIRE(it == ref.end());
+ }
}
}
}