Migrate Arena to hpb namespace with forwarding

PiperOrigin-RevId: 652503898
pull/17426/head
Hong Shin 6 months ago committed by Copybara-Service
parent 9717a07957
commit 16a8034d04
  1. 3
      hpb/BUILD
  2. 7
      hpb/hpb.h
  3. 27
      hpb/repeated_field_iterator_test.cc
  4. 4
      hpb_generator/tests/BUILD
  5. 66
      hpb_generator/tests/test_generated.cc

@ -157,6 +157,9 @@ cc_test(
srcs = ["repeated_field_iterator_test.cc"],
deps = [
":repeated_field",
"//hpb",
"//upb:message",
"@com_google_absl//absl/strings:string_view",
"@com_google_googletest//:gtest",
"@com_google_googletest//:gtest_main",
],

@ -20,9 +20,12 @@
#include "upb/wire/decode.h"
#include "upb/wire/encode.h"
namespace protos {
namespace hpb {
using Arena = ::upb::Arena;
}
namespace protos {
using hpb::Arena;
class ExtensionRegistry;
template <typename T>

@ -17,6 +17,9 @@
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "absl/strings/string_view.h"
#include "google/protobuf/hpb/hpb.h"
#include "upb/message/array.h"
using ::testing::ElementsAre;
@ -45,13 +48,13 @@ struct IteratorTestPeer {
}
template <typename T>
static StringRef<T> MakeStringRefProxy(upb_Array* arr, protos::Arena& arena) {
static StringRef<T> MakeStringRefProxy(upb_Array* arr, hpb::Arena& arena) {
return StringRef<T>({arr, arena.ptr(), 0});
}
template <typename T>
static StringIterator<T> MakeStringIterator(upb_Array* arr,
protos::Arena& arena) {
hpb::Arena& arena) {
return StringIterator<T>({arr, arena.ptr()});
}
};
@ -225,12 +228,12 @@ TEST(ScalarIteratorTest, IteratorBasedAlgorithmsWork) {
EXPECT_THAT(v, ElementsAre(10, 12, 14, 16, 18, 11, 13, 15, 17, 19));
}
const char* CloneString(protos::Arena& arena, absl::string_view str) {
const char* CloneString(hpb::Arena& arena, absl::string_view str) {
char* data = (char*)upb_Arena_Malloc(arena.ptr(), str.size());
memcpy(data, str.data(), str.size());
return data;
}
upb_Array* MakeStringArray(protos::Arena& arena,
upb_Array* MakeStringArray(hpb::Arena& arena,
const std::vector<std::string>& input) {
upb_Array* arr = upb_Array_New(arena.ptr(), kUpb_CType_String);
for (absl::string_view str : input) {
@ -243,7 +246,7 @@ upb_Array* MakeStringArray(protos::Arena& arena,
}
TEST(StringReferenceTest, BasicOperationsWork) {
protos::Arena arena;
hpb::Arena arena;
upb_Array* arr = MakeStringArray(arena, {""});
auto read = [&] {
@ -289,7 +292,7 @@ TEST(StringReferenceTest, BasicOperationsWork) {
}
TEST(StringReferenceTest, AssignmentAndSwap) {
protos::Arena arena;
hpb::Arena arena;
upb_Array* arr1 = MakeStringArray(arena, {"ABC"});
upb_Array* arr2 = MakeStringArray(arena, {"DEF"});
@ -309,7 +312,7 @@ TEST(StringReferenceTest, AssignmentAndSwap) {
}
template <typename T>
void TestStringIterator(protos::Arena& arena, upb_Array* array) {
void TestStringIterator(hpb::Arena& arena, upb_Array* array) {
StringIterator<T> it = IteratorTestPeer::MakeStringIterator<T>(array, arena);
// Copy
auto it2 = it;
@ -349,7 +352,7 @@ void TestStringIterator(protos::Arena& arena, upb_Array* array) {
}
TEST(StringIteratorTest, BasicOperationsWork) {
protos::Arena arena;
hpb::Arena arena;
auto* array = MakeStringArray(
arena, {"10", "11", "12", "13", "14", "15", "16", "17", "18", "19"});
TestStringIterator<const absl::string_view>(arena, array);
@ -357,7 +360,7 @@ TEST(StringIteratorTest, BasicOperationsWork) {
}
TEST(StringIteratorTest, Convertibility) {
protos::Arena arena;
hpb::Arena arena;
auto* array = MakeStringArray(
arena, {"10", "11", "12", "13", "14", "15", "16", "17", "18", "19"});
StringIterator<absl::string_view> it =
@ -381,7 +384,7 @@ TEST(StringIteratorTest, Convertibility) {
}
TEST(StringIteratorTest, MutabilityOnlyWorksOnMutable) {
protos::Arena arena;
hpb::Arena arena;
auto* array = MakeStringArray(
arena, {"10", "11", "12", "13", "14", "15", "16", "17", "18", "19"});
StringIterator<absl::string_view> it =
@ -403,7 +406,7 @@ TEST(StringIteratorTest, MutabilityOnlyWorksOnMutable) {
}
TEST(StringIteratorTest, IteratorReferenceInteraction) {
protos::Arena arena;
hpb::Arena arena;
auto* array = MakeStringArray(
arena, {"10", "11", "12", "13", "14", "15", "16", "17", "18", "19"});
StringIterator<absl::string_view> it =
@ -415,7 +418,7 @@ TEST(StringIteratorTest, IteratorReferenceInteraction) {
}
TEST(StringIteratorTest, IteratorBasedAlgorithmsWork) {
protos::Arena arena;
hpb::Arena arena;
auto* array = MakeStringArray(
arena, {"10", "11", "12", "13", "14", "15", "16", "17", "18", "19"});
StringIterator<absl::string_view> it =

@ -155,9 +155,9 @@
# "@com_google_absl//absl/status:statusor",
# "@com_google_absl//absl/strings",
# "//hpb:requires",
# "//protos",
# "//hpb",
# "//upb:mem",
# "//protos:repeated_field",
# "//hpb:repeated_field",
# ],
# )
# end:google_only

@ -21,10 +21,8 @@
#include "google/protobuf/compiler/hpb/tests/child_model.upb.proto.h"
#include "google/protobuf/compiler/hpb/tests/no_package.upb.proto.h"
#include "google/protobuf/compiler/hpb/tests/test_model.upb.proto.h"
#include "google/protobuf/hpb/hpb.h"
#include "google/protobuf/hpb/requires.h"
#include "protos/protos.h"
#include "protos/repeated_field.h"
#include "protos/repeated_field_iterator.h"
#include "upb/mem/arena.h"
#include "upb/mem/arena.hpp"
@ -67,13 +65,13 @@ TEST(CppGeneratedCode, MessageEnumValue) {
}
TEST(CppGeneratedCode, ArenaConstructor) {
::protos::Arena arena;
::hpb::Arena arena;
auto testModel = ::protos::CreateMessage<TestModel>(arena);
EXPECT_EQ(false, testModel.has_b1());
}
TEST(CppGeneratedCode, Booleans) {
::protos::Arena arena;
::hpb::Arena arena;
auto testModel = ::protos::CreateMessage<TestModel>(arena);
EXPECT_FALSE(testModel.b1());
testModel.set_b1(true);
@ -87,7 +85,7 @@ TEST(CppGeneratedCode, Booleans) {
}
TEST(CppGeneratedCode, ScalarInt32) {
::protos::Arena arena;
::hpb::Arena arena;
auto testModel = ::protos::CreateMessage<TestModel>(arena);
// Test int32 defaults.
EXPECT_EQ(testModel.value(), 0);
@ -130,7 +128,7 @@ TEST(CppGeneratedCode, Strings) {
}
TEST(CppGeneratedCode, ScalarUInt32) {
::protos::Arena arena;
::hpb::Arena arena;
auto testModel = ::protos::CreateMessage<TestModel>(arena);
// Test defaults.
EXPECT_EQ(testModel.optional_uint32(), 0);
@ -150,7 +148,7 @@ TEST(CppGeneratedCode, ScalarUInt32) {
}
TEST(CppGeneratedCode, ScalarInt64) {
::protos::Arena arena;
::hpb::Arena arena;
auto testModel = ::protos::CreateMessage<TestModel>(arena);
// Test defaults.
EXPECT_EQ(testModel.optional_int64(), 0);
@ -174,7 +172,7 @@ TEST(CppGeneratedCode, ScalarInt64) {
}
TEST(CppGeneratedCode, ScalarFloat) {
::protos::Arena arena;
::hpb::Arena arena;
auto testModel = ::protos::CreateMessage<TestModel>(arena);
// Test defaults.
EXPECT_EQ(testModel.optional_float(), 0.0f);
@ -202,7 +200,7 @@ TEST(CppGeneratedCode, ScalarFloat) {
}
TEST(CppGeneratedCode, ScalarDouble) {
::protos::Arena arena;
::hpb::Arena arena;
auto testModel = ::protos::CreateMessage<TestModel>(arena);
// Test defaults.
EXPECT_EQ(testModel.optional_double(), 0.0);
@ -226,7 +224,7 @@ TEST(CppGeneratedCode, ScalarDouble) {
}
TEST(CppGeneratedCode, Enums) {
::protos::Arena arena;
::hpb::Arena arena;
auto testModel = ::protos::CreateMessage<TestModel>(arena);
// Check enum default value.
@ -254,7 +252,7 @@ TEST(CppGeneratedCode, Enums) {
}
TEST(CppGeneratedCode, FieldWithDefaultValue) {
::protos::Arena arena;
::hpb::Arena arena;
auto testModel = ::protos::CreateMessage<TestModel>(arena);
EXPECT_FALSE(testModel.has_int_value_with_default());
@ -269,7 +267,7 @@ TEST(CppGeneratedCode, FieldWithDefaultValue) {
}
TEST(CppGeneratedCode, OneOfFields) {
::protos::Arena arena;
::hpb::Arena arena;
auto test_model = ::protos::CreateMessage<TestModel>(arena);
EXPECT_FALSE(test_model.has_oneof_member1());
@ -297,7 +295,7 @@ TEST(CppGeneratedCode, OneOfFields) {
}
TEST(CppGeneratedCode, Messages) {
::protos::Arena arena;
::hpb::Arena arena;
auto test_model = ::protos::CreateMessage<TestModel>(arena);
EXPECT_EQ(false, test_model.has_child_model_1());
auto child_model = test_model.child_model_1();
@ -321,7 +319,7 @@ TEST(CppGeneratedCode, Messages) {
}
TEST(CppGeneratedCode, NestedMessages) {
::protos::Arena arena;
::hpb::Arena arena;
auto test_model = ::protos::CreateMessage<TestModel>(arena);
auto nested_child = test_model.nested_child_1();
EXPECT_EQ(0, nested_child->nested_child_name().size());
@ -332,7 +330,7 @@ TEST(CppGeneratedCode, NestedMessages) {
}
TEST(CppGeneratedCode, RepeatedMessages) {
::protos::Arena arena;
::hpb::Arena arena;
auto test_model = ::protos::CreateMessage<TestModel>(arena);
EXPECT_EQ(0, test_model.child_models_size());
// Should be able to clear repeated field when empty.
@ -361,7 +359,7 @@ TEST(CppGeneratedCode, RepeatedMessages) {
}
TEST(CppGeneratedCode, RepeatedScalar) {
::protos::Arena arena;
::hpb::Arena arena;
auto test_model = ::protos::CreateMessage<TestModel>(arena);
EXPECT_EQ(0, test_model.value_array_size());
// Should be able to clear repeated field when empty.
@ -382,7 +380,7 @@ TEST(CppGeneratedCode, RepeatedScalar) {
}
TEST(CppGeneratedCode, RepeatedFieldClear) {
::protos::Arena arena;
::hpb::Arena arena;
auto test_model = ::protos::CreateMessage<TestModel>(arena);
test_model.mutable_value_array()->push_back(5);
test_model.mutable_value_array()->push_back(16);
@ -393,7 +391,7 @@ TEST(CppGeneratedCode, RepeatedFieldClear) {
}
TEST(CppGeneratedCode, RepeatedFieldProxyForScalars) {
::protos::Arena arena;
::hpb::Arena arena;
auto test_model = ::protos::CreateMessage<TestModel>(arena);
EXPECT_EQ(0, test_model.value_array().size());
EXPECT_EQ(0, test_model.mutable_value_array()->size());
@ -426,7 +424,7 @@ TEST(CppGeneratedCode, RepeatedFieldProxyForScalars) {
}
TEST(CppGeneratedCode, RepeatedScalarIterator) {
::protos::Arena arena;
::hpb::Arena arena;
auto test_model = ::protos::CreateMessage<TestModel>(arena);
test_model.mutable_value_array()->push_back(5);
test_model.mutable_value_array()->push_back(16);
@ -493,7 +491,7 @@ TEST(CppGeneratedCode, RepeatedScalarIterator) {
}
TEST(CppGeneratedCode, RepeatedFieldProxyForStrings) {
::protos::Arena arena;
::hpb::Arena arena;
auto test_model = ::protos::CreateMessage<TestModel>(arena);
EXPECT_EQ(0, test_model.repeated_string().size());
EXPECT_EQ(0, test_model.mutable_repeated_string()->size());
@ -528,7 +526,7 @@ TEST(CppGeneratedCode, RepeatedFieldProxyForStrings) {
}
TEST(CppGeneratedCode, RepeatedFieldProxyForMessages) {
::protos::Arena arena;
::hpb::Arena arena;
auto test_model = ::protos::CreateMessage<TestModel>(arena);
EXPECT_EQ(0, test_model.child_models().size());
ChildModel1 child1;
@ -573,7 +571,7 @@ TEST(CppGeneratedCode, RepeatedFieldProxyForMessages) {
}
TEST(CppGeneratedCode, EmptyRepeatedFieldProxyForMessages) {
::protos::Arena arena;
::hpb::Arena arena;
auto test_model = ::protos::CreateMessage<TestModel>(arena);
EXPECT_EQ(0, test_model.child_models().size());
ChildModel1 child1;
@ -586,7 +584,7 @@ TEST(CppGeneratedCode, EmptyRepeatedFieldProxyForMessages) {
}
TEST(CppGeneratedCode, RepeatedFieldProxyForMessagesIndexOperator) {
::protos::Arena arena;
::hpb::Arena arena;
auto test_model = ::protos::CreateMessage<TestModel>(arena);
EXPECT_EQ(0, test_model.child_models().size());
ChildModel1 child1;
@ -604,7 +602,7 @@ TEST(CppGeneratedCode, RepeatedFieldProxyForMessagesIndexOperator) {
}
TEST(CppGeneratedCode, RepeatedStrings) {
::protos::Arena arena;
::hpb::Arena arena;
auto test_model = ::protos::CreateMessage<TestModel>(arena);
EXPECT_EQ(0, test_model.repeated_string_size());
// Should be able to clear repeated field when empty.
@ -626,8 +624,8 @@ TEST(CppGeneratedCode, RepeatedStrings) {
TEST(CppGeneratedCode, MessageMapInt32KeyMessageValue) {
const int key_test_value = 3;
::protos::Arena arena;
::protos::Arena child_arena;
::hpb::Arena arena;
::hpb::Arena child_arena;
auto test_model = ::protos::CreateMessage<TestModel>(arena);
EXPECT_EQ(0, test_model.child_map_size());
test_model.clear_child_map();
@ -648,7 +646,7 @@ TEST(CppGeneratedCode, MessageMapInt32KeyMessageValue) {
}
TEST(CppGeneratedCode, MessageMapStringKeyAndStringValue) {
::protos::Arena arena;
::hpb::Arena arena;
auto test_model = ::protos::CreateMessage<TestModel>(arena);
EXPECT_EQ(0, test_model.str_to_str_map_size());
test_model.clear_str_to_str_map();
@ -664,7 +662,7 @@ TEST(CppGeneratedCode, MessageMapStringKeyAndStringValue) {
}
TEST(CppGeneratedCode, MessageMapStringKeyAndInt32Value) {
::protos::Arena arena;
::hpb::Arena arena;
auto test_model = ::protos::CreateMessage<TestModel>(arena);
EXPECT_EQ(0, test_model.str_to_int_map_size());
test_model.clear_str_to_int_map();
@ -725,13 +723,13 @@ TEST(CppGeneratedCode, SetExtension) {
}
TEST(CppGeneratedCode, SetExtensionWithPtr) {
::protos::Arena arena_model;
::hpb::Arena arena_model;
::protos::Ptr<TestModel> model =
::protos::CreateMessage<TestModel>(arena_model);
void* prior_message;
{
// Use a nested scope to make sure the arenas are fused correctly.
::protos::Arena arena;
::hpb::Arena arena;
::protos::Ptr<ThemeExtension> extension1 =
::protos::CreateMessage<ThemeExtension>(arena);
extension1->set_ext_name("Hello World");
@ -748,7 +746,7 @@ TEST(CppGeneratedCode, SetExtensionWithPtr) {
#ifndef _MSC_VER
TEST(CppGeneratedCode, SetExtensionShouldNotCompileForWrongType) {
::protos::Arena arena;
::hpb::Arena arena;
::protos::Ptr<TestModel> model = ::protos::CreateMessage<TestModel>(arena);
ThemeExtension extension1;
ContainerExtension extension2;
@ -769,7 +767,7 @@ TEST(CppGeneratedCode, SetExtensionShouldNotCompileForWrongType) {
#endif
TEST(CppGeneratedCode, SetExtensionWithPtrSameArena) {
::protos::Arena arena;
::hpb::Arena arena;
::protos::Ptr<TestModel> model = ::protos::CreateMessage<TestModel>(arena);
void* prior_message;
{
@ -1227,7 +1225,7 @@ TEST(CppGeneratedCode, HasExtensionAndRegistry) {
ASSERT_TRUE(::protos::SetExtension(&source, theme, extension1).ok());
// Now that we have a source model with extension data, serialize.
::protos::Arena arena;
::hpb::Arena arena;
std::string data = std::string(::protos::Serialize(&source, arena).value());
// Test with ExtensionRegistry

Loading…
Cancel
Save