Files
boost_beast/test/core/flat_buffer.cpp

356 lines
12 KiB
C++
Raw Normal View History

//
// 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/flat_buffer.hpp>
#include "buffer_test.hpp"
2017-06-09 22:36:57 -07:00
#include <beast/core/ostream.hpp>
2017-06-09 22:36:57 -07:00
#include <beast/core/string_view.hpp>
#include <beast/core/detail/read_size_helper.hpp>
2017-04-30 14:36:16 -07:00
#include <beast/test/test_allocator.hpp>
#include <beast/unit_test/suite.hpp>
#include <boost/lexical_cast.hpp>
2017-04-30 14:36:16 -07:00
#include <algorithm>
namespace beast {
static_assert(is_dynamic_buffer<flat_buffer>::value,
"DynamicBuffer requirements not met");
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;
BEAST_EXPECT(b.capacity() == 0);
}
{
flat_buffer b{500};
BEAST_EXPECT(b.capacity() == 0);
BEAST_EXPECT(b.max_size() == 500);
}
{
a_neq_t a1;
basic_flat_buffer<a_neq_t> b{a1};
BEAST_EXPECT(b.get_allocator() == a1);
a_neq_t a2;
BEAST_EXPECT(b.get_allocator() != a2);
}
{
a_neq_t a;
basic_flat_buffer<a_neq_t> b{500, a};
BEAST_EXPECT(b.capacity() == 0);
BEAST_EXPECT(b.max_size() == 500);
}
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};
BEAST_EXPECT(b1.get_allocator()->nmove == 0);
ostream(b1) << "Hello";
basic_flat_buffer<a_t> b2{std::move(b1)};
BEAST_EXPECT(b2.get_allocator()->nmove == 1);
BEAST_EXPECT(b1.size() == 0);
BEAST_EXPECT(b1.capacity() == 0);
BEAST_EXPECT(to_string(b2.data()) == "Hello");
BEAST_EXPECT(b1.max_size() == b2.max_size());
}
{
basic_flat_buffer<a_t> b1{30};
ostream(b1) << "Hello";
a_t a;
basic_flat_buffer<a_t> b2{std::move(b1), a};
BEAST_EXPECT(b1.size() == 0);
BEAST_EXPECT(b1.capacity() == 0);
BEAST_EXPECT(to_string(b2.data()) == "Hello");
BEAST_EXPECT(b1.max_size() == b2.max_size());
}
{
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};
BEAST_EXPECT(b1.size() == 0);
BEAST_EXPECT(b1.capacity() == 0);
BEAST_EXPECT(to_string(b2.data()) == "Hello");
BEAST_EXPECT(b1.max_size() == b2.max_size());
}
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);
BEAST_EXPECT(b1.get_allocator() == b2.get_allocator());
BEAST_EXPECT(to_string(b1.data()) == "Hello");
BEAST_EXPECT(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);
BEAST_EXPECT(b1.get_allocator() != b2.get_allocator());
BEAST_EXPECT(to_string(b1.data()) == "Hello");
BEAST_EXPECT(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(to_string(b1.data()) == "Hello");
BEAST_EXPECT(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);
BEAST_EXPECT(b2.get_allocator() == a);
BEAST_EXPECT(to_string(b1.data()) == "Hello");
BEAST_EXPECT(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);
BEAST_EXPECT(b1.size() == 0);
BEAST_EXPECT(b1.capacity() == 0);
BEAST_EXPECT(to_string(b2.data()) == "Hello");
}
{
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);
BEAST_EXPECT(b1.get_allocator() == b2.get_allocator());
BEAST_EXPECT(b1.size() == 0);
BEAST_EXPECT(b1.capacity() == 0);
BEAST_EXPECT(to_string(b2.data()) == "Hello");
}
{
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);
BEAST_EXPECT(b1.get_allocator() != b2.get_allocator());
BEAST_EXPECT(b1.size() == 0);
BEAST_EXPECT(b1.capacity() == 0);
BEAST_EXPECT(to_string(b2.data()) == "Hello");
}
{
// 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);
BEAST_EXPECT(b1.size() == 0);
BEAST_EXPECT(to_string(b2.data()) == "Hello");
}
{
// 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);
BEAST_EXPECT(b1.size() == 0);
BEAST_EXPECT(to_string(b2.data()) == "Hello");
}
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(to_string(b1.data()) == "Hello");
BEAST_EXPECT(to_string(b2.data()) == "Hello");
basic_flat_buffer<a_t> b3;
b3 = b2;
BEAST_EXPECT(to_string(b3.data()) == "Hello");
}
{
// 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(to_string(b2.data()) == "Hello");
}
{
// 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(to_string(b2.data()) == "Hello");
}
}
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};
BEAST_EXPECT(b1.size() == 0);
BEAST_EXPECT(b1.max_size() == 64);
BEAST_EXPECT(b1.capacity() == 0);
ostream(b1) << s;
BEAST_EXPECT(to_string(b1.data()) == s);
{
flat_buffer b2{b1};
BEAST_EXPECT(to_string(b2.data()) == s);
b2.consume(7);
BEAST_EXPECT(to_string(b2.data()) == s.substr(7));
}
{
flat_buffer b2{64};
b2 = b1;
BEAST_EXPECT(to_string(b2.data()) == s);
b2.consume(7);
BEAST_EXPECT(to_string(b2.data()) == s.substr(7));
}
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(to_string(b.data()) == "4567890123");
2017-04-30 14:36:16 -07:00
}
2017-06-09 22:36:57 -07:00
// read_size_helper
{
2017-06-09 22:36:57 -07:00
using detail::read_size_helper;
flat_buffer b{10};
2017-06-09 22:36:57 -07:00
BEAST_EXPECT(read_size_helper(b, 512) == 10);
b.prepare(4);
b.commit(4);
BEAST_EXPECT(read_size_helper(b, 512) == 6);
b.consume(2);
BEAST_EXPECT(read_size_helper(b, 512) == 8);
b.prepare(8);
b.commit(8);
BEAST_EXPECT(read_size_helper(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;
BEAST_EXPECT(b1.get_allocator() != b2.get_allocator());
swap(b1, b2);
BEAST_EXPECT(b1.get_allocator() != b2.get_allocator());
BEAST_EXPECT(b1.size() == 0);
BEAST_EXPECT(b1.capacity() == 0);
BEAST_EXPECT(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};
BEAST_EXPECT(b1.get_allocator() == a1);
BEAST_EXPECT(b2.get_allocator() == a2);
swap(b1, b2);
BEAST_EXPECT(b1.get_allocator() == b2.get_allocator());
BEAST_EXPECT(b1.size() == 0);
BEAST_EXPECT(b1.capacity() == 0);
BEAST_EXPECT(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);
BEAST_EXPECT(b.capacity() >= 5);
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;
BEAST_EXPECT(b.capacity() == 0);
b.prepare(50);
BEAST_EXPECT(b.capacity() == 50);
b.commit(50);
BEAST_EXPECT(b.capacity() == 50);
b.prepare(75);
BEAST_EXPECT(b.capacity() >= 125);
b.shrink_to_fit();
BEAST_EXPECT(b.capacity() == b.size());
}
}
void
run() override
{
test::check_read_size_helper<flat_buffer>();
2017-06-09 22:36:57 -07:00
testBuffer();
}
};
BEAST_DEFINE_TESTSUITE(flat_buffer,core,beast);
} // beast