Files
beast/test/static_streambuf.cpp
T

112 lines
3.7 KiB
C++
Raw Normal View History

2016-04-01 11:46:32 -04:00
//
// Copyright (c) 2013-2016 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)
//
2014-11-23 06:09:14 -08:00
2016-04-01 11:46:32 -04:00
// Test that header file is self-contained.
2016-04-19 20:29:16 -04:00
#include <beast/static_streambuf.hpp>
2014-11-23 06:09:14 -08:00
2016-04-19 20:29:16 -04:00
#include <beast/detail/unit_test/suite.hpp>
2016-04-01 11:46:32 -04:00
#include <boost/asio/buffer.hpp>
#include <string>
2014-11-23 06:09:14 -08:00
namespace beast {
2016-04-01 11:46:32 -04:00
namespace test {
2014-11-23 06:09:14 -08:00
2016-04-19 20:29:16 -04:00
class static_streambuf_test : public beast::detail::unit_test::suite
2014-11-23 06:09:14 -08:00
{
public:
2016-03-23 09:35:50 -04:00
template<class ConstBufferSequence>
2014-11-23 06:09:14 -08:00
static
std::string
2016-03-23 09:35:50 -04:00
to_string(ConstBufferSequence const& bs)
2014-11-23 06:09:14 -08:00
{
2016-04-01 11:46:32 -04:00
using boost::asio::buffer_cast;
using boost::asio::buffer_size;
2014-11-23 06:09:14 -08:00
std::string s;
2016-03-23 09:35:50 -04:00
s.reserve(buffer_size(bs));
for(auto const& b : bs)
s.append(buffer_cast<char const*>(b),
buffer_size(b));
2014-11-23 06:09:14 -08:00
return s;
}
2016-04-01 11:46:32 -04:00
void testStaticStreambuf()
2015-01-16 09:03:53 -08:00
{
2016-04-01 11:46:32 -04:00
using boost::asio::buffer;
using boost::asio::buffer_cast;
using boost::asio::buffer_size;
2016-03-23 09:35:50 -04:00
char buf[12];
std::string const s = "Hello, world";
expect(s.size() == sizeof(buf));
2016-04-01 11:46:32 -04:00
for(std::size_t i = 1; i < 4; ++i) {
for(std::size_t j = 1; j < 4; ++j) {
2016-03-23 09:35:50 -04:00
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);
2015-01-16 09:03:53 -08:00
{
2016-03-23 09:35:50 -04:00
std::memset(buf, 0, sizeof(buf));
2016-04-01 11:46:32 -04:00
static_streambuf_n<sizeof(buf)> ba;
2016-03-23 09:35:50 -04:00
decltype(ba)::mutable_buffers_type d;
d = ba.prepare(z); expect(buffer_size(d) == z);
d = ba.prepare(0); expect(buffer_size(d) == 0);
d = ba.prepare(y); expect(buffer_size(d) == y);
d = ba.prepare(x); expect(buffer_size(d) == x);
ba.commit(buffer_copy(d, buffer(s.data(), x)));
expect(ba.size() == x);
expect(buffer_size(ba.data()) == ba.size());
d = ba.prepare(x); expect(buffer_size(d) == x);
d = ba.prepare(0); expect(buffer_size(d) == 0);
d = ba.prepare(z); expect(buffer_size(d) == z);
d = ba.prepare(y); expect(buffer_size(d) == y);
ba.commit(buffer_copy(d, buffer(s.data()+x, y)));
ba.commit(1);
expect(ba.size() == x + y);
expect(buffer_size(ba.data()) == ba.size());
d = ba.prepare(x); expect(buffer_size(d) == x);
d = ba.prepare(y); expect(buffer_size(d) == y);
d = ba.prepare(0); expect(buffer_size(d) == 0);
d = ba.prepare(z); expect(buffer_size(d) == z);
ba.commit(buffer_copy(d, buffer(s.data()+x+y, z)));
ba.commit(2);
expect(ba.size() == x + y + z);
expect(buffer_size(ba.data()) == ba.size());
expect(to_string(ba.data()) == s);
ba.consume(t);
d = ba.prepare(0); expect(buffer_size(d) == 0);
expect(to_string(ba.data()) == s.substr(t, std::string::npos));
ba.consume(u);
expect(to_string(ba.data()) == s.substr(t + u, std::string::npos));
ba.consume(v);
expect(to_string(ba.data()) == "");
ba.consume(1);
d = ba.prepare(0); expect(buffer_size(d) == 0);
2016-04-01 11:46:32 -04:00
try
{
ba.prepare(1);
fail();
}
catch(...)
{
pass();
}
2015-01-16 09:03:53 -08:00
}
2016-04-01 11:46:32 -04:00
}}}}}}
2015-01-16 09:03:53 -08:00
}
2016-04-01 11:46:32 -04:00
void run() override
2014-11-23 06:09:14 -08:00
{
2016-04-01 11:46:32 -04:00
testStaticStreambuf();
2014-11-23 06:09:14 -08:00
}
};
2016-04-01 11:46:32 -04:00
BEAST_DEFINE_TESTSUITE(static_streambuf,asio,beast);
2014-11-23 06:09:14 -08:00
2016-04-01 11:46:32 -04:00
} // test
2016-04-19 20:29:16 -04:00
} // beastp