Skip to content

Commit

Permalink
Add and test copy and move ctor/assignment
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexMax committed Dec 10, 2023
1 parent c62e910 commit ab4740f
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
40 changes: 40 additions & 0 deletions include/lexio/bufreader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,49 @@ class GenericBufReader

public:
GenericBufReader() = delete;

GenericBufReader(const GenericBufReader &other)
: m_wrapped(other.m_wrapped), m_buffer(new uint8_t[other.m_allocSize]), m_allocSize(other.m_allocSize),
m_size(other.m_size)
{
std::copy(&other.m_buffer[0], &other.m_buffer[m_size], m_buffer);
}

GenericBufReader(GenericBufReader &&other)
: m_wrapped(std::move(other.m_wrapped)), m_buffer(other.m_buffer), m_allocSize(other.m_allocSize),
m_size(other.m_size)
{
other.m_buffer = nullptr;
}

GenericBufReader(T &&wrapped) : m_wrapped(wrapped) {}
~GenericBufReader() { delete[] m_buffer; }

GenericBufReader &operator=(const GenericBufReader &other)
{
if (this == &other)
{
return *this;
}

GenericBufReader copy{other};
std::swap(m_wrapped, copy.m_wrapped);
std::swap(m_buffer, copy.m_buffer);
std::swap(m_allocSize, copy.m_allocSize);
std::swap(m_size, copy.m_size);
return *this;
}

GenericBufReader &operator=(GenericBufReader &&other) noexcept
{
m_wrapped = std::move(other.m_wrapped);
m_buffer = other.m_buffer;
m_allocSize = other.m_allocSize;
m_size = other.m_size;
other.m_buffer = nullptr;
return *this;
}

size_t LexRead(uint8_t *outDest, const size_t count)
{
BufferView data = LexFillBuffer(count);
Expand Down
49 changes: 49 additions & 0 deletions tests/test_bufreader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,52 @@ TEST_CASE("ConsumeBuffer, EOF")
test = LexIO::GetBuffer(bufReader);
REQUIRE(test.second == 0);
}

TEST_CASE("Copy constructor/Copy assignment")
{
auto stream = GetStream();
auto bufReader = VectorBufReader(std::move(stream));
auto bufTest = LexIO::FillBuffer(bufReader, 8);
VectorBufReader copyReader{bufReader};
auto copyTest = LexIO::FillBuffer(copyReader, 8);

REQUIRE(copyTest.first[0] == bufTest.first[0]);
REQUIRE(copyTest.first[7] == bufTest.first[7]);
REQUIRE(copyTest.second == bufTest.second);

LexIO::FillBuffer(copyReader, 12);
copyReader = bufReader;
copyTest = LexIO::FillBuffer(copyReader, 8);

REQUIRE(copyTest.first[0] == bufTest.first[0]);
REQUIRE(copyTest.first[7] == bufTest.first[7]);
REQUIRE(copyTest.second == bufTest.second);
}

TEST_CASE("Move constructor")
{
auto stream = GetStream();
auto bufReader = VectorBufReader(std::move(stream));
LexIO::FillBuffer(bufReader, 8);
VectorBufReader moveReader{std::move(bufReader)};
auto moveTest = LexIO::FillBuffer(moveReader, 8);

REQUIRE(moveTest.first[0] == 'T');
REQUIRE(moveTest.first[7] == 'c');
REQUIRE(moveTest.second == 8);
}

TEST_CASE("Move assignment")
{
auto stream = GetStream();
auto bufReader = VectorBufReader(std::move(stream));
auto moveReader = bufReader;

LexIO::FillBuffer(bufReader, 8);
moveReader = std::move(bufReader);
auto moveTest = LexIO::FillBuffer(moveReader, 8);

REQUIRE(moveTest.first[0] == 'T');
REQUIRE(moveTest.first[7] == 'c');
REQUIRE(moveTest.second == 8);
}

0 comments on commit ab4740f

Please sign in to comment.