Files
boost_beast/test/beast/core/static_buffer.cpp

185 lines
5.1 KiB
C++
Raw Permalink Normal View History

//
2019-02-21 07:00:31 -08:00
// Copyright (c) 2016-2019 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>
2019-02-07 20:57:50 -08:00
#include "test_buffer.hpp"
2019-03-05 08:25:54 -08:00
#include <boost/beast/core/buffer_traits.hpp>
2017-07-20 13:40:34 -07:00
#include <boost/beast/core/ostream.hpp>
#include <boost/beast/core/read_size.hpp>
2017-07-20 13:40:34 -07:00
#include <boost/beast/core/string.hpp>
2018-11-11 14:07:55 -08:00
#include <boost/beast/_experimental/unit_test/suite.hpp>
#include <boost/asio/buffers_iterator.hpp>
#include <algorithm>
#include <cctype>
#include <string>
2017-07-20 13:40:34 -07:00
namespace boost {
namespace beast {
class static_buffer_test : public beast::unit_test::suite
{
public:
BOOST_STATIC_ASSERT(
is_mutable_dynamic_buffer<
static_buffer<13>>::value);
BOOST_STATIC_ASSERT(
is_mutable_dynamic_buffer<
static_buffer_base>::value);
void
testDynamicBuffer()
{
test_dynamic_buffer(static_buffer<13>{});
}
void
testMembers()
{
string_view const s = "Hello, world!";
// static_buffer_base
{
char buf[64];
static_buffer_base b{buf, sizeof(buf)};
ostream(b) << s;
BEAST_EXPECT(buffers_to_string(b.data()) == s);
b.clear();
BEAST_EXPECT(b.size() == 0);
2019-03-05 08:47:02 -08:00
BEAST_EXPECT(buffer_bytes(b.data()) == 0);
}
// static_buffer
{
static_buffer<64> b1;
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b1.size() == 0);
BEAST_EXPECT(b1.max_size() == 64);
BEAST_EXPECT(b1.capacity() == 64);
ostream(b1) << s;
BEAST_EXPECT(buffers_to_string(b1.data()) == s);
{
static_buffer<64> b2{b1};
BEAST_EXPECT(buffers_to_string(b2.data()) == s);
b2.consume(7);
BEAST_EXPECT(buffers_to_string(b2.data()) == s.substr(7));
}
{
static_buffer<64> b2;
b2 = b1;
BEAST_EXPECT(buffers_to_string(b2.data()) == s);
b2.consume(7);
BEAST_EXPECT(buffers_to_string(b2.data()) == s.substr(7));
}
}
// cause memmove
{
static_buffer<10> b;
ostream(b) << "12345";
b.consume(3);
ostream(b) << "67890123";
BEAST_EXPECT(buffers_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-25 12:35:54 -07:00
BEAST_EXPECT(read_size(b, 512) == 10);
b.prepare(4);
b.commit(4);
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(read_size(b, 512) == 6);
b.consume(2);
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(read_size(b, 512) == 8);
b.prepare(8);
b.commit(8);
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(read_size(b, 512) == 0);
}
// base
{
static_buffer<10> b;
[&](static_buffer_base& base)
{
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(base.max_size() == b.capacity());
}
(b.base());
[&](static_buffer_base const& base)
{
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(base.max_size() == b.capacity());
}
(b.base());
}
// This exercises the wrap-around cases
// for the circular buffer representation
{
static_buffer<5> b;
{
auto const mb = b.prepare(5);
BEAST_EXPECT(buffers_length(mb) == 1);
}
b.commit(4);
BEAST_EXPECT(buffers_length(b.data()) == 1);
BEAST_EXPECT(buffers_length(b.cdata()) == 1);
b.consume(3);
{
auto const mb = b.prepare(3);
BEAST_EXPECT(buffers_length(mb) == 2);
auto it1 = mb.begin();
auto it2 = std::next(it1);
BEAST_EXPECT(
net::const_buffer(*it1).data() >
net::const_buffer(*it2).data());
}
b.commit(2);
{
auto const mb = b.data();
auto it1 = mb.begin();
auto it2 = std::next(it1);
BEAST_EXPECT(
net::const_buffer(*it1).data() >
net::const_buffer(*it2).data());
}
{
auto const cb = b.cdata();
auto it1 = cb.begin();
auto it2 = std::next(it1);
BEAST_EXPECT(
net::const_buffer(*it1).data() >
net::const_buffer(*it2).data());
}
}
}
void
run() override
{
testDynamicBuffer();
testMembers();
}
};
2017-08-01 17:01:57 -07:00
BEAST_DEFINE_TESTSUITE(beast,core,static_buffer);
} // beast
2017-07-20 13:40:34 -07:00
} // boost