From 338d9b2b6fc517df2135089699234232495324d6 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Fri, 1 May 2026 13:11:37 +0200 Subject: kstd/vector: improve append_range tests --- libs/kstd/tests/include/kstd/tests/test_types.hpp | 27 +++++++- libs/kstd/tests/src/vector.cpp | 77 +++++++++++++++++++++-- 2 files changed, 98 insertions(+), 6 deletions(-) (limited to 'libs/kstd') diff --git a/libs/kstd/tests/include/kstd/tests/test_types.hpp b/libs/kstd/tests/include/kstd/tests/test_types.hpp index 9207ee9..8231fd3 100644 --- a/libs/kstd/tests/include/kstd/tests/test_types.hpp +++ b/libs/kstd/tests/include/kstd/tests/test_types.hpp @@ -239,17 +239,29 @@ namespace kstd::tests struct test_input_iterator { using iterator_concept = std::input_iterator_tag; + using iterator_category = std::input_iterator_tag; using difference_type = std::ptrdiff_t; using value_type = int; + using reference = int const &; + using pointer = int const *; //! The current element pointed to by the iterator. int const * current; + //! The number of elements in the range. + std::size_t count; + + explicit test_input_iterator() + : current{nullptr} + , count{0} + {} //! Construct a new test input iterator. //! //! @param current The current element pointed to by the iterator. - constexpr explicit test_input_iterator(int const * current) + //! @param count The number of elements in the range. + explicit test_input_iterator(int const * current, std::size_t count) : current{current} + , count{count} {} //! Dereference the iterator to get the current element. @@ -266,6 +278,7 @@ namespace kstd::tests auto operator++() -> test_input_iterator & { ++current; + --count; return *this; } @@ -281,7 +294,17 @@ namespace kstd::tests //! @return True if the two test input iterators are equal, false otherwise. [[nodiscard]] auto operator==(test_input_iterator const & other) const -> bool { - return current == other.current; + if (current == nullptr && other.current == nullptr) + { + return true; + } + + if (current == nullptr || other.current == nullptr) + { + return count == other.count; + } + + return current == other.current && count == other.count; } }; diff --git a/libs/kstd/tests/src/vector.cpp b/libs/kstd/tests/src/vector.cpp index edf47d7..a02ebf0 100644 --- a/libs/kstd/tests/src/vector.cpp +++ b/libs/kstd/tests/src/vector.cpp @@ -522,16 +522,77 @@ SCENARIO("Vector modifiers", "[vector]") auto const range = std::views::iota(0, 3); v.append_range(range); - THEN("the size and capacity increase and the elements are appended") + THEN("the size increases") { REQUIRE(v.size() == 3); + } + + THEN("the capacity increases") + { REQUIRE(v.capacity() >= 3); + } + + THEN("the elements are appended") + { REQUIRE(v[0] == 0); REQUIRE(v[1] == 1); REQUIRE(v[2] == 2); } } + WHEN("appending from an input range") + { + auto const arr = std::array{1, 2, 3}; + auto const first = kstd::tests::test_input_iterator{arr.data(), arr.size()}; + auto const last = kstd::tests::test_input_iterator{}; + + v.append_range(std::ranges::subrange{first, last}); + + THEN("the size increases") + { + REQUIRE(v.size() == 3); + } + + THEN("the capacity increases") + { + REQUIRE(v.capacity() >= 3); + } + + THEN("the elements are appended") + { + REQUIRE(v[0] == 1); + REQUIRE(v[1] == 2); + REQUIRE(v[2] == 3); + } + } + + WHEN("appending from an input range with sufficient capacity") + { + v.reserve(3); + auto const arr = std::array{1, 2, 3}; + auto const first = kstd::tests::test_input_iterator{arr.data(), arr.size()}; + auto const last = kstd::tests::test_input_iterator{}; + + v.append_range(std::ranges::subrange{first, last}); + + THEN("the size increases") + { + REQUIRE(v.size() == 3); + } + + THEN("the capacity stays the same") + { + REQUIRE(v.capacity() == 3); + } + + THEN("the elements are appended") + { + REQUIRE(v[0] == 1); + REQUIRE(v[1] == 2); + REQUIRE(v[2] == 3); + } + } + WHEN("resizing the vector to a greater size") { v.resize(3); @@ -881,10 +942,18 @@ SCENARIO("Vector modifiers", "[vector]") auto initial_size = v.size(); v.append_range(std::views::iota(0, 3)); - THEN("capacity and size are increased and the elements are appended") + THEN("capacity is increased") { REQUIRE(v.capacity() >= initial_capacity); + } + + THEN("size is increased") + { REQUIRE(v.size() == initial_size + 3); + } + + THEN("the elements are appended") + { REQUIRE(v[initial_size + 0] == 0); REQUIRE(v[initial_size + 1] == 1); REQUIRE(v[initial_size + 2] == 2); @@ -1290,8 +1359,8 @@ SCENARIO("Vector advanced construction", "[vector]") WHEN("constructing from input iterators") { auto const arr = std::array{1, 2, 3}; - auto const first = kstd::tests::test_input_iterator{arr.data()}; - auto const last = kstd::tests::test_input_iterator{arr.data() + arr.size()}; + auto const first = kstd::tests::test_input_iterator{arr.data(), arr.size()}; + auto const last = kstd::tests::test_input_iterator{}; auto v = kstd::vector(first, last); -- cgit v1.2.3