Merge commit '68019a1fb20928beaa7b0cb2d8310af29ffe789e' as 'third_party/protozero'

This commit is contained in:
Michael Krasnyk
2018-04-19 22:03:49 +03:00
321 changed files with 26809 additions and 0 deletions
Binary file not shown.
+2
View File
@@ -0,0 +1,2 @@
x
+2
View File
@@ -0,0 +1,2 @@
foobar
@@ -0,0 +1,106 @@
#include <test.hpp>
TEST_CASE("read string field using get_string: empty") {
const std::string buffer = load_data("string/data-empty");
protozero::pbf_reader item{buffer};
REQUIRE(item.next());
REQUIRE(item.get_string().empty());
REQUIRE_FALSE(item.next());
}
TEST_CASE("read string field using get_string: one") {
const std::string buffer = load_data("string/data-one");
protozero::pbf_reader item{buffer};
REQUIRE(item.next());
REQUIRE(item.get_string() == "x");
REQUIRE_FALSE(item.next());
}
TEST_CASE("read string field using get_string: string") {
const std::string buffer = load_data("string/data-string");
protozero::pbf_reader item{buffer};
REQUIRE(item.next());
REQUIRE(item.get_string() == "foobar");
REQUIRE_FALSE(item.next());
}
TEST_CASE("read string field using get_string: end of buffer") {
const std::string buffer = load_data("string/data-string");
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_string(), const protozero::end_of_buffer_exception&);
}
}
TEST_CASE("read string field using get_view: empty") {
const std::string buffer = load_data("string/data-empty");
protozero::pbf_reader item{buffer};
REQUIRE(item.next());
const auto v = item.get_view();
REQUIRE(v.empty());
REQUIRE_FALSE(item.next());
}
TEST_CASE("read string field using get_view: one") {
const std::string buffer = load_data("string/data-one");
protozero::pbf_reader item{buffer};
REQUIRE(item.next());
const auto v = item.get_view();
REQUIRE(*v.data() == 'x');
REQUIRE(v.size() == 1);
REQUIRE_FALSE(item.next());
}
TEST_CASE("read string field using get_view: string") {
const std::string buffer = load_data("string/data-string");
protozero::pbf_reader item{buffer};
REQUIRE(item.next());
REQUIRE(std::string(item.get_view()) == "foobar");
REQUIRE_FALSE(item.next());
}
TEST_CASE("read string field using get_view: end of buffer") {
const std::string buffer = load_data("string/data-string");
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_view(), const protozero::end_of_buffer_exception&);
}
}
TEST_CASE("write string field") {
std::string buffer_test;
protozero::pbf_writer pbf_test{buffer_test};
SECTION("empty") {
pbf_test.add_string(1, "");
REQUIRE(buffer_test == load_data("string/data-empty"));
}
SECTION("one") {
pbf_test.add_string(1, "x");
REQUIRE(buffer_test == load_data("string/data-one"));
}
SECTION("string") {
pbf_test.add_string(1, "foobar");
REQUIRE(buffer_test == load_data("string/data-string"));
}
}
@@ -0,0 +1,11 @@
option optimize_for = LITE_RUNTIME;
package TestString;
message Test {
required string s = 1;
}
+17
View File
@@ -0,0 +1,17 @@
#include <testcase.hpp>
#include "testcase.pb.h"
int main(int c, char *argv[]) {
TestString::Test msg;
msg.set_s("");
write_to_file(msg, "data-empty.pbf");
msg.set_s("x");
write_to_file(msg, "data-one.pbf");
msg.set_s("foobar");
write_to_file(msg, "data-string.pbf");
}
@@ -0,0 +1,38 @@
#include <test.hpp>
#include "t/string/string_testcase.pb.h"
TEST_CASE("write string field and check with libprotobuf") {
std::string buffer;
protozero::pbf_writer pw{buffer};
TestString::Test msg;
SECTION("empty") {
pw.add_string(1, "");
msg.ParseFromString(buffer);
REQUIRE(msg.s().empty());
}
SECTION("one") {
pw.add_string(1, "x");
msg.ParseFromString(buffer);
REQUIRE(msg.s() == "x");
}
SECTION("string") {
pw.add_string(1, "foobar");
msg.ParseFromString(buffer);
REQUIRE(msg.s() == "foobar");
}
}