#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()); const auto s = reader.available(); std::vector buffer(s); 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!"; auto ptr = std::make_unique(std::strlen(ref) + 1); std::strcpy(ptr.get(), ref); Reader reader(std::move(ptr)); ASSERT_TRUE(reader.valid()); const auto text = reader.more(); ASSERT_STREQ(ref, text); }