From 4b397e522c9501a59753af44f8bd05a2a0067375 Mon Sep 17 00:00:00 2001 From: Protobuf Team Bot Date: Tue, 10 Dec 2024 12:01:14 -0800 Subject: [PATCH] Add a few tests to make sure the repeated field rep is sync'd into the map before some binary operations: copy construct, copy assign, merge. PiperOrigin-RevId: 704796904 --- src/google/protobuf/map_test.inc | 71 ++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/src/google/protobuf/map_test.inc b/src/google/protobuf/map_test.inc index fc8ab62ef62f..3105cdfbd34e 100644 --- a/src/google/protobuf/map_test.inc +++ b/src/google/protobuf/map_test.inc @@ -1392,6 +1392,77 @@ class MapTestCodegenOrDynamic : public testing::TestWithParam {}; INSTANTIATE_TEST_SUITE_P(MapTestCodegenOrDynamicSuite, MapTestCodegenOrDynamic, testing::Bool()); +TEST_F(MapImplTest, CopyConstructMapSyncsRepeatedFieldRep) { + TestMap msg; + + const auto* reflection = msg.GetReflection(); + const auto* field = msg.GetDescriptor()->FindFieldByName("map_int32_int32"); + + { + auto* sub = reflection->AddMessage(&msg, field); + sub->GetReflection()->SetInt32(sub, sub->GetDescriptor()->map_key(), 10); + sub->GetReflection()->SetInt32(sub, sub->GetDescriptor()->map_value(), 100); + } + + TestMap copy = msg; + EXPECT_THAT(msg, + EqualsProto(R"pb(map_int32_int32 { key: 10 value: 100 })pb")); + EXPECT_THAT(copy, + EqualsProto(R"pb(map_int32_int32 { key: 10 value: 100 })pb")); +} + +TEST_F(MapImplTest, CopyAssignMapSyncsRepeatedFieldRep) { + TestMap msg; + + const auto* reflection = msg.GetReflection(); + const auto* field = msg.GetDescriptor()->FindFieldByName("map_int32_int32"); + + { + auto* sub = reflection->AddMessage(&msg, field); + sub->GetReflection()->SetInt32(sub, sub->GetDescriptor()->map_key(), 10); + sub->GetReflection()->SetInt32(sub, sub->GetDescriptor()->map_value(), 100); + } + + TestMap dst; + dst = msg; + EXPECT_THAT(msg, + EqualsProto(R"pb(map_int32_int32 { key: 10 value: 100 })pb")); + EXPECT_THAT(dst, + EqualsProto(R"pb(map_int32_int32 { key: 10 value: 100 })pb")); +} + +TEST_F(MapImplTest, MergeMapSyncsRepeatedFieldRep) { + TestMap msg; + + const auto* reflection = msg.GetReflection(); + const auto* field = msg.GetDescriptor()->FindFieldByName("map_int32_int32"); + + { + auto* sub = reflection->AddMessage(&msg, field); + sub->GetReflection()->SetInt32(sub, sub->GetDescriptor()->map_key(), 10); + sub->GetReflection()->SetInt32(sub, sub->GetDescriptor()->map_value(), 100); + } + + TestMap dst; + { + auto* sub = reflection->AddMessage(&dst, field); + // A duplicate key + sub->GetReflection()->SetInt32(sub, sub->GetDescriptor()->map_key(), 10); + sub->GetReflection()->SetInt32(sub, sub->GetDescriptor()->map_value(), 200); + sub = reflection->AddMessage(&dst, field); + // A unique key + sub->GetReflection()->SetInt32(sub, sub->GetDescriptor()->map_key(), 11); + sub->GetReflection()->SetInt32(sub, sub->GetDescriptor()->map_value(), 17); + } + dst.MergeFrom(msg); + EXPECT_THAT(msg, + EqualsProto(R"pb(map_int32_int32 { key: 10 value: 100 })pb")); + EXPECT_THAT(dst, EqualsProto(R"pb( + map_int32_int32 { key: 10 value: 100 } + map_int32_int32 { key: 11 value: 17 } + )pb")); +} + TEST_P(MapTestCodegenOrDynamic, LookupMapValueSyncsRepeatedFieldRep) { auto* descriptor = UNITTEST::TestMap::descriptor(); bool use_dynamic = GetParam();