rewrote end() for StaticVector

This commit is contained in:
froejdh_e 2025-05-21 15:46:04 +02:00 committed by Dhanya Thattil
parent 1d0eeea7ee
commit 30eab42294
2 changed files with 32 additions and 23 deletions

View File

@ -113,10 +113,10 @@ template <typename T, size_t Capacity> class StaticVector {
// auto begin() noexcept -> decltype(data_.begin()) { return data_.begin();
// }
const_iterator begin() const noexcept { return data_.begin(); }
iterator end() noexcept { return &data_[current_size]; }
const_iterator end() const noexcept { return &data_[current_size]; }
iterator end() noexcept { return data_.begin()+current_size; }
const_iterator end() const noexcept { return data_.begin()+current_size; }
const_iterator cbegin() const noexcept { return data_.cbegin(); }
const_iterator cend() const noexcept { return &data_[current_size]; }
const_iterator cend() const noexcept { return data_.cbegin()+current_size; }
void size_check(size_type s) const {
if (s > Capacity) {

View File

@ -8,12 +8,15 @@
#include <sstream>
#include <vector>
namespace sls {
using sls::StaticVector;
TEST_CASE("StaticVector is a container") {
REQUIRE(is_container<StaticVector<int, 7>>::value == true);
REQUIRE(sls::is_container<StaticVector<int, 7>>::value == true);
}
TEST_CASE("Comparing StaticVector containers") {
StaticVector<int, 5> a{0, 1, 2};
StaticVector<int, 5> b{0, 1, 2};
@ -90,10 +93,17 @@ TEST_CASE("Copy construct from array") {
REQUIRE(fcc == arr);
}
TEST_CASE("Construct from a smaller StaticVector") {
StaticVector<int, 3> sv{1, 2, 3};
StaticVector<int, 5> sv2{sv};
REQUIRE(sv == sv2);
}
TEST_CASE("Free function and method gives the same iterators") {
StaticVector<int, 3> fcc{1, 2, 3};
REQUIRE(std::begin(fcc) == fcc.begin());
}
SCENARIO("StaticVectors can be sized and resized", "[support]") {
GIVEN("A default constructed container") {
@ -246,23 +256,23 @@ SCENARIO("Sorting, removing and other manipulation of a container",
REQUIRE(a[3] == 90);
}
}
// WHEN("Sorting is done using free function for begin and end") {
// std::sort(begin(a), end(a));
// THEN("it also works") {
// REQUIRE(a[0] == 12);
// REQUIRE(a[1] == 12);
// REQUIRE(a[2] == 14);
// REQUIRE(a[3] == 90);
// }
// }
// WHEN("Erasing elements of a certain value") {
// a.erase(std::remove(begin(a), end(a), 12));
// THEN("all elements of that value are removed") {
// REQUIRE(a.size() == 2);
// REQUIRE(a[0] == 14);
// REQUIRE(a[1] == 90);
// }
// }
WHEN("Sorting is done using free function for begin and end") {
std::sort(std::begin(a), std::end(a));
THEN("it also works") {
REQUIRE(a[0] == 12);
REQUIRE(a[1] == 12);
REQUIRE(a[2] == 14);
REQUIRE(a[3] == 90);
}
}
WHEN("Erasing elements of a certain value") {
a.erase(std::remove(std::begin(a), std::end(a), 12));
THEN("all elements of that value are removed") {
REQUIRE(a.size() == 2);
REQUIRE(a[0] == 14);
REQUIRE(a[1] == 90);
}
}
}
}
@ -335,4 +345,3 @@ TEST_CASE("StaticVector stream") {
REQUIRE(oss.str() == "[33, 85667, 2]");
}
} // namespace sls