Refactor buffers_prefix:

* Tidy up tests
* Increase code coverage
* Correct behavior for default constructed iterators
This commit is contained in:
Vinnie Falco
2018-12-15 07:37:19 -08:00
parent 719fe66b77
commit 4cfe860b93
5 changed files with 89 additions and 177 deletions

View File

@@ -6,6 +6,7 @@ Version 200
* Refactor buffers_adaptor * Refactor buffers_adaptor
* Refactor buffers_range * Refactor buffers_range
* Fix and refactor buffers_cat * Fix and refactor buffers_cat
* Refactor buffers_prefix
API Changes: API Changes:

View File

@@ -11,8 +11,8 @@
#define BOOST_BEAST_BUFFERS_PREFIX_HPP #define BOOST_BEAST_BUFFERS_PREFIX_HPP
#include <boost/beast/core/detail/config.hpp> #include <boost/beast/core/detail/config.hpp>
#include <boost/beast/core/buffer_traits.hpp>
#include <boost/beast/core/type_traits.hpp> #include <boost/beast/core/type_traits.hpp>
#include <boost/asio/buffer.hpp>
#include <boost/optional/optional.hpp> // for in_place_init_t #include <boost/optional/optional.hpp> // for in_place_init_t
#include <algorithm> #include <algorithm>
#include <cstdint> #include <cstdint>
@@ -49,13 +49,19 @@ class buffers_prefix_view
std::size_t dist); std::size_t dist);
public: public:
/// The type for each element in the list of buffers. /** The type for each element in the list of buffers.
using value_type = typename std::conditional<
std::is_convertible<typename If the type of the underlying sequence is a mutable buffer
std::iterator_traits<iter_type>::value_type, sequence, then `value_type` is `net::mutable_buffer`. Otherwise,
net::mutable_buffer>::value, `value_type` is `net::const_buffer`.
net::mutable_buffer,
net::const_buffer>::type; @see buffers_type
*/
#if BOOST_BEAST_DOXYGEN
using value_type = __see_below__;
#else
using value_type = buffers_type<ConstBufferSequence>;
#endif
#if BOOST_BEAST_DOXYGEN #if BOOST_BEAST_DOXYGEN
/// A bidirectional iterator type that may be used to read elements. /// A bidirectional iterator type that may be used to read elements.

View File

