protobuf/src/google/protobuf/arenastring_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 <google/protobuf/arenastring.h>
32 
33 #include <algorithm>
34 #include <cstdlib>
35 #include <memory>
36 #include <string>
37 #include <vector>
38 
39 #include <google/protobuf/stubs/logging.h>
40 #include <google/protobuf/stubs/common.h>
41 #include <google/protobuf/io/coded_stream.h>
42 #include <google/protobuf/io/zero_copy_stream_impl.h>
43 #include <google/protobuf/generated_message_util.h>
44 #include <google/protobuf/message_lite.h>
45 #include <gtest/gtest.h>
46 #include <google/protobuf/stubs/strutil.h>
47 
48 
49 // Must be included last.
50 #include <google/protobuf/port_def.inc>
51 
52 namespace google {
53 namespace protobuf {
54 
55 using internal::ArenaStringPtr;
56 
58 
59 const internal::LazyString nonempty_default{{{"default", 7}}, {nullptr}};
61 
62 class SingleArena : public testing::TestWithParam<bool> {
63  public:
64  std::unique_ptr<Arena> GetArena() {
65  if (this->GetParam()) return nullptr;
66  return std::unique_ptr<Arena>(new Arena());
67  }
68 };
69 
70 INSTANTIATE_TEST_SUITE_P(ArenaString, SingleArena, testing::Bool());
71 
72 TEST_P(SingleArena, GetSet) {
73  auto arena = GetArena();
75  field.UnsafeSetDefault(empty_default);
76  EXPECT_EQ("", field.Get());
77  field.Set(empty_default, "Test short", arena.get());
78  EXPECT_EQ("Test short", field.Get());
79  field.Set(empty_default, "Test long long long long value", arena.get());
80  EXPECT_EQ("Test long long long long value", field.Get());
81  field.Set(empty_default, "", arena.get());
82  field.Destroy(empty_default, arena.get());
83 }
84 
85 TEST_P(SingleArena, MutableAccessor) {
86  auto arena = GetArena();
89  field.UnsafeSetDefault(empty_default);
90 
91  std::string* mut = field.Mutable(EmptyDefault{}, arena.get());
92  EXPECT_EQ(mut, field.Mutable(EmptyDefault{}, arena.get()));
93  EXPECT_EQ(mut, &field.Get());
95  EXPECT_EQ("", *mut);
96  *mut = "Test long long long long value"; // ensure string allocates storage
97  EXPECT_EQ("Test long long long long value", field.Get());
98  field.Destroy(empty_default, arena.get());
99 }
100 
101 TEST_P(SingleArena, NullDefault) {
102  auto arena = GetArena();
103 
105  field.UnsafeSetDefault(nullptr);
106  std::string* mut = field.Mutable(nonempty_default, arena.get());
107  EXPECT_EQ(mut, field.Mutable(nonempty_default, arena.get()));
108  EXPECT_EQ(mut, &field.Get());
109  EXPECT_NE(nullptr, mut);
110  EXPECT_EQ("default", *mut);
111  *mut = "Test long long long long value"; // ensure string allocates storage
112  EXPECT_EQ("Test long long long long value", field.Get());
113  field.Destroy(nullptr, arena.get());
114 }
115 
116 class DualArena : public testing::TestWithParam<std::tuple<bool, bool>> {
117  public:
118  std::unique_ptr<Arena> GetLhsArena() {
119  if (std::get<0>(this->GetParam())) return nullptr;
120  return std::unique_ptr<Arena>(new Arena());
121  }
122  std::unique_ptr<Arena> GetRhsArena() {
123  if (std::get<1>(this->GetParam())) return nullptr;
124  return std::unique_ptr<Arena>(new Arena());
125  }
126 };
127 
128 INSTANTIATE_TEST_SUITE_P(ArenaString, DualArena,
130 
132  auto lhs_arena = GetLhsArena();
133  ArenaStringPtr lhs;
135  ArenaStringPtr rhs;
137 
138  {
139  auto rhs_arena = GetRhsArena();
140  lhs.Set(empty_default, "lhs value that has some heft", lhs_arena.get());
141  rhs.Set(empty_default, "rhs value that has some heft", rhs_arena.get());
143  &lhs, lhs_arena.get(), //
144  &rhs, rhs_arena.get());
145  EXPECT_EQ("rhs value that has some heft", lhs.Get());
146  EXPECT_EQ("lhs value that has some heft", rhs.Get());
147  lhs.Destroy(empty_default, rhs_arena.get());
148  }
149  EXPECT_EQ("lhs value that has some heft", rhs.Get());
150  rhs.Destroy(empty_default, lhs_arena.get());
151 }
152 
153 
154 } // namespace protobuf
155 } // namespace google
156 
157 #include <google/protobuf/port_undef.inc>
Arena
struct Arena Arena
Definition: third_party/bloaty/third_party/protobuf/src/google/protobuf/arena.h:189
absl::swap_internal::Swap
void Swap(T &lhs, T &rhs) noexcept(IsNothrowSwappable< T >::value)
Definition: abseil-cpp/absl/meta/type_traits.h:772
google::protobuf.internal::GetArena
Arena * GetArena(MessageLite *msg, int64 arena_offset)
Definition: bloaty/third_party/protobuf/src/google/protobuf/generated_message_table_driven_lite.h:86
google::protobuf.internal::ArenaStringPtr::UnsafeSetDefault
void UnsafeSetDefault(const ::std::string *default_value)
Definition: bloaty/third_party/protobuf/src/google/protobuf/arenastring.h:253
google::protobuf::EmptyDefault
ArenaStringPtr::EmptyDefault EmptyDefault
Definition: protobuf/src/google/protobuf/arenastring_unittest.cc:57
google::protobuf::INSTANTIATE_TEST_SUITE_P
INSTANTIATE_TEST_SUITE_P(UseArena, DynamicMessageTest, ::testing::Bool())
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
google::protobuf
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:12
google::protobuf::DualArena
Definition: protobuf/src/google/protobuf/arenastring_unittest.cc:116
google::protobuf.internal::ArenaStringPtr::Get
const ::std::string & Get() const
Definition: bloaty/third_party/protobuf/src/google/protobuf/arenastring.h:90
google::protobuf.internal::ArenaStringPtr::Destroy
void Destroy(const ::std::string *default_value, Arena *arena)
Definition: bloaty/third_party/protobuf/src/google/protobuf/arenastring.h:209
arena
grpc_core::ScopedArenaPtr arena
Definition: binder_transport_test.cc:237
testing::TestWithParam
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1883
EXPECT_EQ
#define EXPECT_EQ(a, b)
Definition: iomgr/time_averaged_stats_test.cc:27
google::protobuf.internal::ArenaStringPtr::InternalSwap
static PROTOBUF_NDEBUG_INLINE void InternalSwap(const std::string *default_value, ArenaStringPtr *rhs, Arena *rhs_arena, ArenaStringPtr *lhs, Arena *lhs_arena)
Definition: protobuf/src/google/protobuf/arenastring.h:356
google::protobuf::TEST_P
TEST_P(DynamicMessageTest, IndependentOffsets)
Definition: bloaty/third_party/protobuf/src/google/protobuf/dynamic_message_unittest.cc:143
google::protobuf::empty_default
const std::string * empty_default
Definition: protobuf/src/google/protobuf/arenastring_unittest.cc:60
EXPECT_NE
#define EXPECT_NE(val1, val2)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2028
google::protobuf.internal::LazyString
Definition: protobuf/src/google/protobuf/arenastring.h:61
google::protobuf::SingleArena::GetArena
std::unique_ptr< Arena > GetArena()
Definition: protobuf/src/google/protobuf/arenastring_unittest.cc:64
google::protobuf::DualArena::GetLhsArena
std::unique_ptr< Arena > GetLhsArena()
Definition: protobuf/src/google/protobuf/arenastring_unittest.cc:118
google::protobuf::DualArena::GetRhsArena
std::unique_ptr< Arena > GetRhsArena()
Definition: protobuf/src/google/protobuf/arenastring_unittest.cc:122
google::protobuf::SingleArena
Definition: protobuf/src/google/protobuf/arenastring_unittest.cc:62
field
const FieldDescriptor * field
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/parser_unittest.cc:2692
google::protobuf::nonempty_default
const internal::LazyString nonempty_default
Definition: protobuf/src/google/protobuf/arenastring_unittest.cc:59
testing::Combine
internal::CartesianProductHolder< Generator... > Combine(const Generator &... g)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest-param-test.h:410
testing::WithParamInterface< bool >::GetParam
static const ParamType & GetParam()
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1855
google::protobuf.internal::GetEmptyString
const PROTOBUF_EXPORT std::string & GetEmptyString()
Definition: bloaty/third_party/protobuf/src/google/protobuf/generated_message_util.h:87
google::protobuf.internal::ArenaStringPtr::EmptyDefault
Definition: protobuf/src/google/protobuf/arenastring.h:187
testing::Bool
internal::ParamGenerator< bool > Bool()
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest-param-test.h:359
google
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:11
google::protobuf.internal::ArenaStringPtr
Definition: bloaty/third_party/protobuf/src/google/protobuf/arenastring.h:74
google::protobuf.internal::ArenaStringPtr::Set
void Set(const ::std::string *default_value, const ::std::string &value, Arena *arena)
Definition: bloaty/third_party/protobuf/src/google/protobuf/arenastring.h:75


grpc
Author(s):
autogenerated on Fri May 16 2025 02:57:42