multi_buffer members and coverage

fix #458
This commit is contained in:
Vinnie Falco
2017-07-20 08:12:19 -07:00
parent 5d6d486da4
commit 260b506520
4 changed files with 721 additions and 280 deletions
+398 -59
View File
@@ -9,7 +9,9 @@
#include <beast/core/multi_buffer.hpp>
#include "buffer_test.hpp"
#include <beast/core/ostream.hpp>
#include <beast/core/string_view.hpp>
#include <beast/core/type_traits.hpp>
#include <beast/test/test_allocator.hpp>
#include <beast/unit_test/suite.hpp>
@@ -26,22 +28,14 @@ BOOST_STATIC_ASSERT(is_dynamic_buffer<multi_buffer>::value);
class multi_buffer_test : public beast::unit_test::suite
{
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_multi_buffer<Alloc1> const& sb1,
basic_multi_buffer<Alloc2> const& sb2)
eq(basic_multi_buffer<Alloc1> const& mb1,
basic_multi_buffer<Alloc2> const& mb2)
{
return to_string(sb1.data()) == to_string(sb2.data());
return test::to_string(mb1.data()) ==
test::to_string(mb2.data());
}
template<class ConstBufferSequence>
@@ -62,8 +56,10 @@ public:
u = std::forward<V>(v);
}
void testSpecialMembers()
void
testMatrix1()
{
using namespace test;
using boost::asio::buffer;
std::string const s = "Hello, world";
BEAST_EXPECT(s.size() == 12);
@@ -72,27 +68,27 @@ public:
for(std::size_t y = 1; y < 4; ++y) {
std::size_t z = s.size() - (x + y);
{
multi_buffer b;//(i);
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)));
BEAST_EXPECT(to_string(b.data()) == s);
{
multi_buffer sb2(b);
BEAST_EXPECT(eq(b, sb2));
multi_buffer mb2{b};
BEAST_EXPECT(eq(b, mb2));
}
{
multi_buffer sb2;
sb2 = b;
BEAST_EXPECT(eq(b, sb2));
multi_buffer mb2;
mb2 = b;
BEAST_EXPECT(eq(b, mb2));
}
{
multi_buffer sb2(std::move(b));
BEAST_EXPECT(to_string(sb2.data()) == s);
multi_buffer mb2{std::move(b)};
BEAST_EXPECT(to_string(mb2.data()) == s);
expect_size(0, b.data());
b = std::move(sb2);
b = std::move(mb2);
BEAST_EXPECT(to_string(b.data()) == s);
expect_size(0, sb2.data());
expect_size(0, mb2.data());
}
self_assign(b, b);
BEAST_EXPECT(to_string(b.data()) == s);
@@ -100,40 +96,12 @@ public:
BEAST_EXPECT(to_string(b.data()) == s);
}
}}}
try
{
multi_buffer sb0(0);
fail();
}
catch(std::exception const&)
{
pass();
}
}
void
testAllocator()
{
using test::test_allocator;
// VFALCO This needs work
{
using alloc_type =
test_allocator<char, false, false, false, false, false>;
using type = basic_multi_buffer<alloc_type>;
type b;
}
{
using alloc_type =
test_allocator<char, false, false, false, false, false>;
using type = basic_multi_buffer<alloc_type>;
type b;
type b2(b);
type b3(b, alloc_type{});
}
}
void testMatrix()
testMatrix2()
{
using namespace test;
using boost::asio::buffer;
using boost::asio::buffer_size;
std::string const s = "Hello, world";
@@ -146,7 +114,7 @@ public:
std::size_t z = s.size() - (x + y);
std::size_t v = s.size() - (t + u);
{
multi_buffer b;//(i);
multi_buffer b;
{
auto d = b.prepare(z);
BEAST_EXPECT(buffer_size(d) == z);
@@ -226,7 +194,8 @@ public:
}}}}}
}
void testIterators()
void
testIterators()
{
using boost::asio::buffer_size;
multi_buffer b;
@@ -240,14 +209,384 @@ public:
b.commit(2);
}
void run() override
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;
BEAST_EXPECT(b.capacity() == 0);
}
{
multi_buffer b{500};
BEAST_EXPECT(b.capacity() == 0);
BEAST_EXPECT(b.max_size() == 500);
}
{
unequal_t a1;
basic_multi_buffer<unequal_t> b{a1};
BEAST_EXPECT(b.get_allocator() == a1);
BEAST_EXPECT(b.get_allocator() != unequal_t{});
}
}
// move construction
{
{
basic_multi_buffer<equal_t> b1{30};
BEAST_EXPECT(b1.get_allocator()->nmove == 0);
ostream(b1) << "Hello";
basic_multi_buffer<equal_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());
}
// 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};
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());
}
{
// 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};
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_multi_buffer<equal_t> b1;
ostream(b1) << "Hello";
basic_multi_buffer<equal_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_multi_buffer<unequal_t> b1;
ostream(b1) << "Hello";
unequal_t a;
basic_multi_buffer<unequal_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_multi_buffer<equal_t> b1;
ostream(b1) << "Hello";
basic_multi_buffer<unequal_t> b2(b1);
BEAST_EXPECT(to_string(b1.data()) == "Hello");
BEAST_EXPECT(to_string(b2.data()) == "Hello");
}
{
basic_multi_buffer<unequal_t> b1;
ostream(b1) << "Hello";
equal_t a;
basic_multi_buffer<equal_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
{
{
multi_buffer b1;
ostream(b1) << "Hello";
multi_buffer b2;
b2 = std::move(b1);
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_multi_buffer<pocma_t> b1;
ostream(b1) << "Hello";
basic_multi_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_multi_buffer<pocma_t> b1;
ostream(b1) << "Hello";
basic_multi_buffer<pocma_t> b2;
b2 = std::move(b1);
BEAST_EXPECT(b1.size() == 0);
BEAST_EXPECT(to_string(b2.data()) == "Hello");
}
}
// copy assignment
{
{
multi_buffer b1;
ostream(b1) << "Hello";
multi_buffer b2;
b2 = b1;
BEAST_EXPECT(to_string(b1.data()) == "Hello");
BEAST_EXPECT(to_string(b2.data()) == "Hello");
basic_multi_buffer<equal_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_multi_buffer<pocca_t> b1;
ostream(b1) << "Hello";
basic_multi_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_multi_buffer<pocca_t> b1;
ostream(b1) << "Hello";
basic_multi_buffer<pocca_t> b2;
b2 = b1;
BEAST_EXPECT(to_string(b2.data()) == "Hello");
}
}
// 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};
BEAST_EXPECT(b1.size() == 0);
BEAST_EXPECT(b1.max_size() == 64);
BEAST_EXPECT(b1.capacity() == 0);
ostream(b1) << s;
BEAST_EXPECT(to_string(b1.data()) == s);
{
multi_buffer b2{b1};
BEAST_EXPECT(to_string(b2.data()) == s);
b2.consume(7);
BEAST_EXPECT(to_string(b2.data()) == s.substr(7));
}
{
multi_buffer b2{64};
b2 = b1;
BEAST_EXPECT(to_string(b2.data()) == s);
b2.consume(7);
BEAST_EXPECT(to_string(b2.data()) == s.substr(7));
}
}
{
multi_buffer b;
b.prepare(1000);
BEAST_EXPECT(b.capacity() >= 1000);
b.commit(1);
BEAST_EXPECT(b.size() == 1);
BEAST_EXPECT(b.capacity() >= 1000);
b.prepare(1000);
BEAST_EXPECT(b.size() == 1);
BEAST_EXPECT(b.capacity() >= 1000);
b.prepare(1500);
BEAST_EXPECT(b.capacity() >= 1000);
}
{
multi_buffer b;
b.prepare(1000);
BEAST_EXPECT(b.capacity() >= 1000);
b.commit(1);
BEAST_EXPECT(b.capacity() >= 1000);
b.prepare(1000);
BEAST_EXPECT(b.capacity() >= 1000);
b.prepare(2000);
BEAST_EXPECT(b.capacity() >= 2000);
b.commit(2);
}
{
multi_buffer b;
b.prepare(1000);
BEAST_EXPECT(b.capacity() >= 1000);
b.prepare(2000);
BEAST_EXPECT(b.capacity() >= 2000);
b.prepare(4000);
BEAST_EXPECT(b.capacity() >= 4000);
b.prepare(50);
BEAST_EXPECT(b.capacity() >= 50);
}
}
// commit
{
multi_buffer b;
b.prepare(1000);
BEAST_EXPECT(b.capacity() >= 1000);
b.commit(1000);
BEAST_EXPECT(b.size() == 1000);
BEAST_EXPECT(b.capacity() >= 1000);
b.consume(1000);
BEAST_EXPECT(b.size() == 0);
BEAST_EXPECT(b.capacity() == 0);
b.prepare(1000);
b.commit(650);
BEAST_EXPECT(b.size() == 650);
BEAST_EXPECT(b.capacity() >= 1000);
b.prepare(1000);
BEAST_EXPECT(b.capacity() >= 1650);
b.commit(100);
BEAST_EXPECT(b.size() == 750);
BEAST_EXPECT(b.capacity() >= 1000);
b.prepare(1000);
BEAST_EXPECT(b.capacity() >= 2000);
b.commit(500);
}
// consume
{
multi_buffer b;
b.prepare(1000);
BEAST_EXPECT(b.capacity() >= 1000);
b.commit(1000);
BEAST_EXPECT(b.size() == 1000);
BEAST_EXPECT(b.capacity() >= 1000);
b.prepare(1000);
BEAST_EXPECT(b.capacity() >= 2000);
b.commit(750);
BEAST_EXPECT(b.size() == 1750);
b.consume(500);
BEAST_EXPECT(b.size() == 1250);
b.consume(500);
BEAST_EXPECT(b.size() == 750);
b.prepare(250);
b.consume(750);
BEAST_EXPECT(b.size() == 0);
b.prepare(1000);
b.commit(800);
BEAST_EXPECT(b.size() == 800);
b.prepare(1000);
b.commit(600);
BEAST_EXPECT(b.size() == 1400);
b.consume(1400);
BEAST_EXPECT(b.size() == 0);
}
// swap
{
{
// propagate_on_container_swap : true
using pocs_t = test::test_allocator<char,
false, true, true, true, true>;
pocs_t a1, a2;
BEAST_EXPECT(a1 != a2);
basic_multi_buffer<pocs_t> b1{a1};
ostream(b1) << "Hello";
basic_multi_buffer<pocs_t> b2{a2};
BEAST_EXPECT(b1.get_allocator() == a1);
BEAST_EXPECT(b2.get_allocator() == a2);
swap(b1, b2);
BEAST_EXPECT(b1.get_allocator() == a2);
BEAST_EXPECT(b2.get_allocator() == a1);
BEAST_EXPECT(b1.size() == 0);
BEAST_EXPECT(to_string(b2.data()) == "Hello");
swap(b1, b2);
BEAST_EXPECT(b1.get_allocator() == a1);
BEAST_EXPECT(b2.get_allocator() == a2);
BEAST_EXPECT(to_string(b1.data()) == "Hello");
BEAST_EXPECT(b2.size() == 0);
}
{
// propagate_on_container_swap : false
using pocs_t = test::test_allocator<char,
true, true, true, false, true>;
pocs_t a1, a2;
BEAST_EXPECT(a1 == a2);
BEAST_EXPECT(a1.id() != a2.id());
basic_multi_buffer<pocs_t> b1{a1};
ostream(b1) << "Hello";
basic_multi_buffer<pocs_t> b2{a2};
BEAST_EXPECT(b1.get_allocator() == a1);
BEAST_EXPECT(b2.get_allocator() == a2);
swap(b1, b2);
BEAST_EXPECT(b1.get_allocator().id() == a1.id());
BEAST_EXPECT(b2.get_allocator().id() == a2.id());
BEAST_EXPECT(b1.size() == 0);
BEAST_EXPECT(to_string(b2.data()) == "Hello");
swap(b1, b2);
BEAST_EXPECT(b1.get_allocator().id() == a1.id());
BEAST_EXPECT(b2.get_allocator().id() == a2.id());
BEAST_EXPECT(to_string(b1.data()) == "Hello");
BEAST_EXPECT(b2.size() == 0);
}
}
// read_size_helper
{
using detail::read_size_helper;
multi_buffer b{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);
}
}
void
run() override
{
test::check_read_size_helper<multi_buffer>();
testSpecialMembers();
testAllocator();
testMatrix();
testMatrix1();
testMatrix2();
testIterators();
testMembers();
}
};