@@ -26,7 +26,7 @@ class buffers_prefix_view<Buffers>::const_iterator
friend class buffers_prefix_view<Buffers>; friend class buffers_prefix_view<Buffers>;
buffers_prefix_view const* b_ = nullptr; buffers_prefix_view const* b_ = nullptr;
std::size_t remain_; std::size_t remain_ = 0;
iter_type it_; iter_type it_;
public: public:
@@ -49,20 +49,7 @@ public:
bool bool
operator==(const_iterator const& other) const operator==(const_iterator const& other) const
{ {
return return b_ == other.b_ && it_ == other.it_;
(b_ == nullptr) ?
(
other.b_ == nullptr ||
other.it_ == other.b_->end_
):(
(other.b_ == nullptr) ?
(
it_ == b_->end_
): (
b_ == other.b_ &&
it_ == other.it_
)
);
} }
bool bool
@@ -111,7 +98,8 @@ public:
} }
private: private:
const_iterator(buffers_prefix_view const& b, const_iterator(
buffers_prefix_view const& b,
std::true_type) std::true_type)
: b_(&b) : b_(&b)
, remain_(b.remain_) , remain_(b.remain_)
@@ -119,7 +107,8 @@ private:
{ {
} }
const_iterator(buffers_prefix_view const& b, const_iterator(
buffers_prefix_view const& b,
std::false_type) std::false_type)
: b_(&b) : b_(&b)
, remain_(b_->size_) , remain_(b_->size_)
@@ -222,18 +211,22 @@ buffers_prefix_view(
template<class Buffers> template<class Buffers>
auto auto
buffers_prefix_view<Buffers>::begin() const -> buffers_prefix_view<Buffers>::
begin() const ->
const_iterator const_iterator
{ {
return const_iterator{*this, std::false_type{}}; return const_iterator{
*this, std::false_type{}};
} }
template<class Buffers> template<class Buffers>
auto auto
buffers_prefix_view<Buffers>::end() const -> buffers_prefix_view<Buffers>::
end() const ->
const_iterator const_iterator
{ {
return const_iterator{*this, std::true_type{}}; return const_iterator{
*this, std::true_type{}};
} }
template<class Buffers> template<class Buffers>

View File

@@ -205,6 +205,7 @@ void test_mutable_buffers(
{ {
using net::buffer_size; using net::buffer_size;
string_view src = "Hello, world!"; string_view src = "Hello, world!";
BOOST_ASSERT(buffer_size(b) <= src.size());
if(src.size() > buffer_size(b)) if(src.size() > buffer_size(b))
src = {src.data(), buffer_size(b)}; src = {src.data(), buffer_size(b)};
net::buffer_copy(b, net::const_buffer( net::buffer_copy(b, net::const_buffer(

View File

@@ -10,11 +10,11 @@
// Test that header file is self-contained. // Test that header file is self-contained.
#include <boost/beast/core/buffers_prefix.hpp> #include <boost/beast/core/buffers_prefix.hpp>
#include <boost/beast/core/buffers_suffix.hpp> #include "buffer_test.hpp"
#include <boost/beast/core/buffers_to_string.hpp> #include <boost/beast/core/buffers_to_string.hpp>
#include <boost/beast/core/type_traits.hpp> #include <boost/beast/core/type_traits.hpp>
#include <boost/beast/_experimental/unit_test/suite.hpp> #include <boost/beast/_experimental/unit_test/suite.hpp>
#include <boost/asio/buffer.hpp>
#include <string> #include <string>
namespace boost { namespace boost {
@@ -23,109 +23,19 @@ namespace beast {
class buffers_prefix_test : public beast::unit_test::suite class buffers_prefix_test : public beast::unit_test::suite
{ {
public: public:
template<class ConstBufferSequence> void
static testBufferSequence()
std::size_t
bsize1(ConstBufferSequence const& bs)
{ {
std::size_t n = 0; char buf[13];
for(auto it = bs.begin(); it != bs.end(); ++it) auto const b =
n += net::buffer_size(*it); buffers_triple(buf, sizeof(buf));
return n; for(std::size_t i = 1; i <= sizeof(buf); ++i)
test_buffer_sequence(*this,
buffers_prefix(i, b));
} }
template<class ConstBufferSequence> void
static testInPlaceInit()
std::size_t
bsize2(ConstBufferSequence const& bs)
{
std::size_t n = 0;
for(auto it = bs.begin(); it != bs.end(); it++)
n += net::buffer_size(*it);
return n;
}
template<class ConstBufferSequence>
static
std::size_t
bsize3(ConstBufferSequence const& bs)
{
std::size_t n = 0;
for(auto it = bs.end(); it != bs.begin();)
n += net::buffer_size(*--it);
return n;
}
template<class ConstBufferSequence>
static
std::size_t
bsize4(ConstBufferSequence const& bs)
{
std::size_t n = 0;
for(auto it = bs.end(); it != bs.begin();)
{
it--;
n += net::buffer_size(*it);
}
return n;
}
template<class BufferType>
void testMatrix()
{
using net::buffer_size;
std::string s = "Hello, world";
BEAST_EXPECT(s.size() == 12);
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);
{
std::array<BufferType, 3> bs{{
BufferType{&s[0], x},
BufferType{&s[x], y},
BufferType{&s[x+y], z}}};
for(std::size_t i = 0; i <= s.size() + 1; ++i)
{
auto pb = buffers_prefix(i, bs);
BEAST_EXPECT(buffers_to_string(pb) == s.substr(0, i));
auto pb2 = pb;
BEAST_EXPECT(buffers_to_string(pb2) == buffers_to_string(pb));
pb = buffers_prefix(0, bs);
pb2 = pb;
BEAST_EXPECT(buffer_size(pb2) == 0);
pb2 = buffers_prefix(i, bs);
BEAST_EXPECT(buffers_to_string(pb2) == s.substr(0, i));
}
}
}}
}
void testEmptyBuffers()
{
using net::buffer_size;
auto pb0 = buffers_prefix(0, net::mutable_buffer{});
BEAST_EXPECT(buffer_size(pb0) == 0);
auto pb1 = buffers_prefix(1, net::mutable_buffer{});
BEAST_EXPECT(buffer_size(pb1) == 0);
BEAST_EXPECT(net::buffer_copy(pb0, pb1) == 0);
#if 0
using pb_type = decltype(pb0);
buffers_suffix<pb_type> cb(pb0);
BEAST_EXPECT(buffer_size(cb) == 0);
BEAST_EXPECT(net::buffer_copy(cb, pb1) == 0);
cb.consume(1);
BEAST_EXPECT(buffer_size(cb) == 0);
BEAST_EXPECT(net::buffer_copy(cb, pb1) == 0);
auto pbc = buffers_prefix(2, cb);
BEAST_EXPECT(buffer_size(pbc) == 0);
BEAST_EXPECT(net::buffer_copy(pbc, cb) == 0);
#endif
}
void testInPlaceInit()
{ {
{ {
class test_buffers class test_buffers
@@ -166,6 +76,7 @@ public:
2, boost::in_place_init, c, sizeof(c)); 2, boost::in_place_init, c, sizeof(c));
BEAST_EXPECT(buffer_size(v) == 2); BEAST_EXPECT(buffer_size(v) == 2);
} }
{ {
char c[2]; char c[2];
buffers_prefix_view<net::mutable_buffer> v( buffers_prefix_view<net::mutable_buffer> v(
@@ -174,56 +85,56 @@ public:
} }
} }
void testIterators() template<class BufferType>
void
testPrefixes()
{ {
char b[3]; using net::buffer_size;
std::array<net::const_buffer, 3> bs{{ std::string s = "Hello, world";
net::const_buffer{&b[0], 1}, BEAST_EXPECT(s.size() == 12);
net::const_buffer{&b[1], 1}, for(std::size_t x = 1; x < 4; ++x) {
net::const_buffer{&b[2], 1}}}; for(std::size_t y = 1; y < 4; ++y) {
auto pb = buffers_prefix(2, bs); {
BEAST_EXPECT(bsize1(pb) == 2); std::size_t z = s.size() - (x + y);
BEAST_EXPECT(bsize2(pb) == 2); std::array<BufferType, 3> bs{{
BEAST_EXPECT(bsize3(pb) == 2); BufferType{&s[0], x},
BEAST_EXPECT(bsize4(pb) == 2); BufferType{&s[x], y},
BufferType{&s[x+y], z}}};
for(std::size_t i = 0; i <= s.size() + 1; ++i)
{
auto pb = buffers_prefix(i, bs);
BEAST_EXPECT(buffers_to_string(pb) == s.substr(0, i));
auto pb2 = pb;
BEAST_EXPECT(buffers_to_string(pb2) == buffers_to_string(pb));
pb = buffers_prefix(0, bs);
pb2 = pb;
BEAST_EXPECT(buffer_size(pb2) == 0);
pb2 = buffers_prefix(i, bs);
BEAST_EXPECT(buffers_to_string(pb2) == s.substr(0, i));
}
}
} }
}
void testEmpty()
{
using net::buffer_size;
auto pb0 = buffers_prefix(0, net::mutable_buffer{});
BEAST_EXPECT(buffer_size(pb0) == 0);
auto pb1 = buffers_prefix(1, net::mutable_buffer{});
BEAST_EXPECT(buffer_size(pb1) == 0);
BEAST_EXPECT(net::buffer_copy(pb0, pb1) == 0);
} }
void void
testDefaultIterators() run() override
{ {
// default ctor is one past the end testBufferSequence();
char b[3];
std::array<net::const_buffer, 3> bs{{
net::const_buffer{&b[0], 1},
net::const_buffer{&b[1], 1},
net::const_buffer{&b[2], 1}}};
auto pb = buffers_prefix(2, bs);
decltype(pb)::const_iterator it;
BEAST_EXPECT(pb.end() == it);
BEAST_EXPECT(it == pb.end());
decltype(pb)::const_iterator it2;
BEAST_EXPECT(it == it2);
BEAST_EXPECT(it2 == it);
it = pb.end();
it2 = pb.end();
BEAST_EXPECT(it == it2);
BEAST_EXPECT(it2 == it);
decltype(pb)::const_iterator it3(it2);
BEAST_EXPECT(it3 == it2);
it = pb.begin();
BEAST_EXPECT(it != it3);
it = it3;
BEAST_EXPECT(it == it3);
}
void run() override
{
testMatrix<net::const_buffer>();
testMatrix<net::mutable_buffer>();
testEmptyBuffers();
testInPlaceInit(); testInPlaceInit();
testIterators(); testPrefixes<net::const_buffer>();
testDefaultIterators(); testPrefixes<net::mutable_buffer>();
testEmpty();
} }
}; };