Chromium Code Reviews| Index: components/tracing/test/proto_zero_generation_unittest.cc |
| diff --git a/components/tracing/test/proto_zero_generation_unittest.cc b/components/tracing/test/proto_zero_generation_unittest.cc |
| index eb6e8f759ff918ec143a4340cc002f94062fd9a4..bdf76cc74b81316a6eef9993997273be4dda3189 100644 |
| --- a/components/tracing/test/proto_zero_generation_unittest.cc |
| +++ b/components/tracing/test/proto_zero_generation_unittest.cc |
| @@ -2,21 +2,158 @@ |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| +#include <limits> |
| +#include <memory> |
| +#include <vector> |
| + |
| +#include "components/tracing/core/proto_zero_message_handle.h" |
| #include "components/tracing/test/example_proto/library.pbzero.h" |
| +#include "components/tracing/test/example_proto/test_messages.pb.h" |
| #include "components/tracing/test/example_proto/test_messages.pbzero.h" |
| +#include "components/tracing/test/fake_scattered_buffer.h" |
| #include "testing/gtest/include/gtest/gtest.h" |
| namespace tracing { |
| namespace proto { |
| -using foo::bar::CamelCaseFields; |
| +using namespace pbzero::foo::bar; |
|
Primiano Tucci (use gerrit)
2016/08/25 10:31:51
probably a bit more readable if you wrap this into
|
| +namespace pbgold = foo::bar; // Official C++ protobuf compiler. |
| -TEST(ProtoZeroTest, Simple) { |
| - // TODO(kraynov) Put tests in the next CL (crbug.com/608721). |
| +using tracing::v2::FakeScatteredBuffer; |
| +using tracing::v2::ProtoZeroMessage; |
| +using tracing::v2::ScatteredStreamWriter; |
| +using tracing::v2::ProtoZeroMessageHandle; |
| +using tracing::v2::ProtoZeroMessageHandleBase; |
| + |
| +constexpr size_t kChunkSize = 42; |
| + |
| +class ProtoZeroConformanceTest : public ::testing::Test { |
| + public: |
| + void SetUp() override { |
| + buffer_.reset(new FakeScatteredBuffer(kChunkSize)); |
| + stream_writer_.reset(new ScatteredStreamWriter(buffer_.get())); |
| + } |
| + |
| + void TearDown() override { |
| + root_messages_.clear(); |
| + stream_writer_.reset(); |
| + buffer_.reset(); |
| + google::protobuf::ShutdownProtobufLibrary(); |
| + } |
| + |
| + protected: |
| + template <class T> |
| + void CreateMessage(T** message_ptr) { |
|
Primiano Tucci (use gerrit)
2016/08/25 10:31:51
This signature is a bit odd. Why don't you return
|
| + *message_ptr = new T(); |
| + root_messages_.push_back(std::unique_ptr<T>(*message_ptr)); |
| + (*message_ptr)->Reset(stream_writer_.get()); |
| + } |
| + |
| + size_t GetNumSerializedBytes() { |
| + return buffer_->chunks().size() * kChunkSize - |
| + stream_writer_->bytes_available(); |
| + } |
| + |
| + void GetSerializedBytes(size_t start, size_t length, uint8_t* buffer) { |
| + return buffer_->GetBytes(start, length, buffer); |
| + } |
| + |
| + private: |
| + std::unique_ptr<FakeScatteredBuffer> buffer_; |
| + std::unique_ptr<ScatteredStreamWriter> stream_writer_; |
| + std::vector<std::unique_ptr<ProtoZeroMessage>> root_messages_; |
| +}; |
| + |
| +TEST_F(ProtoZeroConformanceTest, SimpleFieldsNoNesting) { |
| + EveryField* msg; |
| + CreateMessage(&msg); |
| + |
| + msg->set_field_int32(-1); |
| + msg->set_field_int64(-333123456789ll); |
| + msg->set_field_uint32(600); |
| + msg->set_field_uint64(333123456789ll); |
| + msg->set_field_sint32(-5); |
| + msg->set_field_sint64(-9000); |
| + msg->set_field_fixed32(12345); |
| + msg->set_field_fixed64(444123450000ll); |
| + msg->set_field_sfixed32(-69999); |
| + msg->set_field_sfixed64(-200); |
| + msg->set_field_float(3.14f); |
| + msg->set_field_double(0.5555); |
| + msg->set_field_bool(true); |
| + msg->set_small_enum(SmallEnum::TO_BE); |
| + msg->set_signed_enum(SignedEnum::NEGATIVE); |
| + msg->set_big_enum(BigEnum::BEGIN); |
| + msg->set_field_string("FizzBuzz"); |
| + msg->set_field_bytes(reinterpret_cast<const uint8_t*>("\x11\x00\xBE\xEF"), 4); |
| + msg->add_repeated_int32(1); |
| + msg->add_repeated_int32(-1); |
| + msg->add_repeated_int32(100); |
| + msg->add_repeated_int32(2000000); |
| + size_t msg_size = GetNumSerializedBytes(); |
| + EXPECT_EQ(126u, msg_size); |
| + |
| + std::unique_ptr<uint8_t[]> msg_binary(new uint8_t[msg_size]); |
| + GetSerializedBytes(0, msg_size, msg_binary.get()); |
| + |
| + pbgold::EveryField gold; |
|
Primiano Tucci (use gerrit)
2016/08/25 10:31:51
maybe s/gold/gold_msg/ to make it clear this is an
|
| + gold.ParseFromArray(msg_binary.get(), msg_size); |
| + EXPECT_EQ(-1, gold.field_int32()); |
| + EXPECT_EQ(-333123456789ll, gold.field_int64()); |
| + EXPECT_EQ(600u, gold.field_uint32()); |
| + EXPECT_EQ(333123456789ull, gold.field_uint64()); |
| + EXPECT_EQ(-5, gold.field_sint32()); |
| + EXPECT_EQ(-9000, gold.field_sint64()); |
| + EXPECT_EQ(12345u, gold.field_fixed32()); |
| + EXPECT_EQ(444123450000ull, gold.field_fixed64()); |
| + EXPECT_EQ(-69999, gold.field_sfixed32()); |
| + EXPECT_EQ(-200, gold.field_sfixed64()); |
| + EXPECT_EQ(3.14f, gold.field_float()); |
| + EXPECT_EQ(0.5555, gold.field_double()); |
| + EXPECT_EQ(true, gold.field_bool()); |
| + EXPECT_EQ(pbgold::SmallEnum::TO_BE, gold.small_enum()); |
| + EXPECT_EQ(pbgold::SignedEnum::NEGATIVE, gold.signed_enum()); |
| + EXPECT_EQ(pbgold::BigEnum::BEGIN, gold.big_enum()); |
| + EXPECT_EQ("FizzBuzz", gold.field_string()); |
| + EXPECT_EQ(std::string("\x11\x00\xBE\xEF", 4), gold.field_bytes()); |
| + EXPECT_EQ(4, gold.repeated_int32_size()); |
| + EXPECT_EQ(1, gold.repeated_int32(0)); |
| + EXPECT_EQ(-1, gold.repeated_int32(1)); |
| + EXPECT_EQ(100, gold.repeated_int32(2)); |
| + EXPECT_EQ(2000000, gold.repeated_int32(3)); |
| +} |
| + |
| +TEST_F(ProtoZeroConformanceTest, NestedMessages) { |
| + NestedA* msg_a; |
| + CreateMessage(&msg_a); |
| + |
| + NestedA::NestedB* msg_b = msg_a->add_repeated_a(); |
| + NestedA::NestedB::NestedC* msg_c = msg_b->set_value_b(); |
| + msg_c->set_value_c(321); |
| + msg_b = msg_a->add_repeated_a(); |
| + msg_c = msg_a->set_super_nested(); |
| + msg_c->set_value_c(1000); |
| + msg_a->Finalize(); |
| + |
| + size_t msg_size = GetNumSerializedBytes(); |
| + EXPECT_EQ(26u, msg_size); |
| + |
| + std::unique_ptr<uint8_t[]> msg_binary(new uint8_t[msg_size]); |
| + GetSerializedBytes(0, msg_size, msg_binary.get()); |
| + |
| + pbgold::NestedA gold_a; |
| + gold_a.ParseFromArray(msg_binary.get(), msg_size); |
| + EXPECT_EQ(2, gold_a.repeated_a_size()); |
| + EXPECT_EQ(321, gold_a.repeated_a(0).value_b().value_c()); |
| + EXPECT_FALSE(gold_a.repeated_a(1).has_value_b()); |
| + EXPECT_EQ(1000, gold_a.super_nested().value_c()); |
| +} |
| + |
| +TEST(ProtoZeroTest, Simple) { |
| // Test the includes for indirect public import: library.pbzero.h -> |
| // library_internals/galaxies.pbzero.h -> upper_import.pbzero.h . |
| - EXPECT_LE(0u, sizeof(foo::bar::TrickyPublicImport)); |
| + EXPECT_LE(0u, sizeof(TrickyPublicImport)); |
| } |
| TEST(ProtoZeroTest, FieldNumbers) { |