summaryrefslogtreecommitdiffhomepage
path: root/FoundationEx/test/reader.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'FoundationEx/test/reader.cpp')
-rw-r--r--FoundationEx/test/reader.cpp65
1 files changed, 65 insertions, 0 deletions
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 <cstring>
+#include <memory>
+#include <utility>
+#include <vector>
+
+#include <gtest/gtest.h>
+
+#include <starshatter/foundation/reader.h>
+#include <Text.h>
+
+
+using starshatter::foundation::Direction;
+using starshatter::foundation::Reader;
+
+
+TEST(FoundationEx, ReadFromView)
+{
+ Reader reader("Hello, World!");
+ ASSERT_TRUE(reader.valid());
+ std::vector<char> 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<char> 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<char[]>(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);
+}