summaryrefslogtreecommitdiffstats
path: root/java
diff options
context:
space:
mode:
authorMax Cai <maxtroy@google.com>2013-11-13 18:21:28 +0000
committerMax Cai <maxtroy@google.com>2013-11-18 11:47:25 +0000
commitfea3fd5cb6ff88b51da60b1f33004944d93a9fce (patch)
tree9bcd9aeaf2ff8f459af7385cebe3b8cbfad191d8 /java
parent3e0d99fab8c1da2a8f36637b7bf5e8581143e36a (diff)
downloadexternal_protobuf-fea3fd5cb6ff88b51da60b1f33004944d93a9fce.zip
external_protobuf-fea3fd5cb6ff88b51da60b1f33004944d93a9fce.tar.gz
external_protobuf-fea3fd5cb6ff88b51da60b1f33004944d93a9fce.tar.bz2
Align with main: two ways of parsing repeated packable fields.
It is a requirement for parsing code to handle packed and unpacked forms on the wire for repeated packable fields. This change aligns the javanano's behavior with the java's. Bonus: optimize array length calculation when parsing repeated fixed-size-element-type fields. Bonus 2: lose "xMemoizedSerializedSize" for repeated enum fields, and make the serialized size calculation match that for repeated int32 fields. Change-Id: I8a06103d9290234adb46b0971b5ed155544fe86a
Diffstat (limited to 'java')
-rw-r--r--java/README.txt1
-rw-r--r--java/pom.xml1
-rw-r--r--java/src/test/java/com/google/protobuf/NanoTest.java72
3 files changed, 74 insertions, 0 deletions
diff --git a/java/README.txt b/java/README.txt
index 3a004c7..976ec84 100644
--- a/java/README.txt
+++ b/java/README.txt
@@ -429,6 +429,7 @@ Except:
- Similar rename from CodedOutputStreamMicro to
CodedOutputByteBufferNano.
- Repeated fields are in arrays, not ArrayList or Vector.
+- Full support of serializing/deserializing repeated packed fields.
- Unset messages/groups are null, not an immutable empty default
instance.
- Required fields are always serialized.
diff --git a/java/pom.xml b/java/pom.xml
index cfa35a5..2f40b98 100644
--- a/java/pom.xml
+++ b/java/pom.xml
@@ -171,6 +171,7 @@
<arg value="--proto_path=src/test/java" />
<arg value="../src/google/protobuf/unittest_enum_class_nano.proto" />
<arg value="../src/google/protobuf/unittest_enum_class_multiple_nano.proto" />
+ <arg value="../src/google/protobuf/unittest_repeated_packables_nano.proto" />
</exec>
<exec executable="../src/protoc">
<arg value="--javanano_out=optional_field_style=reftypes,generate_equals=true:target/generated-test-sources" />
diff --git a/java/src/test/java/com/google/protobuf/NanoTest.java b/java/src/test/java/com/google/protobuf/NanoTest.java
index 724e741..0e1c9bc 100644
--- a/java/src/test/java/com/google/protobuf/NanoTest.java
+++ b/java/src/test/java/com/google/protobuf/NanoTest.java
@@ -49,6 +49,7 @@ import com.google.protobuf.nano.NanoHasOuterClass.TestAllTypesNanoHas;
import com.google.protobuf.nano.NanoOuterClass;
import com.google.protobuf.nano.NanoOuterClass.TestAllTypesNano;
import com.google.protobuf.nano.NanoReferenceTypes;
+import com.google.protobuf.nano.NanoRepeatedPackables;
import com.google.protobuf.nano.TestRepeatedMergeNano;
import com.google.protobuf.nano.UnittestImportNano;
import com.google.protobuf.nano.UnittestMultipleNano;
@@ -3064,6 +3065,77 @@ public class NanoTest extends TestCase {
assertEquals(30, firstContainer.contained.repeatedInt32[2]);
}
+ public void testRepeatedPackables() throws Exception {
+ // Check that repeated fields with packable types can accept both packed and unpacked
+ // serialized forms.
+ NanoRepeatedPackables.NonPacked nonPacked = new NanoRepeatedPackables.NonPacked();
+ nonPacked.int32S = new int[] {1, 2, 3};
+ nonPacked.int64S = new long[] {4, 5, 6};
+ nonPacked.uint32S = new int[] {7, 8, 9};
+ nonPacked.uint64S = new long[] {10, 11, 12};
+ nonPacked.sint32S = new int[] {13, 14, 15};
+ nonPacked.sint64S = new long[] {16, 17, 18};
+ nonPacked.fixed32S = new int[] {19, 20, 21};
+ nonPacked.fixed64S = new long[] {22, 23, 24};
+ nonPacked.sfixed32S = new int[] {25, 26, 27};
+ nonPacked.sfixed64S = new long[] {28, 29, 30};
+ nonPacked.floats = new float[] {31, 32, 33};
+ nonPacked.doubles = new double[] {34, 35, 36};
+ nonPacked.bools = new boolean[] {false, true};
+ nonPacked.enums = new int[] {
+ NanoRepeatedPackables.Enum.OPTION_ONE,
+ NanoRepeatedPackables.Enum.OPTION_TWO,
+ };
+ nonPacked.noise = 13579;
+
+ byte[] nonPackedSerialized = MessageNano.toByteArray(nonPacked);
+
+ NanoRepeatedPackables.Packed packed =
+ MessageNano.mergeFrom(new NanoRepeatedPackables.Packed(), nonPackedSerialized);
+ assertRepeatedPackablesEqual(nonPacked, packed);
+
+ byte[] packedSerialized = MessageNano.toByteArray(packed);
+ // Just a cautious check that the two serialized forms are different,
+ // to make sure the remaining of this test is useful:
+ assertFalse(Arrays.equals(nonPackedSerialized, packedSerialized));
+
+ nonPacked = MessageNano.mergeFrom(new NanoRepeatedPackables.NonPacked(), packedSerialized);
+ assertRepeatedPackablesEqual(nonPacked, packed);
+
+ // Test mixed serialized form.
+ byte[] mixedSerialized = new byte[nonPackedSerialized.length + packedSerialized.length];
+ System.arraycopy(nonPackedSerialized, 0, mixedSerialized, 0, nonPackedSerialized.length);
+ System.arraycopy(packedSerialized, 0,
+ mixedSerialized, nonPackedSerialized.length, packedSerialized.length);
+
+ nonPacked = MessageNano.mergeFrom(new NanoRepeatedPackables.NonPacked(), mixedSerialized);
+ packed = MessageNano.mergeFrom(new NanoRepeatedPackables.Packed(), mixedSerialized);
+ assertRepeatedPackablesEqual(nonPacked, packed);
+ assertTrue(Arrays.equals(new int[] {1, 2, 3, 1, 2, 3}, nonPacked.int32S));
+ assertTrue(Arrays.equals(new int[] {13, 14, 15, 13, 14, 15}, nonPacked.sint32S));
+ assertTrue(Arrays.equals(new int[] {25, 26, 27, 25, 26, 27}, nonPacked.sfixed32S));
+ assertTrue(Arrays.equals(new boolean[] {false, true, false, true}, nonPacked.bools));
+ }
+
+ private void assertRepeatedPackablesEqual(
+ NanoRepeatedPackables.NonPacked nonPacked, NanoRepeatedPackables.Packed packed) {
+ // Not using MessageNano.equals() -- that belongs to a separate test.
+ assertTrue(Arrays.equals(nonPacked.int32S, packed.int32S));
+ assertTrue(Arrays.equals(nonPacked.int64S, packed.int64S));
+ assertTrue(Arrays.equals(nonPacked.uint32S, packed.uint32S));
+ assertTrue(Arrays.equals(nonPacked.uint64S, packed.uint64S));
+ assertTrue(Arrays.equals(nonPacked.sint32S, packed.sint32S));
+ assertTrue(Arrays.equals(nonPacked.sint64S, packed.sint64S));
+ assertTrue(Arrays.equals(nonPacked.fixed32S, packed.fixed32S));
+ assertTrue(Arrays.equals(nonPacked.fixed64S, packed.fixed64S));
+ assertTrue(Arrays.equals(nonPacked.sfixed32S, packed.sfixed32S));
+ assertTrue(Arrays.equals(nonPacked.sfixed64S, packed.sfixed64S));
+ assertTrue(Arrays.equals(nonPacked.floats, packed.floats));
+ assertTrue(Arrays.equals(nonPacked.doubles, packed.doubles));
+ assertTrue(Arrays.equals(nonPacked.bools, packed.bools));
+ assertTrue(Arrays.equals(nonPacked.enums, packed.enums));
+ }
+
private void assertHasWireData(MessageNano message, boolean expected) {
byte[] bytes = MessageNano.toByteArray(message);
int wireLength = bytes.length;