forked from boostorg/beast
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflat_static_buffer.cpp
More file actions
139 lines (123 loc) · 3.7 KB
/
Copy pathflat_static_buffer.cpp
File metadata and controls
139 lines (123 loc) · 3.7 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
//
// Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// Official repository: https://github.com/boostorg/beast
//
// Test that header file is self-contained.
#include <boost/beast/core/flat_static_buffer.hpp>
#include "test_buffer.hpp"
#include <boost/beast/core/buffer_traits.hpp>
#include <boost/beast/core/ostream.hpp>
#include <boost/beast/core/read_size.hpp>
#include <boost/beast/core/string.hpp>
#include <boost/beast/_experimental/unit_test/suite.hpp>
#include <boost/asio/buffers_iterator.hpp>
#include <algorithm>
#include <cctype>
#include <string>
namespace boost {
namespace beast {
class flat_static_buffer_test : public beast::unit_test::suite
{
public:
BOOST_STATIC_ASSERT(
is_mutable_dynamic_buffer<
flat_static_buffer<13>>::value);
void
testDynamicBuffer()
{
test_dynamic_buffer(flat_static_buffer<13>{});
}
void
testMembers()
{
string_view const s = "Hello, world!";
// flat_static_buffer_base
{
char buf[64];
flat_static_buffer_base b{buf, sizeof(buf)};
ostream(b) << s;
BEAST_EXPECT(buffers_to_string(b.data()) == s);
b.clear();
BEAST_EXPECT(b.size() == 0);
BEAST_EXPECT(buffer_bytes(b.data()) == 0);
}
// flat_static_buffer
{
flat_static_buffer<64> b1;
BEAST_EXPECT(b1.size() == 0);
BEAST_EXPECT(b1.max_size() == 64);
BEAST_EXPECT(b1.capacity() == 64);
ostream(b1) << s;
BEAST_EXPECT(buffers_to_string(b1.data()) == s);
{
flat_static_buffer<64> b2{b1};
BEAST_EXPECT(buffers_to_string(b2.data()) == s);
b2.consume(7);
BEAST_EXPECT(buffers_to_string(b2.data()) == s.substr(7));
}
{
flat_static_buffer<64> b2;
b2 = b1;
BEAST_EXPECT(buffers_to_string(b2.data()) == s);
b2.consume(7);
BEAST_EXPECT(buffers_to_string(b2.data()) == s.substr(7));
}
}
// cause memmove
{
flat_static_buffer<10> b;
ostream(b) << "12345";
b.consume(3);
ostream(b) << "67890123";
BEAST_EXPECT(buffers_to_string(b.data()) == "4567890123");
try
{
b.prepare(1);
fail("", __FILE__, __LINE__);
}
catch(std::length_error const&)
{
pass();
}
}
// read_size
{
flat_static_buffer<10> b;
BEAST_EXPECT(read_size(b, 512) == 10);
b.prepare(4);
b.commit(4);
BEAST_EXPECT(read_size(b, 512) == 6);
b.consume(2);
BEAST_EXPECT(read_size(b, 512) == 8);
b.prepare(8);
b.commit(8);
BEAST_EXPECT(read_size(b, 512) == 0);
}
// base
{
flat_static_buffer<10> b;
[&](flat_static_buffer_base& base)
{
BEAST_EXPECT(base.max_size() == b.capacity());
}
(b.base());
[&](flat_static_buffer_base const& base)
{
BEAST_EXPECT(base.max_size() == b.capacity());
}
(b.base());
}
}
void run() override
{
testDynamicBuffer();
testMembers();
}
};
BEAST_DEFINE_TESTSUITE(beast,core,flat_static_buffer);
} // beast
} // boost