Basic serialization/parsing test.
This commit is contained in:
parent
3c0355ef37
commit
0d52964597
@ -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 <K, V> void setMap(Map<K, V> 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 <K, V> void assertMapSet(
|
||||
Map<K, V> 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<Integer, Integer>();
|
||||
testMap.int32ToBytesField = new HashMap<Integer, byte[]>();
|
||||
testMap.int32ToEnumField = new HashMap<Integer, Integer>();
|
||||
testMap.int32ToMessageField =
|
||||
new HashMap<Integer, MapTestProto.TestMap.MessageValue>();
|
||||
testMap.int32ToStringField = new HashMap<Integer, String>();
|
||||
testMap.stringToInt32Field = new HashMap<String, Integer>();
|
||||
testMap.boolToBoolField = new HashMap<Boolean, Boolean>();
|
||||
testMap.uint32ToUint32Field = new HashMap<Integer, Integer>();
|
||||
testMap.sint32ToSint32Field = new HashMap<Integer, Integer>();
|
||||
testMap.fixed32ToFixed32Field = new HashMap<Integer, Integer>();
|
||||
testMap.sfixed32ToSfixed32Field = new HashMap<Integer, Integer>();
|
||||
testMap.int64ToInt64Field = new HashMap<Long, Long>();
|
||||
testMap.uint64ToUint64Field = new HashMap<Long, Long>();
|
||||
testMap.sint64ToSint64Field = new HashMap<Long, Long>();
|
||||
testMap.fixed64ToFixed64Field = new HashMap<Long, Long>();
|
||||
testMap.sfixed64ToSfixed64Field = new HashMap<Long, Long>();
|
||||
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.
|
||||
|
@ -52,4 +52,18 @@ message TestMap {
|
||||
map<int32, EnumValue> int32_to_enum_field = 4;
|
||||
map<int32, MessageValue> int32_to_message_field = 5;
|
||||
map<string, int32> string_to_int32_field = 6;
|
||||
map<bool, bool> 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, uint32> uint32_to_uint32_field = 11;
|
||||
map<sint32, sint32> sint32_to_sint32_field = 12;
|
||||
map<fixed32, fixed32> fixed32_to_fixed32_field = 13;
|
||||
map<sfixed32, sfixed32> sfixed32_to_sfixed32_field = 14;
|
||||
map<int64, int64> int64_to_int64_field = 15;
|
||||
map<uint64, uint64> uint64_to_uint64_field = 16;
|
||||
map<sint64, sint64> sint64_to_sint64_field = 17;
|
||||
map<fixed64, fixed64> fixed64_to_fixed64_field = 18;
|
||||
map<sfixed64, sfixed64> sfixed64_to_sfixed64_field = 19;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user