aboutsummaryrefslogtreecommitdiff
path: root/libs/kstd/tests/src
diff options
context:
space:
mode:
authorFelix Morgner <felix.morgner@ost.ch>2026-05-01 17:22:53 +0200
committerFelix Morgner <felix.morgner@ost.ch>2026-05-01 17:22:53 +0200
commit2773e457773e3c88c212d6403950cef1fc96f880 (patch)
tree173f3e5c91ac5e85c8bb2f72bf53e710149a5153 /libs/kstd/tests/src
parent338d9b2b6fc517df2135089699234232495324d6 (diff)
downloadkernel-2773e457773e3c88c212d6403950cef1fc96f880.tar.xz
kernel-2773e457773e3c88c212d6403950cef1fc96f880.zip
kstd/vector: implement insert_range
Diffstat (limited to 'libs/kstd/tests/src')
-rw-r--r--libs/kstd/tests/src/vector.cpp291
1 files changed, 291 insertions, 0 deletions
diff --git a/libs/kstd/tests/src/vector.cpp b/libs/kstd/tests/src/vector.cpp
index a02ebf0..fd44437 100644
--- a/libs/kstd/tests/src/vector.cpp
+++ b/libs/kstd/tests/src/vector.cpp
@@ -620,6 +620,92 @@ SCENARIO("Vector modifiers", "[vector]")
REQUIRE(v[2] == 2);
}
}
+
+ WHEN("inserting a range at the beginning")
+ {
+ auto const arr = std::array<int, 3>{1, 2, 3};
+ auto it = v.insert_range(v.begin(), arr);
+
+ THEN("the size increases")
+ {
+ REQUIRE(v.size() == 3);
+ }
+
+ THEN("the capacity increases")
+ {
+ REQUIRE(v.capacity() >= 3);
+ }
+
+ THEN("the elements are inserted")
+ {
+ REQUIRE(v[0] == 1);
+ REQUIRE(v[1] == 2);
+ REQUIRE(v[2] == 3);
+ }
+
+ THEN("the returned iterator points to the beginning of the inserted range")
+ {
+ REQUIRE(it == v.begin());
+ }
+ }
+
+ WHEN("inserting a range at the end")
+ {
+ auto const arr = std::array<int, 3>{1, 2, 3};
+ auto it = v.insert_range(v.end(), arr);
+
+ THEN("the size increases")
+ {
+ REQUIRE(v.size() == 3);
+ }
+
+ THEN("the capacity increases")
+ {
+ REQUIRE(v.capacity() >= 3);
+ }
+
+ THEN("the elements are inserted")
+ {
+ REQUIRE(v[0] == 1);
+ REQUIRE(v[1] == 2);
+ REQUIRE(v[2] == 3);
+ }
+
+ THEN("the returned iterator points to the beginning of the inserted range")
+ {
+ REQUIRE(it == v.begin());
+ }
+ }
+
+ WHEN("inserting from an input range without sufficient capacity")
+ {
+ auto const arr = std::array<int, 3>{1, 2, 3};
+ auto const first = kstd::tests::test_input_iterator{arr.data(), arr.size()};
+ auto const last = kstd::tests::test_input_iterator{};
+ auto it = v.insert_range(v.begin(), 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 inserted")
+ {
+ REQUIRE(v[0] == 1);
+ REQUIRE(v[1] == 2);
+ REQUIRE(v[2] == 3);
+ }
+
+ THEN("the returned iterator points to the beginning of the inserted range")
+ {
+ REQUIRE(it == v.begin());
+ }
+ }
}
GIVEN("A populated vector")
@@ -1000,6 +1086,211 @@ SCENARIO("Vector modifiers", "[vector]")
REQUIRE(v[0] == 10);
}
}
+
+ WHEN("inserting an empty range")
+ {
+ auto initial_size = v.size();
+ auto it = v.insert_range(v.begin(), std::views::empty<int>);
+
+ THEN("the size does not change")
+ {
+ REQUIRE(v.size() == initial_size);
+ }
+
+ THEN("the capacity does not change")
+ {
+ REQUIRE(v.capacity() == initial_capacity);
+ }
+
+ THEN("the content is unchanged")
+ {
+ REQUIRE(v[0] == 10);
+ REQUIRE(v[1] == 20);
+ REQUIRE(v[2] == 30);
+ }
+
+ THEN("the returned iterator points to the position of insertion")
+ {
+ REQUIRE(it == v.begin());
+ }
+ }
+
+ WHEN("inserting a range at the beginning")
+ {
+ auto initial_size = v.size();
+ auto const arr = std::array<int, 3>{1, 2, 3};
+ auto it = v.insert_range(v.begin(), arr);
+
+ THEN("the size increases")
+ {
+ REQUIRE(v.size() == initial_size + 3);
+ }
+
+ THEN("the capacity increases")
+ {
+ REQUIRE(v.capacity() >= initial_size + 3);
+ }
+
+ THEN("the elements are inserted")
+ {
+ REQUIRE(v[0] == 1);
+ REQUIRE(v[1] == 2);
+ REQUIRE(v[2] == 3);
+ REQUIRE(v[3] == 10);
+ REQUIRE(v[4] == 20);
+ REQUIRE(v[5] == 30);
+ }
+
+ THEN("the returned iterator points to the beginning of the inserted range")
+ {
+ REQUIRE(it == v.begin());
+ }
+ }
+
+ WHEN("inserting a range at the end")
+ {
+ auto initial_size = v.size();
+ auto const arr = std::array<int, 3>{1, 2, 3};
+ auto it = v.insert_range(v.end(), arr);
+
+ THEN("the size increases")
+ {
+ REQUIRE(v.size() == initial_size + 3);
+ }
+
+ THEN("the capacity increases")
+ {
+ REQUIRE(v.capacity() >= initial_size + 3);
+ }
+
+ THEN("the elements are inserted")
+ {
+ REQUIRE(v[0] == 10);
+ REQUIRE(v[1] == 20);
+ REQUIRE(v[2] == 30);
+ REQUIRE(v[3] == 1);
+ REQUIRE(v[4] == 2);
+ REQUIRE(v[5] == 3);
+ }
+
+ THEN("the returned iterator points to the beginning of the inserted range")
+ {
+ REQUIRE(it == v.begin() + initial_size);
+ }
+ }
+
+ WHEN("inserting a range that causes reallocation")
+ {
+ auto initial_size = v.size();
+ auto const arr = std::array<int, 3>{1, 2, 3};
+ auto it = v.insert_range(v.begin() + 1, arr);
+
+ THEN("the size increases")
+ {
+ REQUIRE(v.size() == initial_size + 3);
+ }
+
+ THEN("the capacity increases")
+ {
+ REQUIRE(v.capacity() >= initial_size + 3);
+ }
+
+ THEN("the elements are inserted")
+ {
+ REQUIRE(v[0] == 10);
+ REQUIRE(v[1] == 1);
+ REQUIRE(v[2] == 2);
+ REQUIRE(v[3] == 3);
+ REQUIRE(v[4] == 20);
+ REQUIRE(v[5] == 30);
+ }
+
+ THEN("the returned iterator points to the beginning of the inserted range")
+ {
+ REQUIRE(it == v.begin() + 1);
+ }
+ }
+
+ WHEN("inserting fewer elements than the suffix without reallocation")
+ {
+ v.reserve(10);
+ auto const capacity = v.capacity();
+ auto const arr = std::array<int, 1>{1};
+ auto it = v.insert_range(v.begin() + 1, arr);
+
+ THEN("the elements are correctly placed")
+ {
+ REQUIRE(v.size() == 4);
+ REQUIRE(v[0] == 10);
+ REQUIRE(v[1] == 1);
+ REQUIRE(v[2] == 20);
+ REQUIRE(v[3] == 30);
+ }
+
+ THEN("no reallocation occurs")
+ {
+ REQUIRE(v.capacity() == capacity);
+ }
+
+ THEN("the returned iterator points to the inserted element")
+ {
+ REQUIRE(it == v.begin() + 1);
+ }
+ }
+
+ WHEN("inserting fewer elements than the suffix without sufficient capacity")
+ {
+ v.shrink_to_fit();
+ auto const arr = std::array<int, 1>{1};
+ auto it = v.insert_range(v.begin() + 1, arr);
+
+ THEN("the elements are correctly placed")
+ {
+ REQUIRE(v.size() == 4);
+ REQUIRE(v[0] == 10);
+ REQUIRE(v[1] == 1);
+ REQUIRE(v[2] == 20);
+ REQUIRE(v[3] == 30);
+ }
+
+ THEN("the returned iterator points to the inserted element")
+ {
+ REQUIRE(it == v.begin() + 1);
+ }
+ }
+
+ WHEN("inserting from an input range without sufficient capacity")
+ {
+ auto const arr = std::array<int, 3>{1, 2, 3};
+ auto const first = kstd::tests::test_input_iterator{arr.data(), arr.size()};
+ auto const last = kstd::tests::test_input_iterator{};
+ auto it = v.insert_range(v.begin(), std::ranges::subrange{first, last});
+
+ THEN("the size increases")
+ {
+ REQUIRE(v.size() == 6);
+ }
+
+ THEN("the capacity increases")
+ {
+ REQUIRE(v.capacity() >= 6);
+ }
+
+ THEN("the elements are inserted")
+ {
+ REQUIRE(v[0] == 1);
+ REQUIRE(v[1] == 2);
+ REQUIRE(v[2] == 3);
+ REQUIRE(v[3] == 10);
+ REQUIRE(v[4] == 20);
+ REQUIRE(v[5] == 30);
+ }
+
+ THEN("the returned iterator points to the beginning of the inserted range")
+ {
+ REQUIRE(it == v.begin());
+ }
+ }
}
}