From bc03650b3eefab6aea5b1b7019bffef8a74790c4 Mon Sep 17 00:00:00 2001 From: Mark Hansen Date: Wed, 17 Jul 2024 17:15:32 -0700 Subject: [PATCH] Add more tests for UnknownFieldSet Builder reusability Split up from testFieldBuildersAreReusable This is to build confidence I'm not breaking things with my upcoming changes. PiperOrigin-RevId: 653410161 --- .../google/protobuf/UnknownFieldSetTest.java | 60 ++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) diff --git a/java/core/src/test/java/com/google/protobuf/UnknownFieldSetTest.java b/java/core/src/test/java/com/google/protobuf/UnknownFieldSetTest.java index f34a448c51..58ac736d4a 100644 --- a/java/core/src/test/java/com/google/protobuf/UnknownFieldSetTest.java +++ b/java/core/src/test/java/com/google/protobuf/UnknownFieldSetTest.java @@ -80,7 +80,7 @@ public class UnknownFieldSetTest { // ================================================================= @Test - public void testFieldBuildersAreReusable() { + public void testFixed32FieldBuildersAreReusable() { UnknownFieldSet.Field.Builder fieldBuilder = UnknownFieldSet.Field.newBuilder(); fieldBuilder.addFixed32(10); UnknownFieldSet.Field first = fieldBuilder.build(); @@ -92,6 +92,64 @@ public class UnknownFieldSetTest { assertThat(first).isNotEqualTo(third); } + @Test + public void testFixed64FieldBuildersAreReusable() { + UnknownFieldSet.Field.Builder fieldBuilder = UnknownFieldSet.Field.newBuilder(); + fieldBuilder.addFixed64(10); + UnknownFieldSet.Field first = fieldBuilder.build(); + UnknownFieldSet.Field second = fieldBuilder.build(); + fieldBuilder.addFixed64(11); + UnknownFieldSet.Field third = fieldBuilder.build(); + + assertThat(first).isEqualTo(second); + assertThat(first).isNotEqualTo(third); + } + + @Test + public void testVarintFieldBuildersAreReusable() { + UnknownFieldSet.Field.Builder fieldBuilder = UnknownFieldSet.Field.newBuilder(); + fieldBuilder.addVarint(10); + UnknownFieldSet.Field first = fieldBuilder.build(); + UnknownFieldSet.Field second = fieldBuilder.build(); + fieldBuilder.addVarint(11); + UnknownFieldSet.Field third = fieldBuilder.build(); + + assertThat(first).isEqualTo(second); + assertThat(first).isNotEqualTo(third); + } + + @Test + public void testLengthDelimitedFieldBuildersAreReusable() { + UnknownFieldSet.Field.Builder fieldBuilder = UnknownFieldSet.Field.newBuilder(); + fieldBuilder.addLengthDelimited(ByteString.copyFromUtf8("foo")); + UnknownFieldSet.Field first = fieldBuilder.build(); + UnknownFieldSet.Field second = fieldBuilder.build(); + fieldBuilder.addLengthDelimited(ByteString.copyFromUtf8("bar")); + UnknownFieldSet.Field third = fieldBuilder.build(); + + assertThat(first).isEqualTo(second); + assertThat(first).isNotEqualTo(third); + } + + @Test + public void testGroupFieldBuildersAreReusable() { + UnknownFieldSet.Field.Builder fieldBuilder = UnknownFieldSet.Field.newBuilder(); + fieldBuilder.addGroup( + UnknownFieldSet.newBuilder() + .addField(10, UnknownFieldSet.Field.newBuilder().addVarint(10).build()) + .build()); + UnknownFieldSet.Field first = fieldBuilder.build(); + UnknownFieldSet.Field second = fieldBuilder.build(); + fieldBuilder.addGroup( + UnknownFieldSet.newBuilder() + .addField(11, UnknownFieldSet.Field.newBuilder().addVarint(11).build()) + .build()); + UnknownFieldSet.Field third = fieldBuilder.build(); + + assertThat(first).isEqualTo(second); + assertThat(first).isNotEqualTo(third); + } + @Test public void testClone() { UnknownFieldSet.Builder unknownSetBuilder = UnknownFieldSet.newBuilder();