From 2aa26759e12da36d68641ea3abda9b53292a2cbd Mon Sep 17 00:00:00 2001 From: Aki Date: Mon, 18 Mar 2024 00:39:44 +0100 Subject: Bulk busy work towards Reader replacement to inject into DataLoader --- FoundationEx/test/reader.cpp | 65 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 FoundationEx/test/reader.cpp (limited to 'FoundationEx/test/reader.cpp') diff --git a/FoundationEx/test/reader.cpp b/FoundationEx/test/reader.cpp new file mode 100644 index 0000000..395ad81 --- /dev/null +++ b/FoundationEx/test/reader.cpp @@ -0,0 +1,65 @@ +#include +#include +#include +#include + +#include + +#include +#include + + +using starshatter::foundation::Direction; +using starshatter::foundation::Reader; + + +TEST(FoundationEx, ReadFromView) +{ + Reader reader("Hello, World!"); + ASSERT_TRUE(reader.valid()); + std::vector buffer(reader.available()); + ASSERT_EQ(14, buffer.size()); + const auto bytes = reader.read(buffer.data()); + ASSERT_EQ(14, bytes); + ASSERT_EQ(0, reader.available()); + ASSERT_STREQ("Hello, World!", buffer.data()); +} + + +TEST(FoundationEx, PeekIntoView) +{ + Reader reader("Hello, World!"); + ASSERT_TRUE(reader.valid()); + reader.seek(7); + std::vector buffer(reader.available()); + ASSERT_EQ(7, buffer.size()); + const auto bytes = reader.peek(buffer.data()); + ASSERT_EQ(7, bytes); + ASSERT_EQ(7, reader.available()); + ASSERT_STREQ("World!", buffer.data()); +} + + +TEST(FoundationEx, RelativeSeek) +{ + Reader reader("Hello, World!"); + ASSERT_TRUE(reader.valid()); + ASSERT_EQ(11, reader.seek(-3, Direction::End)); + EXPECT_EQ(8, reader.seek(-3, Direction::Current)); + ASSERT_EQ(5, reader.seek(5, Direction::Start)); + EXPECT_EQ(7, reader.seek(2, Direction::Current)); +} + + +TEST(FoundationEx, CreateTextFromReader) +{ + const char* ref = "Hello!"; + const auto size = 7; + auto ptr = std::make_unique(size); + for (auto i = 0; i < size; ++i) + ptr[i] = ref[i]; + Reader reader(std::move(ptr)); + ASSERT_TRUE(reader.valid()); + Text text(reader.data()); + ASSERT_EQ("Hello!", text); +} -- cgit v1.1