Files
rippled/test/core/prepare_buffers.cpp
Vinnie Falco 2a8de0fd6b Parser concept, fixes:
A new concept Parser is introduced with routines to read from a stream
into the parser. This solves a problem with the old read interface where
messages must be default constructible and move assignable.

Parser fixes:

* Fix detect invalid reason-phrase octets
* Fix write_eof to set the 'complete' state on success
* Fix consider parse complete if eof received on empty body

WebSocket:

* Increase coverage
2016-05-12 19:20:57 -04:00

117 lines
3.3 KiB
C++

//
// 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)
//
// Test that header file is self-contained.
#include <beast/core/prepare_buffers.hpp>
#include <beast/core/consuming_buffers.hpp>
#include <beast/unit_test/suite.hpp>
#include <boost/asio/buffer.hpp>
#include <string>
namespace beast {
class prepare_buffers_test : public beast::unit_test::suite
{
public:
template<class ConstBufferSequence>
static
std::string
to_string(ConstBufferSequence const& bs)
{
using boost::asio::buffer_cast;
using boost::asio::buffer_size;
std::string s;
s.reserve(buffer_size(bs));
for(auto const& b : bs)
s.append(buffer_cast<char const*>(b),
buffer_size(b));
return s;
}
template<class BufferType>
void testMatrix()
{
using boost::asio::buffer_size;
std::string s = "Hello, world";
expect(s.size() == 12);
for(std::size_t x = 1; x < 4; ++x) {
for(std::size_t y = 1; y < 4; ++y) {
std::size_t z = s.size() - (x + y);
{
std::array<BufferType, 3> bs{{
BufferType{&s[0], x},
BufferType{&s[x], y},
BufferType{&s[x+y], z}}};
for(std::size_t i = 0; i <= s.size() + 1; ++i)
{
auto pb = prepare_buffers(i, bs);
expect(to_string(pb) == s.substr(0, i));
auto pb2 = pb;
expect(to_string(pb2) == to_string(pb));
pb = prepare_buffers(0, bs);
pb2 = pb;
expect(buffer_size(pb2) == 0);
pb2 = prepare_buffers(i, bs);
expect(to_string(pb2) == s.substr(0, i));
}
}
}}
}
void testNullBuffers()
{
using boost::asio::buffer_copy;
using boost::asio::buffer_size;
using boost::asio::null_buffers;
auto pb0 = prepare_buffers(0, null_buffers{});
expect(buffer_size(pb0) == 0);
auto pb1 = prepare_buffers(1, null_buffers{});
expect(buffer_size(pb1) == 0);
expect(buffer_copy(pb0, pb1) == 0);
using pb_type = decltype(pb0);
consuming_buffers<pb_type> cb(pb0);
expect(buffer_size(cb) == 0);
expect(buffer_copy(cb, pb1) == 0);
cb.consume(1);
expect(buffer_size(cb) == 0);
expect(buffer_copy(cb, pb1) == 0);
auto pbc = prepare_buffers(2, cb);
expect(buffer_size(pbc) == 0);
expect(buffer_copy(pbc, cb) == 0);
}
void testIterator()
{
using boost::asio::const_buffer;
char b[3];
std::array<const_buffer, 3> bs{{
const_buffer{&b[0], 1},
const_buffer{&b[1], 1},
const_buffer{&b[2], 1}}};
auto pb = prepare_buffers(2, bs);
std::size_t n = 0;
for(auto it = pb.end(); it != pb.begin(); --it)
++n;
expect(n == 2);
}
void run() override
{
testMatrix<boost::asio::const_buffer>();
testMatrix<boost::asio::mutable_buffer>();
testNullBuffers();
testIterator();
}
};
BEAST_DEFINE_TESTSUITE(prepare_buffers,core,beast);
} // beast