Skip to content

Commit

Permalink
Test both partial and too-big reads and writes
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexMax committed Dec 29, 2023
1 parent 924a2fa commit a6a1de4
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 29 deletions.
19 changes: 14 additions & 5 deletions tests/test.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,24 @@ inline LexIO::VectorStream GetStream()
return rvo;
}

class PartialVectorStream
inline LexIO::ArrayStream<4> GetStreamTrunc()
{
LexIO::VectorStream m_stream;
LexIO::ArrayStream<4> rvo;
rvo.LexWrite(&BUFFER_TEXT[0], 4);
rvo.LexSeek(LexIO::SeekPos(0, LexIO::Whence::start));
return rvo;
}

template <typename STREAM>
class PartialStream
{
STREAM m_stream;

public:
PartialVectorStream() = delete;
PartialVectorStream(LexIO::VectorStream &&stream) : m_stream(stream) {}
PartialStream() = delete;
PartialStream(STREAM &&stream) : m_stream(stream) {}

const LexIO::VectorStream &Stream() const { return m_stream; }
const STREAM &Stream() const { return m_stream; }

size_t LexRead(uint8_t *outDest, const size_t count)
{
Expand Down
129 changes: 105 additions & 24 deletions tests/test_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ TEST(Core, IsReaderVBadReturn)

TEST(Core, RawRead)
{
LexIO::VectorStream stream = GetStream();
auto stream = GetStream();

size_t i = 0;
uint8_t buffer[5] = {0};
Expand All @@ -135,9 +135,9 @@ TEST(Core, RawRead)
EXPECT_EQ(buffer[i++], 'q');
}

TEST(Core, RawReadPartial)
TEST(Core, RawReadTrunc)
{
auto stream = PartialVectorStream(GetStream());
auto stream = GetStreamTrunc();

size_t i = 0;
uint8_t buffer[5] = {0};
Expand All @@ -149,7 +149,7 @@ TEST(Core, RawReadPartial)
}
TEST(Core, ReadPtrLen)
{
LexIO::VectorStream stream = GetStream();
auto stream = GetStream();

size_t i = 0;
uint8_t buffer[5] = {0};
Expand All @@ -163,7 +163,7 @@ TEST(Core, ReadPtrLen)

TEST(Core, ReadPtrLenPartial)
{
auto stream = PartialVectorStream(GetStream());
auto stream = PartialStream(GetStream());

size_t i = 0;
uint8_t buffer[5] = {0};
Expand All @@ -175,9 +175,22 @@ TEST(Core, ReadPtrLenPartial)
EXPECT_EQ(buffer[i++], 'q');
}

TEST(Core, ReadPtrLenTrunc)
{
auto stream = GetStreamTrunc();

size_t i = 0;
uint8_t buffer[5] = {0};
EXPECT_EQ(LexIO::Read(&buffer[0], sizeof(buffer), stream), 4);
EXPECT_EQ(buffer[i++], 'T');
EXPECT_EQ(buffer[i++], 'h');
EXPECT_EQ(buffer[i++], 'e');
EXPECT_EQ(buffer[i++], ' ');
}

TEST(Core, ReadArray)
{
LexIO::VectorStream stream = GetStream();
auto stream = GetStream();

size_t i = 0;
uint8_t buffer[5] = {0};
Expand All @@ -191,7 +204,7 @@ TEST(Core, ReadArray)

TEST(Core, ReadArrayPartial)
{
auto stream = PartialVectorStream(GetStream());
auto stream = PartialStream(GetStream());

size_t i = 0;
uint8_t buffer[5] = {0};
Expand All @@ -203,9 +216,22 @@ TEST(Core, ReadArrayPartial)
EXPECT_EQ(buffer[i++], 'q');
}

TEST(Core, ReadArrayTrunc)
{
auto stream = GetStreamTrunc();

size_t i = 0;
uint8_t buffer[5] = {0};
EXPECT_EQ(LexIO::Read(buffer, stream), 4);
EXPECT_EQ(buffer[i++], 'T');
EXPECT_EQ(buffer[i++], 'h');
EXPECT_EQ(buffer[i++], 'e');
EXPECT_EQ(buffer[i++], ' ');
}

TEST(Core, ReadIterator)
{
LexIO::VectorStream stream = GetStream();
auto stream = GetStream();

size_t i = 0;
uint8_t buffer[5] = {0};
Expand All @@ -219,7 +245,7 @@ TEST(Core, ReadIterator)

TEST(Core, ReadIteratorPartial)
{
auto stream = PartialVectorStream(GetStream());
auto stream = PartialStream(GetStream());

size_t i = 0;
uint8_t buffer[5] = {0};
Expand All @@ -231,9 +257,22 @@ TEST(Core, ReadIteratorPartial)
EXPECT_EQ(buffer[i++], 'q');
}

TEST(Core, ReadIteratorTrunc)
{
auto stream = GetStreamTrunc();

size_t i = 0;
uint8_t buffer[5] = {0};
EXPECT_EQ(LexIO::Read(&buffer[0], &buffer[5], stream), 4);
EXPECT_EQ(buffer[i++], 'T');
EXPECT_EQ(buffer[i++], 'h');
EXPECT_EQ(buffer[i++], 'e');
EXPECT_EQ(buffer[i++], ' ');
}

TEST(Core, ReadToEOF)
{
LexIO::VectorStream stream = GetStream();
auto stream = GetStream();
auto buffer = VectorBufReader(std::move(stream));

std::vector<uint8_t> data;
Expand All @@ -258,7 +297,7 @@ TEST(Core, ReadToEOFSmallBuffer)

TEST(Core, ReadUntil)
{
LexIO::VectorStream stream = GetStream();
auto stream = GetStream();
auto buffer = VectorBufReader(std::move(stream));

std::vector<uint8_t> data;
Expand Down Expand Up @@ -300,7 +339,7 @@ TEST(Core, ReadUntilSmallBuffer)

TEST(Core, RawWrite)
{
LexIO::VectorStream stream = GetStream();
auto stream = LexIO::ArrayStream<5>{};
const auto &cstream = stream;

size_t i = 0;
Expand All @@ -313,23 +352,23 @@ TEST(Core, RawWrite)
EXPECT_EQ(cstream.Container()[i++], 'Y');
}

TEST(Core, RawWritePartial)
TEST(Core, RawWriteTrunc)
{
auto stream = PartialVectorStream(GetStream());
auto stream = LexIO::ArrayStream<4>{};
const auto &cstream = stream;

size_t i = 0;
const uint8_t data[] = {'X', 'Y', 'Z', 'Z', 'Y'};
EXPECT_EQ(LexIO::RawWrite(stream, &data[0], sizeof(data)), 4);
EXPECT_EQ(cstream.Stream().Container()[i++], 'X');
EXPECT_EQ(cstream.Stream().Container()[i++], 'Y');
EXPECT_EQ(cstream.Stream().Container()[i++], 'Z');
EXPECT_EQ(cstream.Stream().Container()[i++], 'Z');
EXPECT_EQ(cstream.Container()[i++], 'X');
EXPECT_EQ(cstream.Container()[i++], 'Y');
EXPECT_EQ(cstream.Container()[i++], 'Z');
EXPECT_EQ(cstream.Container()[i++], 'Z');
}

TEST(Core, WritePtrLen)
{
LexIO::VectorStream stream = GetStream();
auto stream = LexIO::ArrayStream<5>{};
const auto &cstream = stream;

size_t i = 0;
Expand All @@ -344,7 +383,7 @@ TEST(Core, WritePtrLen)

TEST(Core, WritePtrLenPartial)
{
auto stream = PartialVectorStream(GetStream());
auto stream = PartialStream{LexIO::ArrayStream<5>{}};
const auto &cstream = stream;

size_t i = 0;
Expand All @@ -357,9 +396,23 @@ TEST(Core, WritePtrLenPartial)
EXPECT_EQ(cstream.Stream().Container()[i++], 'Y');
}

TEST(Core, WritePtrLenTrunc)
{
auto stream = LexIO::ArrayStream<4>{};
const auto &cstream = stream;

size_t i = 0;
const uint8_t data[] = {'X', 'Y', 'Z', 'Z', 'Y'};
EXPECT_EQ(LexIO::Write(stream, &data[0], sizeof(data)), 4);
EXPECT_EQ(cstream.Container()[i++], 'X');
EXPECT_EQ(cstream.Container()[i++], 'Y');
EXPECT_EQ(cstream.Container()[i++], 'Z');
EXPECT_EQ(cstream.Container()[i++], 'Z');
}

TEST(Core, WriteArray)
{
LexIO::VectorStream stream = GetStream();
auto stream = LexIO::ArrayStream<5>{};
const auto &cstream = stream;

size_t i = 0;
Expand All @@ -374,7 +427,7 @@ TEST(Core, WriteArray)

TEST(Core, WriteArrayPartial)
{
auto stream = PartialVectorStream(GetStream());
auto stream = PartialStream{LexIO::ArrayStream<5>{}};
const auto &cstream = stream;

size_t i = 0;
Expand All @@ -387,9 +440,23 @@ TEST(Core, WriteArrayPartial)
EXPECT_EQ(cstream.Stream().Container()[i++], 'Y');
}

TEST(Core, WriteArrayTrunc)
{
auto stream = LexIO::ArrayStream<4>{};
const auto &cstream = stream;

size_t i = 0;
const uint8_t data[] = {'X', 'Y', 'Z', 'Z', 'Y'};
EXPECT_EQ(LexIO::Write(stream, data), 4);
EXPECT_EQ(cstream.Container()[i++], 'X');
EXPECT_EQ(cstream.Container()[i++], 'Y');
EXPECT_EQ(cstream.Container()[i++], 'Z');
EXPECT_EQ(cstream.Container()[i++], 'Z');
}

TEST(Core, WriteIterator)
{
LexIO::VectorStream stream = GetStream();
auto stream = LexIO::ArrayStream<5>{};
const auto &cstream = stream;

size_t i = 0;
Expand All @@ -404,7 +471,7 @@ TEST(Core, WriteIterator)

TEST(Core, WriteIteratorPartial)
{
auto stream = PartialVectorStream(GetStream());
auto stream = PartialStream{LexIO::ArrayStream<5>{}};
const auto &cstream = stream;

size_t i = 0;
Expand All @@ -417,6 +484,20 @@ TEST(Core, WriteIteratorPartial)
EXPECT_EQ(cstream.Stream().Container()[i++], 'Y');
}

TEST(Core, WriteIteratorTrunc)
{
auto stream = LexIO::ArrayStream<4>{};
const auto &cstream = stream;

size_t i = 0;
const uint8_t data[] = {'X', 'Y', 'Z', 'Z', 'Y'};
EXPECT_EQ(LexIO::Write(stream, &data[0], &data[5]), 4);
EXPECT_EQ(cstream.Container()[i++], 'X');
EXPECT_EQ(cstream.Container()[i++], 'Y');
EXPECT_EQ(cstream.Container()[i++], 'Z');
EXPECT_EQ(cstream.Container()[i++], 'Z');
}

TEST(Core, Rewind)
{
LexIO::VectorStream stream = GetStream();
Expand Down

0 comments on commit a6a1de4

Please sign in to comment.