Skip to content

Commit

Permalink
TinyVector resize method, and some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chriselrod committed Feb 15, 2024
1 parent f662145 commit 478f2ef
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
9 changes: 9 additions & 0 deletions include/Containers/TinyVector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ template <class T, size_t N, typename L = ptrdiff_t> class TinyVector {
invariant(len > 0);
--len;
}
constexpr auto pop_back_val() -> T {
invariant(len > 0);
return std::move(data.data()[--len]);
}
[[nodiscard]] constexpr auto size() const -> ptrdiff_t {
ptrdiff_t l = ptrdiff_t(len);
invariant(l >= 0);
Expand All @@ -85,5 +89,10 @@ template <class T, size_t N, typename L = ptrdiff_t> class TinyVector {
constexpr auto begin() const -> const T * { return data.data(); }
constexpr auto end() -> T * { return data.data() + size(); }
constexpr auto end() const -> const T * { return data.data() + size(); }
constexpr void resize(L new_size) {
// initialize new data
for (L i = len; i < new_size; ++i) data.data()[i] = T{};
len = new_size;
}
};
} // namespace poly::containers
36 changes: 36 additions & 0 deletions test/matrix_test.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "Alloc/Arena.hpp"
#include "Containers/TinyVector.hpp"
#include "Math/Array.hpp"
#include "Math/Math.hpp"
#include "Math/MatrixDimensions.hpp"
Expand Down Expand Up @@ -310,3 +311,38 @@ TEST(SVectorTest, BasicAssertions) {
EXPECT_EQ(b, 22);
EXPECT_EQ(c, 33);
}
TEST(TinyVectorTest, BasicAssertions) {
poly::containers::TinyVector<int, 5> v{};
EXPECT_TRUE(v.empty());
EXPECT_EQ(v.size(), 0);
v.resize(3);
EXPECT_FALSE(v.empty());
EXPECT_EQ(v.size(), 3);
EXPECT_EQ(v.back(), 0);
v.push_back(2);
EXPECT_EQ(v.size(), 4);
EXPECT_EQ(v.back(), 2);
EXPECT_EQ(v.pop_back_val(), 2);
EXPECT_EQ(v.front(), 0);
EXPECT_EQ(v.back(), 0);
EXPECT_EQ(v.size(), 3);
v.pop_back();
EXPECT_EQ(v.size(), 2);
v.pop_back();
EXPECT_FALSE(v.empty());
EXPECT_EQ(v.size(), 1);
v.pop_back();
EXPECT_TRUE(v.empty());
EXPECT_EQ(v.size(), 0);
v.push_back(5);
EXPECT_EQ(v.front(), 5);
EXPECT_EQ(v.back(), 5);
v.push_back(2);
EXPECT_EQ(v.front(), 5);
EXPECT_EQ(v.back(), 2);
v.push_back(21);
EXPECT_EQ(v.back(), 21);
int s = 0;
for (auto x : v) s += x;
EXPECT_EQ(s, 28);
}

0 comments on commit 478f2ef

Please sign in to comment.