summaryrefslogtreecommitdiffhomepage
path: root/FoundationEx/test/reader.cpp
blob: 395ad8116bda37fb142796c896dc07d1adf5c39d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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);
}