diff options
Diffstat (limited to 'libs')
| -rw-r--r-- | libs/kstd/include/kstd/vector | 7 | ||||
| -rw-r--r-- | libs/kstd/tests/src/vector.cpp | 44 |
2 files changed, 47 insertions, 4 deletions
diff --git a/libs/kstd/include/kstd/vector b/libs/kstd/include/kstd/vector index 74aefa9..771fc87 100644 --- a/libs/kstd/include/kstd/vector +++ b/libs/kstd/include/kstd/vector @@ -548,7 +548,6 @@ namespace kstd if (new_capacity > max_size()) { kstd::os::panic("[kstd:vector] Tried to reserve more space than theoretically possible."); - return; } auto new_data = allocate_n(new_capacity); @@ -690,6 +689,12 @@ namespace kstd return begin() + prefix_size; } + //! Erase an element at a given position. + //! + //! @note This function will panic if position == end() + //! + //! @param position An interator pointing to the element to delete + //! @return An iterator pointing to the element after the deleted element constexpr auto erase(const_iterator position) -> iterator { if (position == end()) diff --git a/libs/kstd/tests/src/vector.cpp b/libs/kstd/tests/src/vector.cpp index 02b8786..913427c 100644 --- a/libs/kstd/tests/src/vector.cpp +++ b/libs/kstd/tests/src/vector.cpp @@ -475,6 +475,20 @@ SCENARIO("Vector modifiers", "[vector]") REQUIRE(it == v.begin()); } } + + WHEN("inserting an lvalue element") + { + auto const value = 42; + auto it = v.insert(v.cbegin(), value); + + THEN("the size and capacity increase and the element is inserted") + { + REQUIRE(v.size() == 1); + REQUIRE(v.capacity() >= 1); + REQUIRE(v[0] == 42); + REQUIRE(it == v.begin()); + } + } } GIVEN("A populated vector") @@ -597,6 +611,22 @@ SCENARIO("Vector modifiers", "[vector]") } } + WHEN("inserting an lvalue at the end") + { + auto const value = 40; + auto it = v.insert(v.cend(), value); + + THEN("the element is inserted at the back") + { + REQUIRE(v.size() == 4); + REQUIRE(v[0] == 10); + REQUIRE(v[1] == 20); + REQUIRE(v[2] == 30); + REQUIRE(v[3] == 40); + REQUIRE(it == v.begin() + 3); + } + } + WHEN("inserting when capacity is sufficient") { v.reserve(10); @@ -722,6 +752,14 @@ SCENARIO("Vector modifiers", "[vector]") REQUIRE(it == v.end()); } } + + WHEN("erasing the end() iterator") + { + THEN("a panic is triggered") + { + REQUIRE_THROWS_AS(v.erase(v.end()), kstd::tests::os_panic); + } + } } } @@ -944,7 +982,7 @@ SCENARIO("Vector modifier move semantics", "[vector]") WHEN("erasing an element in the middle") { - for (auto& elem : v) + for (auto & elem : v) { elem.was_copied = false; elem.was_moved = false; @@ -955,7 +993,7 @@ SCENARIO("Vector modifier move semantics", "[vector]") THEN("the subsequent elements are move-assigned leftwards") { REQUIRE(v.size() == 2); - + REQUIRE(v[0].value == 10); REQUIRE_FALSE(v[0].was_moved); REQUIRE_FALSE(v[0].was_copied); @@ -970,7 +1008,7 @@ SCENARIO("Vector modifier move semantics", "[vector]") WHEN("erasing the last element") { - for (auto& elem : v) + for (auto & elem : v) { elem.was_copied = false; elem.was_moved = false; |
