Files
boost_beast/test/core/static_buffer.cpp

239 lines
7.2 KiB
C++
Raw Normal View History

//
2017-07-24 09:42:36 -07:00
// Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
2017-07-20 13:40:34 -07:00
// Official repository: https://github.com/boostorg/beast
//
// Test that header file is self-contained.
2017-07-20 13:40:34 -07:00
#include <boost/beast/core/static_buffer.hpp>
#include "buffer_test.hpp"
2017-07-20 13:40:34 -07:00
#include <boost/beast/core/ostream.hpp>
#include <boost/beast/core/string.hpp>
#include <boost/beast/unit_test/suite.hpp>
#include <string>
2017-07-20 13:40:34 -07:00
namespace boost {
namespace beast {
static_assert(
is_dynamic_buffer<static_buffer_base>::value,
"DynamicBuffer requirements not met");
class static_buffer_test : public beast::unit_test::suite
{
public:
void
testStaticBuffer()
{
using namespace test;
using boost::asio::buffer;
using boost::asio::buffer_cast;
using boost::asio::buffer_size;
char buf[12];
std::string const s = "Hello, world";
2017-07-20 13:40:34 -07:00
BOOST_BEAST_EXPECT(s.size() == sizeof(buf));
for(std::size_t i = 1; i < 4; ++i) {
for(std::size_t j = 1; j < 4; ++j) {
for(std::size_t x = 1; x < 4; ++x) {
for(std::size_t y = 1; y < 4; ++y) {
for(std::size_t t = 1; t < 4; ++ t) {
for(std::size_t u = 1; u < 4; ++ u) {
std::size_t z = sizeof(buf) - (x + y);
std::size_t v = sizeof(buf) - (t + u);
{
std::memset(buf, 0, sizeof(buf));
static_buffer<sizeof(buf)> ba;
{
auto d = ba.prepare(z);
2017-07-20 13:40:34 -07:00
BOOST_BEAST_EXPECT(buffer_size(d) == z);
}
{
auto d = ba.prepare(0);
2017-07-20 13:40:34 -07:00
BOOST_BEAST_EXPECT(buffer_size(d) == 0);
}
{
auto d = ba.prepare(y);
2017-07-20 13:40:34 -07:00
BOOST_BEAST_EXPECT(buffer_size(d) == y);
}
{
auto d = ba.prepare(x);
2017-07-20 13:40:34 -07:00
BOOST_BEAST_EXPECT(buffer_size(d) == x);
ba.commit(buffer_copy(d, buffer(s.data(), x)));
}
2017-07-20 13:40:34 -07:00
BOOST_BEAST_EXPECT(ba.size() == x);
BOOST_BEAST_EXPECT(buffer_size(ba.data()) == ba.size());
{
auto d = ba.prepare(x);
2017-07-20 13:40:34 -07:00
BOOST_BEAST_EXPECT(buffer_size(d) == x);
}
{
auto d = ba.prepare(0);
2017-07-20 13:40:34 -07:00
BOOST_BEAST_EXPECT(buffer_size(d) == 0);
}
{
auto d = ba.prepare(z);
2017-07-20 13:40:34 -07:00
BOOST_BEAST_EXPECT(buffer_size(d) == z);
}
{
auto d = ba.prepare(y);
2017-07-20 13:40:34 -07:00
BOOST_BEAST_EXPECT(buffer_size(d) == y);
ba.commit(buffer_copy(d, buffer(s.data()+x, y)));
}
ba.commit(1);
2017-07-20 13:40:34 -07:00
BOOST_BEAST_EXPECT(ba.size() == x + y);
BOOST_BEAST_EXPECT(buffer_size(ba.data()) == ba.size());
{
auto d = ba.prepare(x);
2017-07-20 13:40:34 -07:00
BOOST_BEAST_EXPECT(buffer_size(d) == x);
}
{
auto d = ba.prepare(y);
2017-07-20 13:40:34 -07:00
BOOST_BEAST_EXPECT(buffer_size(d) == y);
}
{
auto d = ba.prepare(0);
2017-07-20 13:40:34 -07:00
BOOST_BEAST_EXPECT(buffer_size(d) == 0);
}
{
auto d = ba.prepare(z);
2017-07-20 13:40:34 -07:00
BOOST_BEAST_EXPECT(buffer_size(d) == z);
ba.commit(buffer_copy(d, buffer(s.data()+x+y, z)));
}
ba.commit(2);
2017-07-20 13:40:34 -07:00
BOOST_BEAST_EXPECT(buffer_size(ba.data()) == buffer_size(ba.mutable_data()));
BOOST_BEAST_EXPECT(ba.size() == x + y + z);
BOOST_BEAST_EXPECT(buffer_size(ba.data()) == ba.size());
BOOST_BEAST_EXPECT(to_string(ba.data()) == s);
ba.consume(t);
{
auto d = ba.prepare(0);
2017-07-20 13:40:34 -07:00
BOOST_BEAST_EXPECT(buffer_size(d) == 0);
}
2017-07-20 13:40:34 -07:00
BOOST_BEAST_EXPECT(to_string(ba.data()) == s.substr(t, std::string::npos));
ba.consume(u);
2017-07-20 13:40:34 -07:00
BOOST_BEAST_EXPECT(to_string(ba.data()) == s.substr(t + u, std::string::npos));
ba.consume(v);
2017-07-20 13:40:34 -07:00
BOOST_BEAST_EXPECT(to_string(ba.data()) == "");
ba.consume(1);
{
auto d = ba.prepare(0);
2017-07-20 13:40:34 -07:00
BOOST_BEAST_EXPECT(buffer_size(d) == 0);
}
try
{
ba.prepare(ba.capacity() - ba.size() + 1);
fail();
}
catch(...)
{
pass();
}
}
}}}}}}
}
void
testBuffer()
{
using namespace test;
string_view const s = "Hello, world!";
// static_buffer_base
{
char buf[64];
static_buffer_base b{buf, sizeof(buf)};
ostream(b) << s;
2017-07-20 13:40:34 -07:00
BOOST_BEAST_EXPECT(to_string(b.data()) == s);
b.consume(b.size());
2017-07-20 13:40:34 -07:00
BOOST_BEAST_EXPECT(to_string(b.data()) == "");
}
// static_buffer
{
static_buffer<64> b1;
2017-07-20 13:40:34 -07:00
BOOST_BEAST_EXPECT(b1.size() == 0);
BOOST_BEAST_EXPECT(b1.max_size() == 64);
BOOST_BEAST_EXPECT(b1.capacity() == 64);
ostream(b1) << s;
2017-07-20 13:40:34 -07:00
BOOST_BEAST_EXPECT(to_string(b1.data()) == s);
{
static_buffer<64> b2{b1};
2017-07-20 13:40:34 -07:00
BOOST_BEAST_EXPECT(to_string(b2.data()) == s);
b2.consume(7);
2017-07-20 13:40:34 -07:00
BOOST_BEAST_EXPECT(to_string(b2.data()) == s.substr(7));
}
{
static_buffer<64> b2;
b2 = b1;
2017-07-20 13:40:34 -07:00
BOOST_BEAST_EXPECT(to_string(b2.data()) == s);
b2.consume(7);
2017-07-20 13:40:34 -07:00
BOOST_BEAST_EXPECT(to_string(b2.data()) == s.substr(7));
}
}
// cause memmove
{
static_buffer<10> b;
write_buffer(b, "12345");
b.consume(3);
write_buffer(b, "67890123");
2017-07-20 13:40:34 -07:00
BOOST_BEAST_EXPECT(to_string(b.data()) == "4567890123");
try
{
b.prepare(1);
fail("", __FILE__, __LINE__);
}
catch(std::length_error const&)
{
pass();
}
}
// read_size
{
static_buffer<10> b;
2017-07-20 13:40:34 -07:00
BOOST_BEAST_EXPECT(read_size(b, 512) == 10);
b.prepare(4);
b.commit(4);
2017-07-20 13:40:34 -07:00
BOOST_BEAST_EXPECT(read_size(b, 512) == 6);
b.consume(2);
2017-07-20 13:40:34 -07:00
BOOST_BEAST_EXPECT(read_size(b, 512) == 8);
b.prepare(8);
b.commit(8);
2017-07-20 13:40:34 -07:00
BOOST_BEAST_EXPECT(read_size(b, 512) == 0);
}
// base
{
static_buffer<10> b;
[&](static_buffer_base& base)
{
2017-07-20 13:40:34 -07:00
BOOST_BEAST_EXPECT(base.max_size() == b.capacity());
}
(b.base());
[&](static_buffer_base const& base)
{
2017-07-20 13:40:34 -07:00
BOOST_BEAST_EXPECT(base.max_size() == b.capacity());
}
(b.base());
}
}
void run() override
{
testBuffer();
//testStaticBuffer();
}
};
2017-07-20 13:40:34 -07:00
BOOST_BEAST_DEFINE_TESTSUITE(static_buffer,core,beast);
} // beast
2017-07-20 13:40:34 -07:00
} // boost