Test self copy, self move for dynamic buffers:

* Swap
* Copy assignment to self
* Move assignment to self
This commit is contained in:
Vinnie Falco
2018-12-15 18:55:37 -08:00
parent 05d22802ec
commit 74293fb8a5
7 changed files with 71 additions and 32 deletions

View File

@@ -15,6 +15,7 @@ Version 200
* Tidy up flat_buffer tests
* Fix ostream prepare calculation for low limits
* Tidy up flat_static_buffer tests
* Add more tests for dynamic buffers
API Changes:

View File

@@ -246,7 +246,7 @@ protected:
private:
static
std::size_t
dist(char const* first, char const* last)
dist(char const* first, char const* last) noexcept
{
return static_cast<std::size_t>(last - first);
}

View File

@@ -113,7 +113,7 @@ class buffers_adaptor<MutableBufferSequence>::
readable_bytes<isMutable>::
const_iterator
{
iter_type it_;
iter_type it_{};
buffers_adaptor const* b_ = nullptr;
public:
@@ -259,7 +259,7 @@ template<class MutableBufferSequence>
class buffers_adaptor<MutableBufferSequence>::
mutable_buffers_type::const_iterator
{
iter_type it_;
iter_type it_{};
buffers_adaptor const* b_ = nullptr;
public:
@@ -442,6 +442,8 @@ buffers_adaptor<MutableBufferSequence>::
operator=(buffers_adaptor const& other) ->
buffers_adaptor&
{
if(this == &other)
return *this;
auto const nbegin = std::distance<iter_type>(
net::buffer_sequence_begin(other.bs_),
other.begin_);

View File

@@ -30,9 +30,10 @@ template<class Allocator>
basic_flat_buffer<Allocator>::
~basic_flat_buffer()
{
if(begin_)
alloc_traits::deallocate(
this->get(), begin_, capacity());
if(! begin_)
return;
alloc_traits::deallocate(
this->get(), begin_, capacity());
}
template<class Allocator>
@@ -124,24 +125,23 @@ basic_flat_buffer(
max_ = other.max_;
copy_from(other);
other.clear();
return;
}
else
{
begin_ = other.begin_;
in_ = other.in_;
out_ = other.out_;
last_ = other.out_; // invalidate
end_ = other.end_;
max_ = other.max_;
BOOST_ASSERT(
alloc_traits::max_size(this->get()) ==
alloc_traits::max_size(other.get()));
other.begin_ = nullptr;
other.in_ = nullptr;
other.out_ = nullptr;
other.last_ = nullptr;
other.end_ = nullptr;
}
begin_ = other.begin_;
in_ = other.in_;
out_ = other.out_;
last_ = other.out_; // invalidate
end_ = other.end_;
max_ = other.max_;
BOOST_ASSERT(
alloc_traits::max_size(this->get()) ==
alloc_traits::max_size(other.get()));
other.begin_ = nullptr;
other.in_ = nullptr;
other.out_ = nullptr;
other.last_ = nullptr;
other.end_ = nullptr;
}
template<class Allocator>
@@ -217,8 +217,9 @@ basic_flat_buffer<Allocator>::
operator=(basic_flat_buffer&& other) noexcept ->
basic_flat_buffer&
{
if(this != &other)
move_assign(other, pocma{});
if(this == &other)
return *this;
move_assign(other, pocma{});
return *this;
}
@@ -228,8 +229,9 @@ basic_flat_buffer<Allocator>::
operator=(basic_flat_buffer const& other) ->
basic_flat_buffer&
{
if(this != &other)
copy_assign(other, pocca{});
if(this == &other)
return *this;
copy_assign(other, pocca{});
return *this;
}

View File

@@ -102,6 +102,8 @@ operator=(flat_static_buffer const& other) ->
flat_static_buffer<N>&
{
using net::buffer_copy;
if(this == &other)
return *this;
this->consume(this->size());
this->commit(buffer_copy(
this->prepare(other.size()), other.data()));

View File

@@ -145,6 +145,8 @@ static_buffer<N>::
operator=(static_buffer const& other) noexcept ->
static_buffer<N>&
{
if(this == &other)
return *this;
this->consume(this->size());
this->commit(net::buffer_copy(
this->prepare(other.size()), other.data()));

View File

@@ -479,7 +479,7 @@ test_dynamic_buffer(
SUITE_EXPECT(test, b0.size() == 0);
SUITE_EXPECT(test, buffer_size(b0.data()) == 0);
// special members
// members
{
string_view src = "Hello, world!";
@@ -503,8 +503,8 @@ test_dynamic_buffer(
DynamicBuffer b3(std::move(b2));
SUITE_EXPECT(test, b3.size() == b1.size());
SUITE_EXPECT(test,
buffers_to_string(b1.data()) ==
buffers_to_string(b3.data()));
buffers_to_string(b3.data()) ==
buffers_to_string(b1.data()));
}
// copy assignment
@@ -515,6 +515,13 @@ test_dynamic_buffer(
SUITE_EXPECT(test,
buffers_to_string(b1.data()) ==
buffers_to_string(b2.data()));
// self assignment
b2 = b2;
SUITE_EXPECT(test, b2.size() == b1.size());
SUITE_EXPECT(test,
buffers_to_string(b2.data()) ==
buffers_to_string(b1.data()));
}
// move assignment
@@ -524,8 +531,30 @@ test_dynamic_buffer(
b3 = std::move(b2);
SUITE_EXPECT(test, b3.size() == b1.size());
SUITE_EXPECT(test,
buffers_to_string(b1.data()) ==
buffers_to_string(b3.data()));
buffers_to_string(b3.data()) ==
buffers_to_string(b1.data()));
// self move
b3 = std::move(b3);
SUITE_EXPECT(test, b3.size() == b1.size());
SUITE_EXPECT(test,
buffers_to_string(b3.data()) ==
buffers_to_string(b1.data()));
}
// swap
{
DynamicBuffer b2(b1);
DynamicBuffer b3(b0);
SUITE_EXPECT(test, b2.size() == b1.size());
SUITE_EXPECT(test, b3.size() == b0.size());
using std::swap;
swap(b2, b3);
SUITE_EXPECT(test, b2.size() == b0.size());
SUITE_EXPECT(test, b3.size() == b1.size());
SUITE_EXPECT(test,
buffers_to_string(b3.data()) ==
buffers_to_string(b1.data()));
}
}
@@ -571,6 +600,7 @@ test_dynamic_buffer(
}
}
// setup source buffer
char buf[13];
unsigned char k0 = 0;
string_view src(buf, sizeof(buf));