Files
boost_beast/test/beast/core/multi_buffer.cpp
T

652 lines
22 KiB
C++
Raw Normal View History

//
2017-07-24 09:42:36 -07:00
// Copyright (c) 2016-2017 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)
//
2017-07-20 13:40:34 -07:00
// Official repository: https://github.com/boostorg/beast
//
// Test that header file is self-contained.
2017-07-20 13:40:34 -07:00
#include <boost/beast/core/multi_buffer.hpp>
#include "buffer_test.hpp"
2017-06-10 07:48:17 -07:00
2017-07-20 13:40:34 -07:00
#include <boost/beast/core/ostream.hpp>
#include <boost/beast/core/string.hpp>
#include <boost/beast/core/type_traits.hpp>
#include <boost/beast/test/test_allocator.hpp>
2018-11-11 14:07:55 -08:00
#include <boost/beast/_experimental/unit_test/suite.hpp>
#include <boost/asio/buffer.hpp>
2018-11-29 12:57:29 -08:00
#include <boost/asio/buffers_iterator.hpp>
#include <algorithm>
#include <atomic>
2018-11-29 12:57:29 -08:00
#include <cctype>
#include <memory>
#include <string>
2017-07-20 13:40:34 -07:00
namespace boost {
namespace beast {
class multi_buffer_test : public beast::unit_test::suite
{
public:
2018-11-29 12:57:29 -08:00
BOOST_STATIC_ASSERT(
2018-11-30 14:58:38 -08:00
net::is_dynamic_buffer<
2018-11-29 12:57:29 -08:00
multi_buffer>::value);
BOOST_STATIC_ASSERT(
2018-11-30 14:58:38 -08:00
net::is_const_buffer_sequence<
2018-11-29 12:57:29 -08:00
multi_buffer::const_buffers_type>::value);
BOOST_STATIC_ASSERT(
2018-11-30 14:58:38 -08:00
net::is_mutable_buffer_sequence<
2018-11-29 12:57:29 -08:00
multi_buffer::mutable_data_type>::value);
BOOST_STATIC_ASSERT(
2018-11-30 14:58:38 -08:00
net::is_mutable_buffer_sequence<
2018-11-29 12:57:29 -08:00
multi_buffer::mutable_buffers_type>::value);
BOOST_STATIC_ASSERT(std::is_convertible<
multi_buffer::mutable_data_type,
multi_buffer::const_buffers_type>::value);
#if ! defined( BOOST_LIBSTDCXX_VERSION ) || BOOST_LIBSTDCXX_VERSION >= 50000
BOOST_STATIC_ASSERT(std::is_trivially_copyable<
multi_buffer::const_buffers_type>::value);
BOOST_STATIC_ASSERT(std::is_trivially_copyable<
multi_buffer::mutable_data_type>::value);
#endif
2018-11-29 12:57:29 -08:00
template<class Alloc1, class Alloc2>
static
bool
2017-06-10 07:48:17 -07:00
eq(basic_multi_buffer<Alloc1> const& mb1,
basic_multi_buffer<Alloc2> const& mb2)
{
2018-04-24 10:55:39 -07:00
return buffers_to_string(mb1.data()) ==
buffers_to_string(mb2.data());
}
template<class ConstBufferSequence>
void
expect_size(std::size_t n, ConstBufferSequence const& buffers)
{
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(test::size_pre(buffers) == n);
BEAST_EXPECT(test::size_post(buffers) == n);
BEAST_EXPECT(test::size_rev_pre(buffers) == n);
BEAST_EXPECT(test::size_rev_post(buffers) == n);
}
template<class U, class V>
static
void
self_assign(U& u, V&& v)
{
u = std::forward<V>(v);
}
2018-11-29 12:57:29 -08:00
template<class DynamicBuffer>
void
testMutableData()
{
DynamicBuffer b;
DynamicBuffer const& cb = b;
ostream(b) << "Hello";
BOOST_STATIC_ASSERT(
2018-11-30 14:58:38 -08:00
net::is_const_buffer_sequence<
2018-11-29 12:57:29 -08:00
decltype(cb.data())>::value &&
2018-11-30 14:58:38 -08:00
! net::is_mutable_buffer_sequence<
2018-11-29 12:57:29 -08:00
decltype(cb.data())>::value);
BOOST_STATIC_ASSERT(
2018-11-30 14:58:38 -08:00
net::is_const_buffer_sequence<
2018-11-29 12:57:29 -08:00
decltype(cb.cdata())>::value &&
2018-11-30 14:58:38 -08:00
! net::is_mutable_buffer_sequence<
2018-11-29 12:57:29 -08:00
decltype(cb.cdata())>::value);
BOOST_STATIC_ASSERT(
2018-11-30 14:58:38 -08:00
net::is_mutable_buffer_sequence<
2018-11-29 12:57:29 -08:00
decltype(b.data())>::value);
std::for_each(
2018-11-30 14:58:38 -08:00
net::buffers_iterator<decltype(b.data())>::begin(b.data()),
net::buffers_iterator<decltype(b.data())>::end(b.data()),
2018-11-29 12:57:29 -08:00
[](char& c)
{
c = static_cast<char>(std::toupper(c));
});
BEAST_EXPECT(buffers_to_string(b.data()) == "HELLO");
BEAST_EXPECT(buffers_to_string(b.cdata()) == "HELLO");
}
2017-06-10 07:48:17 -07:00
void
testMatrix1()
{
2017-06-10 07:48:17 -07:00
using namespace test;
2018-11-30 14:58:38 -08:00
using net::buffer;
std::string const s = "Hello, world";
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(s.size() == 12);
for(std::size_t i = 1; i < 12; ++i) {
for(std::size_t x = 1; x < 4; ++x) {
for(std::size_t y = 1; y < 4; ++y) {
std::size_t z = s.size() - (x + y);
{
2017-06-10 07:48:17 -07:00
multi_buffer b;
b.commit(buffer_copy(b.prepare(x), buffer(s.data(), x)));
b.commit(buffer_copy(b.prepare(y), buffer(s.data()+x, y)));
b.commit(buffer_copy(b.prepare(z), buffer(s.data()+x+y, z)));
2018-04-24 10:55:39 -07:00
BEAST_EXPECT(buffers_to_string(b.data()) == s);
{
2017-06-10 07:48:17 -07:00
multi_buffer mb2{b};
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(eq(b, mb2));
}
{
2017-06-10 07:48:17 -07:00
multi_buffer mb2;
mb2 = b;
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(eq(b, mb2));
}
{
2017-06-10 07:48:17 -07:00
multi_buffer mb2{std::move(b)};
2018-04-24 10:55:39 -07:00
BEAST_EXPECT(buffers_to_string(mb2.data()) == s);
expect_size(0, b.data());
2017-06-10 07:48:17 -07:00
b = std::move(mb2);
2018-04-24 10:55:39 -07:00
BEAST_EXPECT(buffers_to_string(b.data()) == s);
2017-06-10 07:48:17 -07:00
expect_size(0, mb2.data());
}
self_assign(b, b);
2018-04-24 10:55:39 -07:00
BEAST_EXPECT(buffers_to_string(b.data()) == s);
self_assign(b, std::move(b));
2018-04-24 10:55:39 -07:00
BEAST_EXPECT(buffers_to_string(b.data()) == s);
}
}}}
}
void
2017-06-10 07:48:17 -07:00
testMatrix2()
{
2017-06-10 07:48:17 -07:00
using namespace test;
2018-11-30 14:58:38 -08:00
using net::buffer;
using net::buffer_size;
std::string const s = "Hello, world";
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(s.size() == 12);
for(std::size_t i = 1; i < 12; ++i) {
for(std::size_t x = 1; x < 4; ++x) {
for(std::size_t y = 1; y < 4; ++y) {
for(std::size_t t = 1; t < 4; ++ t) {
for(std::size_t u = 1; u < 4; ++ u) {
std::size_t z = s.size() - (x + y);
std::size_t v = s.size() - (t + u);
{
2017-06-10 07:48:17 -07:00
multi_buffer b;
{
auto d = b.prepare(z);
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(buffer_size(d) == z);
}
{
auto d = b.prepare(0);
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(buffer_size(d) == 0);
}
{
auto d = b.prepare(y);
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(buffer_size(d) == y);
}
{
auto d = b.prepare(x);
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(buffer_size(d) == x);
b.commit(buffer_copy(d, buffer(s.data(), x)));
}
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b.size() == x);
BEAST_EXPECT(buffer_size(b.data()) == b.size());
{
auto d = b.prepare(x);
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(buffer_size(d) == x);
}
{
auto d = b.prepare(0);
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(buffer_size(d) == 0);
}
{
auto d = b.prepare(z);
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(buffer_size(d) == z);
}
{
auto d = b.prepare(y);
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(buffer_size(d) == y);
b.commit(buffer_copy(d, buffer(s.data()+x, y)));
}
b.commit(1);
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b.size() == x + y);
BEAST_EXPECT(buffer_size(b.data()) == b.size());
{
auto d = b.prepare(x);
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(buffer_size(d) == x);
}
{
auto d = b.prepare(y);
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(buffer_size(d) == y);
}
{
auto d = b.prepare(0);
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(buffer_size(d) == 0);
}
{
auto d = b.prepare(z);
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(buffer_size(d) == z);
b.commit(buffer_copy(d, buffer(s.data()+x+y, z)));
}
b.commit(2);
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b.size() == x + y + z);
BEAST_EXPECT(buffer_size(b.data()) == b.size());
2018-04-24 10:55:39 -07:00
BEAST_EXPECT(buffers_to_string(b.data()) == s);
b.consume(t);
{
auto d = b.prepare(0);
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(buffer_size(d) == 0);
}
2018-04-24 10:55:39 -07:00
BEAST_EXPECT(buffers_to_string(b.data()) == s.substr(t, std::string::npos));
b.consume(u);
2018-04-24 10:55:39 -07:00
BEAST_EXPECT(buffers_to_string(b.data()) == s.substr(t + u, std::string::npos));
b.consume(v);
2018-04-24 10:55:39 -07:00
BEAST_EXPECT(buffers_to_string(b.data()) == "");
b.consume(1);
{
auto d = b.prepare(0);
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(buffer_size(d) == 0);
}
}
}}}}}
}
2017-06-10 07:48:17 -07:00
void
testIterators()
{
2018-11-30 14:58:38 -08:00
using net::buffer_size;
multi_buffer b;
b.prepare(1);
b.commit(1);
b.prepare(2);
b.commit(2);
expect_size(3, b.data());
b.prepare(1);
expect_size(3, b.prepare(3));
b.commit(2);
}
2017-06-10 07:48:17 -07:00
void
testMembers()
{
using namespace test;
// compare equal
using equal_t = test::test_allocator<char,
true, true, true, true, true>;
// compare not equal
using unequal_t = test::test_allocator<char,
false, true, true, true, true>;
// construction
{
{
multi_buffer b;
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b.capacity() == 0);
2017-06-10 07:48:17 -07:00
}
{
multi_buffer b{500};
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b.capacity() == 0);
BEAST_EXPECT(b.max_size() == 500);
2017-06-10 07:48:17 -07:00
}
{
unequal_t a1;
basic_multi_buffer<unequal_t> b{a1};
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b.get_allocator() == a1);
BEAST_EXPECT(b.get_allocator() != unequal_t{});
2017-06-10 07:48:17 -07:00
}
}
// move construction
{
{
basic_multi_buffer<equal_t> b1{30};
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b1.get_allocator()->nmove == 0);
2017-06-10 07:48:17 -07:00
ostream(b1) << "Hello";
basic_multi_buffer<equal_t> b2{std::move(b1)};
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b2.get_allocator()->nmove == 1);
BEAST_EXPECT(b1.size() == 0);
BEAST_EXPECT(b1.capacity() == 0);
2018-04-24 10:55:39 -07:00
BEAST_EXPECT(buffers_to_string(b2.data()) == "Hello");
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b1.max_size() == b2.max_size());
2017-06-10 07:48:17 -07:00
}
// allocators equal
{
basic_multi_buffer<equal_t> b1{30};
ostream(b1) << "Hello";
equal_t a;
basic_multi_buffer<equal_t> b2{std::move(b1), a};
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b1.size() == 0);
BEAST_EXPECT(b1.capacity() == 0);
2018-04-24 10:55:39 -07:00
BEAST_EXPECT(buffers_to_string(b2.data()) == "Hello");
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b1.max_size() == b2.max_size());
2017-06-10 07:48:17 -07:00
}
{
// allocators unequal
basic_multi_buffer<unequal_t> b1{30};
ostream(b1) << "Hello";
unequal_t a;
basic_multi_buffer<unequal_t> b2{std::move(b1), a};
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b1.size() == 0);
BEAST_EXPECT(b1.capacity() == 0);
2018-04-24 10:55:39 -07:00
BEAST_EXPECT(buffers_to_string(b2.data()) == "Hello");
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b1.max_size() == b2.max_size());
2017-06-10 07:48:17 -07:00
}
}
// copy construction
{
{
basic_multi_buffer<equal_t> b1;
ostream(b1) << "Hello";
basic_multi_buffer<equal_t> b2{b1};
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b1.get_allocator() == b2.get_allocator());
2018-04-24 10:55:39 -07:00
BEAST_EXPECT(buffers_to_string(b1.data()) == "Hello");
BEAST_EXPECT(buffers_to_string(b2.data()) == "Hello");
2017-06-10 07:48:17 -07:00
}
{
basic_multi_buffer<unequal_t> b1;
ostream(b1) << "Hello";
unequal_t a;
basic_multi_buffer<unequal_t> b2(b1, a);
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b1.get_allocator() != b2.get_allocator());
2018-04-24 10:55:39 -07:00
BEAST_EXPECT(buffers_to_string(b1.data()) == "Hello");
BEAST_EXPECT(buffers_to_string(b2.data()) == "Hello");
2017-06-10 07:48:17 -07:00
}
{
basic_multi_buffer<equal_t> b1;
ostream(b1) << "Hello";
basic_multi_buffer<unequal_t> b2(b1);
2018-04-24 10:55:39 -07:00
BEAST_EXPECT(buffers_to_string(b1.data()) == "Hello");
BEAST_EXPECT(buffers_to_string(b2.data()) == "Hello");
2017-06-10 07:48:17 -07:00
}
{
basic_multi_buffer<unequal_t> b1;
ostream(b1) << "Hello";
equal_t a;
basic_multi_buffer<equal_t> b2(b1, a);
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b2.get_allocator() == a);
2018-04-24 10:55:39 -07:00
BEAST_EXPECT(buffers_to_string(b1.data()) == "Hello");
BEAST_EXPECT(buffers_to_string(b2.data()) == "Hello");
2017-06-10 07:48:17 -07:00
}
}
// move assignment
{
{
multi_buffer b1;
ostream(b1) << "Hello";
multi_buffer b2;
b2 = std::move(b1);
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b1.size() == 0);
BEAST_EXPECT(b1.capacity() == 0);
2018-04-24 10:55:39 -07:00
BEAST_EXPECT(buffers_to_string(b2.data()) == "Hello");
2017-06-10 07:48:17 -07:00
}
{
// propagate_on_container_move_assignment : true
using pocma_t = test::test_allocator<char,
true, true, true, true, true>;
basic_multi_buffer<pocma_t> b1;
ostream(b1) << "Hello";
basic_multi_buffer<pocma_t> b2;
b2 = std::move(b1);
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b1.size() == 0);
2018-04-24 10:55:39 -07:00
BEAST_EXPECT(buffers_to_string(b2.data()) == "Hello");
2017-06-10 07:48:17 -07:00
}
{
// propagate_on_container_move_assignment : false
using pocma_t = test::test_allocator<char,
true, true, false, true, true>;
basic_multi_buffer<pocma_t> b1;
ostream(b1) << "Hello";
basic_multi_buffer<pocma_t> b2;
b2 = std::move(b1);
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b1.size() == 0);
2018-04-24 10:55:39 -07:00
BEAST_EXPECT(buffers_to_string(b2.data()) == "Hello");
2017-06-10 07:48:17 -07:00
}
}
// copy assignment
{
{
multi_buffer b1;
ostream(b1) << "Hello";
multi_buffer b2;
b2 = b1;
2018-04-24 10:55:39 -07:00
BEAST_EXPECT(buffers_to_string(b1.data()) == "Hello");
BEAST_EXPECT(buffers_to_string(b2.data()) == "Hello");
2017-06-10 07:48:17 -07:00
basic_multi_buffer<equal_t> b3;
b3 = b2;
2018-04-24 10:55:39 -07:00
BEAST_EXPECT(buffers_to_string(b3.data()) == "Hello");
2017-06-10 07:48:17 -07:00
}
{
// propagate_on_container_copy_assignment : true
using pocca_t = test::test_allocator<char,
true, true, true, true, true>;
basic_multi_buffer<pocca_t> b1;
ostream(b1) << "Hello";
basic_multi_buffer<pocca_t> b2;
b2 = b1;
2018-04-24 10:55:39 -07:00
BEAST_EXPECT(buffers_to_string(b2.data()) == "Hello");
2017-06-10 07:48:17 -07:00
}
{
// propagate_on_container_copy_assignment : false
using pocca_t = test::test_allocator<char,
true, false, true, true, true>;
basic_multi_buffer<pocca_t> b1;
ostream(b1) << "Hello";
basic_multi_buffer<pocca_t> b2;
b2 = b1;
2018-04-24 10:55:39 -07:00
BEAST_EXPECT(buffers_to_string(b2.data()) == "Hello");
2017-06-10 07:48:17 -07:00
}
}
// prepare
{
{
multi_buffer b{100};
try
{
b.prepare(b.max_size() + 1);
fail("", __FILE__, __LINE__);
}
catch(std::length_error const&)
{
pass();
}
}
{
string_view const s = "Hello, world!";
multi_buffer b1{64};
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b1.size() == 0);
BEAST_EXPECT(b1.max_size() == 64);
BEAST_EXPECT(b1.capacity() == 0);
2017-06-10 07:48:17 -07:00
ostream(b1) << s;
2018-04-24 10:55:39 -07:00
BEAST_EXPECT(buffers_to_string(b1.data()) == s);
2017-06-10 07:48:17 -07:00
{
multi_buffer b2{b1};
2018-04-24 10:55:39 -07:00
BEAST_EXPECT(buffers_to_string(b2.data()) == s);
2017-06-10 07:48:17 -07:00
b2.consume(7);
2018-04-24 10:55:39 -07:00
BEAST_EXPECT(buffers_to_string(b2.data()) == s.substr(7));
2017-06-10 07:48:17 -07:00
}
{
multi_buffer b2{64};
b2 = b1;
2018-04-24 10:55:39 -07:00
BEAST_EXPECT(buffers_to_string(b2.data()) == s);
2017-06-10 07:48:17 -07:00
b2.consume(7);
2018-04-24 10:55:39 -07:00
BEAST_EXPECT(buffers_to_string(b2.data()) == s.substr(7));
2017-06-10 07:48:17 -07:00
}
}
{
multi_buffer b;
b.prepare(1000);
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b.capacity() >= 1000);
2017-06-10 07:48:17 -07:00
b.commit(1);
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b.size() == 1);
BEAST_EXPECT(b.capacity() >= 1000);
2017-06-10 07:48:17 -07:00
b.prepare(1000);
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b.size() == 1);
BEAST_EXPECT(b.capacity() >= 1000);
2017-06-10 07:48:17 -07:00
b.prepare(1500);
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b.capacity() >= 1000);
2017-06-10 07:48:17 -07:00
}
{
multi_buffer b;
b.prepare(1000);
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b.capacity() >= 1000);
2017-06-10 07:48:17 -07:00
b.commit(1);
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b.capacity() >= 1000);
2017-06-10 07:48:17 -07:00
b.prepare(1000);
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b.capacity() >= 1000);
2017-06-10 07:48:17 -07:00
b.prepare(2000);
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b.capacity() >= 2000);
2017-06-10 07:48:17 -07:00
b.commit(2);
}
{
multi_buffer b;
b.prepare(1000);
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b.capacity() >= 1000);
2017-06-10 07:48:17 -07:00
b.prepare(2000);
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b.capacity() >= 2000);
2017-06-10 07:48:17 -07:00
b.prepare(4000);
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b.capacity() >= 4000);
2017-06-10 07:48:17 -07:00
b.prepare(50);
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b.capacity() >= 50);
2017-06-10 07:48:17 -07:00
}
}
// commit
{
multi_buffer b;
b.prepare(1000);
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b.capacity() >= 1000);
2017-06-10 07:48:17 -07:00
b.commit(1000);
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b.size() == 1000);
BEAST_EXPECT(b.capacity() >= 1000);
2017-06-10 07:48:17 -07:00
b.consume(1000);
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b.size() == 0);
BEAST_EXPECT(b.capacity() == 0);
2017-06-10 07:48:17 -07:00
b.prepare(1000);
b.commit(650);
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b.size() == 650);
BEAST_EXPECT(b.capacity() >= 1000);
2017-06-10 07:48:17 -07:00
b.prepare(1000);
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b.capacity() >= 1650);
2017-06-10 07:48:17 -07:00
b.commit(100);
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b.size() == 750);
BEAST_EXPECT(b.capacity() >= 1000);
2017-06-10 07:48:17 -07:00
b.prepare(1000);
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b.capacity() >= 2000);
2017-06-10 07:48:17 -07:00
b.commit(500);
}
// consume
{
multi_buffer b;
b.prepare(1000);
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b.capacity() >= 1000);
2017-06-10 07:48:17 -07:00
b.commit(1000);
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b.size() == 1000);
BEAST_EXPECT(b.capacity() >= 1000);
2017-06-10 07:48:17 -07:00
b.prepare(1000);
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b.capacity() >= 2000);
2017-06-10 07:48:17 -07:00
b.commit(750);
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b.size() == 1750);
2017-06-10 07:48:17 -07:00
b.consume(500);
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b.size() == 1250);
2017-06-10 07:48:17 -07:00
b.consume(500);
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b.size() == 750);
2017-06-10 07:48:17 -07:00
b.prepare(250);
b.consume(750);
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b.size() == 0);
2017-06-10 07:48:17 -07:00
b.prepare(1000);
b.commit(800);
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b.size() == 800);
2017-06-10 07:48:17 -07:00
b.prepare(1000);
b.commit(600);
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b.size() == 1400);
2017-06-10 07:48:17 -07:00
b.consume(1400);
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b.size() == 0);
2017-06-10 07:48:17 -07:00
}
// swap
{
{
// propagate_on_container_swap : true
using pocs_t = test::test_allocator<char,
false, true, true, true, true>;
pocs_t a1, a2;
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(a1 != a2);
2017-06-10 07:48:17 -07:00
basic_multi_buffer<pocs_t> b1{a1};
ostream(b1) << "Hello";
basic_multi_buffer<pocs_t> b2{a2};
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b1.get_allocator() == a1);
BEAST_EXPECT(b2.get_allocator() == a2);
2017-06-10 07:48:17 -07:00
swap(b1, b2);
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b1.get_allocator() == a2);
BEAST_EXPECT(b2.get_allocator() == a1);
BEAST_EXPECT(b1.size() == 0);
2018-04-24 10:55:39 -07:00
BEAST_EXPECT(buffers_to_string(b2.data()) == "Hello");
2017-06-10 07:48:17 -07:00
swap(b1, b2);
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b1.get_allocator() == a1);
BEAST_EXPECT(b2.get_allocator() == a2);
2018-04-24 10:55:39 -07:00
BEAST_EXPECT(buffers_to_string(b1.data()) == "Hello");
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b2.size() == 0);
2017-06-10 07:48:17 -07:00
}
{
// propagate_on_container_swap : false
using pocs_t = test::test_allocator<char,
true, true, true, false, true>;
pocs_t a1, a2;
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(a1 == a2);
BEAST_EXPECT(a1.id() != a2.id());
2017-06-10 07:48:17 -07:00
basic_multi_buffer<pocs_t> b1{a1};
ostream(b1) << "Hello";
basic_multi_buffer<pocs_t> b2{a2};
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b1.get_allocator() == a1);
BEAST_EXPECT(b2.get_allocator() == a2);
2017-06-10 07:48:17 -07:00
swap(b1, b2);
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b1.get_allocator().id() == a1.id());
BEAST_EXPECT(b2.get_allocator().id() == a2.id());
BEAST_EXPECT(b1.size() == 0);
2018-04-24 10:55:39 -07:00
BEAST_EXPECT(buffers_to_string(b2.data()) == "Hello");
2017-06-10 07:48:17 -07:00
swap(b1, b2);
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b1.get_allocator().id() == a1.id());
BEAST_EXPECT(b2.get_allocator().id() == a2.id());
2018-04-24 10:55:39 -07:00
BEAST_EXPECT(buffers_to_string(b1.data()) == "Hello");
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b2.size() == 0);
2017-06-10 07:48:17 -07:00
}
}
2017-06-12 01:51:52 -07:00
// read_size
2017-06-10 07:48:17 -07:00
{
multi_buffer b{10};
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(read_size(b, 512) == 10);
2017-06-10 07:48:17 -07:00
b.prepare(4);
b.commit(4);
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(read_size(b, 512) == 6);
2017-06-10 07:48:17 -07:00
b.consume(2);
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(read_size(b, 512) == 8);
2017-06-10 07:48:17 -07:00
b.prepare(8);
b.commit(8);
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(read_size(b, 512) == 0);
2017-06-10 07:48:17 -07:00
}
}
void
run() override
{
2017-06-10 07:48:17 -07:00
testMatrix1();
testMatrix2();
testIterators();
2017-06-10 07:48:17 -07:00
testMembers();
2018-11-29 12:57:29 -08:00
testMutableData<multi_buffer>();
}
};
2017-08-01 17:01:57 -07:00
BEAST_DEFINE_TESTSUITE(beast,core,multi_buffer);
} // beast
2017-07-20 13:40:34 -07:00
} // boost