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

356 lines
12 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/flat_buffer.hpp>
#include "buffer_test.hpp"
2017-06-09 22:36:57 -07:00
2017-07-20 13:40:34 -07:00
#include <boost/beast/core/ostream.hpp>
#include <boost/beast/core/read_size.hpp>
#include <boost/beast/core/string.hpp>
#include <boost/beast/test/test_allocator.hpp>
2018-11-11 14:07:55 -08:00
#include <boost/beast/_experimental/unit_test/suite.hpp>
2017-04-30 14:36:16 -07:00
#include <algorithm>
2017-07-20 13:40:34 -07:00
namespace boost {
namespace beast {
BOOST_STATIC_ASSERT(
boost::asio::is_dynamic_buffer<flat_buffer>::value);
class flat_buffer_test : public beast::unit_test::suite
{
public:
2017-06-09 22:36:57 -07:00
void
testBuffer()
{
2017-06-09 22:36:57 -07:00
using namespace test;
2017-06-09 22:36:57 -07:00
using a_t = test::test_allocator<char,
true, true, true, true, true>;
2017-06-09 22:36:57 -07:00
// Equal == false
using a_neq_t = test::test_allocator<char,
false, true, true, true, true>;
// construction
2017-04-30 14:36:16 -07:00
{
2017-06-09 22:36:57 -07:00
{
flat_buffer b;
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b.capacity() == 0);
2017-06-09 22:36:57 -07:00
}
{
flat_buffer b{500};
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b.capacity() == 0);
BEAST_EXPECT(b.max_size() == 500);
2017-06-09 22:36:57 -07:00
}
{
a_neq_t a1;
basic_flat_buffer<a_neq_t> b{a1};
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b.get_allocator() == a1);
2017-06-09 22:36:57 -07:00
a_neq_t a2;
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b.get_allocator() != a2);
2017-06-09 22:36:57 -07:00
}
{
a_neq_t a;
basic_flat_buffer<a_neq_t> b{500, a};
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b.capacity() == 0);
BEAST_EXPECT(b.max_size() == 500);
2017-06-09 22:36:57 -07:00
}
2017-04-30 14:36:16 -07:00
}
2017-06-09 22:36:57 -07:00
// move construction
2017-04-30 14:36:16 -07:00
{
2017-06-09 22:36:57 -07:00
{
basic_flat_buffer<a_t> b1{30};
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b1.get_allocator()->nmove == 0);
2017-06-09 22:36:57 -07:00
ostream(b1) << "Hello";
basic_flat_buffer<a_t> b2{std::move(b1)};
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b2.get_allocator()->nmove == 1);
BEAST_EXPECT(b1.size() == 0);
BEAST_EXPECT(b1.capacity() == 0);
BEAST_EXPECT(buffers_to_string(b2.data()) == "Hello");
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b1.max_size() == b2.max_size());
2017-06-09 22:36:57 -07:00
}
{
basic_flat_buffer<a_t> b1{30};
ostream(b1) << "Hello";
a_t a;
basic_flat_buffer<a_t> b2{std::move(b1), a};
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b1.size() == 0);
BEAST_EXPECT(b1.capacity() == 0);
BEAST_EXPECT(buffers_to_string(b2.data()) == "Hello");
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b1.max_size() == b2.max_size());
2017-06-09 22:36:57 -07:00
}
{
basic_flat_buffer<a_neq_t> b1{30};
ostream(b1) << "Hello";
a_neq_t a;
basic_flat_buffer<a_neq_t> b2{std::move(b1), a};
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b1.size() == 0);
BEAST_EXPECT(b1.capacity() == 0);
BEAST_EXPECT(buffers_to_string(b2.data()) == "Hello");
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b1.max_size() == b2.max_size());
2017-06-09 22:36:57 -07:00
}
2017-04-30 14:36:16 -07:00
}
2017-06-09 22:36:57 -07:00
// copy construction
2017-04-30 14:36:16 -07:00
{
2017-06-09 22:36:57 -07:00
basic_flat_buffer<a_t> b1;
ostream(b1) << "Hello";
basic_flat_buffer<a_t> b2(b1);
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b1.get_allocator() == b2.get_allocator());
BEAST_EXPECT(buffers_to_string(b1.data()) == "Hello");
BEAST_EXPECT(buffers_to_string(b2.data()) == "Hello");
2017-04-30 14:36:16 -07:00
}
{
2017-06-09 22:36:57 -07:00
basic_flat_buffer<a_neq_t> b1;
ostream(b1) << "Hello";
a_neq_t a;
basic_flat_buffer<a_neq_t> b2(b1, a);
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b1.get_allocator() != b2.get_allocator());
BEAST_EXPECT(buffers_to_string(b1.data()) == "Hello");
BEAST_EXPECT(buffers_to_string(b2.data()) == "Hello");
2017-04-30 14:36:16 -07:00
}
{
2017-06-09 22:36:57 -07:00
basic_flat_buffer<a_t> b1;
ostream(b1) << "Hello";
basic_flat_buffer<a_neq_t> b2(b1);
BEAST_EXPECT(buffers_to_string(b1.data()) == "Hello");
BEAST_EXPECT(buffers_to_string(b2.data()) == "Hello");
2017-04-30 14:36:16 -07:00
}
{
2017-06-09 22:36:57 -07:00
basic_flat_buffer<a_neq_t> b1;
ostream(b1) << "Hello";
a_t a;
basic_flat_buffer<a_t> b2(b1, a);
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b2.get_allocator() == a);
BEAST_EXPECT(buffers_to_string(b1.data()) == "Hello");
BEAST_EXPECT(buffers_to_string(b2.data()) == "Hello");
2017-04-30 14:36:16 -07:00
}
2017-06-09 22:36:57 -07:00
// move assignment
2017-04-30 14:36:16 -07:00
{
2017-06-09 22:36:57 -07:00
{
flat_buffer b1;
ostream(b1) << "Hello";
flat_buffer b2;
b2 = std::move(b1);
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b1.size() == 0);
BEAST_EXPECT(b1.capacity() == 0);
BEAST_EXPECT(buffers_to_string(b2.data()) == "Hello");
2017-06-09 22:36:57 -07:00
}
{
using na_t = test::test_allocator<char,
true, true, false, true, true>;
basic_flat_buffer<na_t> b1;
ostream(b1) << "Hello";
basic_flat_buffer<na_t> b2;
b2 = std::move(b1);
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b1.get_allocator() == b2.get_allocator());
BEAST_EXPECT(b1.size() == 0);
BEAST_EXPECT(b1.capacity() == 0);
BEAST_EXPECT(buffers_to_string(b2.data()) == "Hello");
2017-06-09 22:36:57 -07:00
}
{
using na_t = test::test_allocator<char,
false, true, false, true, true>;
basic_flat_buffer<na_t> b1;
ostream(b1) << "Hello";
basic_flat_buffer<na_t> b2;
b2 = std::move(b1);
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b1.get_allocator() != b2.get_allocator());
BEAST_EXPECT(b1.size() == 0);
BEAST_EXPECT(b1.capacity() == 0);
BEAST_EXPECT(buffers_to_string(b2.data()) == "Hello");
2017-06-09 22:36:57 -07:00
}
{
// propagate_on_container_move_assignment : true
using pocma_t = test::test_allocator<char,
true, true, true, true, true>;
basic_flat_buffer<pocma_t> b1;
ostream(b1) << "Hello";
basic_flat_buffer<pocma_t> b2;
b2 = std::move(b1);
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b1.size() == 0);
BEAST_EXPECT(buffers_to_string(b2.data()) == "Hello");
2017-06-09 22:36:57 -07:00
}
{
// propagate_on_container_move_assignment : false
using pocma_t = test::test_allocator<char,
true, true, false, true, true>;
basic_flat_buffer<pocma_t> b1;
ostream(b1) << "Hello";
basic_flat_buffer<pocma_t> b2;
b2 = std::move(b1);
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b1.size() == 0);
BEAST_EXPECT(buffers_to_string(b2.data()) == "Hello");
2017-06-09 22:36:57 -07:00
}
2017-04-30 14:36:16 -07:00
}
2017-06-09 22:36:57 -07:00
// copy assignment
{
{
flat_buffer b1;
ostream(b1) << "Hello";
flat_buffer b2;
b2 = b1;
BEAST_EXPECT(buffers_to_string(b1.data()) == "Hello");
BEAST_EXPECT(buffers_to_string(b2.data()) == "Hello");
2017-06-09 22:36:57 -07:00
basic_flat_buffer<a_t> b3;
b3 = b2;
BEAST_EXPECT(buffers_to_string(b3.data()) == "Hello");
2017-06-09 22:36:57 -07:00
}
{
// propagate_on_container_copy_assignment : true
using pocca_t = test::test_allocator<char,
true, true, true, true, true>;
basic_flat_buffer<pocca_t> b1;
ostream(b1) << "Hello";
basic_flat_buffer<pocca_t> b2;
b2 = b1;
BEAST_EXPECT(buffers_to_string(b2.data()) == "Hello");
2017-06-09 22:36:57 -07:00
}
{
// propagate_on_container_copy_assignment : false
using pocca_t = test::test_allocator<char,
true, false, true, true, true>;
basic_flat_buffer<pocca_t> b1;
ostream(b1) << "Hello";
basic_flat_buffer<pocca_t> b2;
b2 = b1;
BEAST_EXPECT(buffers_to_string(b2.data()) == "Hello");
2017-06-09 22:36:57 -07:00
}
}
2017-04-30 14:36:16 -07:00
2017-06-09 22:36:57 -07:00
// operations
2017-04-30 14:36:16 -07:00
{
2017-06-09 22:36:57 -07:00
string_view const s = "Hello, world!";
flat_buffer b1{64};
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b1.size() == 0);
BEAST_EXPECT(b1.max_size() == 64);
BEAST_EXPECT(b1.capacity() == 0);
2017-06-09 22:36:57 -07:00
ostream(b1) << s;
BEAST_EXPECT(buffers_to_string(b1.data()) == s);
2017-06-09 22:36:57 -07:00
{
flat_buffer b2{b1};
BEAST_EXPECT(buffers_to_string(b2.data()) == s);
2017-06-09 22:36:57 -07:00
b2.consume(7);
BEAST_EXPECT(buffers_to_string(b2.data()) == s.substr(7));
2017-06-09 22:36:57 -07:00
}
{
flat_buffer b2{64};
b2 = b1;
BEAST_EXPECT(buffers_to_string(b2.data()) == s);
2017-06-09 22:36:57 -07:00
b2.consume(7);
BEAST_EXPECT(buffers_to_string(b2.data()) == s.substr(7));
2017-06-09 22:36:57 -07:00
}
2017-04-30 14:36:16 -07:00
}
2017-06-09 22:36:57 -07:00
// cause memmove
2017-04-30 14:36:16 -07:00
{
flat_buffer b{20};
ostream(b) << "12345";
b.consume(3);
ostream(b) << "67890123";
BEAST_EXPECT(buffers_to_string(b.data()) == "4567890123");
2017-04-30 14:36:16 -07:00
}
// read_size
{
flat_buffer b{10};
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(read_size(b, 512) == 10);
2017-06-09 22:36:57 -07:00
b.prepare(4);
b.commit(4);
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(read_size(b, 512) == 6);
2017-06-09 22:36:57 -07:00
b.consume(2);
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(read_size(b, 512) == 8);
2017-06-09 22:36:57 -07:00
b.prepare(8);
b.commit(8);
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(read_size(b, 512) == 0);
}
2017-06-09 22:36:57 -07:00
// swap
{
{
2017-06-09 22:36:57 -07:00
basic_flat_buffer<a_neq_t> b1;
ostream(b1) << "Hello";
basic_flat_buffer<a_neq_t> b2;
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b1.get_allocator() != b2.get_allocator());
2017-06-09 22:36:57 -07:00
swap(b1, b2);
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b1.get_allocator() != b2.get_allocator());
BEAST_EXPECT(b1.size() == 0);
BEAST_EXPECT(b1.capacity() == 0);
BEAST_EXPECT(buffers_to_string(b2.data()) == "Hello");
}
{
2017-06-09 22:36:57 -07:00
using na_t = test::test_allocator<char,
true, true, true, false, true>;
na_t a1;
basic_flat_buffer<na_t> b1{a1};
na_t a2;
ostream(b1) << "Hello";
basic_flat_buffer<na_t> b2{a2};
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b1.get_allocator() == a1);
BEAST_EXPECT(b2.get_allocator() == a2);
2017-06-09 22:36:57 -07:00
swap(b1, b2);
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b1.get_allocator() == b2.get_allocator());
BEAST_EXPECT(b1.size() == 0);
BEAST_EXPECT(b1.capacity() == 0);
BEAST_EXPECT(buffers_to_string(b2.data()) == "Hello");
}
2017-06-09 22:36:57 -07:00
}
// prepare
{
flat_buffer b{100};
b.prepare(10);
b.commit(10);
b.prepare(5);
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b.capacity() >= 5);
2017-06-09 22:36:57 -07:00
try
{
2017-06-09 22:36:57 -07:00
b.prepare(1000);
fail("", __FILE__, __LINE__);
}
2017-06-09 22:36:57 -07:00
catch(std::length_error const&)
{
2017-06-09 22:36:57 -07:00
pass();
}
}
2017-06-09 22:36:57 -07:00
// shrink to fit
{
2017-06-09 22:36:57 -07:00
flat_buffer b;
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b.capacity() == 0);
2017-06-09 22:36:57 -07:00
b.prepare(50);
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b.capacity() == 50);
2017-06-09 22:36:57 -07:00
b.commit(50);
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b.capacity() == 50);
2017-06-09 22:36:57 -07:00
b.prepare(75);
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b.capacity() >= 125);
2017-06-09 22:36:57 -07:00
b.shrink_to_fit();
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(b.capacity() == b.size());
2017-06-09 22:36:57 -07:00
}
}
void
run() override
{
2017-06-09 22:36:57 -07:00
testBuffer();
}
};
2017-08-01 17:01:57 -07:00
BEAST_DEFINE_TESTSUITE(beast,core,flat_buffer);
} // beast
2017-07-20 13:40:34 -07:00
} // boost