From bf59e881f3d4069d01e1dfad7c2cd8db54975763 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Fri, 2 Oct 2026 15:47:50 +0200 Subject: kstd: ring_buffer: add more iterator tests --- libs/kstd/kstd/ring_buffer.tests.cpp | 64 ++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/libs/kstd/kstd/ring_buffer.tests.cpp b/libs/kstd/kstd/ring_buffer.tests.cpp index 12a88e8f..99dbe3eb 100644 --- a/libs/kstd/kstd/ring_buffer.tests.cpp +++ b/libs/kstd/kstd/ring_buffer.tests.cpp @@ -1047,6 +1047,70 @@ SCENARIO("Ring Buffer iterators", "[kstd][ring_buffer]") REQUIRE(it == buffer.crend()); } + + THEN("a const_iterator can be constructed from an iterator") + { + auto it = decltype(buffer)::const_iterator{buffer.begin()}; + + AND_THEN("the const_iterator points to the same element as the original iterator") + { + REQUIRE(*it == *buffer.begin()); + } + } + + THEN("two iterators constructed from begin() and cbegin() compare equal") + { + auto it = buffer.begin(); + auto cit = buffer.cbegin(); + + REQUIRE(it == cit); + } + + THEN("an iterator to the second element compares greater than begin()") + { + REQUIRE(++buffer.begin() > buffer.begin()); + } + + THEN("an iterator to the second element compares less than the iterator to the third element") + { + REQUIRE(++buffer.begin() < std::next(buffer.begin(), 2)); + } + + THEN("the adding 1 to the iterator returned by begin() yields an iterator to the second element") + { + REQUIRE((buffer.begin() + 1) == ++buffer.begin()); + REQUIRE((1 + buffer.begin()) == ++buffer.begin()); + } + + THEN("subtracting 1 from the iterator returned by the second element yields an iterator to the first element") + { + REQUIRE(((buffer.begin() + 1) - 1) == buffer.begin()); + } + + THEN("postfix incrementing the iterator returned by begin() yields an iterator to the first element") + { + REQUIRE(buffer.begin()++ == buffer.begin()); + } + + THEN("postfix decrementing the iterator one after begin() yields an iterator to the first element") + { + REQUIRE((buffer.begin() + 1)-- == (buffer.begin() + 1)); + } + + THEN("accessing an element by subscripting the iterator yields the correct element") + { + auto it = buffer.begin(); + REQUIRE(std::addressof(it[0]) == std::addressof(buffer.at(0))); + REQUIRE(std::addressof(it[1]) == std::addressof(buffer.at(1))); + REQUIRE(std::addressof(it[2]) == std::addressof(buffer.at(2))); + } + + THEN("access to an element via member-of-pointer yields the correct element") + { + auto it = buffer.begin(); + REQUIRE(it.operator->() == std::addressof(buffer.at(0))); + } + WHEN("working through a const reference") { auto const & ref = buffer; -- cgit v1.2.3