Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

marl::containers::vector fixes #144

Merged
merged 1 commit into from
Jun 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions include/marl/containers.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ class vector {

inline ~vector();

inline vector& operator=(const vector&);

template <int BASE_CAPACITY_2>
inline vector<T, BASE_CAPACITY>& operator=(const vector<T, BASE_CAPACITY_2>&);

Expand All @@ -72,6 +74,8 @@ class vector {
private:
using TStorage = typename marl::aligned_storage<sizeof(T), alignof(T)>::type;

vector(const vector&) = delete;

inline void free();

Allocator* const allocator;
Expand Down Expand Up @@ -110,6 +114,18 @@ vector<T, BASE_CAPACITY>::~vector() {
free();
}

template <typename T, int BASE_CAPACITY>
vector<T, BASE_CAPACITY>& vector<T, BASE_CAPACITY>::operator=(
const vector& other) {
free();
reserve(other.size());
count = other.size();
for (size_t i = 0; i < count; i++) {
new (&reinterpret_cast<T*>(elements)[i]) T(other[i]);
}
return *this;
}

template <typename T, int BASE_CAPACITY>
template <int BASE_CAPACITY_2>
vector<T, BASE_CAPACITY>& vector<T, BASE_CAPACITY>::operator=(
Expand Down Expand Up @@ -238,6 +254,7 @@ void vector<T, BASE_CAPACITY>::free() {

if (allocation.ptr != nullptr) {
allocator->free(allocation);
allocation = {};
elements = nullptr;
}
}
Expand Down
47 changes: 47 additions & 0 deletions src/containers_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,60 @@ TEST_F(ContainersVectorTest, CopyConstruct) {
vectorA[1] = "B";
vectorA[2] = "C";

marl::containers::vector<std::string, 4> vectorB(vectorA, allocator);
ASSERT_EQ(vectorB.size(), size_t(3));
ASSERT_EQ(vectorB[0], "A");
ASSERT_EQ(vectorB[1], "B");
ASSERT_EQ(vectorB[2], "C");
}

TEST_F(ContainersVectorTest, CopyConstructDifferentBaseCapacity) {
marl::containers::vector<std::string, 4> vectorA(allocator);

vectorA.resize(3);
vectorA[0] = "A";
vectorA[1] = "B";
vectorA[2] = "C";

marl::containers::vector<std::string, 2> vectorB(vectorA, allocator);
ASSERT_EQ(vectorB.size(), size_t(3));
ASSERT_EQ(vectorB[0], "A");
ASSERT_EQ(vectorB[1], "B");
ASSERT_EQ(vectorB[2], "C");
}

TEST_F(ContainersVectorTest, CopyAssignment) {
marl::containers::vector<std::string, 4> vectorA(allocator);

vectorA.resize(3);
vectorA[0] = "A";
vectorA[1] = "B";
vectorA[2] = "C";

marl::containers::vector<std::string, 4> vectorB(allocator);
vectorB = vectorA;
ASSERT_EQ(vectorB.size(), size_t(3));
ASSERT_EQ(vectorB[0], "A");
ASSERT_EQ(vectorB[1], "B");
ASSERT_EQ(vectorB[2], "C");
}

TEST_F(ContainersVectorTest, CopyAssignmentDifferentBaseCapacity) {
marl::containers::vector<std::string, 4> vectorA(allocator);

vectorA.resize(3);
vectorA[0] = "A";
vectorA[1] = "B";
vectorA[2] = "C";

marl::containers::vector<std::string, 2> vectorB(allocator);
vectorB = vectorA;
ASSERT_EQ(vectorB.size(), size_t(3));
ASSERT_EQ(vectorB[0], "A");
ASSERT_EQ(vectorB[1], "B");
ASSERT_EQ(vectorB[2], "C");
}

TEST_F(ContainersVectorTest, MoveConstruct) {
marl::containers::vector<std::string, 4> vectorA(allocator);

Expand Down