2017-07-15 20:10:31 -07:00
|
|
|
//
|
2019-02-21 07:00:31 -08:00
|
|
|
// Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
|
2017-07-15 20:10:31 -07:00
|
|
|
//
|
|
|
|
|
// 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
|
|
|
|
|
//
|
2017-07-15 20:10:31 -07:00
|
|
|
|
|
|
|
|
// Test that header file is self-contained.
|
2017-07-20 13:40:34 -07:00
|
|
|
#include <boost/beast/core/static_buffer.hpp>
|
2017-07-15 20:10:31 -07:00
|
|
|
|
2019-02-07 20:57:50 -08:00
|
|
|
#include "test_buffer.hpp"
|
2017-07-15 20:10:31 -07:00
|
|
|
|
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>
|
2018-12-16 11:30:34 -08:00
|
|
|
#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>
|
2018-11-29 12:57:29 -08:00
|
|
|
#include <boost/asio/buffers_iterator.hpp>
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
#include <cctype>
|
2017-07-15 20:10:31 -07:00
|
|
|
#include <string>
|
|
|
|
|
|
2017-07-20 13:40:34 -07:00
|
|
|
namespace boost {
|
2017-07-15 20:10:31 -07:00
|
|
|
namespace beast {
|
|
|
|
|
|
|
|
|
|
class static_buffer_test : public beast::unit_test::suite
|
|
|
|
|
{
|
|
|
|
|
public:
|
2018-11-29 12:57:29 -08:00
|
|
|
BOOST_STATIC_ASSERT(
|
2018-12-16 15:07:16 -08:00
|
|
|
is_mutable_dynamic_buffer<
|
|
|
|
|
static_buffer<13>>::value);
|
|
|
|
|
|
2018-11-29 12:57:29 -08:00
|
|
|
BOOST_STATIC_ASSERT(
|
2018-12-16 15:07:16 -08:00
|
|
|
is_mutable_dynamic_buffer<
|
|
|
|
|
static_buffer_base>::value);
|
|
|
|
|
|
2018-11-29 12:57:29 -08:00
|
|
|
void
|
2018-12-16 15:07:16 -08:00
|
|
|
testDynamicBuffer()
|
2018-11-29 12:57:29 -08:00
|
|
|
{
|
2019-01-16 06:39:10 -08:00
|
|
|
test_dynamic_buffer(static_buffer<13>{});
|
2018-11-29 12:57:29 -08:00
|
|
|
}
|
|
|
|
|
|
2017-07-15 20:10:31 -07:00
|
|
|
void
|
2018-12-16 15:07:16 -08:00
|
|
|
testMembers()
|
2017-07-15 20:10:31 -07:00
|
|
|
{
|
|
|
|
|
string_view const s = "Hello, world!";
|
|
|
|
|
|
|
|
|
|
// static_buffer_base
|
|
|
|
|
{
|
|
|
|
|
char buf[64];
|
|
|
|
|
static_buffer_base b{buf, sizeof(buf)};
|
|
|
|
|
ostream(b) << s;
|
2018-04-24 10:55:39 -07:00
|
|
|
BEAST_EXPECT(buffers_to_string(b.data()) == s);
|
2018-12-16 15:07:16 -08:00
|
|
|
b.clear();
|
|
|
|
|
BEAST_EXPECT(b.size() == 0);
|
2019-03-05 08:47:02 -08:00
|
|
|
BEAST_EXPECT(buffer_bytes(b.data()) == 0);
|
2017-07-15 20:10:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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);
|
2017-07-15 20:10:31 -07:00
|
|
|
ostream(b1) << s;
|
2018-04-24 10:55:39 -07:00
|
|
|
BEAST_EXPECT(buffers_to_string(b1.data()) == s);
|
2017-07-15 20:10:31 -07:00
|
|
|
{
|
|
|
|
|
static_buffer<64> b2{b1};
|
2018-04-24 10:55:39 -07:00
|
|
|
BEAST_EXPECT(buffers_to_string(b2.data()) == s);
|
2017-07-15 20:10:31 -07:00
|
|
|
b2.consume(7);
|
2018-04-24 10:55:39 -07:00
|
|
|
BEAST_EXPECT(buffers_to_string(b2.data()) == s.substr(7));
|
2017-07-15 20:10:31 -07:00
|
|
|
}
|
|
|
|
|
{
|
|
|
|
|
static_buffer<64> b2;
|
|
|
|
|
b2 = b1;
|
2018-04-24 10:55:39 -07:00
|
|
|
BEAST_EXPECT(buffers_to_string(b2.data()) == s);
|
2017-07-15 20:10:31 -07:00
|
|
|
b2.consume(7);
|
2018-04-24 10:55:39 -07:00
|
|
|
BEAST_EXPECT(buffers_to_string(b2.data()) == s.substr(7));
|
2017-07-15 20:10:31 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// cause memmove
|
|
|
|
|
{
|
|
|
|
|
static_buffer<10> b;
|
2018-12-15 18:48:32 -08:00
|
|
|
ostream(b) << "12345";
|
2017-07-15 20:10:31 -07:00
|
|
|
b.consume(3);
|
2018-12-15 18:48:32 -08:00
|
|
|
ostream(b) << "67890123";
|
2018-04-24 10:55:39 -07:00
|
|
|
BEAST_EXPECT(buffers_to_string(b.data()) == "4567890123");
|
2017-07-15 20:10:31 -07:00
|
|
|
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);
|
2017-07-15 20:10:31 -07:00
|
|
|
b.prepare(4);
|
|
|
|
|
b.commit(4);
|
2017-07-25 12:35:54 -07:00
|
|
|
BEAST_EXPECT(read_size(b, 512) == 6);
|
2017-07-15 20:10:31 -07:00
|
|
|
b.consume(2);
|
2017-07-25 12:35:54 -07:00
|
|
|
BEAST_EXPECT(read_size(b, 512) == 8);
|
2017-07-15 20:10:31 -07:00
|
|
|
b.prepare(8);
|
|
|
|
|
b.commit(8);
|
2017-07-25 12:35:54 -07:00
|
|
|
BEAST_EXPECT(read_size(b, 512) == 0);
|
2017-07-15 20:10:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// base
|
|
|
|
|
{
|
|
|
|
|
static_buffer<10> b;
|
|
|
|
|
[&](static_buffer_base& base)
|
|
|
|
|
{
|
2017-07-25 12:35:54 -07:00
|
|
|
BEAST_EXPECT(base.max_size() == b.capacity());
|
2017-07-15 20:10:31 -07:00
|
|
|
}
|
|
|
|
|
(b.base());
|
|
|
|
|
|
|
|
|
|
[&](static_buffer_base const& base)
|
|
|
|
|
{
|
2017-07-25 12:35:54 -07:00
|
|
|
BEAST_EXPECT(base.max_size() == b.capacity());
|
2017-07-15 20:10:31 -07:00
|
|
|
}
|
|
|
|
|
(b.base());
|
|
|
|
|
}
|
2018-12-16 15:07:16 -08:00
|
|
|
|
|
|
|
|
// 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());
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-07-15 20:10:31 -07:00
|
|
|
}
|
|
|
|
|
|
2018-12-16 15:07:16 -08:00
|
|
|
void
|
|
|
|
|
run() override
|
2017-07-15 20:10:31 -07:00
|
|
|
{
|
2018-12-16 15:07:16 -08:00
|
|
|
testDynamicBuffer();
|
|
|
|
|
testMembers();
|
2017-07-15 20:10:31 -07:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2017-08-01 17:01:57 -07:00
|
|
|
BEAST_DEFINE_TESTSUITE(beast,core,static_buffer);
|
2017-07-15 20:10:31 -07:00
|
|
|
|
|
|
|
|
} // beast
|
2017-07-20 13:40:34 -07:00
|
|
|
} // boost
|