diff options
Diffstat (limited to 'csharp/src/Google.Protobuf.Test/WellKnownTypes/WrappersTest.cs')
-rw-r--r-- | csharp/src/Google.Protobuf.Test/WellKnownTypes/WrappersTest.cs | 35 |
1 files changed, 29 insertions, 6 deletions
diff --git a/csharp/src/Google.Protobuf.Test/WellKnownTypes/WrappersTest.cs b/csharp/src/Google.Protobuf.Test/WellKnownTypes/WrappersTest.cs index c87ceb2f..a2c833fe 100644 --- a/csharp/src/Google.Protobuf.Test/WellKnownTypes/WrappersTest.cs +++ b/csharp/src/Google.Protobuf.Test/WellKnownTypes/WrappersTest.cs @@ -149,8 +149,34 @@ namespace Google.Protobuf.WellKnownTypes } [Test] + public void RepeatedWrappersBinaryFormat() + { + // At one point we accidentally used a packed format for repeated wrappers, which is wrong (and weird). + // This test is just to prove that we use the right format. + + var rawOutput = new MemoryStream(); + var output = new CodedOutputStream(rawOutput); + // Write a value of 5 + output.WriteTag(RepeatedWellKnownTypes.Int32FieldFieldNumber, WireFormat.WireType.LengthDelimited); + output.WriteLength(2); + output.WriteTag(WrappersReflection.WrapperValueFieldNumber, WireFormat.WireType.Varint); + output.WriteInt32(5); + // Write a value of 0 (empty message) + output.WriteTag(RepeatedWellKnownTypes.Int32FieldFieldNumber, WireFormat.WireType.LengthDelimited); + output.WriteLength(0); + output.Flush(); + var expectedBytes = rawOutput.ToArray(); + + var message = new RepeatedWellKnownTypes { Int32Field = { 5, 0 } }; + var actualBytes = message.ToByteArray(); + Assert.AreEqual(expectedBytes, actualBytes); + } + + [Test] public void MapWrappersSerializeDeserialize() { + // Note: no null values here, as they are prohibited in map fields + // (despite being representable). var message = new MapWellKnownTypes { BoolField = { { 10, false }, { 20, true } }, @@ -158,13 +184,12 @@ namespace Google.Protobuf.WellKnownTypes { -1, ByteString.CopyFrom(1, 2, 3) }, { 10, ByteString.CopyFrom(4, 5, 6) }, { 1000, ByteString.Empty }, - { 10000, null } }, DoubleField = { { 1, 12.5 }, { 10, -1.5 }, { 20, 0d } }, FloatField = { { 2, 123.25f }, { 3, -20f }, { 4, 0f } }, Int32Field = { { 5, int.MaxValue }, { 6, int.MinValue }, { 7, 0 } }, Int64Field = { { 8, long.MaxValue }, { 9, long.MinValue }, { 10, 0L } }, - StringField = { { 11, "First" }, { 12, "Second" }, { 13, "" }, { 14, null } }, + StringField = { { 11, "First" }, { 12, "Second" }, { 13, "" } }, Uint32Field = { { 15, uint.MaxValue }, { 16, uint.MinValue }, { 17, 0U } }, Uint64Field = { { 18, ulong.MaxValue }, { 19, ulong.MinValue }, { 20, 0UL } }, }; @@ -224,13 +249,11 @@ namespace Google.Protobuf.WellKnownTypes [Test] public void Reflection_MapFields() { - // Just a single example... note that we can't have a null value here - var message = new MapWellKnownTypes { Int32Field = { { 1, 2 }, { 3, null } } }; + // Just a single example... note that we can't have a null value here despite the value type being int? + var message = new MapWellKnownTypes { Int32Field = { { 1, 2 } } }; var fields = MapWellKnownTypes.Descriptor.Fields; var dictionary = (IDictionary) fields[MapWellKnownTypes.Int32FieldFieldNumber].Accessor.GetValue(message); Assert.AreEqual(2, dictionary[1]); - Assert.IsNull(dictionary[3]); - Assert.IsTrue(dictionary.Contains(3)); } [Test] |