-
Notifications
You must be signed in to change notification settings - Fork 2
/
output_buffer_test.cpp
75 lines (62 loc) · 1.95 KB
/
output_buffer_test.cpp
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
66
67
68
69
70
71
72
73
74
75
#include "buffer.h"
#include <fcntl.h>
#include <unistd.h>
#include <catch2/catch.hpp>
class StringSink : public DataSink
{
std::string m_result;
public:
virtual int WriteOut(const uint8_t *buffer, size_t bytes_available) override
{
m_result.append(reinterpret_cast<const char *>(buffer), bytes_available);
return static_cast<int>(bytes_available);
}
const std::string &GetResult() const
{
return m_result;
}
};
TEST_CASE("OutputStreamBuffer")
{
StringSink sink;
OutputStreamBuffer osb;
osb.SetSink(&sink);
REQUIRE(osb.GetOffset() == 0);
osb.Append(reinterpret_cast<const uint8_t *>("ABCD"), 4);
REQUIRE(sink.GetResult().empty());
osb.Flush();
REQUIRE(sink.GetResult() == "ABCD");
osb.Append(reinterpret_cast<const uint8_t *>("EFGH"), 4);
REQUIRE(sink.GetResult() == "ABCD");
SECTION("Bookmark")
{
osb.SetBookmark();
REQUIRE(sink.GetResult() == "ABCDEFGH");
osb.Append(reinterpret_cast<const uint8_t *>("IJKL"), 4);
REQUIRE(sink.GetResult() == "ABCDEFGH");
// Flush with bookmark active should have no effect
osb.Flush();
REQUIRE(sink.GetResult() == "ABCDEFGH");
SECTION("ClearBookmark")
{
osb.ClearBookmark();
REQUIRE(sink.GetResult() == "ABCDEFGHIJKL");
osb.Append(reinterpret_cast<const uint8_t *>("MNOP"), 4);
REQUIRE(sink.GetResult() == "ABCDEFGHIJKLMNOP");
}
SECTION("GoToBookmark")
{
osb.GoToBookmark();
osb.Append(reinterpret_cast<const uint8_t *>("123"), 3);
REQUIRE(sink.GetResult() == "ABCDEFGH123");
}
}
SECTION("Large")
{
for(auto i = 0; i < 1000; ++i)
osb.Append(reinterpret_cast<const uint8_t *>("0123456789"), 10);
REQUIRE(sink.GetResult().size() == 8198);
osb.Flush();
REQUIRE(sink.GetResult().size() == 10008);
}
}