diff --git a/CHANGELOG.md b/CHANGELOG.md index 44ffd709..ad16645f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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: diff --git a/include/boost/beast/core/flat_static_buffer.hpp b/include/boost/beast/core/flat_static_buffer.hpp index d993181a..954080c9 100644 --- a/include/boost/beast/core/flat_static_buffer.hpp +++ b/include/boost/beast/core/flat_static_buffer.hpp @@ -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(last - first); } diff --git a/include/boost/beast/core/impl/buffers_adaptor.hpp b/include/boost/beast/core/impl/buffers_adaptor.hpp index 2ee0c274..a3fe1499 100644 --- a/include/boost/beast/core/impl/buffers_adaptor.hpp +++ b/include/boost/beast/core/impl/buffers_adaptor.hpp @@ -113,7 +113,7 @@ class buffers_adaptor:: readable_bytes:: const_iterator { - iter_type it_; + iter_type it_{}; buffers_adaptor const* b_ = nullptr; public: @@ -259,7 +259,7 @@ template class buffers_adaptor:: mutable_buffers_type::const_iterator { - iter_type it_; + iter_type it_{}; buffers_adaptor const* b_ = nullptr; public: @@ -442,6 +442,8 @@ buffers_adaptor:: operator=(buffers_adaptor const& other) -> buffers_adaptor& { + if(this == &other) + return *this; auto const nbegin = std::distance( net::buffer_sequence_begin(other.bs_), other.begin_); diff --git a/include/boost/beast/core/impl/flat_buffer.hpp b/include/boost/beast/core/impl/flat_buffer.hpp index 15e7246f..c1a0f401 100644 --- a/include/boost/beast/core/impl/flat_buffer.hpp +++ b/include/boost/beast/core/impl/flat_buffer.hpp @@ -30,9 +30,10 @@ template basic_flat_buffer:: ~basic_flat_buffer() { - if(begin_) - alloc_traits::deallocate( - this->get(), begin_, capacity()); + if(! begin_) + return; + alloc_traits::deallocate( + this->get(), begin_, capacity()); } template @@ -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 @@ -217,8 +217,9 @@ basic_flat_buffer:: 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:: 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; } diff --git a/include/boost/beast/core/impl/flat_static_buffer.hpp b/include/boost/beast/core/impl/flat_static_buffer.hpp index 80722aee..8c29dfd7 100644 --- a/include/boost/beast/core/impl/flat_static_buffer.hpp +++ b/include/boost/beast/core/impl/flat_static_buffer.hpp @@ -102,6 +102,8 @@ operator=(flat_static_buffer const& other) -> flat_static_buffer& { using net::buffer_copy; + if(this == &other) + return *this; this->consume(this->size()); this->commit(buffer_copy( this->prepare(other.size()), other.data())); diff --git a/include/boost/beast/core/impl/static_buffer.hpp b/include/boost/beast/core/impl/static_buffer.hpp index e272e37c..fbc2cfc9 100644 --- a/include/boost/beast/core/impl/static_buffer.hpp +++ b/include/boost/beast/core/impl/static_buffer.hpp @@ -145,6 +145,8 @@ static_buffer:: operator=(static_buffer const& other) noexcept -> static_buffer& { + if(this == &other) + return *this; this->consume(this->size()); this->commit(net::buffer_copy( this->prepare(other.size()), other.data())); diff --git a/test/beast/core/buffer_test.hpp b/test/beast/core/buffer_test.hpp index 896b02b9..574d30c6 100644 --- a/test/beast/core/buffer_test.hpp +++ b/test/beast/core/buffer_test.hpp @@ -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));