summaryrefslogtreecommitdiffhomepage
path: root/FoundationEx/test/reader.cpp
blob: 4bc4882d05cae63463405cee2c265ef875f4c056 (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
#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());
	const auto s = reader.available();
	std::vector<char> 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<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!";
	auto ptr = std::make_unique<char[]>(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);
}