]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/beast/test/beast/core/static_buffer.cpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / libs / beast / test / beast / core / static_buffer.cpp
1 //
2 // Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //
7 // Official repository: https://github.com/boostorg/beast
8 //
9
10 // Test that header file is self-contained.
11 #include <boost/beast/core/static_buffer.hpp>
12
13 #include "buffer_test.hpp"
14
15 #include <boost/beast/core/ostream.hpp>
16 #include <boost/beast/core/string.hpp>
17 #include <boost/beast/unit_test/suite.hpp>
18 #include <string>
19
20 namespace boost {
21 namespace beast {
22
23 BOOST_STATIC_ASSERT(
24 boost::asio::is_dynamic_buffer<static_buffer_base>::value);
25
26 class static_buffer_test : public beast::unit_test::suite
27 {
28 public:
29 void
30 testStaticBuffer()
31 {
32 using namespace test;
33 using boost::asio::buffer;
34 using boost::asio::buffer_size;
35 char buf[12];
36 std::string const s = "Hello, world";
37 BEAST_EXPECT(s.size() == sizeof(buf));
38 for(std::size_t i = 1; i < 4; ++i) {
39 for(std::size_t j = 1; j < 4; ++j) {
40 for(std::size_t x = 1; x < 4; ++x) {
41 for(std::size_t y = 1; y < 4; ++y) {
42 for(std::size_t t = 1; t < 4; ++ t) {
43 for(std::size_t u = 1; u < 4; ++ u) {
44 std::size_t z = sizeof(buf) - (x + y);
45 std::size_t v = sizeof(buf) - (t + u);
46 {
47 std::memset(buf, 0, sizeof(buf));
48 static_buffer<sizeof(buf)> ba;
49 {
50 auto d = ba.prepare(z);
51 BEAST_EXPECT(buffer_size(d) == z);
52 }
53 {
54 auto d = ba.prepare(0);
55 BEAST_EXPECT(buffer_size(d) == 0);
56 }
57 {
58 auto d = ba.prepare(y);
59 BEAST_EXPECT(buffer_size(d) == y);
60 }
61 {
62 auto d = ba.prepare(x);
63 BEAST_EXPECT(buffer_size(d) == x);
64 ba.commit(buffer_copy(d, buffer(s.data(), x)));
65 }
66 BEAST_EXPECT(ba.size() == x);
67 BEAST_EXPECT(buffer_size(ba.data()) == ba.size());
68 {
69 auto d = ba.prepare(x);
70 BEAST_EXPECT(buffer_size(d) == x);
71 }
72 {
73 auto d = ba.prepare(0);
74 BEAST_EXPECT(buffer_size(d) == 0);
75 }
76 {
77 auto d = ba.prepare(z);
78 BEAST_EXPECT(buffer_size(d) == z);
79 }
80 {
81 auto d = ba.prepare(y);
82 BEAST_EXPECT(buffer_size(d) == y);
83 ba.commit(buffer_copy(d, buffer(s.data()+x, y)));
84 }
85 ba.commit(1);
86 BEAST_EXPECT(ba.size() == x + y);
87 BEAST_EXPECT(buffer_size(ba.data()) == ba.size());
88 {
89 auto d = ba.prepare(x);
90 BEAST_EXPECT(buffer_size(d) == x);
91 }
92 {
93 auto d = ba.prepare(y);
94 BEAST_EXPECT(buffer_size(d) == y);
95 }
96 {
97 auto d = ba.prepare(0);
98 BEAST_EXPECT(buffer_size(d) == 0);
99 }
100 {
101 auto d = ba.prepare(z);
102 BEAST_EXPECT(buffer_size(d) == z);
103 ba.commit(buffer_copy(d, buffer(s.data()+x+y, z)));
104 }
105 ba.commit(2);
106 BEAST_EXPECT(ba.size() == x + y + z);
107 BEAST_EXPECT(buffer_size(ba.data()) == ba.size());
108 BEAST_EXPECT(to_string(ba.data()) == s);
109 ba.consume(t);
110 {
111 auto d = ba.prepare(0);
112 BEAST_EXPECT(buffer_size(d) == 0);
113 }
114 BEAST_EXPECT(to_string(ba.data()) == s.substr(t, std::string::npos));
115 ba.consume(u);
116 BEAST_EXPECT(to_string(ba.data()) == s.substr(t + u, std::string::npos));
117 ba.consume(v);
118 BEAST_EXPECT(to_string(ba.data()) == "");
119 ba.consume(1);
120 {
121 auto d = ba.prepare(0);
122 BEAST_EXPECT(buffer_size(d) == 0);
123 }
124 try
125 {
126 ba.prepare(ba.capacity() - ba.size() + 1);
127 fail();
128 }
129 catch(...)
130 {
131 pass();
132 }
133 }
134 }}}}}}
135 }
136
137 void
138 testBuffer()
139 {
140 using namespace test;
141 string_view const s = "Hello, world!";
142
143 // static_buffer_base
144 {
145 char buf[64];
146 static_buffer_base b{buf, sizeof(buf)};
147 ostream(b) << s;
148 BEAST_EXPECT(to_string(b.data()) == s);
149 b.consume(b.size());
150 BEAST_EXPECT(to_string(b.data()) == "");
151 }
152
153 // static_buffer
154 {
155 static_buffer<64> b1;
156 BEAST_EXPECT(b1.size() == 0);
157 BEAST_EXPECT(b1.max_size() == 64);
158 BEAST_EXPECT(b1.capacity() == 64);
159 ostream(b1) << s;
160 BEAST_EXPECT(to_string(b1.data()) == s);
161 {
162 static_buffer<64> b2{b1};
163 BEAST_EXPECT(to_string(b2.data()) == s);
164 b2.consume(7);
165 BEAST_EXPECT(to_string(b2.data()) == s.substr(7));
166 }
167 {
168 static_buffer<64> b2;
169 b2 = b1;
170 BEAST_EXPECT(to_string(b2.data()) == s);
171 b2.consume(7);
172 BEAST_EXPECT(to_string(b2.data()) == s.substr(7));
173 }
174 }
175
176 // cause memmove
177 {
178 static_buffer<10> b;
179 write_buffer(b, "12345");
180 b.consume(3);
181 write_buffer(b, "67890123");
182 BEAST_EXPECT(to_string(b.data()) == "4567890123");
183 try
184 {
185 b.prepare(1);
186 fail("", __FILE__, __LINE__);
187 }
188 catch(std::length_error const&)
189 {
190 pass();
191 }
192 }
193
194 // read_size
195 {
196 static_buffer<10> b;
197 BEAST_EXPECT(read_size(b, 512) == 10);
198 b.prepare(4);
199 b.commit(4);
200 BEAST_EXPECT(read_size(b, 512) == 6);
201 b.consume(2);
202 BEAST_EXPECT(read_size(b, 512) == 8);
203 b.prepare(8);
204 b.commit(8);
205 BEAST_EXPECT(read_size(b, 512) == 0);
206 }
207
208 // base
209 {
210 static_buffer<10> b;
211 [&](static_buffer_base& base)
212 {
213 BEAST_EXPECT(base.max_size() == b.capacity());
214 }
215 (b.base());
216
217 [&](static_buffer_base const& base)
218 {
219 BEAST_EXPECT(base.max_size() == b.capacity());
220 }
221 (b.base());
222 }
223 }
224
225 void run() override
226 {
227 testBuffer();
228 //testStaticBuffer();
229 }
230 };
231
232 BEAST_DEFINE_TESTSUITE(beast,core,static_buffer);
233
234 } // beast
235 } // boost