mirror of
https://github.com/boostorg/beast.git
synced 2025-07-30 04:47:29 +02:00
flat_buffer coverage
This commit is contained in:
@ -1,6 +1,7 @@
|
|||||||
Version 54:
|
Version 54:
|
||||||
|
|
||||||
* static_buffer coverage
|
* static_buffer coverage
|
||||||
|
* flat_buffer coverage
|
||||||
|
|
||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
@ -17,42 +17,47 @@ namespace test {
|
|||||||
|
|
||||||
struct test_allocator_info
|
struct test_allocator_info
|
||||||
{
|
{
|
||||||
|
std::size_t id;
|
||||||
std::size_t ncopy = 0;
|
std::size_t ncopy = 0;
|
||||||
std::size_t nmove = 0;
|
std::size_t nmove = 0;
|
||||||
|
std::size_t nmassign = 0;
|
||||||
|
std::size_t ncpassign = 0;
|
||||||
std::size_t nselect = 0;
|
std::size_t nselect = 0;
|
||||||
|
|
||||||
|
test_allocator_info()
|
||||||
|
: id([]
|
||||||
|
{
|
||||||
|
static std::atomic<std::size_t> sid(0);
|
||||||
|
return ++sid;
|
||||||
|
}())
|
||||||
|
{
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template<class T,
|
template<class T, bool Equal, bool Assign, bool Move, bool Swap, bool Select>
|
||||||
bool Equal, bool Assign, bool Move, bool Swap, bool Select>
|
|
||||||
class test_allocator;
|
class test_allocator;
|
||||||
|
|
||||||
template<class T,
|
template<class T, bool Equal, bool Assign, bool Move, bool Swap, bool Select>
|
||||||
bool Equal, bool Assign, bool Move, bool Swap, bool Select>
|
|
||||||
struct test_allocator_base
|
struct test_allocator_base
|
||||||
{
|
{
|
||||||
};
|
};
|
||||||
|
|
||||||
template<class T,
|
template<class T, bool Equal, bool Assign, bool Move, bool Swap>
|
||||||
bool Equal, bool Assign, bool Move, bool Swap>
|
struct test_allocator_base<T, Equal, Assign, Move, Swap, true>
|
||||||
struct test_allocator_base<
|
|
||||||
T, Equal, Assign, Move, Swap, true>
|
|
||||||
{
|
{
|
||||||
static
|
static
|
||||||
test_allocator<T, Equal, Assign, Move, Swap, true>
|
test_allocator<T, Equal, Assign, Move, Swap, true>
|
||||||
select_on_container_copy_construction(test_allocator<
|
select_on_container_copy_construction(test_allocator<
|
||||||
T, Equal, Assign, Move, Swap, true> const& a)
|
T, Equal, Assign, Move, Swap, true> const& a)
|
||||||
{
|
{
|
||||||
return test_allocator<T,
|
return test_allocator<T, Equal, Assign, Move, Swap, true>{};
|
||||||
Equal, Assign, Move, Swap, true>{};
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template<class T,
|
template<class T, bool Equal, bool Assign, bool Move, bool Swap, bool Select>
|
||||||
bool Equal, bool Assign, bool Move, bool Swap, bool Select>
|
|
||||||
class test_allocator : public test_allocator_base<
|
class test_allocator : public test_allocator_base<
|
||||||
T, Equal, Assign, Move, Swap, Select>
|
T, Equal, Assign, Move, Swap, Select>
|
||||||
{
|
{
|
||||||
std::size_t id_;
|
|
||||||
std::shared_ptr<test_allocator_info> info_;
|
std::shared_ptr<test_allocator_info> info_;
|
||||||
|
|
||||||
template<class, bool, bool, bool, bool, bool>
|
template<class, bool, bool, bool, bool, bool>
|
||||||
@ -73,24 +78,16 @@ public:
|
|||||||
template<class U>
|
template<class U>
|
||||||
struct rebind
|
struct rebind
|
||||||
{
|
{
|
||||||
using other = test_allocator<
|
using other = test_allocator<U, Equal, Assign, Move, Swap, Select>;
|
||||||
U, Equal, Assign, Move, Swap, Select>;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
test_allocator()
|
test_allocator()
|
||||||
: id_([]
|
: info_(std::make_shared<test_allocator_info>())
|
||||||
{
|
|
||||||
static std::atomic<
|
|
||||||
std::size_t> sid(0);
|
|
||||||
return ++sid;
|
|
||||||
}())
|
|
||||||
, info_(std::make_shared<test_allocator_info>())
|
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
test_allocator(test_allocator const& u) noexcept
|
test_allocator(test_allocator const& u) noexcept
|
||||||
: id_(u.id_)
|
: info_(u.info_)
|
||||||
, info_(u.info_)
|
|
||||||
{
|
{
|
||||||
++info_->ncopy;
|
++info_->ncopy;
|
||||||
}
|
}
|
||||||
@ -98,19 +95,33 @@ public:
|
|||||||
template<class U>
|
template<class U>
|
||||||
test_allocator(test_allocator<U,
|
test_allocator(test_allocator<U,
|
||||||
Equal, Assign, Move, Swap, Select> const& u) noexcept
|
Equal, Assign, Move, Swap, Select> const& u) noexcept
|
||||||
: id_(u.id_)
|
: info_(u.info_)
|
||||||
, info_(u.info_)
|
|
||||||
{
|
{
|
||||||
++info_->ncopy;
|
++info_->ncopy;
|
||||||
}
|
}
|
||||||
|
|
||||||
test_allocator(test_allocator&& t)
|
test_allocator(test_allocator&& t)
|
||||||
: id_(t.id_)
|
: info_(t.info_)
|
||||||
, info_(t.info_)
|
|
||||||
{
|
{
|
||||||
++info_->nmove;
|
++info_->nmove;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
test_allocator&
|
||||||
|
operator=(test_allocator const& u) noexcept
|
||||||
|
{
|
||||||
|
info_ = u.info_;
|
||||||
|
++info_->ncpassign;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
test_allocator&
|
||||||
|
operator=(test_allocator&& u) noexcept
|
||||||
|
{
|
||||||
|
info_ = u.info_;
|
||||||
|
++info_->nmassign;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
value_type*
|
value_type*
|
||||||
allocate(std::size_t n)
|
allocate(std::size_t n)
|
||||||
{
|
{
|
||||||
@ -127,7 +138,7 @@ public:
|
|||||||
bool
|
bool
|
||||||
operator==(test_allocator const& other) const
|
operator==(test_allocator const& other) const
|
||||||
{
|
{
|
||||||
return id_ == other.id_ || Equal;
|
return id() == other.id() || Equal;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
@ -139,7 +150,7 @@ public:
|
|||||||
std::size_t
|
std::size_t
|
||||||
id() const
|
id() const
|
||||||
{
|
{
|
||||||
return id_;
|
return info_->id;
|
||||||
}
|
}
|
||||||
|
|
||||||
test_allocator_info const*
|
test_allocator_info const*
|
||||||
|
@ -204,6 +204,16 @@ public:
|
|||||||
basic_flat_buffer&
|
basic_flat_buffer&
|
||||||
operator=(basic_flat_buffer const& other);
|
operator=(basic_flat_buffer const& other);
|
||||||
|
|
||||||
|
/** Copy assignment
|
||||||
|
|
||||||
|
After the copy, `*this` will have an empty output sequence.
|
||||||
|
|
||||||
|
@param other The object to copy from.
|
||||||
|
*/
|
||||||
|
template<class OtherAlloc>
|
||||||
|
basic_flat_buffer&
|
||||||
|
operator=(basic_flat_buffer<OtherAlloc> const& other);
|
||||||
|
|
||||||
/// Returns a copy of the associated allocator.
|
/// Returns a copy of the associated allocator.
|
||||||
allocator_type
|
allocator_type
|
||||||
get_allocator() const
|
get_allocator() const
|
||||||
@ -275,21 +285,6 @@ public:
|
|||||||
void
|
void
|
||||||
consume(std::size_t n);
|
consume(std::size_t n);
|
||||||
|
|
||||||
/** Reserve space in the stream.
|
|
||||||
|
|
||||||
This reallocates the buffer if necessary.
|
|
||||||
|
|
||||||
@note All previous buffers sequences obtained from
|
|
||||||
calls to @ref data or @ref prepare are invalidated.
|
|
||||||
|
|
||||||
@param n The number of bytes to reserve. Upon success,
|
|
||||||
the capacity will be at least `n`.
|
|
||||||
|
|
||||||
@throws std::length_error if `n` exceeds `max_size()`.
|
|
||||||
*/
|
|
||||||
void
|
|
||||||
reserve(std::size_t n);
|
|
||||||
|
|
||||||
/** Reallocate the buffer to fit the input sequence.
|
/** Reallocate the buffer to fit the input sequence.
|
||||||
|
|
||||||
@note All previous buffers sequences obtained from
|
@note All previous buffers sequences obtained from
|
||||||
|
@ -19,25 +19,6 @@ namespace beast {
|
|||||||
begin_ ..|.. in_ ..|.. out_ ..|.. last_ ..|.. end_
|
begin_ ..|.. in_ ..|.. out_ ..|.. last_ ..|.. end_
|
||||||
*/
|
*/
|
||||||
|
|
||||||
namespace detail {
|
|
||||||
|
|
||||||
inline
|
|
||||||
std::size_t
|
|
||||||
next_pow2(std::size_t x)
|
|
||||||
{
|
|
||||||
std::size_t n = 0;
|
|
||||||
while(x > 0)
|
|
||||||
{
|
|
||||||
++n;
|
|
||||||
x >>= 1;
|
|
||||||
}
|
|
||||||
return std::size_t{1} << n;
|
|
||||||
}
|
|
||||||
|
|
||||||
} // detail
|
|
||||||
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
|
|
||||||
template<class Allocator>
|
template<class Allocator>
|
||||||
basic_flat_buffer<Allocator>::
|
basic_flat_buffer<Allocator>::
|
||||||
~basic_flat_buffer()
|
~basic_flat_buffer()
|
||||||
@ -235,6 +216,19 @@ operator=(basic_flat_buffer const& other) ->
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<class Allocator>
|
||||||
|
template<class OtherAlloc>
|
||||||
|
auto
|
||||||
|
basic_flat_buffer<Allocator>::
|
||||||
|
operator=(basic_flat_buffer<OtherAlloc> const& other) ->
|
||||||
|
basic_flat_buffer&
|
||||||
|
{
|
||||||
|
reset();
|
||||||
|
max_ = other.max_;
|
||||||
|
copy_from(other);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
//------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------
|
||||||
|
|
||||||
template<class Allocator>
|
template<class Allocator>
|
||||||
@ -266,9 +260,9 @@ prepare(std::size_t n) ->
|
|||||||
BOOST_THROW_EXCEPTION(std::length_error{
|
BOOST_THROW_EXCEPTION(std::length_error{
|
||||||
"basic_flat_buffer overflow"});
|
"basic_flat_buffer overflow"});
|
||||||
// allocate a new buffer
|
// allocate a new buffer
|
||||||
auto const new_size = (std::min)(max_,
|
auto const new_size = std::min<std::size_t>(
|
||||||
std::max<std::size_t>(
|
max_,
|
||||||
detail::next_pow2(len + n), min_size));
|
std::max<std::size_t>(2 * len, len + n));
|
||||||
auto const p = alloc_traits::allocate(
|
auto const p = alloc_traits::allocate(
|
||||||
this->member(), new_size);
|
this->member(), new_size);
|
||||||
if(begin_)
|
if(begin_)
|
||||||
@ -301,37 +295,6 @@ consume(std::size_t n)
|
|||||||
in_ += n;
|
in_ += n;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class Allocator>
|
|
||||||
void
|
|
||||||
basic_flat_buffer<Allocator>::
|
|
||||||
reserve(std::size_t n)
|
|
||||||
{
|
|
||||||
if(n <= capacity())
|
|
||||||
return;
|
|
||||||
if(n > max_)
|
|
||||||
BOOST_THROW_EXCEPTION(std::length_error{
|
|
||||||
"basic_flat_buffer overflow"});
|
|
||||||
auto const new_size = (std::min)(max_,
|
|
||||||
std::max<std::size_t>(
|
|
||||||
detail::next_pow2(n), min_size));
|
|
||||||
auto const p = alloc_traits::allocate(
|
|
||||||
this->member(), new_size);
|
|
||||||
auto const len = size();
|
|
||||||
if(begin_)
|
|
||||||
{
|
|
||||||
BOOST_ASSERT(begin_);
|
|
||||||
BOOST_ASSERT(in_);
|
|
||||||
std::memcpy(p, in_, len);
|
|
||||||
alloc_traits::deallocate(
|
|
||||||
this->member(), begin_, capacity());
|
|
||||||
}
|
|
||||||
begin_ = p;
|
|
||||||
in_ = begin_;
|
|
||||||
out_ = begin_ + len;
|
|
||||||
last_ = out_;
|
|
||||||
end_ = begin_ + new_size;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<class Allocator>
|
template<class Allocator>
|
||||||
void
|
void
|
||||||
basic_flat_buffer<Allocator>::
|
basic_flat_buffer<Allocator>::
|
||||||
@ -434,6 +397,7 @@ basic_flat_buffer<Allocator>::
|
|||||||
copy_assign(basic_flat_buffer const& other, std::true_type)
|
copy_assign(basic_flat_buffer const& other, std::true_type)
|
||||||
{
|
{
|
||||||
reset();
|
reset();
|
||||||
|
max_ = other.max_;
|
||||||
this->member() = other.member();
|
this->member() = other.member();
|
||||||
copy_from(other);
|
copy_from(other);
|
||||||
}
|
}
|
||||||
@ -445,6 +409,7 @@ basic_flat_buffer<Allocator>::
|
|||||||
copy_assign(basic_flat_buffer const& other, std::false_type)
|
copy_assign(basic_flat_buffer const& other, std::false_type)
|
||||||
{
|
{
|
||||||
reset();
|
reset();
|
||||||
|
max_ = other.max_;
|
||||||
copy_from(other);
|
copy_from(other);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -454,8 +419,8 @@ void
|
|||||||
basic_flat_buffer<Allocator>::
|
basic_flat_buffer<Allocator>::
|
||||||
swap(basic_flat_buffer& other)
|
swap(basic_flat_buffer& other)
|
||||||
{
|
{
|
||||||
swap(other,
|
swap(other, typename
|
||||||
typename alloc_traits::propagate_on_container_swap{});
|
alloc_traits::propagate_on_container_swap{});
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class Allocator>
|
template<class Allocator>
|
||||||
@ -466,12 +431,13 @@ swap(basic_flat_buffer& other, std::true_type)
|
|||||||
{
|
{
|
||||||
using std::swap;
|
using std::swap;
|
||||||
swap(this->member(), other.member());
|
swap(this->member(), other.member());
|
||||||
swap(this->begin_, other.begin_);
|
swap(max_, other.max_);
|
||||||
swap(this->in_, other.in_);
|
swap(begin_, other.begin_);
|
||||||
swap(this->out_, other.out_);
|
swap(in_, other.in_);
|
||||||
this->last_ = this->out_;
|
swap(out_, other.out_);
|
||||||
other->last_ = other->out_;
|
last_ = this->out_;
|
||||||
swap(this->end_, other.end_);
|
other.last_ = other.out_;
|
||||||
|
swap(end_, other.end_);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class Allocator>
|
template<class Allocator>
|
||||||
@ -482,12 +448,13 @@ swap(basic_flat_buffer& other, std::false_type)
|
|||||||
{
|
{
|
||||||
BOOST_ASSERT(this->member() == other.member());
|
BOOST_ASSERT(this->member() == other.member());
|
||||||
using std::swap;
|
using std::swap;
|
||||||
swap(this->begin_, other.begin_);
|
swap(max_, other.max_);
|
||||||
swap(this->in_, other.in_);
|
swap(begin_, other.begin_);
|
||||||
swap(this->out_, other.out_);
|
swap(in_, other.in_);
|
||||||
this->last_ = this->out_;
|
swap(out_, other.out_);
|
||||||
other->last_ = other->out_;
|
last_ = this->out_;
|
||||||
swap(this->end_, other.end_);
|
other.last_ = other.out_;
|
||||||
|
swap(end_, other.end_);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class Allocator>
|
template<class Allocator>
|
||||||
|
@ -9,7 +9,10 @@
|
|||||||
#include <beast/core/flat_buffer.hpp>
|
#include <beast/core/flat_buffer.hpp>
|
||||||
|
|
||||||
#include "buffer_test.hpp"
|
#include "buffer_test.hpp"
|
||||||
|
|
||||||
#include <beast/core/ostream.hpp>
|
#include <beast/core/ostream.hpp>
|
||||||
|
#include <beast/core/string_view.hpp>
|
||||||
|
#include <beast/core/detail/read_size_helper.hpp>
|
||||||
#include <beast/test/test_allocator.hpp>
|
#include <beast/test/test_allocator.hpp>
|
||||||
#include <beast/unit_test/suite.hpp>
|
#include <beast/unit_test/suite.hpp>
|
||||||
#include <boost/lexical_cast.hpp>
|
#include <boost/lexical_cast.hpp>
|
||||||
@ -23,125 +26,233 @@ static_assert(is_dynamic_buffer<flat_buffer>::value,
|
|||||||
class flat_buffer_test : public beast::unit_test::suite
|
class flat_buffer_test : public beast::unit_test::suite
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
template<class ConstBufferSequence>
|
|
||||||
static
|
|
||||||
std::string
|
|
||||||
to_string(ConstBufferSequence const& bs)
|
|
||||||
{
|
|
||||||
return boost::lexical_cast<
|
|
||||||
std::string>(buffers(bs));
|
|
||||||
}
|
|
||||||
|
|
||||||
template<class Alloc1, class Alloc2>
|
|
||||||
static
|
|
||||||
bool
|
|
||||||
eq(basic_flat_buffer<Alloc1> const& sb1,
|
|
||||||
basic_flat_buffer<Alloc2> const& sb2)
|
|
||||||
{
|
|
||||||
return to_string(sb1.data()) == to_string(sb2.data());
|
|
||||||
}
|
|
||||||
|
|
||||||
template<
|
|
||||||
bool Equal, bool Assign, bool Move, bool Swap, bool Select>
|
|
||||||
void
|
void
|
||||||
testCtor()
|
testBuffer()
|
||||||
{
|
{
|
||||||
using allocator = test::test_allocator<char,
|
using namespace test;
|
||||||
Equal, Assign, Move, Swap, Select>;
|
|
||||||
|
using a_t = test::test_allocator<char,
|
||||||
|
true, true, true, true, true>;
|
||||||
|
|
||||||
|
// Equal == false
|
||||||
|
using a_neq_t = test::test_allocator<char,
|
||||||
|
false, true, true, true, true>;
|
||||||
|
|
||||||
|
// construction
|
||||||
{
|
{
|
||||||
using boost::asio::buffer_size;
|
{
|
||||||
basic_flat_buffer<allocator> b1{10};
|
flat_buffer b;
|
||||||
|
BEAST_EXPECT(b.capacity() == 0);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
flat_buffer b{500};
|
||||||
|
BEAST_EXPECT(b.capacity() == 0);
|
||||||
|
BEAST_EXPECT(b.max_size() == 500);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
a_neq_t a1;
|
||||||
|
basic_flat_buffer<a_neq_t> b{a1};
|
||||||
|
BEAST_EXPECT(b.get_allocator() == a1);
|
||||||
|
a_neq_t a2;
|
||||||
|
BEAST_EXPECT(b.get_allocator() != a2);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
a_neq_t a;
|
||||||
|
basic_flat_buffer<a_neq_t> b{500, a};
|
||||||
|
BEAST_EXPECT(b.capacity() == 0);
|
||||||
|
BEAST_EXPECT(b.max_size() == 500);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// move construction
|
||||||
|
{
|
||||||
|
{
|
||||||
|
basic_flat_buffer<a_t> b1{30};
|
||||||
|
BEAST_EXPECT(b1.get_allocator()->nmove == 0);
|
||||||
|
ostream(b1) << "Hello";
|
||||||
|
basic_flat_buffer<a_t> b2{std::move(b1)};
|
||||||
|
BEAST_EXPECT(b2.get_allocator()->nmove == 1);
|
||||||
|
BEAST_EXPECT(b1.size() == 0);
|
||||||
|
BEAST_EXPECT(b1.capacity() == 0);
|
||||||
|
BEAST_EXPECT(to_string(b2.data()) == "Hello");
|
||||||
|
BEAST_EXPECT(b1.max_size() == b2.max_size());
|
||||||
|
}
|
||||||
|
{
|
||||||
|
basic_flat_buffer<a_t> b1{30};
|
||||||
|
ostream(b1) << "Hello";
|
||||||
|
a_t a;
|
||||||
|
basic_flat_buffer<a_t> b2{std::move(b1), a};
|
||||||
|
BEAST_EXPECT(b1.size() == 0);
|
||||||
|
BEAST_EXPECT(b1.capacity() == 0);
|
||||||
|
BEAST_EXPECT(to_string(b2.data()) == "Hello");
|
||||||
|
BEAST_EXPECT(b1.max_size() == b2.max_size());
|
||||||
|
}
|
||||||
|
{
|
||||||
|
basic_flat_buffer<a_neq_t> b1{30};
|
||||||
|
ostream(b1) << "Hello";
|
||||||
|
a_neq_t a;
|
||||||
|
basic_flat_buffer<a_neq_t> b2{std::move(b1), a};
|
||||||
|
BEAST_EXPECT(b1.size() == 0);
|
||||||
|
BEAST_EXPECT(b1.capacity() == 0);
|
||||||
|
BEAST_EXPECT(to_string(b2.data()) == "Hello");
|
||||||
|
BEAST_EXPECT(b1.max_size() == b2.max_size());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// copy construction
|
||||||
|
{
|
||||||
|
basic_flat_buffer<a_t> b1;
|
||||||
|
ostream(b1) << "Hello";
|
||||||
|
basic_flat_buffer<a_t> b2(b1);
|
||||||
|
BEAST_EXPECT(b1.get_allocator() == b2.get_allocator());
|
||||||
|
BEAST_EXPECT(to_string(b1.data()) == "Hello");
|
||||||
|
BEAST_EXPECT(to_string(b2.data()) == "Hello");
|
||||||
|
}
|
||||||
|
{
|
||||||
|
basic_flat_buffer<a_neq_t> b1;
|
||||||
|
ostream(b1) << "Hello";
|
||||||
|
a_neq_t a;
|
||||||
|
basic_flat_buffer<a_neq_t> b2(b1, a);
|
||||||
|
BEAST_EXPECT(b1.get_allocator() != b2.get_allocator());
|
||||||
|
BEAST_EXPECT(to_string(b1.data()) == "Hello");
|
||||||
|
BEAST_EXPECT(to_string(b2.data()) == "Hello");
|
||||||
|
}
|
||||||
|
{
|
||||||
|
basic_flat_buffer<a_t> b1;
|
||||||
|
ostream(b1) << "Hello";
|
||||||
|
basic_flat_buffer<a_neq_t> b2(b1);
|
||||||
|
BEAST_EXPECT(to_string(b1.data()) == "Hello");
|
||||||
|
BEAST_EXPECT(to_string(b2.data()) == "Hello");
|
||||||
|
}
|
||||||
|
{
|
||||||
|
basic_flat_buffer<a_neq_t> b1;
|
||||||
|
ostream(b1) << "Hello";
|
||||||
|
a_t a;
|
||||||
|
basic_flat_buffer<a_t> b2(b1, a);
|
||||||
|
BEAST_EXPECT(b2.get_allocator() == a);
|
||||||
|
BEAST_EXPECT(to_string(b1.data()) == "Hello");
|
||||||
|
BEAST_EXPECT(to_string(b2.data()) == "Hello");
|
||||||
|
}
|
||||||
|
|
||||||
|
// move assignment
|
||||||
|
{
|
||||||
|
{
|
||||||
|
flat_buffer b1;
|
||||||
|
ostream(b1) << "Hello";
|
||||||
|
flat_buffer b2;
|
||||||
|
b2 = std::move(b1);
|
||||||
|
BEAST_EXPECT(b1.size() == 0);
|
||||||
|
BEAST_EXPECT(b1.capacity() == 0);
|
||||||
|
BEAST_EXPECT(to_string(b2.data()) == "Hello");
|
||||||
|
}
|
||||||
|
{
|
||||||
|
using na_t = test::test_allocator<char,
|
||||||
|
true, true, false, true, true>;
|
||||||
|
basic_flat_buffer<na_t> b1;
|
||||||
|
ostream(b1) << "Hello";
|
||||||
|
basic_flat_buffer<na_t> b2;
|
||||||
|
b2 = std::move(b1);
|
||||||
|
BEAST_EXPECT(b1.get_allocator() == b2.get_allocator());
|
||||||
|
BEAST_EXPECT(b1.size() == 0);
|
||||||
|
BEAST_EXPECT(b1.capacity() == 0);
|
||||||
|
BEAST_EXPECT(to_string(b2.data()) == "Hello");
|
||||||
|
}
|
||||||
|
{
|
||||||
|
using na_t = test::test_allocator<char,
|
||||||
|
false, true, false, true, true>;
|
||||||
|
basic_flat_buffer<na_t> b1;
|
||||||
|
ostream(b1) << "Hello";
|
||||||
|
basic_flat_buffer<na_t> b2;
|
||||||
|
b2 = std::move(b1);
|
||||||
|
BEAST_EXPECT(b1.get_allocator() != b2.get_allocator());
|
||||||
|
BEAST_EXPECT(b1.size() == 0);
|
||||||
|
BEAST_EXPECT(b1.capacity() == 0);
|
||||||
|
BEAST_EXPECT(to_string(b2.data()) == "Hello");
|
||||||
|
}
|
||||||
|
{
|
||||||
|
// propagate_on_container_move_assignment : true
|
||||||
|
using pocma_t = test::test_allocator<char,
|
||||||
|
true, true, true, true, true>;
|
||||||
|
basic_flat_buffer<pocma_t> b1;
|
||||||
|
ostream(b1) << "Hello";
|
||||||
|
basic_flat_buffer<pocma_t> b2;
|
||||||
|
b2 = std::move(b1);
|
||||||
|
BEAST_EXPECT(b1.size() == 0);
|
||||||
|
BEAST_EXPECT(to_string(b2.data()) == "Hello");
|
||||||
|
}
|
||||||
|
{
|
||||||
|
// propagate_on_container_move_assignment : false
|
||||||
|
using pocma_t = test::test_allocator<char,
|
||||||
|
true, true, false, true, true>;
|
||||||
|
basic_flat_buffer<pocma_t> b1;
|
||||||
|
ostream(b1) << "Hello";
|
||||||
|
basic_flat_buffer<pocma_t> b2;
|
||||||
|
b2 = std::move(b1);
|
||||||
|
BEAST_EXPECT(b1.size() == 0);
|
||||||
|
BEAST_EXPECT(to_string(b2.data()) == "Hello");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// copy assignment
|
||||||
|
{
|
||||||
|
{
|
||||||
|
flat_buffer b1;
|
||||||
|
ostream(b1) << "Hello";
|
||||||
|
flat_buffer b2;
|
||||||
|
b2 = b1;
|
||||||
|
BEAST_EXPECT(to_string(b1.data()) == "Hello");
|
||||||
|
BEAST_EXPECT(to_string(b2.data()) == "Hello");
|
||||||
|
basic_flat_buffer<a_t> b3;
|
||||||
|
b3 = b2;
|
||||||
|
BEAST_EXPECT(to_string(b3.data()) == "Hello");
|
||||||
|
}
|
||||||
|
{
|
||||||
|
// propagate_on_container_copy_assignment : true
|
||||||
|
using pocca_t = test::test_allocator<char,
|
||||||
|
true, true, true, true, true>;
|
||||||
|
basic_flat_buffer<pocca_t> b1;
|
||||||
|
ostream(b1) << "Hello";
|
||||||
|
basic_flat_buffer<pocca_t> b2;
|
||||||
|
b2 = b1;
|
||||||
|
BEAST_EXPECT(to_string(b2.data()) == "Hello");
|
||||||
|
}
|
||||||
|
{
|
||||||
|
// propagate_on_container_copy_assignment : false
|
||||||
|
using pocca_t = test::test_allocator<char,
|
||||||
|
true, false, true, true, true>;
|
||||||
|
basic_flat_buffer<pocca_t> b1;
|
||||||
|
ostream(b1) << "Hello";
|
||||||
|
basic_flat_buffer<pocca_t> b2;
|
||||||
|
b2 = b1;
|
||||||
|
BEAST_EXPECT(to_string(b2.data()) == "Hello");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// operations
|
||||||
|
{
|
||||||
|
string_view const s = "Hello, world!";
|
||||||
|
flat_buffer b1{64};
|
||||||
BEAST_EXPECT(b1.size() == 0);
|
BEAST_EXPECT(b1.size() == 0);
|
||||||
|
BEAST_EXPECT(b1.max_size() == 64);
|
||||||
BEAST_EXPECT(b1.capacity() == 0);
|
BEAST_EXPECT(b1.capacity() == 0);
|
||||||
BEAST_EXPECT(b1.max_size() == 10);
|
ostream(b1) << s;
|
||||||
b1.prepare(1);
|
BEAST_EXPECT(to_string(b1.data()) == s);
|
||||||
b1.commit(1);
|
{
|
||||||
basic_flat_buffer<allocator> b2{std::move(b1)};
|
flat_buffer b2{b1};
|
||||||
BEAST_EXPECT(b1.capacity() == 0);
|
BEAST_EXPECT(to_string(b2.data()) == s);
|
||||||
BEAST_EXPECT(b1.max_size() == 10);
|
b2.consume(7);
|
||||||
BEAST_EXPECT(b2.size() == 1);
|
BEAST_EXPECT(to_string(b2.data()) == s.substr(7));
|
||||||
BEAST_EXPECT(b2.max_size() == 10);
|
}
|
||||||
BEAST_EXPECT(buffer_size(b1.data()) == 0);
|
{
|
||||||
BEAST_EXPECT(buffer_size(b1.prepare(1)) == 1);
|
flat_buffer b2{64};
|
||||||
|
b2 = b1;
|
||||||
|
BEAST_EXPECT(to_string(b2.data()) == s);
|
||||||
|
b2.consume(7);
|
||||||
|
BEAST_EXPECT(to_string(b2.data()) == s.substr(7));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
{
|
|
||||||
basic_flat_buffer<allocator> b1{10};
|
|
||||||
basic_flat_buffer<allocator> b2{std::move(b1), allocator{}};
|
|
||||||
}
|
|
||||||
{
|
|
||||||
basic_flat_buffer<allocator> b1{10};
|
|
||||||
basic_flat_buffer<allocator> b2{b1};
|
|
||||||
}
|
|
||||||
{
|
|
||||||
basic_flat_buffer<allocator> b1{10};
|
|
||||||
basic_flat_buffer<allocator> b2{b1, allocator{}};
|
|
||||||
}
|
|
||||||
{
|
|
||||||
flat_buffer b1{10};
|
|
||||||
b1.prepare(1);
|
|
||||||
b1.commit(1);
|
|
||||||
basic_flat_buffer<allocator> b2{b1};
|
|
||||||
BEAST_EXPECT(b2.size() == 1);
|
|
||||||
}
|
|
||||||
{
|
|
||||||
basic_flat_buffer<allocator> b1{10};
|
|
||||||
}
|
|
||||||
{
|
|
||||||
basic_flat_buffer<allocator> b1{10, allocator{}};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
// cause memmove
|
||||||
testCtors()
|
|
||||||
{
|
|
||||||
testCtor<false, false, false, false, false>();
|
|
||||||
testCtor<false, false, false, false, true>();
|
|
||||||
testCtor<false, false, false, true, false>();
|
|
||||||
testCtor<false, false, false, true, true>();
|
|
||||||
testCtor<false, false, true, false, false>();
|
|
||||||
testCtor<false, false, true, false, true>();
|
|
||||||
testCtor<false, false, true, true, false>();
|
|
||||||
testCtor<false, false, true, true, true>();
|
|
||||||
testCtor<false, true, false, false, false>();
|
|
||||||
testCtor<false, true, false, false, true>();
|
|
||||||
testCtor<false, true, false, true, false>();
|
|
||||||
testCtor<false, true, false, true, true>();
|
|
||||||
testCtor<false, true, true, false, false>();
|
|
||||||
testCtor<false, true, true, false, true>();
|
|
||||||
testCtor<false, true, true, true, false>();
|
|
||||||
testCtor<false, true, true, true, true>();
|
|
||||||
testCtor< true, false, false, false, false>();
|
|
||||||
testCtor< true, false, false, false, true>();
|
|
||||||
testCtor< true, false, false, true, false>();
|
|
||||||
testCtor< true, false, false, true, true>();
|
|
||||||
testCtor< true, false, true, false, false>();
|
|
||||||
testCtor< true, false, true, false, true>();
|
|
||||||
testCtor< true, false, true, true, false>();
|
|
||||||
testCtor< true, false, true, true, true>();
|
|
||||||
testCtor< true, true, false, false, false>();
|
|
||||||
testCtor< true, true, false, false, true>();
|
|
||||||
testCtor< true, true, false, true, false>();
|
|
||||||
testCtor< true, true, false, true, true>();
|
|
||||||
testCtor< true, true, true, false, false>();
|
|
||||||
testCtor< true, true, true, false, true>();
|
|
||||||
testCtor< true, true, true, true, false>();
|
|
||||||
testCtor< true, true, true, true, true>();
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
testOperations()
|
|
||||||
{
|
|
||||||
//
|
|
||||||
// reserve
|
|
||||||
//
|
|
||||||
|
|
||||||
{
|
|
||||||
flat_buffer b{10};
|
|
||||||
b.prepare(1);
|
|
||||||
b.commit(1);
|
|
||||||
b.reserve(2);
|
|
||||||
BEAST_EXPECT(b.size() == 1);
|
|
||||||
}
|
|
||||||
{
|
{
|
||||||
flat_buffer b{20};
|
flat_buffer b{20};
|
||||||
ostream(b) << "12345";
|
ostream(b) << "12345";
|
||||||
@ -149,132 +260,84 @@ public:
|
|||||||
ostream(b) << "67890123";
|
ostream(b) << "67890123";
|
||||||
BEAST_EXPECT(to_string(b.data()) == "4567890123");
|
BEAST_EXPECT(to_string(b.data()) == "4567890123");
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
void
|
// read_size_helper
|
||||||
testSpecialMembers()
|
|
||||||
{
|
|
||||||
using boost::asio::buffer;
|
|
||||||
using boost::asio::buffer_copy;
|
|
||||||
{
|
{
|
||||||
|
using detail::read_size_helper;
|
||||||
flat_buffer b{10};
|
flat_buffer b{10};
|
||||||
BEAST_EXPECT(b.max_size() == 10);
|
BEAST_EXPECT(read_size_helper(b, 512) == 10);
|
||||||
|
b.prepare(4);
|
||||||
|
b.commit(4);
|
||||||
|
BEAST_EXPECT(read_size_helper(b, 512) == 6);
|
||||||
|
b.consume(2);
|
||||||
|
BEAST_EXPECT(read_size_helper(b, 512) == 8);
|
||||||
|
b.prepare(8);
|
||||||
|
b.commit(8);
|
||||||
|
BEAST_EXPECT(read_size_helper(b, 512) == 0);
|
||||||
}
|
}
|
||||||
{
|
|
||||||
flat_buffer b{1024};
|
|
||||||
BEAST_EXPECT(b.max_size() == 1024);
|
|
||||||
}
|
|
||||||
std::string const s = "Hello, world!";
|
|
||||||
for(std::size_t i = 1; i < s.size() - 1; ++i)
|
|
||||||
{
|
|
||||||
flat_buffer b{1024};
|
|
||||||
b.commit(buffer_copy(
|
|
||||||
b.prepare(i), buffer(s)));
|
|
||||||
b.commit(buffer_copy(
|
|
||||||
b.prepare(s.size() - i),
|
|
||||||
buffer(s.data() + i, s.size() - i)));
|
|
||||||
BEAST_EXPECT(to_string(b.data()) == s);
|
|
||||||
{
|
|
||||||
flat_buffer b2{b};
|
|
||||||
BEAST_EXPECT(eq(b2, b));
|
|
||||||
flat_buffer b3{std::move(b2)};
|
|
||||||
BEAST_EXPECT(eq(b3, b));
|
|
||||||
BEAST_EXPECT(! eq(b2, b3));
|
|
||||||
BEAST_EXPECT(b2.size() == 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
using alloc_type = std::allocator<double>;
|
// swap
|
||||||
using type =
|
{
|
||||||
basic_flat_buffer<alloc_type>;
|
|
||||||
alloc_type alloc;
|
|
||||||
{
|
{
|
||||||
type fba{1, alloc};
|
basic_flat_buffer<a_neq_t> b1;
|
||||||
BEAST_EXPECT(fba.max_size() == 1);
|
ostream(b1) << "Hello";
|
||||||
|
basic_flat_buffer<a_neq_t> b2;
|
||||||
|
BEAST_EXPECT(b1.get_allocator() != b2.get_allocator());
|
||||||
|
swap(b1, b2);
|
||||||
|
BEAST_EXPECT(b1.get_allocator() != b2.get_allocator());
|
||||||
|
BEAST_EXPECT(b1.size() == 0);
|
||||||
|
BEAST_EXPECT(b1.capacity() == 0);
|
||||||
|
BEAST_EXPECT(to_string(b2.data()) == "Hello");
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
type fba{1024, alloc};
|
using na_t = test::test_allocator<char,
|
||||||
BEAST_EXPECT(fba.max_size() == 1024);
|
true, true, true, false, true>;
|
||||||
}
|
na_t a1;
|
||||||
{
|
basic_flat_buffer<na_t> b1{a1};
|
||||||
type fb2{b};
|
na_t a2;
|
||||||
BEAST_EXPECT(eq(fb2, b));
|
ostream(b1) << "Hello";
|
||||||
type fb3{std::move(fb2)};
|
basic_flat_buffer<na_t> b2{a2};
|
||||||
BEAST_EXPECT(eq(fb3, b));
|
BEAST_EXPECT(b1.get_allocator() == a1);
|
||||||
BEAST_EXPECT(! eq(fb2, fb3));
|
BEAST_EXPECT(b2.get_allocator() == a2);
|
||||||
BEAST_EXPECT(fb2.size() == 0);
|
swap(b1, b2);
|
||||||
}
|
BEAST_EXPECT(b1.get_allocator() == b2.get_allocator());
|
||||||
{
|
BEAST_EXPECT(b1.size() == 0);
|
||||||
type fb2{b, alloc};
|
BEAST_EXPECT(b1.capacity() == 0);
|
||||||
BEAST_EXPECT(eq(fb2, b));
|
BEAST_EXPECT(to_string(b2.data()) == "Hello");
|
||||||
type fb3{std::move(fb2), alloc};
|
|
||||||
BEAST_EXPECT(eq(fb3, b));
|
|
||||||
BEAST_EXPECT(! eq(fb2, fb3));
|
|
||||||
BEAST_EXPECT(fb2.size() == 0);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
void
|
// prepare
|
||||||
testStream()
|
|
||||||
{
|
|
||||||
using boost::asio::buffer_size;
|
|
||||||
|
|
||||||
flat_buffer b{100};
|
|
||||||
BEAST_EXPECT(b.size() == 0);
|
|
||||||
BEAST_EXPECT(b.capacity() == 0);
|
|
||||||
|
|
||||||
BEAST_EXPECT(buffer_size(b.prepare(100)) == 100);
|
|
||||||
BEAST_EXPECT(b.size() == 0);
|
|
||||||
BEAST_EXPECT(b.capacity() > 0);
|
|
||||||
|
|
||||||
b.commit(20);
|
|
||||||
BEAST_EXPECT(b.size() == 20);
|
|
||||||
BEAST_EXPECT(b.capacity() == 100);
|
|
||||||
|
|
||||||
b.consume(5);
|
|
||||||
BEAST_EXPECT(b.size() == 15);
|
|
||||||
BEAST_EXPECT(b.capacity() == 100);
|
|
||||||
|
|
||||||
b.prepare(80);
|
|
||||||
b.commit(80);
|
|
||||||
BEAST_EXPECT(b.size() == 95);
|
|
||||||
BEAST_EXPECT(b.capacity() == 100);
|
|
||||||
|
|
||||||
b.shrink_to_fit();
|
|
||||||
BEAST_EXPECT(b.size() == 95);
|
|
||||||
BEAST_EXPECT(b.capacity() == 95);
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
testPrepare()
|
|
||||||
{
|
|
||||||
flat_buffer b{100};
|
|
||||||
b.prepare(20);
|
|
||||||
BEAST_EXPECT(b.capacity() == 100);
|
|
||||||
b.commit(10);
|
|
||||||
BEAST_EXPECT(b.capacity() == 100);
|
|
||||||
b.consume(4);
|
|
||||||
BEAST_EXPECT(b.capacity() == 100);
|
|
||||||
b.prepare(14);
|
|
||||||
BEAST_EXPECT(b.size() == 6);
|
|
||||||
BEAST_EXPECT(b.capacity() == 100);
|
|
||||||
b.consume(10);
|
|
||||||
BEAST_EXPECT(b.size() == 0);
|
|
||||||
BEAST_EXPECT(b.capacity() == 100);
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
testMax()
|
|
||||||
{
|
|
||||||
flat_buffer b{1};
|
|
||||||
try
|
|
||||||
{
|
{
|
||||||
b.prepare(2);
|
flat_buffer b{100};
|
||||||
fail("", __FILE__, __LINE__);
|
b.prepare(10);
|
||||||
|
b.commit(10);
|
||||||
|
b.prepare(5);
|
||||||
|
BEAST_EXPECT(b.capacity() >= 5);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
b.prepare(1000);
|
||||||
|
fail("", __FILE__, __LINE__);
|
||||||
|
}
|
||||||
|
catch(std::length_error const&)
|
||||||
|
{
|
||||||
|
pass();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch(std::length_error const&)
|
|
||||||
|
// shrink to fit
|
||||||
{
|
{
|
||||||
pass();
|
flat_buffer b;
|
||||||
|
BEAST_EXPECT(b.capacity() == 0);
|
||||||
|
b.prepare(50);
|
||||||
|
BEAST_EXPECT(b.capacity() == 50);
|
||||||
|
b.commit(50);
|
||||||
|
BEAST_EXPECT(b.capacity() == 50);
|
||||||
|
b.prepare(75);
|
||||||
|
BEAST_EXPECT(b.capacity() >= 125);
|
||||||
|
b.shrink_to_fit();
|
||||||
|
BEAST_EXPECT(b.capacity() == b.size());
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -283,13 +346,7 @@ public:
|
|||||||
{
|
{
|
||||||
test::check_read_size_helper<flat_buffer>();
|
test::check_read_size_helper<flat_buffer>();
|
||||||
|
|
||||||
testCtors();
|
testBuffer();
|
||||||
testOperations();
|
|
||||||
|
|
||||||
testSpecialMembers();
|
|
||||||
testStream();
|
|
||||||
testPrepare();
|
|
||||||
testMax();
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user