From b3e7a0077418cd9cb9b820c0197c3b8edb7007dd Mon Sep 17 00:00:00 2001 From: Protobuf Team Bot Date: Sun, 28 Apr 2024 17:17:31 -0700 Subject: [PATCH] Add unit test enum showing allocation in repeated enum lists TestPackedTypes' field is unused, it's just there to make the messages symmetric between packed and unpacked versions. PiperOrigin-RevId: 628913968 --- .../protobuf/internal/descriptor_test.py | 4 +++ .../protobuf/internal/reflection_test.py | 30 +++++++++++++++---- src/google/protobuf/compiler/cpp/unittest.inc | 4 +-- src/google/protobuf/unittest.proto | 1 + src/google/protobuf/unittest_proto3.proto | 3 +- .../protobuf/unittest_proto3_arena.proto | 1 + 6 files changed, 34 insertions(+), 9 deletions(-) diff --git a/python/google/protobuf/internal/descriptor_test.py b/python/google/protobuf/internal/descriptor_test.py index ce16d0363ac0..2c8423efac79 100755 --- a/python/google/protobuf/internal/descriptor_test.py +++ b/python/google/protobuf/internal/descriptor_test.py @@ -884,6 +884,10 @@ def testCopyToProto_ForeignEnum(self): name: 'FOREIGN_BAX' number: 32 > + value: < + name: 'FOREIGN_LARGE' + number: 123456 + > """ self._InternalTestCopyToProto( diff --git a/python/google/protobuf/internal/reflection_test.py b/python/google/protobuf/internal/reflection_test.py index 459bef345eda..171b9cda81cc 100755 --- a/python/google/protobuf/internal/reflection_test.py +++ b/python/google/protobuf/internal/reflection_test.py @@ -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, diff --git a/src/google/protobuf/compiler/cpp/unittest.inc b/src/google/protobuf/compiler/cpp/unittest.inc index c4b27ae94a24..8da0de29ea48 100644 --- a/src/google/protobuf/compiler/cpp/unittest.inc +++ b/src/google/protobuf/compiler/cpp/unittest.inc @@ -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); diff --git a/src/google/protobuf/unittest.proto b/src/google/protobuf/unittest.proto index 434325f4d19e..3d9dc3a7759a 100644 --- a/src/google/protobuf/unittest.proto +++ b/src/google/protobuf/unittest.proto @@ -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 { diff --git a/src/google/protobuf/unittest_proto3.proto b/src/google/protobuf/unittest_proto3.proto index 1c0f65bc9a19..9067728ae206 100644 --- a/src/google/protobuf/unittest_proto3.proto +++ b/src/google/protobuf/unittest_proto3.proto @@ -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; @@ -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. diff --git a/src/google/protobuf/unittest_proto3_arena.proto b/src/google/protobuf/unittest_proto3_arena.proto index 032ce6ff6777..96a9ea85e295 100644 --- a/src/google/protobuf/unittest_proto3_arena.proto +++ b/src/google/protobuf/unittest_proto3_arena.proto @@ -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.