]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/beast/test/beast/core/multi_buffer.cpp
import new upstream nautilus stable release 14.2.8
[ceph.git] / ceph / src / boost / libs / beast / test / beast / core / multi_buffer.cpp
1 //
2 // Copyright (c) 2016-2019 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/multi_buffer.hpp>
12
13 #include "test_buffer.hpp"
14
15 #include <boost/beast/core/buffer_traits.hpp>
16 #include <boost/beast/core/ostream.hpp>
17 #include <boost/beast/core/read_size.hpp>
18 #include <boost/beast/core/string.hpp>
19 #include <boost/beast/test/test_allocator.hpp>
20 #include <boost/beast/_experimental/unit_test/suite.hpp>
21 #include <boost/asio/buffer.hpp>
22 #include <boost/asio/buffers_iterator.hpp>
23 #include <algorithm>
24 #include <atomic>
25 #include <cctype>
26 #include <memory>
27 #include <string>
28
29 namespace boost {
30 namespace beast {
31
32 class multi_buffer_test : public beast::unit_test::suite
33 {
34 public:
35 BOOST_STATIC_ASSERT(
36 is_mutable_dynamic_buffer<multi_buffer>::value);
37
38 #if ! BOOST_WORKAROUND(BOOST_LIBSTDCXX_VERSION, < 50000) && \
39 ! BOOST_WORKAROUND(BOOST_MSVC, < 1910)
40 BOOST_STATIC_ASSERT(std::is_trivially_copyable<
41 multi_buffer::const_buffers_type>::value);
42 BOOST_STATIC_ASSERT(std::is_trivially_copyable<
43 multi_buffer::mutable_data_type>::value);
44 #endif
45
46 template<class Alloc1, class Alloc2>
47 static
48 bool
49 eq( basic_multi_buffer<Alloc1> const& mb1,
50 basic_multi_buffer<Alloc2> const& mb2)
51 {
52 return buffers_to_string(mb1.data()) ==
53 buffers_to_string(mb2.data());
54 }
55
56 void
57 testShrinkToFit()
58 {
59 // empty list
60
61 {
62 multi_buffer b;
63 BEAST_EXPECT(b.size() == 0);
64 BEAST_EXPECT(b.capacity() == 0);
65 b.shrink_to_fit();
66 BEAST_EXPECT(b.size() == 0);
67 BEAST_EXPECT(b.capacity() == 0);
68 }
69
70 // zero readable bytes
71
72 {
73 multi_buffer b;
74 b.prepare(512);
75 b.shrink_to_fit();
76 BEAST_EXPECT(b.size() == 0);
77 BEAST_EXPECT(b.capacity() == 0);
78 }
79
80 {
81 multi_buffer b;
82 b.prepare(512);
83 b.commit(32);
84 b.consume(32);
85 b.shrink_to_fit();
86 BEAST_EXPECT(b.size() == 0);
87 BEAST_EXPECT(b.capacity() == 0);
88 }
89
90 {
91 multi_buffer b;
92 b.prepare(512);
93 b.commit(512);
94 b.prepare(512);
95 b.clear();
96 b.shrink_to_fit();
97 BEAST_EXPECT(b.size() == 0);
98 BEAST_EXPECT(b.capacity() == 0);
99 }
100
101 // unused list
102
103 {
104 multi_buffer b;
105 b.prepare(512);
106 b.commit(512);
107 b.prepare(512);
108 b.shrink_to_fit();
109 BEAST_EXPECT(b.size() == 512);
110 BEAST_EXPECT(b.capacity() == 512);
111 }
112
113 {
114 multi_buffer b;
115 b.prepare(512);
116 b.commit(512);
117 b.prepare(512);
118 b.prepare(1024);
119 b.shrink_to_fit();
120 BEAST_EXPECT(b.size() == 512);
121 BEAST_EXPECT(b.capacity() == 512);
122 }
123
124 // partial last buffer
125
126 {
127 multi_buffer b;
128 b.prepare(512);
129 b.commit(512);
130 b.prepare(512);
131 b.commit(88);
132 b.shrink_to_fit();
133 BEAST_EXPECT(b.size() == 600);
134 BEAST_EXPECT(b.capacity() == 600);
135 }
136
137 // shrink front of first buffer
138
139 {
140 multi_buffer b;
141 b.prepare(512);
142 b.commit(512);
143 b.consume(12);
144 b.shrink_to_fit();
145 BEAST_EXPECT(b.size() == 500);
146 BEAST_EXPECT(b.capacity() == 500);
147 }
148
149 // shrink ends of first buffer
150
151 {
152 multi_buffer b;
153 b.prepare(512);
154 b.commit(500);
155 b.consume(100);
156 b.shrink_to_fit();
157 BEAST_EXPECT(b.size() == 400);
158 BEAST_EXPECT(b.capacity() == 400);
159 }
160 }
161
162 void
163 testDynamicBuffer()
164 {
165 multi_buffer b(30);
166 BEAST_EXPECT(b.max_size() == 30);
167 test_dynamic_buffer(b);
168 }
169
170 void
171 testMembers()
172 {
173 using namespace test;
174
175 // compare equal
176 using equal_t = test::test_allocator<char,
177 true, true, true, true, true>;
178
179 // compare not equal
180 using unequal_t = test::test_allocator<char,
181 false, true, true, true, true>;
182
183 // construction
184 {
185 {
186 multi_buffer b;
187 BEAST_EXPECT(b.capacity() == 0);
188 }
189 {
190 multi_buffer b{500};
191 BEAST_EXPECT(b.capacity() == 0);
192 BEAST_EXPECT(b.max_size() == 500);
193 }
194 {
195 unequal_t a1;
196 basic_multi_buffer<unequal_t> b{a1};
197 BEAST_EXPECT(b.get_allocator() == a1);
198 BEAST_EXPECT(b.get_allocator() != unequal_t{});
199 }
200 {
201 unequal_t a1;
202 basic_multi_buffer<unequal_t> b{500, a1};
203 BEAST_EXPECT(b.capacity() == 0);
204 BEAST_EXPECT(b.max_size() == 500);
205 BEAST_EXPECT(b.get_allocator() == a1);
206 BEAST_EXPECT(b.get_allocator() != unequal_t{});
207 }
208 }
209
210 // move construction
211 {
212 {
213 basic_multi_buffer<equal_t> b1{30};
214 BEAST_EXPECT(b1.get_allocator()->nmove == 0);
215 ostream(b1) << "Hello";
216 basic_multi_buffer<equal_t> b2{std::move(b1)};
217 BEAST_EXPECT(b2.get_allocator()->nmove == 1);
218 BEAST_EXPECT(b1.size() == 0);
219 BEAST_EXPECT(b1.capacity() == 0);
220 BEAST_EXPECT(buffers_to_string(b2.data()) == "Hello");
221 BEAST_EXPECT(b1.max_size() == b2.max_size());
222 }
223 // allocators equal
224 {
225 basic_multi_buffer<equal_t> b1{30};
226 ostream(b1) << "Hello";
227 equal_t a;
228 basic_multi_buffer<equal_t> b2{std::move(b1), a};
229 BEAST_EXPECT(b1.size() == 0);
230 BEAST_EXPECT(b1.capacity() == 0);
231 BEAST_EXPECT(buffers_to_string(b2.data()) == "Hello");
232 BEAST_EXPECT(b1.max_size() == b2.max_size());
233 }
234 {
235 // allocators unequal
236 basic_multi_buffer<unequal_t> b1{30};
237 ostream(b1) << "Hello";
238 unequal_t a;
239 basic_multi_buffer<unequal_t> b2{std::move(b1), a};
240 BEAST_EXPECT(b1.size() == 0);
241 BEAST_EXPECT(b1.capacity() == 0);
242 BEAST_EXPECT(buffers_to_string(b2.data()) == "Hello");
243 BEAST_EXPECT(b1.max_size() == b2.max_size());
244 }
245 }
246
247 // copy construction
248 {
249 {
250 basic_multi_buffer<equal_t> b1;
251 ostream(b1) << "Hello";
252 basic_multi_buffer<equal_t> b2{b1};
253 BEAST_EXPECT(b1.get_allocator() == b2.get_allocator());
254 BEAST_EXPECT(buffers_to_string(b1.data()) == "Hello");
255 BEAST_EXPECT(buffers_to_string(b2.data()) == "Hello");
256 }
257 {
258 basic_multi_buffer<unequal_t> b1;
259 ostream(b1) << "Hello";
260 unequal_t a;
261 basic_multi_buffer<unequal_t> b2(b1, a);
262 BEAST_EXPECT(b1.get_allocator() != b2.get_allocator());
263 BEAST_EXPECT(buffers_to_string(b1.data()) == "Hello");
264 BEAST_EXPECT(buffers_to_string(b2.data()) == "Hello");
265 }
266 {
267 basic_multi_buffer<equal_t> b1;
268 ostream(b1) << "Hello";
269 basic_multi_buffer<unequal_t> b2(b1);
270 BEAST_EXPECT(buffers_to_string(b1.data()) == "Hello");
271 BEAST_EXPECT(buffers_to_string(b2.data()) == "Hello");
272 }
273 {
274 basic_multi_buffer<unequal_t> b1;
275 ostream(b1) << "Hello";
276 equal_t a;
277 basic_multi_buffer<equal_t> b2(b1, a);
278 BEAST_EXPECT(b2.get_allocator() == a);
279 BEAST_EXPECT(buffers_to_string(b1.data()) == "Hello");
280 BEAST_EXPECT(buffers_to_string(b2.data()) == "Hello");
281 }
282 }
283
284 // move assignment
285 {
286 {
287 multi_buffer b1;
288 ostream(b1) << "Hello";
289 multi_buffer b2;
290 b2 = std::move(b1);
291 BEAST_EXPECT(b1.size() == 0);
292 BEAST_EXPECT(b1.capacity() == 0);
293 BEAST_EXPECT(buffers_to_string(b2.data()) == "Hello");
294 }
295 {
296 using na_t = test::test_allocator<char,
297 false, true, false, true, true>;
298 basic_multi_buffer<na_t> b1;
299 ostream(b1) << "Hello";
300 basic_multi_buffer<na_t> b2;
301 b2 = std::move(b1);
302 BEAST_EXPECT(b1.get_allocator() != b2.get_allocator());
303 BEAST_EXPECT(b1.size() == 0);
304 BEAST_EXPECT(b1.capacity() == 0);
305 BEAST_EXPECT(buffers_to_string(b2.data()) == "Hello");
306 }
307 {
308 // propagate_on_container_move_assignment : true
309 using pocma_t = test::test_allocator<char,
310 true, true, true, true, true>;
311 basic_multi_buffer<pocma_t> b1;
312 ostream(b1) << "Hello";
313 basic_multi_buffer<pocma_t> b2;
314 b2 = std::move(b1);
315 BEAST_EXPECT(b1.size() == 0);
316 BEAST_EXPECT(buffers_to_string(b2.data()) == "Hello");
317 }
318 {
319 // propagate_on_container_move_assignment : false
320 using pocma_t = test::test_allocator<char,
321 true, true, false, true, true>;
322 basic_multi_buffer<pocma_t> b1;
323 ostream(b1) << "Hello";
324 basic_multi_buffer<pocma_t> b2;
325 b2 = std::move(b1);
326 BEAST_EXPECT(b1.size() == 0);
327 BEAST_EXPECT(buffers_to_string(b2.data()) == "Hello");
328 }
329 }
330
331 // copy assignment
332 {
333 {
334 multi_buffer b1;
335 ostream(b1) << "Hello";
336 multi_buffer b2;
337 b2 = b1;
338 BEAST_EXPECT(buffers_to_string(b1.data()) == "Hello");
339 BEAST_EXPECT(buffers_to_string(b2.data()) == "Hello");
340 basic_multi_buffer<equal_t> b3;
341 b3 = b2;
342 BEAST_EXPECT(buffers_to_string(b3.data()) == "Hello");
343 }
344 {
345 // propagate_on_container_copy_assignment : true
346 using pocca_t = test::test_allocator<char,
347 true, true, true, true, true>;
348 basic_multi_buffer<pocca_t> b1;
349 ostream(b1) << "Hello";
350 basic_multi_buffer<pocca_t> b2;
351 b2 = b1;
352 BEAST_EXPECT(buffers_to_string(b2.data()) == "Hello");
353 }
354 {
355 // propagate_on_container_copy_assignment : false
356 using pocca_t = test::test_allocator<char,
357 true, false, true, true, true>;
358 basic_multi_buffer<pocca_t> b1;
359 ostream(b1) << "Hello";
360 basic_multi_buffer<pocca_t> b2;
361 b2 = b1;
362 BEAST_EXPECT(buffers_to_string(b2.data()) == "Hello");
363 }
364 }
365
366 // max_size
367 {
368 multi_buffer b{10};
369 BEAST_EXPECT(b.max_size() == 10);
370 b.max_size(32);
371 BEAST_EXPECT(b.max_size() == 32);
372 }
373
374 // allocator max_size
375 {
376 basic_multi_buffer<equal_t> b;
377 auto a = b.get_allocator();
378 BOOST_STATIC_ASSERT(
379 ! std::is_const<decltype(a)>::value);
380 a->max_size = 30;
381 try
382 {
383 b.prepare(1000);
384 fail("", __FILE__, __LINE__);
385 }
386 catch(std::length_error const&)
387 {
388 pass();
389 }
390 try
391 {
392 b.reserve(1000);
393 fail("", __FILE__, __LINE__);
394 }
395 catch(std::length_error const&)
396 {
397 pass();
398 }
399 }
400
401
402 // prepare
403 {
404 {
405 multi_buffer b{100};
406 try
407 {
408 b.prepare(b.max_size() + 1);
409 fail("", __FILE__, __LINE__);
410 }
411 catch(std::length_error const&)
412 {
413 pass();
414 }
415 }
416 {
417 string_view const s = "Hello, world!";
418 multi_buffer b1{64};
419 BEAST_EXPECT(b1.size() == 0);
420 BEAST_EXPECT(b1.max_size() == 64);
421 BEAST_EXPECT(b1.capacity() == 0);
422 ostream(b1) << s;
423 BEAST_EXPECT(buffers_to_string(b1.data()) == s);
424 {
425 multi_buffer b2{b1};
426 BEAST_EXPECT(buffers_to_string(b2.data()) == s);
427 b2.consume(7);
428 BEAST_EXPECT(buffers_to_string(b2.data()) == s.substr(7));
429 }
430 {
431 multi_buffer b2{64};
432 b2 = b1;
433 BEAST_EXPECT(buffers_to_string(b2.data()) == s);
434 b2.consume(7);
435 BEAST_EXPECT(buffers_to_string(b2.data()) == s.substr(7));
436 }
437 }
438 {
439 multi_buffer b;
440 b.prepare(1000);
441 BEAST_EXPECT(b.capacity() >= 1000);
442 b.commit(1);
443 BEAST_EXPECT(b.size() == 1);
444 BEAST_EXPECT(b.capacity() >= 1000);
445 b.prepare(1000);
446 BEAST_EXPECT(b.size() == 1);
447 BEAST_EXPECT(b.capacity() >= 1000);
448 b.prepare(1500);
449 BEAST_EXPECT(b.capacity() >= 1000);
450 }
451 {
452 multi_buffer b;
453 b.prepare(1000);
454 BEAST_EXPECT(b.capacity() >= 1000);
455 b.commit(1);
456 BEAST_EXPECT(b.capacity() >= 1000);
457 b.prepare(1000);
458 BEAST_EXPECT(b.capacity() >= 1000);
459 b.prepare(2000);
460 BEAST_EXPECT(b.capacity() >= 2000);
461 b.commit(2);
462 }
463 {
464 multi_buffer b;
465 b.prepare(1000);
466 BEAST_EXPECT(b.capacity() >= 1000);
467 b.prepare(2000);
468 BEAST_EXPECT(b.capacity() >= 2000);
469 b.prepare(4000);
470 BEAST_EXPECT(b.capacity() >= 4000);
471 b.prepare(50);
472 BEAST_EXPECT(b.capacity() >= 50);
473 }
474 }
475
476 // commit
477 {
478 {
479 multi_buffer b;
480 b.prepare(16);
481 b.commit(16);
482 auto const n =
483 b.capacity() - b.size();
484 b.prepare(n);
485 b.commit(n);
486 auto const size =
487 b.size();
488 auto const capacity =
489 b.capacity();
490 b.commit(1);
491 BEAST_EXPECT(b.size() == size);
492 BEAST_EXPECT(b.capacity() == capacity);
493 }
494
495 multi_buffer b;
496 b.prepare(1000);
497 BEAST_EXPECT(b.capacity() >= 1000);
498 b.commit(1000);
499 BEAST_EXPECT(b.size() == 1000);
500 BEAST_EXPECT(b.capacity() >= 1000);
501 b.consume(1000);
502 BEAST_EXPECT(b.size() == 0);
503 BEAST_EXPECT(b.capacity() == 0);
504 b.prepare(1000);
505 b.commit(650);
506 BEAST_EXPECT(b.size() == 650);
507 BEAST_EXPECT(b.capacity() >= 1000);
508 b.prepare(1000);
509 BEAST_EXPECT(b.capacity() >= 1650);
510 b.commit(100);
511 BEAST_EXPECT(b.size() == 750);
512 BEAST_EXPECT(b.capacity() >= 1000);
513 b.prepare(1000);
514 BEAST_EXPECT(b.capacity() >= 2000);
515 b.commit(500);
516 }
517
518 // consume
519 {
520 multi_buffer b;
521 b.prepare(1000);
522 BEAST_EXPECT(b.capacity() >= 1000);
523 b.commit(1000);
524 BEAST_EXPECT(b.size() == 1000);
525 BEAST_EXPECT(b.capacity() >= 1000);
526 b.prepare(1000);
527 BEAST_EXPECT(b.capacity() >= 2000);
528 b.commit(750);
529 BEAST_EXPECT(b.size() == 1750);
530 b.consume(500);
531 BEAST_EXPECT(b.size() == 1250);
532 b.consume(500);
533 BEAST_EXPECT(b.size() == 750);
534 b.prepare(250);
535 b.consume(750);
536 BEAST_EXPECT(b.size() == 0);
537 b.prepare(1000);
538 b.commit(800);
539 BEAST_EXPECT(b.size() == 800);
540 b.prepare(1000);
541 b.commit(600);
542 BEAST_EXPECT(b.size() == 1400);
543 b.consume(1400);
544 BEAST_EXPECT(b.size() == 0);
545 }
546
547 // reserve
548 {
549 multi_buffer b;
550 BEAST_EXPECT(b.capacity() == 0);
551 b.reserve(50);
552 BEAST_EXPECT(b.capacity() >= 50);
553 b.prepare(20);
554 b.commit(20);
555 b.reserve(50);
556 BEAST_EXPECT(b.capacity() >= 50);
557 BEAST_EXPECT(b.size() > 1);
558 auto capacity = b.capacity();
559 b.reserve(b.size() - 1);
560 BEAST_EXPECT(b.capacity() == capacity);
561 b.reserve(b.capacity() + 1);
562 BEAST_EXPECT(b.capacity() > capacity);
563 capacity = b.capacity();
564 BEAST_EXPECT(buffers_length(
565 b.prepare(b.capacity() + 200)) > 1);
566 BEAST_EXPECT(b.capacity() > capacity);
567 b.reserve(b.capacity() + 2);
568 BEAST_EXPECT(b.capacity() > capacity);
569 capacity = b.capacity();
570 b.reserve(b.capacity());
571 BEAST_EXPECT(b.capacity() == capacity);
572 }
573
574 // shrink to fit
575 {
576 {
577 multi_buffer b;
578 BEAST_EXPECT(b.capacity() == 0);
579 b.prepare(50);
580 BEAST_EXPECT(b.capacity() >= 50);
581 b.commit(50);
582 BEAST_EXPECT(b.capacity() >= 50);
583 b.prepare(75);
584 BEAST_EXPECT(b.capacity() >= 125);
585 b.shrink_to_fit();
586 BEAST_EXPECT(b.capacity() >= b.size());
587 }
588 {
589 multi_buffer b;
590 b.prepare(2000);
591 BEAST_EXPECT(b.capacity() == 2000);
592 b.commit(1800);
593 BEAST_EXPECT(b.size() == 1800);
594 BEAST_EXPECT(b.capacity() == 2000);
595 b.prepare(5000);
596 BEAST_EXPECT(b.capacity() == 6800);
597 b.shrink_to_fit();
598 BEAST_EXPECT(b.capacity() == 2000);
599 }
600 }
601
602 // clear
603 {
604 multi_buffer b;
605 BEAST_EXPECT(b.capacity() == 0);
606 b.prepare(50);
607 b.commit(50);
608 BEAST_EXPECT(b.size() == 50);
609 BEAST_EXPECT(b.capacity() == 512);
610 b.clear();
611 BEAST_EXPECT(b.size() == 0);
612 BEAST_EXPECT(b.capacity() == 512);
613 }
614
615 // swap
616 {
617 {
618 // propagate_on_container_swap : true
619 using pocs_t = test::test_allocator<char,
620 false, true, true, true, true>;
621 pocs_t a1, a2;
622 BEAST_EXPECT(a1 != a2);
623 basic_multi_buffer<pocs_t> b1{a1};
624 ostream(b1) << "Hello";
625 basic_multi_buffer<pocs_t> b2{a2};
626 BEAST_EXPECT(b1.get_allocator() == a1);
627 BEAST_EXPECT(b2.get_allocator() == a2);
628 swap(b1, b2);
629 BEAST_EXPECT(b1.get_allocator() == a2);
630 BEAST_EXPECT(b2.get_allocator() == a1);
631 BEAST_EXPECT(b1.size() == 0);
632 BEAST_EXPECT(buffers_to_string(b2.data()) == "Hello");
633 swap(b1, b2);
634 BEAST_EXPECT(b1.get_allocator() == a1);
635 BEAST_EXPECT(b2.get_allocator() == a2);
636 BEAST_EXPECT(buffers_to_string(b1.data()) == "Hello");
637 BEAST_EXPECT(b2.size() == 0);
638 }
639 {
640 // propagate_on_container_swap : false
641 using pocs_t = test::test_allocator<char,
642 true, true, true, false, true>;
643 pocs_t a1, a2;
644 BEAST_EXPECT(a1 == a2);
645 BEAST_EXPECT(a1.id() != a2.id());
646 basic_multi_buffer<pocs_t> b1{a1};
647 ostream(b1) << "Hello";
648 basic_multi_buffer<pocs_t> b2{a2};
649 BEAST_EXPECT(b1.get_allocator() == a1);
650 BEAST_EXPECT(b2.get_allocator() == a2);
651 swap(b1, b2);
652 BEAST_EXPECT(b1.get_allocator().id() == a1.id());
653 BEAST_EXPECT(b2.get_allocator().id() == a2.id());
654 BEAST_EXPECT(b1.size() == 0);
655 BEAST_EXPECT(buffers_to_string(b2.data()) == "Hello");
656 swap(b1, b2);
657 BEAST_EXPECT(b1.get_allocator().id() == a1.id());
658 BEAST_EXPECT(b2.get_allocator().id() == a2.id());
659 BEAST_EXPECT(buffers_to_string(b1.data()) == "Hello");
660 BEAST_EXPECT(b2.size() == 0);
661 }
662 }
663
664 // read_size
665 {
666 multi_buffer b{10};
667 BEAST_EXPECT(read_size(b, 512) == 10);
668 b.prepare(4);
669 b.commit(4);
670 BEAST_EXPECT(read_size(b, 512) == 6);
671 b.consume(2);
672 BEAST_EXPECT(read_size(b, 512) == 8);
673 b.prepare(8);
674 b.commit(8);
675 BEAST_EXPECT(read_size(b, 512) == 0);
676 }
677 }
678
679 void
680 testMatrix1()
681 {
682 string_view s = "Hello, world";
683 BEAST_EXPECT(s.size() == 12);
684 for(std::size_t i = 1; i < 12; ++i) {
685 for(std::size_t x = 1; x < 4; ++x) {
686 for(std::size_t y = 1; y < 4; ++y) {
687 std::size_t z = s.size() - (x + y);
688 {
689 multi_buffer b;
690 b.commit(net::buffer_copy(
691 b.prepare(x), net::buffer(s.data(), x)));
692 b.commit(net::buffer_copy(
693 b.prepare(y), net::buffer(s.data()+x, y)));
694 b.commit(net::buffer_copy(
695 b.prepare(z), net::buffer(s.data()+x+y, z)));
696 BEAST_EXPECT(buffers_to_string(b.data()) == s);
697 {
698 multi_buffer mb2{b};
699 BEAST_EXPECT(eq(b, mb2));
700 }
701 {
702 multi_buffer mb2;
703 mb2 = b;
704 BEAST_EXPECT(eq(b, mb2));
705 }
706 {
707 multi_buffer mb2{std::move(b)};
708 BEAST_EXPECT(buffers_to_string(mb2.data()) == s);
709 BEAST_EXPECT(b.size() == 0);
710 BEAST_EXPECT(buffer_bytes(b.data()) == 0);
711 b = std::move(mb2);
712 BEAST_EXPECT(buffers_to_string(b.data()) == s);
713 BEAST_EXPECT(mb2.size() == 0);
714 BEAST_EXPECT(buffer_bytes(mb2.data()) == 0);
715 }
716 }
717 }}}
718 }
719
720 void
721 testMatrix2()
722 {
723 using namespace test;
724 std::string const s = "Hello, world";
725 BEAST_EXPECT(s.size() == 12);
726 for(std::size_t i = 1; i < 12; ++i) {
727 for(std::size_t x = 1; x < 4; ++x) {
728 for(std::size_t y = 1; y < 4; ++y) {
729 for(std::size_t t = 1; t < 4; ++ t) {
730 for(std::size_t u = 1; u < 4; ++ u) {
731 std::size_t z = s.size() - (x + y);
732 std::size_t v = s.size() - (t + u);
733 {
734 multi_buffer b;
735 {
736 auto d = b.prepare(z);
737 BEAST_EXPECT(buffer_bytes(d) == z);
738 }
739 {
740 auto d = b.prepare(0);
741 BEAST_EXPECT(buffer_bytes(d) == 0);
742 }
743 {
744 auto d = b.prepare(y);
745 BEAST_EXPECT(buffer_bytes(d) == y);
746 }
747 {
748 auto d = b.prepare(x);
749 BEAST_EXPECT(buffer_bytes(d) == x);
750 b.commit(buffer_copy(d, net::buffer(s.data(), x)));
751 }
752 BEAST_EXPECT(b.size() == x);
753 BEAST_EXPECT(buffer_bytes(b.data()) == b.size());
754 {
755 auto d = b.prepare(x);
756 BEAST_EXPECT(buffer_bytes(d) == x);
757 }
758 {
759 auto d = b.prepare(0);
760 BEAST_EXPECT(buffer_bytes(d) == 0);
761 }
762 {
763 auto d = b.prepare(z);
764 BEAST_EXPECT(buffer_bytes(d) == z);
765 }
766 {
767 auto d = b.prepare(y);
768 BEAST_EXPECT(buffer_bytes(d) == y);
769 b.commit(buffer_copy(d, net::buffer(s.data()+x, y)));
770 }
771 b.commit(1);
772 BEAST_EXPECT(b.size() == x + y);
773 BEAST_EXPECT(buffer_bytes(b.data()) == b.size());
774 {
775 auto d = b.prepare(x);
776 BEAST_EXPECT(buffer_bytes(d) == x);
777 }
778 {
779 auto d = b.prepare(y);
780 BEAST_EXPECT(buffer_bytes(d) == y);
781 }
782 {
783 auto d = b.prepare(0);
784 BEAST_EXPECT(buffer_bytes(d) == 0);
785 }
786 {
787 auto d = b.prepare(z);
788 BEAST_EXPECT(buffer_bytes(d) == z);
789 b.commit(buffer_copy(d, net::buffer(s.data()+x+y, z)));
790 }
791 b.commit(2);
792 BEAST_EXPECT(b.size() == x + y + z);
793 BEAST_EXPECT(buffer_bytes(b.data()) == b.size());
794 BEAST_EXPECT(buffers_to_string(b.data()) == s);
795 b.consume(t);
796 {
797 auto d = b.prepare(0);
798 BEAST_EXPECT(buffer_bytes(d) == 0);
799 }
800 BEAST_EXPECT(buffers_to_string(b.data()) ==
801 s.substr(t, std::string::npos));
802 b.consume(u);
803 BEAST_EXPECT(buffers_to_string(b.data()) ==
804 s.substr(t + u, std::string::npos));
805 b.consume(v);
806 BEAST_EXPECT(buffers_to_string(b.data()).empty());
807 b.consume(1);
808 {
809 auto d = b.prepare(0);
810 BEAST_EXPECT(buffer_bytes(d) == 0);
811 }
812 }
813 }}}}}
814 }
815
816 void
817 run() override
818 {
819 #if 1
820 testShrinkToFit();
821 testDynamicBuffer();
822 testMembers();
823 testMatrix1();
824 testMatrix2();
825 #endif
826 }
827 };
828
829 BEAST_DEFINE_TESTSUITE(beast,core,multi_buffer);
830
831 } // beast
832 } // boost