Skip to content

Commit

Permalink
Add a few tests to make sure the repeated field rep is sync'd into th…
Browse files Browse the repository at this point in the history
…e map before some binary operations: copy construct, copy assign, merge.

PiperOrigin-RevId: 704796904
  • Loading branch information
protobuf-github-bot authored and copybara-github committed Dec 10, 2024
1 parent cbed3b8 commit 4b397e5
Showing 1 changed file with 71 additions and 0 deletions.
71 changes: 71 additions & 0 deletions src/google/protobuf/map_test.inc
Original file line number Diff line number Diff line change
Expand Up @@ -1392,6 +1392,77 @@ class MapTestCodegenOrDynamic : public testing::TestWithParam<bool> {};
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();
Expand Down

0 comments on commit 4b397e5

Please sign in to comment.