Updated bundled protozero to v1.7.0

This commit is contained in:
Denis Chaplygin
2020-10-16 10:25:52 +03:00
parent df3ed43d70
commit a8362d75b5
126 changed files with 18489 additions and 11079 deletions
+162
View File
@@ -0,0 +1,162 @@
#ifndef BUFFER_HPP
#define BUFFER_HPP
#include "test.hpp"
#include <protozero/buffer_fixed.hpp>
#include <protozero/buffer_string.hpp>
#include <protozero/buffer_vector.hpp>
// This "simulates" an externally defined buffer type to make sure our
// buffer adaptor functions do the right thing.
namespace test_external {
class ext_buffer : public std::string {
};
} // namespace test_external
namespace protozero {
template <>
struct buffer_customization<test_external::ext_buffer> {
static std::size_t size(const test_external::ext_buffer* buffer) noexcept {
return buffer->size();
}
static void append(test_external::ext_buffer* buffer, const char* data, std::size_t count) {
buffer->append(data, count);
}
static void append_zeros(test_external::ext_buffer* buffer, std::size_t count) {
buffer->append(count, '\0');
}
static void resize(test_external::ext_buffer* buffer, std::size_t size) {
protozero_assert(size < buffer->size());
buffer->resize(size);
}
static void reserve_additional(test_external::ext_buffer* buffer, std::size_t size) {
buffer->reserve(buffer->size() + size);
}
static void erase_range(test_external::ext_buffer* buffer, std::size_t from, std::size_t to) {
protozero_assert(from <= buffer->size());
protozero_assert(to <= buffer->size());
protozero_assert(from <= to);
buffer->erase(std::next(buffer->begin(), from), std::next(buffer->begin(), to));
}
static char* at_pos(test_external::ext_buffer* buffer, std::size_t pos) {
protozero_assert(pos <= buffer->size());
return (&*buffer->begin()) + pos;
}
static void push_back(test_external::ext_buffer* buffer, char ch) {
buffer->push_back(ch);
}
};
} // namespace protozero
// The following structs are used in many tests using TEMPLATE_TEST_CASE() to
// test the different buffer types:
//
// 1. Dynamically sized buffer based on std::string.
// 2. Dynamically sized buffer based on std::vector<char>.
// 3. Statically sized buffer based on std::array<char, N>.
// 4. Externally defined buffer.
class buffer_test_string {
std::string m_buffer;
public:
using type = std::string;
using writer_type = protozero::pbf_writer; // == protozero::basic_pbf_writer<type>;
type& buffer() noexcept {
return m_buffer;
}
const char *data() const noexcept {
return m_buffer.data();
}
std::size_t size() const noexcept {
return m_buffer.size();
}
}; // class buffer_test_string
class buffer_test_vector {
std::vector<char> m_buffer;
public:
using type = std::vector<char>;
using writer_type = protozero::basic_pbf_writer<type>;
type& buffer() noexcept {
return m_buffer;
}
const char *data() const noexcept {
return m_buffer.data();
}
std::size_t size() const noexcept {
return m_buffer.size();
}
}; // class buffer_test_vector
class buffer_test_array {
public:
using type = protozero::fixed_size_buffer_adaptor;
using writer_type = protozero::basic_pbf_writer<type>;
type& buffer() noexcept {
return adaptor;
}
const char *data() const noexcept {
return adaptor.data();
}
std::size_t size() const noexcept {
return adaptor.size();
}
private:
std::array<char, 1024> m_buffer = {{0}};
type adaptor{m_buffer};
}; // class buffer_test_array
class buffer_test_external {
test_external::ext_buffer m_buffer;
public:
using type = test_external::ext_buffer;
using writer_type = protozero::basic_pbf_writer<type>;
type& buffer() noexcept {
return m_buffer;
}
const char *data() const noexcept {
return m_buffer.data();
}
std::size_t size() const noexcept {
return m_buffer.size();
}
}; // class buffer_test_external
#endif // BUFFER_HPP
+18 -9
View File
@@ -1,5 +1,8 @@
// NOLINT(llvm-header-guard)
#include <array>
#include <sstream>
#define PBF_TYPE_NAME PROTOZERO_TEST_STRING(PBF_TYPE)
#define GET_TYPE PROTOZERO_TEST_CONCAT(get_packed_, PBF_TYPE)
#define ADD_TYPE PROTOZERO_TEST_CONCAT(add_packed_, PBF_TYPE)
@@ -92,7 +95,7 @@ TEST_CASE("read repeated packed field: " PBF_TYPE_NAME) {
for (std::string::size_type i = 1; i < abuffer.size() - n; ++i) {
protozero::pbf_reader item{abuffer.data() + n, i};
REQUIRE(item.next());
REQUIRE_THROWS_AS(item.GET_TYPE(), const protozero::end_of_buffer_exception&);
REQUIRE_THROWS_AS(item.GET_TYPE(), protozero::end_of_buffer_exception);
}
}
@@ -105,21 +108,27 @@ TEST_CASE("write repeated packed field: " PBF_TYPE_NAME) {
protozero::pbf_writer pw{buffer};
SECTION("empty") {
cpp_type data[] = { 17 };
std::array<cpp_type, 1> data = {{ 17 }};
pw.ADD_TYPE(1, std::begin(data), std::begin(data) /* !!!! */);
REQUIRE(buffer == load_data("repeated_packed_" PBF_TYPE_NAME "/data-empty"));
}
SECTION("one") {
cpp_type data[] = { 17 };
std::array<cpp_type, 1> data = {{ 17 }};
pw.ADD_TYPE(1, std::begin(data), std::end(data));
REQUIRE(buffer == load_data("repeated_packed_" PBF_TYPE_NAME "/data-one"));
}
SECTION("many") {
cpp_type data[] = {
std::array<cpp_type,
#if PBF_TYPE_IS_SIGNED
8
#else
5
#endif
> data = {{
17
, 200
, 0
@@ -130,7 +139,7 @@ TEST_CASE("write repeated packed field: " PBF_TYPE_NAME) {
, -1
,std::numeric_limits<cpp_type>::min()
#endif
};
}};
pw.ADD_TYPE(1, std::begin(data), std::end(data));
REQUIRE(buffer == load_data("repeated_packed_" PBF_TYPE_NAME "/data-many"));
@@ -246,9 +255,9 @@ TEST_CASE("write from different types of iterators: " PBF_TYPE_NAME) {
SECTION("from uint16_t") {
#if PBF_TYPE_IS_SIGNED
const int16_t data[] = { 1, 4, 9, 16, 25 };
const std::array< int16_t, 5> data = {{ 1, 4, 9, 16, 25 }};
#else
const uint16_t data[] = { 1, 4, 9, 16, 25 };
const std::array<uint16_t, 5> data = {{ 1, 4, 9, 16, 25 }};
#endif
pw.ADD_TYPE(1, std::begin(data), std::end(data));
@@ -290,7 +299,7 @@ TEST_CASE("write from different types of iterators: " PBF_TYPE_NAME) {
REQUIRE(std::distance(it_range.begin(), it_range.end()) == 0);
REQUIRE(it_range.size() == 0); // NOLINT(readability-container-size-empty)
REQUIRE_THROWS_AS(it_range.front(), const assert_error&);
REQUIRE_THROWS_AS(it_range.drop_front(), const assert_error&);
REQUIRE_THROWS_AS(it_range.front(), assert_error);
REQUIRE_THROWS_AS(it_range.drop_front(), assert_error);
}
+1 -1
View File
@@ -88,7 +88,7 @@ TEST_CASE("read field: " PBF_TYPE_NAME) {
for (std::string::size_type i = 1; i < buffer.size(); ++i) {
protozero::pbf_reader item{buffer.data(), i};
REQUIRE(item.next());
REQUIRE_THROWS_AS(item.GET_TYPE(), const protozero::end_of_buffer_exception&);
REQUIRE_THROWS_AS(item.GET_TYPE(), protozero::end_of_buffer_exception);
}
}
}
+3
View File
@@ -3,6 +3,9 @@
#include <catch.hpp>
#include <array>
#include <vector>
#include <stdexcept>
// Define protozero_assert() to throw this error. This allows the tests to
// check that the assert fails.