]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/beast/test/beast/core/multi_buffer.cpp
2 // Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
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)
7 // Official repository: https://github.com/boostorg/beast
10 // Test that header file is self-contained.
11 #include <boost/beast/core/multi_buffer.hpp>
13 #include "buffer_test.hpp"
15 #include <boost/beast/core/ostream.hpp>
16 #include <boost/beast/core/string.hpp>
17 #include <boost/beast/core/type_traits.hpp>
18 #include <boost/beast/test/test_allocator.hpp>
19 #include <boost/beast/unit_test/suite.hpp>
20 #include <boost/asio/buffer.hpp>
30 boost::asio::is_dynamic_buffer
<multi_buffer
>::value
);
32 class multi_buffer_test
: public beast::unit_test::suite
35 template<class Alloc1
, class Alloc2
>
38 eq(basic_multi_buffer
<Alloc1
> const& mb1
,
39 basic_multi_buffer
<Alloc2
> const& mb2
)
41 return test::to_string(mb1
.data()) ==
42 test::to_string(mb2
.data());
45 template<class ConstBufferSequence
>
47 expect_size(std::size_t n
, ConstBufferSequence
const& buffers
)
49 BEAST_EXPECT(test::size_pre(buffers
) == n
);
50 BEAST_EXPECT(test::size_post(buffers
) == n
);
51 BEAST_EXPECT(test::size_rev_pre(buffers
) == n
);
52 BEAST_EXPECT(test::size_rev_post(buffers
) == n
);
55 template<class U
, class V
>
58 self_assign(U
& u
, V
&& v
)
60 u
= std::forward
<V
>(v
);
67 using boost::asio::buffer
;
68 std::string
const s
= "Hello, world";
69 BEAST_EXPECT(s
.size() == 12);
70 for(std::size_t i
= 1; i
< 12; ++i
) {
71 for(std::size_t x
= 1; x
< 4; ++x
) {
72 for(std::size_t y
= 1; y
< 4; ++y
) {
73 std::size_t z
= s
.size() - (x
+ y
);
76 b
.commit(buffer_copy(b
.prepare(x
), buffer(s
.data(), x
)));
77 b
.commit(buffer_copy(b
.prepare(y
), buffer(s
.data()+x
, y
)));
78 b
.commit(buffer_copy(b
.prepare(z
), buffer(s
.data()+x
+y
, z
)));
79 BEAST_EXPECT(to_string(b
.data()) == s
);
82 BEAST_EXPECT(eq(b
, mb2
));
87 BEAST_EXPECT(eq(b
, mb2
));
90 multi_buffer mb2
{std::move(b
)};
91 BEAST_EXPECT(to_string(mb2
.data()) == s
);
92 expect_size(0, b
.data());
94 BEAST_EXPECT(to_string(b
.data()) == s
);
95 expect_size(0, mb2
.data());
98 BEAST_EXPECT(to_string(b
.data()) == s
);
99 self_assign(b
, std::move(b
));
100 BEAST_EXPECT(to_string(b
.data()) == s
);
108 using namespace test
;
109 using boost::asio::buffer
;
110 using boost::asio::buffer_size
;
111 std::string
const s
= "Hello, world";
112 BEAST_EXPECT(s
.size() == 12);
113 for(std::size_t i
= 1; i
< 12; ++i
) {
114 for(std::size_t x
= 1; x
< 4; ++x
) {
115 for(std::size_t y
= 1; y
< 4; ++y
) {
116 for(std::size_t t
= 1; t
< 4; ++ t
) {
117 for(std::size_t u
= 1; u
< 4; ++ u
) {
118 std::size_t z
= s
.size() - (x
+ y
);
119 std::size_t v
= s
.size() - (t
+ u
);
123 auto d
= b
.prepare(z
);
124 BEAST_EXPECT(buffer_size(d
) == z
);
127 auto d
= b
.prepare(0);
128 BEAST_EXPECT(buffer_size(d
) == 0);
131 auto d
= b
.prepare(y
);
132 BEAST_EXPECT(buffer_size(d
) == y
);
135 auto d
= b
.prepare(x
);
136 BEAST_EXPECT(buffer_size(d
) == x
);
137 b
.commit(buffer_copy(d
, buffer(s
.data(), x
)));
139 BEAST_EXPECT(b
.size() == x
);
140 BEAST_EXPECT(buffer_size(b
.data()) == b
.size());
142 auto d
= b
.prepare(x
);
143 BEAST_EXPECT(buffer_size(d
) == x
);
146 auto d
= b
.prepare(0);
147 BEAST_EXPECT(buffer_size(d
) == 0);
150 auto d
= b
.prepare(z
);
151 BEAST_EXPECT(buffer_size(d
) == z
);
154 auto d
= b
.prepare(y
);
155 BEAST_EXPECT(buffer_size(d
) == y
);
156 b
.commit(buffer_copy(d
, buffer(s
.data()+x
, y
)));
159 BEAST_EXPECT(b
.size() == x
+ y
);
160 BEAST_EXPECT(buffer_size(b
.data()) == b
.size());
162 auto d
= b
.prepare(x
);
163 BEAST_EXPECT(buffer_size(d
) == x
);
166 auto d
= b
.prepare(y
);
167 BEAST_EXPECT(buffer_size(d
) == y
);
170 auto d
= b
.prepare(0);
171 BEAST_EXPECT(buffer_size(d
) == 0);
174 auto d
= b
.prepare(z
);
175 BEAST_EXPECT(buffer_size(d
) == z
);
176 b
.commit(buffer_copy(d
, buffer(s
.data()+x
+y
, z
)));
179 BEAST_EXPECT(b
.size() == x
+ y
+ z
);
180 BEAST_EXPECT(buffer_size(b
.data()) == b
.size());
181 BEAST_EXPECT(to_string(b
.data()) == s
);
184 auto d
= b
.prepare(0);
185 BEAST_EXPECT(buffer_size(d
) == 0);
187 BEAST_EXPECT(to_string(b
.data()) == s
.substr(t
, std::string::npos
));
189 BEAST_EXPECT(to_string(b
.data()) == s
.substr(t
+ u
, std::string::npos
));
191 BEAST_EXPECT(to_string(b
.data()) == "");
194 auto d
= b
.prepare(0);
195 BEAST_EXPECT(buffer_size(d
) == 0);
204 using boost::asio::buffer_size
;
210 expect_size(3, b
.data());
212 expect_size(3, b
.prepare(3));
219 using namespace test
;
222 using equal_t
= test::test_allocator
<char,
223 true, true, true, true, true>;
226 using unequal_t
= test::test_allocator
<char,
227 false, true, true, true, true>;
233 BEAST_EXPECT(b
.capacity() == 0);
237 BEAST_EXPECT(b
.capacity() == 0);
238 BEAST_EXPECT(b
.max_size() == 500);
242 basic_multi_buffer
<unequal_t
> b
{a1
};
243 BEAST_EXPECT(b
.get_allocator() == a1
);
244 BEAST_EXPECT(b
.get_allocator() != unequal_t
{});
251 basic_multi_buffer
<equal_t
> b1
{30};
252 BEAST_EXPECT(b1
.get_allocator()->nmove
== 0);
253 ostream(b1
) << "Hello";
254 basic_multi_buffer
<equal_t
> b2
{std::move(b1
)};
255 BEAST_EXPECT(b2
.get_allocator()->nmove
== 1);
256 BEAST_EXPECT(b1
.size() == 0);
257 BEAST_EXPECT(b1
.capacity() == 0);
258 BEAST_EXPECT(to_string(b2
.data()) == "Hello");
259 BEAST_EXPECT(b1
.max_size() == b2
.max_size());
263 basic_multi_buffer
<equal_t
> b1
{30};
264 ostream(b1
) << "Hello";
266 basic_multi_buffer
<equal_t
> b2
{std::move(b1
), a
};
267 BEAST_EXPECT(b1
.size() == 0);
268 BEAST_EXPECT(b1
.capacity() == 0);
269 BEAST_EXPECT(to_string(b2
.data()) == "Hello");
270 BEAST_EXPECT(b1
.max_size() == b2
.max_size());
273 // allocators unequal
274 basic_multi_buffer
<unequal_t
> b1
{30};
275 ostream(b1
) << "Hello";
277 basic_multi_buffer
<unequal_t
> b2
{std::move(b1
), a
};
278 BEAST_EXPECT(b1
.size() == 0);
279 BEAST_EXPECT(b1
.capacity() == 0);
280 BEAST_EXPECT(to_string(b2
.data()) == "Hello");
281 BEAST_EXPECT(b1
.max_size() == b2
.max_size());
288 basic_multi_buffer
<equal_t
> b1
;
289 ostream(b1
) << "Hello";
290 basic_multi_buffer
<equal_t
> b2
{b1
};
291 BEAST_EXPECT(b1
.get_allocator() == b2
.get_allocator());
292 BEAST_EXPECT(to_string(b1
.data()) == "Hello");
293 BEAST_EXPECT(to_string(b2
.data()) == "Hello");
296 basic_multi_buffer
<unequal_t
> b1
;
297 ostream(b1
) << "Hello";
299 basic_multi_buffer
<unequal_t
> b2(b1
, a
);
300 BEAST_EXPECT(b1
.get_allocator() != b2
.get_allocator());
301 BEAST_EXPECT(to_string(b1
.data()) == "Hello");
302 BEAST_EXPECT(to_string(b2
.data()) == "Hello");
305 basic_multi_buffer
<equal_t
> b1
;
306 ostream(b1
) << "Hello";
307 basic_multi_buffer
<unequal_t
> b2(b1
);
308 BEAST_EXPECT(to_string(b1
.data()) == "Hello");
309 BEAST_EXPECT(to_string(b2
.data()) == "Hello");
312 basic_multi_buffer
<unequal_t
> b1
;
313 ostream(b1
) << "Hello";
315 basic_multi_buffer
<equal_t
> b2(b1
, a
);
316 BEAST_EXPECT(b2
.get_allocator() == a
);
317 BEAST_EXPECT(to_string(b1
.data()) == "Hello");
318 BEAST_EXPECT(to_string(b2
.data()) == "Hello");
326 ostream(b1
) << "Hello";
329 BEAST_EXPECT(b1
.size() == 0);
330 BEAST_EXPECT(b1
.capacity() == 0);
331 BEAST_EXPECT(to_string(b2
.data()) == "Hello");
334 // propagate_on_container_move_assignment : true
335 using pocma_t
= test::test_allocator
<char,
336 true, true, true, true, true>;
337 basic_multi_buffer
<pocma_t
> b1
;
338 ostream(b1
) << "Hello";
339 basic_multi_buffer
<pocma_t
> b2
;
341 BEAST_EXPECT(b1
.size() == 0);
342 BEAST_EXPECT(to_string(b2
.data()) == "Hello");
345 // propagate_on_container_move_assignment : false
346 using pocma_t
= test::test_allocator
<char,
347 true, true, false, true, true>;
348 basic_multi_buffer
<pocma_t
> b1
;
349 ostream(b1
) << "Hello";
350 basic_multi_buffer
<pocma_t
> b2
;
352 BEAST_EXPECT(b1
.size() == 0);
353 BEAST_EXPECT(to_string(b2
.data()) == "Hello");
361 ostream(b1
) << "Hello";
364 BEAST_EXPECT(to_string(b1
.data()) == "Hello");
365 BEAST_EXPECT(to_string(b2
.data()) == "Hello");
366 basic_multi_buffer
<equal_t
> b3
;
368 BEAST_EXPECT(to_string(b3
.data()) == "Hello");
371 // propagate_on_container_copy_assignment : true
372 using pocca_t
= test::test_allocator
<char,
373 true, true, true, true, true>;
374 basic_multi_buffer
<pocca_t
> b1
;
375 ostream(b1
) << "Hello";
376 basic_multi_buffer
<pocca_t
> b2
;
378 BEAST_EXPECT(to_string(b2
.data()) == "Hello");
381 // propagate_on_container_copy_assignment : false
382 using pocca_t
= test::test_allocator
<char,
383 true, false, true, true, true>;
384 basic_multi_buffer
<pocca_t
> b1
;
385 ostream(b1
) << "Hello";
386 basic_multi_buffer
<pocca_t
> b2
;
388 BEAST_EXPECT(to_string(b2
.data()) == "Hello");
398 b
.prepare(b
.max_size() + 1);
399 fail("", __FILE__
, __LINE__
);
401 catch(std::length_error
const&)
407 string_view
const s
= "Hello, world!";
409 BEAST_EXPECT(b1
.size() == 0);
410 BEAST_EXPECT(b1
.max_size() == 64);
411 BEAST_EXPECT(b1
.capacity() == 0);
413 BEAST_EXPECT(to_string(b1
.data()) == s
);
416 BEAST_EXPECT(to_string(b2
.data()) == s
);
418 BEAST_EXPECT(to_string(b2
.data()) == s
.substr(7));
423 BEAST_EXPECT(to_string(b2
.data()) == s
);
425 BEAST_EXPECT(to_string(b2
.data()) == s
.substr(7));
431 BEAST_EXPECT(b
.capacity() >= 1000);
433 BEAST_EXPECT(b
.size() == 1);
434 BEAST_EXPECT(b
.capacity() >= 1000);
436 BEAST_EXPECT(b
.size() == 1);
437 BEAST_EXPECT(b
.capacity() >= 1000);
439 BEAST_EXPECT(b
.capacity() >= 1000);
444 BEAST_EXPECT(b
.capacity() >= 1000);
446 BEAST_EXPECT(b
.capacity() >= 1000);
448 BEAST_EXPECT(b
.capacity() >= 1000);
450 BEAST_EXPECT(b
.capacity() >= 2000);
456 BEAST_EXPECT(b
.capacity() >= 1000);
458 BEAST_EXPECT(b
.capacity() >= 2000);
460 BEAST_EXPECT(b
.capacity() >= 4000);
462 BEAST_EXPECT(b
.capacity() >= 50);
470 BEAST_EXPECT(b
.capacity() >= 1000);
472 BEAST_EXPECT(b
.size() == 1000);
473 BEAST_EXPECT(b
.capacity() >= 1000);
475 BEAST_EXPECT(b
.size() == 0);
476 BEAST_EXPECT(b
.capacity() == 0);
479 BEAST_EXPECT(b
.size() == 650);
480 BEAST_EXPECT(b
.capacity() >= 1000);
482 BEAST_EXPECT(b
.capacity() >= 1650);
484 BEAST_EXPECT(b
.size() == 750);
485 BEAST_EXPECT(b
.capacity() >= 1000);
487 BEAST_EXPECT(b
.capacity() >= 2000);
495 BEAST_EXPECT(b
.capacity() >= 1000);
497 BEAST_EXPECT(b
.size() == 1000);
498 BEAST_EXPECT(b
.capacity() >= 1000);
500 BEAST_EXPECT(b
.capacity() >= 2000);
502 BEAST_EXPECT(b
.size() == 1750);
504 BEAST_EXPECT(b
.size() == 1250);
506 BEAST_EXPECT(b
.size() == 750);
509 BEAST_EXPECT(b
.size() == 0);
512 BEAST_EXPECT(b
.size() == 800);
515 BEAST_EXPECT(b
.size() == 1400);
517 BEAST_EXPECT(b
.size() == 0);
523 // propagate_on_container_swap : true
524 using pocs_t
= test::test_allocator
<char,
525 false, true, true, true, true>;
527 BEAST_EXPECT(a1
!= a2
);
528 basic_multi_buffer
<pocs_t
> b1
{a1
};
529 ostream(b1
) << "Hello";
530 basic_multi_buffer
<pocs_t
> b2
{a2
};
531 BEAST_EXPECT(b1
.get_allocator() == a1
);
532 BEAST_EXPECT(b2
.get_allocator() == a2
);
534 BEAST_EXPECT(b1
.get_allocator() == a2
);
535 BEAST_EXPECT(b2
.get_allocator() == a1
);
536 BEAST_EXPECT(b1
.size() == 0);
537 BEAST_EXPECT(to_string(b2
.data()) == "Hello");
539 BEAST_EXPECT(b1
.get_allocator() == a1
);
540 BEAST_EXPECT(b2
.get_allocator() == a2
);
541 BEAST_EXPECT(to_string(b1
.data()) == "Hello");
542 BEAST_EXPECT(b2
.size() == 0);
545 // propagate_on_container_swap : false
546 using pocs_t
= test::test_allocator
<char,
547 true, true, true, false, true>;
549 BEAST_EXPECT(a1
== a2
);
550 BEAST_EXPECT(a1
.id() != a2
.id());
551 basic_multi_buffer
<pocs_t
> b1
{a1
};
552 ostream(b1
) << "Hello";
553 basic_multi_buffer
<pocs_t
> b2
{a2
};
554 BEAST_EXPECT(b1
.get_allocator() == a1
);
555 BEAST_EXPECT(b2
.get_allocator() == a2
);
557 BEAST_EXPECT(b1
.get_allocator().id() == a1
.id());
558 BEAST_EXPECT(b2
.get_allocator().id() == a2
.id());
559 BEAST_EXPECT(b1
.size() == 0);
560 BEAST_EXPECT(to_string(b2
.data()) == "Hello");
562 BEAST_EXPECT(b1
.get_allocator().id() == a1
.id());
563 BEAST_EXPECT(b2
.get_allocator().id() == a2
.id());
564 BEAST_EXPECT(to_string(b1
.data()) == "Hello");
565 BEAST_EXPECT(b2
.size() == 0);
572 BEAST_EXPECT(read_size(b
, 512) == 10);
575 BEAST_EXPECT(read_size(b
, 512) == 6);
577 BEAST_EXPECT(read_size(b
, 512) == 8);
580 BEAST_EXPECT(read_size(b
, 512) == 0);
594 BEAST_DEFINE_TESTSUITE(beast
,core
,multi_buffer
);