From 0d52964597d0552f5ab4442f9908fd39d8650b23 Mon Sep 17 00:00:00 2001 From: Jisi Liu Date: Thu, 5 Feb 2015 14:19:46 -0800 Subject: Basic serialization/parsing test. --- .../java/com/google/protobuf/nano/NanoTest.java | 117 ++++++++++++++++++++- .../java/com/google/protobuf/nano/map_test.proto | 14 +++ 2 files changed, 130 insertions(+), 1 deletion(-) diff --git a/javanano/src/test/java/com/google/protobuf/nano/NanoTest.java b/javanano/src/test/java/com/google/protobuf/nano/NanoTest.java index d9428432..0edf6d6d 100644 --- a/javanano/src/test/java/com/google/protobuf/nano/NanoTest.java +++ b/javanano/src/test/java/com/google/protobuf/nano/NanoTest.java @@ -30,6 +30,7 @@ package com.google.protobuf.nano; +import com.google.protobuf.nano.MapTestProto.TestMap; import com.google.protobuf.nano.NanoAccessorsOuterClass.TestNanoAccessors; import com.google.protobuf.nano.NanoHasOuterClass.TestAllTypesNanoHas; import com.google.protobuf.nano.NanoOuterClass.TestAllTypesNano; @@ -45,6 +46,7 @@ import junit.framework.TestCase; import java.util.Arrays; import java.util.HashMap; +import java.util.Map; /** * Test nano runtime. @@ -3733,12 +3735,125 @@ public class NanoTest extends TestCase { } public void testMapsSerializeAndParse() throws Exception { - // TODO(liujisi): Test basic serialization/parsing roundtrip. + TestMap origin = new TestMap(); + setMapMessage(origin); + assertMapMessageSet(origin); + + byte[] output = MessageNano.toByteArray(origin); + TestMap parsed = new TestMap(); + MessageNano.mergeFrom(parsed, output); // TODO(liujisi): Test null values in serialization. // TODO(liujisi): Test merging message type values. // TODO(liujisi): Test missing key/value in parsing. } + private static final Integer[] int32Values = new Integer[] { + 0, 1, -1, Integer.MAX_VALUE, Integer.MIN_VALUE, + }; + private static final Long[] int64Values = new Long[] { + 0L, 1L, -1L, Long.MAX_VALUE, Long.MIN_VALUE, + }; + private static final String[] stringValues = new String[] { + "", "hello", "world", "foo", "bar", + }; + private static final byte[][] bytesValues = new byte[][] { + new byte[] {}, + new byte[] {0}, + new byte[] {1, -1}, + new byte[] {127, -128}, + new byte[] {'a', 'b', '0', '1'}, + }; + private static final Boolean[] boolValues = new Boolean[] { + false, true, + }; + private static final Integer[] enumValues = new Integer[] { + TestMap.FOO, TestMap.BAR, TestMap.BAZ, TestMap.QUX, + Integer.MAX_VALUE /* unknown */, + }; + private static final TestMap.MessageValue[] messageValues = + new TestMap.MessageValue[] { + newMapValueMessage(0), + newMapValueMessage(1), + newMapValueMessage(-1), + newMapValueMessage(Integer.MAX_VALUE), + newMapValueMessage(Integer.MIN_VALUE), + }; + private static TestMap.MessageValue newMapValueMessage(int value) { + TestMap.MessageValue result = new TestMap.MessageValue(); + result.value = value; + return result; + } + + private void setMap(Map map, K[] keys, V[] values) { + assert(keys.length == values.length); + for (int i = 0; i < keys.length; ++i) { + map.put(keys[i], values[i]); + } + } + + private void assertMapSet( + Map map, K[] keys, V[] values) throws Exception { + assert(keys.length == values.length); + for (int i = 0; i < values.length; ++i) { + assertEquals(values[i], map.get(keys[i])); + } + assertEquals(keys.length, map.size()); + } + + private void setMapMessage(TestMap testMap) { + testMap.int32ToInt32Field = new HashMap(); + testMap.int32ToBytesField = new HashMap(); + testMap.int32ToEnumField = new HashMap(); + testMap.int32ToMessageField = + new HashMap(); + testMap.int32ToStringField = new HashMap(); + testMap.stringToInt32Field = new HashMap(); + testMap.boolToBoolField = new HashMap(); + testMap.uint32ToUint32Field = new HashMap(); + testMap.sint32ToSint32Field = new HashMap(); + testMap.fixed32ToFixed32Field = new HashMap(); + testMap.sfixed32ToSfixed32Field = new HashMap(); + testMap.int64ToInt64Field = new HashMap(); + testMap.uint64ToUint64Field = new HashMap(); + testMap.sint64ToSint64Field = new HashMap(); + testMap.fixed64ToFixed64Field = new HashMap(); + testMap.sfixed64ToSfixed64Field = new HashMap(); + setMap(testMap.int32ToInt32Field, int32Values, int32Values); + setMap(testMap.int32ToBytesField, int32Values, bytesValues); + setMap(testMap.int32ToEnumField, int32Values, enumValues); + setMap(testMap.int32ToMessageField, int32Values, messageValues); + setMap(testMap.int32ToStringField, int32Values, stringValues); + setMap(testMap.stringToInt32Field, stringValues, int32Values); + setMap(testMap.boolToBoolField, boolValues, boolValues); + setMap(testMap.uint32ToUint32Field, int32Values, int32Values); + setMap(testMap.sint32ToSint32Field, int32Values, int32Values); + setMap(testMap.fixed32ToFixed32Field, int32Values, int32Values); + setMap(testMap.sfixed32ToSfixed32Field, int32Values, int32Values); + setMap(testMap.int64ToInt64Field, int64Values, int64Values); + setMap(testMap.uint64ToUint64Field, int64Values, int64Values); + setMap(testMap.sint64ToSint64Field, int64Values, int64Values); + setMap(testMap.fixed64ToFixed64Field, int64Values, int64Values); + setMap(testMap.sfixed64ToSfixed64Field, int64Values, int64Values); + } + private void assertMapMessageSet(TestMap testMap) throws Exception { + assertMapSet(testMap.int32ToInt32Field, int32Values, int32Values); + assertMapSet(testMap.int32ToBytesField, int32Values, bytesValues); + assertMapSet(testMap.int32ToEnumField, int32Values, enumValues); + assertMapSet(testMap.int32ToMessageField, int32Values, messageValues); + assertMapSet(testMap.int32ToStringField, int32Values, stringValues); + assertMapSet(testMap.stringToInt32Field, stringValues, int32Values); + assertMapSet(testMap.boolToBoolField, boolValues, boolValues); + assertMapSet(testMap.uint32ToUint32Field, int32Values, int32Values); + assertMapSet(testMap.sint32ToSint32Field, int32Values, int32Values); + assertMapSet(testMap.fixed32ToFixed32Field, int32Values, int32Values); + assertMapSet(testMap.sfixed32ToSfixed32Field, int32Values, int32Values); + assertMapSet(testMap.int64ToInt64Field, int64Values, int64Values); + assertMapSet(testMap.uint64ToUint64Field, int64Values, int64Values); + assertMapSet(testMap.sint64ToSint64Field, int64Values, int64Values); + assertMapSet(testMap.fixed64ToFixed64Field, int64Values, int64Values); + assertMapSet(testMap.sfixed64ToSfixed64Field, int64Values, int64Values); + } + private void assertRepeatedPackablesEqual( NanoRepeatedPackables.NonPacked nonPacked, NanoRepeatedPackables.Packed packed) { // Not using MessageNano.equals() -- that belongs to a separate test. diff --git a/javanano/src/test/java/com/google/protobuf/nano/map_test.proto b/javanano/src/test/java/com/google/protobuf/nano/map_test.proto index 482c499e..5ea86717 100644 --- a/javanano/src/test/java/com/google/protobuf/nano/map_test.proto +++ b/javanano/src/test/java/com/google/protobuf/nano/map_test.proto @@ -52,4 +52,18 @@ message TestMap { map int32_to_enum_field = 4; map int32_to_message_field = 5; map string_to_int32_field = 6; + map bool_to_bool_field = 7; + + // Test all the other primitive types. As the key and value are not coupled in + // the implementation, we do not test all the combinations of key/value pairs, + // so that we can keep the number of test cases manageable + map uint32_to_uint32_field = 11; + map sint32_to_sint32_field = 12; + map fixed32_to_fixed32_field = 13; + map sfixed32_to_sfixed32_field = 14; + map int64_to_int64_field = 15; + map uint64_to_uint64_field = 16; + map sint64_to_sint64_field = 17; + map fixed64_to_fixed64_field = 18; + map sfixed64_to_sfixed64_field = 19; } -- cgit v1.2.3