forked from boostorg/beast
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflat_stream.cpp
More file actions
222 lines (193 loc) · 6.2 KB
/
Copy pathflat_stream.cpp
File metadata and controls
222 lines (193 loc) · 6.2 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
//
// 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_stream.hpp>
#include "stream_tests.hpp"
#include <boost/beast/_experimental/unit_test/suite.hpp>
#include <boost/beast/_experimental/test/stream.hpp>
#include <boost/beast/core/role.hpp>
#include <initializer_list>
#include <vector>
namespace boost {
namespace beast {
class flat_stream_test : public unit_test::suite
{
public:
void
testMembers()
{
net::io_context ioc;
test_sync_stream<flat_stream<test::stream>>();
test_async_stream<flat_stream<test::stream>>();
// read/write
{
error_code ec;
flat_stream<test::stream> s(ioc);
{
// VFALCO Hack to make test stream code = eof
test::stream ts(ioc);
s.next_layer().connect(ts);
}
char buf[1];
net::mutable_buffer m1 = net::buffer(buf);
BEAST_EXPECT(s.read_some(net::mutable_buffer{}) == 0);
BEAST_EXPECT(s.read_some(net::mutable_buffer{}, ec) == 0);
BEAST_EXPECTS(! ec, ec.message());
try
{
s.read_some(m1);
BEAST_FAIL();
}
catch(std::exception const&)
{
BEAST_PASS();
}
catch(...)
{
BEAST_FAIL();
}
BEAST_EXPECT(s.write_some(net::const_buffer{}) == 0);
BEAST_EXPECT(s.write_some(net::const_buffer{}, ec) == 0);
BEAST_EXPECTS(! ec, ec.message());
try
{
s.write_some(m1);
BEAST_FAIL();
}
catch(std::exception const&)
{
BEAST_PASS();
}
catch(...)
{
BEAST_FAIL();
}
bool invoked;
invoked = false;
s.async_read_some(net::mutable_buffer{},
[&](error_code ec, std::size_t)
{
invoked = true;
BEAST_EXPECTS(! ec, ec.message());
});
ioc.run();
ioc.restart();
BEAST_EXPECT(invoked);
invoked = false;
s.async_write_some(net::const_buffer{},
[&](error_code ec, std::size_t)
{
invoked = true;
BEAST_EXPECTS(! ec, ec.message());
});
ioc.run();
ioc.restart();
BEAST_EXPECT(invoked);
}
// stack_write_some
{
char b[detail::flat_stream_base::max_size];
std::array<net::const_buffer, 3> bs;
bs[0] = net::const_buffer(b, 100);
bs[1] = net::const_buffer(b + 100, 200);
bs[2] = net::const_buffer(b + 100 + 200, 300);
BEAST_EXPECT(buffer_bytes(bs) <=
detail::flat_stream_base::max_stack);
flat_stream<test::stream> s(ioc);
error_code ec;
s.write_some(bs, ec);
}
// write_some
{
char b[detail::flat_stream_base::max_size];
std::array<net::const_buffer, 2> bs;
bs[0] = net::const_buffer(b,
detail::flat_stream_base::max_stack);
bs[1] = net::const_buffer(b + bs[0].size(), 1024);
BEAST_EXPECT(buffer_bytes(bs) <=
detail::flat_stream_base::max_size);
flat_stream<test::stream> s(ioc);
error_code ec;
s.write_some(bs, ec);
}
// async_write_some
{
char b[detail::flat_stream_base::max_size];
std::array<net::const_buffer, 2> bs;
bs[0] = net::const_buffer(b,
detail::flat_stream_base::max_stack);
bs[1] = net::const_buffer(b + bs[0].size(), 1024);
BEAST_EXPECT(buffer_bytes(bs) <=
detail::flat_stream_base::max_size);
flat_stream<test::stream> s(ioc);
s.async_write_some(bs,
[](error_code, std::size_t)
{
});
}
// teardown
{
test::stream ts(ioc);
flat_stream<test::stream> s(ioc);
ts.connect(s.next_layer());
error_code ec;
teardown(role_type::client, s, ec);
}
{
test::stream ts(ioc);
flat_stream<test::stream> s(ioc);
ts.connect(s.next_layer());
async_teardown(role_type::client, s,
[](error_code)
{
});
}
}
void
testSplit()
{
auto const check =
[&](
std::initializer_list<int> v0,
std::size_t limit,
unsigned long count,
bool copy)
{
std::vector<net::const_buffer> v;
v.reserve(v0.size());
for(auto const n : v0)
v.emplace_back("", n);
auto const result =
boost::beast::detail::flat_stream_base::flatten(v, limit);
BEAST_EXPECT(result.size == count);
BEAST_EXPECT(result.flatten == copy);
return result;
};
check({}, 1, 0, false);
check({1,2}, 1, 1, false);
check({1,2}, 2, 1, false);
check({1,2}, 3, 3, true);
check({1,2}, 4, 3, true);
check({1,2,3}, 1, 1, false);
check({1,2,3}, 2, 1, false);
check({1,2,3}, 3, 3, true);
check({1,2,3}, 4, 3, true);
check({1,2,3}, 7, 6, true);
check({1,2,3,4}, 3, 3, true);
}
void
run() override
{
testMembers();
testSplit();
}
};
BEAST_DEFINE_TESTSUITE(beast,core,flat_stream);
} // beast
} // boost