Skip to content

Commit

Permalink
Add unit test enum showing allocation in repeated enum lists
Browse files Browse the repository at this point in the history
TestPackedTypes' field is unused, it's just there to make the messages symmetric between packed and unpacked versions.

PiperOrigin-RevId: 628913968
  • Loading branch information
protobuf-github-bot authored and copybara-github committed Apr 29, 2024
1 parent ea4c77c commit b3e7a00
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 9 deletions.
4 changes: 4 additions & 0 deletions python/google/protobuf/internal/descriptor_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,10 @@ def testCopyToProto_ForeignEnum(self):
name: 'FOREIGN_BAX'
number: 32
>
value: <
name: 'FOREIGN_LARGE'
number: 123456
>
"""

self._InternalTestCopyToProto(
Expand Down
30 changes: 24 additions & 6 deletions python/google/protobuf/internal/reflection_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -594,19 +594,37 @@ def testEnum_Value(self, message_module):

def testEnum_KeysAndValues(self, message_module):
if message_module == unittest_pb2:
keys = ['FOREIGN_FOO', 'FOREIGN_BAR', 'FOREIGN_BAZ', 'FOREIGN_BAX']
values = [4, 5, 6, 32]
keys = [
'FOREIGN_FOO',
'FOREIGN_BAR',
'FOREIGN_BAZ',
'FOREIGN_BAX',
'FOREIGN_LARGE',
]
values = [4, 5, 6, 32, 123456]
items = [
('FOREIGN_FOO', 4),
('FOREIGN_BAR', 5),
('FOREIGN_BAZ', 6),
('FOREIGN_BAX', 32),
('FOREIGN_LARGE', 123456),
]
else:
keys = ['FOREIGN_ZERO', 'FOREIGN_FOO', 'FOREIGN_BAR', 'FOREIGN_BAZ']
values = [0, 4, 5, 6]
items = [('FOREIGN_ZERO', 0), ('FOREIGN_FOO', 4),
('FOREIGN_BAR', 5), ('FOREIGN_BAZ', 6)]
keys = [
'FOREIGN_ZERO',
'FOREIGN_FOO',
'FOREIGN_BAR',
'FOREIGN_BAZ',
'FOREIGN_LARGE',
]
values = [0, 4, 5, 6, 123456]
items = [
('FOREIGN_ZERO', 0),
('FOREIGN_FOO', 4),
('FOREIGN_BAR', 5),
('FOREIGN_BAZ', 6),
('FOREIGN_LARGE', 123456),
]
self.assertEqual(keys,
list(message_module.ForeignEnum.keys()))
self.assertEqual(values,
Expand Down
4 changes: 2 additions & 2 deletions src/google/protobuf/compiler/cpp/unittest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -1022,8 +1022,8 @@ TEST(GENERATED_ENUM_TEST_NAME, MinAndMax) {
EXPECT_EQ(4, UNITTEST::TestAllTypes::NestedEnum_ARRAYSIZE);

EXPECT_EQ(UNITTEST::FOREIGN_FOO, UNITTEST::ForeignEnum_MIN);
EXPECT_EQ(UNITTEST::FOREIGN_BAX, UNITTEST::ForeignEnum_MAX);
EXPECT_EQ(33, UNITTEST::ForeignEnum_ARRAYSIZE);
EXPECT_EQ(UNITTEST::FOREIGN_LARGE, UNITTEST::ForeignEnum_MAX);
EXPECT_EQ(123457, UNITTEST::ForeignEnum_ARRAYSIZE);

EXPECT_EQ(1, UNITTEST::TestEnumWithDupValue_MIN);
EXPECT_EQ(3, UNITTEST::TestEnumWithDupValue_MAX);
Expand Down
1 change: 1 addition & 0 deletions src/google/protobuf/unittest.proto
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ enum ForeignEnum {
FOREIGN_BAR = 5;
FOREIGN_BAZ = 6;
FOREIGN_BAX = 32; // (1 << 32) to generate a 64b bitmask would be incorrect.
FOREIGN_LARGE = 123456; // Large enough to escape the Boxed Integer cache.
}

message TestReservedFields {
Expand Down
3 changes: 2 additions & 1 deletion src/google/protobuf/unittest_proto3.proto
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ message TestAllTypes {
repeated string repeated_string_piece = 54 [ctype = STRING_PIECE];
repeated string repeated_cord = 55 [ctype = CORD];

repeated NestedMessage repeated_lazy_message = 57 ;
repeated NestedMessage repeated_lazy_message = 57;

oneof oneof_field {
uint32 oneof_uint32 = 111;
Expand Down Expand Up @@ -178,6 +178,7 @@ enum ForeignEnum {
FOREIGN_FOO = 4;
FOREIGN_BAR = 5;
FOREIGN_BAZ = 6;
FOREIGN_LARGE = 123456; // Large enough to escape the Boxed Integer cache.
}

// TestEmptyMessage is used to test behavior of unknown fields.
Expand Down
1 change: 1 addition & 0 deletions src/google/protobuf/unittest_proto3_arena.proto
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ enum ForeignEnum {
FOREIGN_FOO = 4;
FOREIGN_BAR = 5;
FOREIGN_BAZ = 6;
FOREIGN_LARGE = 123456; // Large enough to escape the Boxed Integer cache.
}

// TestEmptyMessage is used to test behavior of unknown fields.
Expand Down

0 comments on commit b3e7a00

Please sign in to comment.