protobuf/src/google/protobuf/proto3_arena_lite_unittest.cc
Go to the documentation of this file.
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 // * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 #include <memory>
32 #include <string>
33 #include <vector>
34 
35 #include <google/protobuf/unittest_proto3_arena.pb.h>
36 #include <google/protobuf/arena.h>
37 #include <google/protobuf/testing/googletest.h>
38 #include <gtest/gtest.h>
39 
40 using proto3_arena_unittest::TestAllTypes;
41 
42 namespace google {
43 namespace protobuf {
44 namespace {
45 // We selectively set/check a few representative fields rather than all fields
46 // as this test is only expected to cover the basics of arena support.
47 void SetAllFields(TestAllTypes* m) {
48  m->set_optional_int32(100);
49  m->set_optional_string("asdf");
50  m->set_optional_bytes("jkl;");
51  m->mutable_optional_nested_message()->set_bb(42);
52  m->mutable_optional_foreign_message()->set_c(43);
53  m->set_optional_nested_enum(proto3_arena_unittest::TestAllTypes::BAZ);
54  m->set_optional_foreign_enum(proto3_arena_unittest::FOREIGN_BAZ);
55  m->mutable_optional_lazy_message()->set_bb(45);
56  m->add_repeated_int32(100);
57  m->add_repeated_string("asdf");
58  m->add_repeated_bytes("jkl;");
59  m->add_repeated_nested_message()->set_bb(46);
60  m->add_repeated_foreign_message()->set_c(47);
61  m->add_repeated_nested_enum(proto3_arena_unittest::TestAllTypes::BAZ);
62  m->add_repeated_foreign_enum(proto3_arena_unittest::FOREIGN_BAZ);
63  m->add_repeated_lazy_message()->set_bb(49);
64 
65  m->set_oneof_uint32(1);
66  m->mutable_oneof_nested_message()->set_bb(50);
67  m->set_oneof_string("test"); // only this one remains set
68 }
69 
70 void ExpectAllFieldsSet(const TestAllTypes& m) {
71  EXPECT_EQ(100, m.optional_int32());
72  EXPECT_EQ("asdf", m.optional_string());
73  EXPECT_EQ("jkl;", m.optional_bytes());
74  EXPECT_EQ(true, m.has_optional_nested_message());
75  EXPECT_EQ(42, m.optional_nested_message().bb());
76  EXPECT_EQ(true, m.has_optional_foreign_message());
77  EXPECT_EQ(43, m.optional_foreign_message().c());
78  EXPECT_EQ(proto3_arena_unittest::TestAllTypes::BAZ, m.optional_nested_enum());
79  EXPECT_EQ(proto3_arena_unittest::FOREIGN_BAZ, m.optional_foreign_enum());
80  EXPECT_EQ(true, m.has_optional_lazy_message());
81  EXPECT_EQ(45, m.optional_lazy_message().bb());
82 
83  EXPECT_EQ(1, m.repeated_int32_size());
84  EXPECT_EQ(100, m.repeated_int32(0));
85  EXPECT_EQ(1, m.repeated_string_size());
86  EXPECT_EQ("asdf", m.repeated_string(0));
87  EXPECT_EQ(1, m.repeated_bytes_size());
88  EXPECT_EQ("jkl;", m.repeated_bytes(0));
89  EXPECT_EQ(1, m.repeated_nested_message_size());
90  EXPECT_EQ(46, m.repeated_nested_message(0).bb());
91  EXPECT_EQ(1, m.repeated_foreign_message_size());
92  EXPECT_EQ(47, m.repeated_foreign_message(0).c());
93  EXPECT_EQ(1, m.repeated_nested_enum_size());
94  EXPECT_EQ(proto3_arena_unittest::TestAllTypes::BAZ,
95  m.repeated_nested_enum(0));
96  EXPECT_EQ(1, m.repeated_foreign_enum_size());
97  EXPECT_EQ(proto3_arena_unittest::FOREIGN_BAZ, m.repeated_foreign_enum(0));
98  EXPECT_EQ(1, m.repeated_lazy_message_size());
99  EXPECT_EQ(49, m.repeated_lazy_message(0).bb());
100 
101  EXPECT_EQ(proto3_arena_unittest::TestAllTypes::kOneofString,
102  m.oneof_field_case());
103  EXPECT_EQ("test", m.oneof_string());
104 }
105 
106 // In this file we only test some basic functionalities of arena support in
107 // proto3 and expect the arena support to be fully tested in proto2 unittests
108 // because proto3 shares most code with proto2.
109 
110 TEST(Proto3ArenaLiteTest, Parsing) {
111  TestAllTypes original;
112  SetAllFields(&original);
113 
114  Arena arena;
115  TestAllTypes* arena_message = Arena::CreateMessage<TestAllTypes>(&arena);
116  arena_message->ParseFromString(original.SerializeAsString());
117  ExpectAllFieldsSet(*arena_message);
118 }
119 
120 TEST(Proto3ArenaLiteTest, Swap) {
121  Arena arena1;
122  Arena arena2;
123 
124  // Test Swap().
125  TestAllTypes* arena1_message = Arena::CreateMessage<TestAllTypes>(&arena1);
126  TestAllTypes* arena2_message = Arena::CreateMessage<TestAllTypes>(&arena2);
127  arena1_message->Swap(arena2_message);
128  EXPECT_EQ(&arena1, arena1_message->GetArena());
129  EXPECT_EQ(&arena2, arena2_message->GetArena());
130 }
131 
132 TEST(Proto3ArenaLiteTest, SetAllocatedMessage) {
133  Arena arena;
134  TestAllTypes* arena_message = Arena::CreateMessage<TestAllTypes>(&arena);
135  TestAllTypes::NestedMessage* nested = new TestAllTypes::NestedMessage;
136  nested->set_bb(118);
137  arena_message->set_allocated_optional_nested_message(nested);
138  EXPECT_EQ(118, arena_message->optional_nested_message().bb());
139 }
140 
141 TEST(Proto3ArenaLiteTest, ReleaseMessage) {
142  Arena arena;
143  TestAllTypes* arena_message = Arena::CreateMessage<TestAllTypes>(&arena);
144  arena_message->mutable_optional_nested_message()->set_bb(118);
145  std::unique_ptr<TestAllTypes::NestedMessage> nested(
146  arena_message->release_optional_nested_message());
147  EXPECT_EQ(118, nested->bb());
148 }
149 
150 } // namespace
151 } // namespace protobuf
152 } // namespace google
absl::swap_internal::Swap
void Swap(T &lhs, T &rhs) noexcept(IsNothrowSwappable< T >::value)
Definition: abseil-cpp/absl/meta/type_traits.h:772
Arena
Definition: arena.c:39
google::protobuf
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:12
arena
grpc_core::ScopedArenaPtr arena
Definition: binder_transport_test.cc:237
tests.google.protobuf.internal.test_util.SetAllFields
def SetAllFields(message)
Definition: bloaty/third_party/protobuf/python/compatibility_tests/v2.5.0/tests/google/protobuf/internal/test_util.py:182
EXPECT_EQ
#define EXPECT_EQ(a, b)
Definition: iomgr/time_averaged_stats_test.cc:27
tests.google.protobuf.internal.test_util.ExpectAllFieldsSet
def ExpectAllFieldsSet(test_case, message)
Definition: bloaty/third_party/protobuf/python/compatibility_tests/v2.5.0/tests/google/protobuf/internal/test_util.py:367
google::protobuf::TEST
TEST(ArenaTest, ArenaConstructable)
Definition: bloaty/third_party/protobuf/src/google/protobuf/arena_unittest.cc:156
nested
static int nested
Definition: test-callback-stack.c:39
regress.m
m
Definition: regress/regress.py:25
google
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:11


grpc
Author(s):
autogenerated on Fri May 16 2025 02:59:46