forked from boostorg/beast
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatic_buffer.cpp
More file actions
184 lines (166 loc) · 5.09 KB
/
Copy pathstatic_buffer.cpp
File metadata and controls
184 lines (166 loc) · 5.09 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
//
// 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/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 static_buffer_test : public beast::unit_test::suite
{
public:
BOOST_STATIC_ASSERT(
is_mutable_dynamic_buffer<
static_buffer<13>>::value);
BOOST_STATIC_ASSERT(
is_mutable_dynamic_buffer<
static_buffer_base>::value);
void
testDynamicBuffer()
{
test_dynamic_buffer(static_buffer<13>{});
}
void
testMembers()
{
string_view const s = "Hello, world!";
// static_buffer_base
{
char buf[64];
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);
}
// static_buffer
{
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);
{
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));
}
{
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
{
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
{
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
{
static_buffer<10> b;
[&](static_buffer_base& base)
{
BEAST_EXPECT(base.max_size() == b.capacity());
}
(b.base());
[&](static_buffer_base const& base)
{
BEAST_EXPECT(base.max_size() == b.capacity());
}
(b.base());
}
// This exercises the wrap-around cases
// for the circular buffer representation
{
static_buffer<5> b;
{
auto const mb = b.prepare(5);
BEAST_EXPECT(buffers_length(mb) == 1);
}
b.commit(4);
BEAST_EXPECT(buffers_length(b.data()) == 1);
BEAST_EXPECT(buffers_length(b.cdata()) == 1);
b.consume(3);
{
auto const mb = b.prepare(3);
BEAST_EXPECT(buffers_length(mb) == 2);
auto it1 = mb.begin();
auto it2 = std::next(it1);
BEAST_EXPECT(
net::const_buffer(*it1).data() >
net::const_buffer(*it2).data());
}
b.commit(2);
{
auto const mb = b.data();
auto it1 = mb.begin();
auto it2 = std::next(it1);
BEAST_EXPECT(
net::const_buffer(*it1).data() >
net::const_buffer(*it2).data());
}
{
auto const cb = b.cdata();
auto it1 = cb.begin();
auto it2 = std::next(it1);
BEAST_EXPECT(
net::const_buffer(*it1).data() >
net::const_buffer(*it2).data());
}
}
}
void
run() override
{
testDynamicBuffer();
testMembers();
}
};
BEAST_DEFINE_TESTSUITE(beast,core,static_buffer);
} // beast
} // boost