diff --git a/csharp/src/Google.Protobuf.Test/GeneratedMessageTest.cs b/csharp/src/Google.Protobuf.Test/GeneratedMessageTest.cs index 3499e6617d..06af5e9e9a 100644 --- a/csharp/src/Google.Protobuf.Test/GeneratedMessageTest.cs +++ b/csharp/src/Google.Protobuf.Test/GeneratedMessageTest.cs @@ -132,6 +132,8 @@ namespace Google.Protobuf byte[] bytes = message.ToByteArray(); Assert.AreEqual(0, bytes.Length); + MessageParsingHelpers.AssertWritingMessage(message); + MessageParsingHelpers.AssertRoundtrip(TestAllTypes.Parser, message); } @@ -164,7 +166,7 @@ namespace Google.Protobuf SingleUint64 = ulong.MaxValue }; - byte[] bytes = message.ToByteArray(); + MessageParsingHelpers.AssertWritingMessage(message); MessageParsingHelpers.AssertRoundtrip(TestAllTypes.Parser, message); } @@ -198,7 +200,7 @@ namespace Google.Protobuf RepeatedUint64 = { ulong.MaxValue, uint.MinValue } }; - byte[] bytes = message.ToByteArray(); + MessageParsingHelpers.AssertWritingMessage(message); MessageParsingHelpers.AssertRoundtrip(TestAllTypes.Parser, message); } @@ -230,7 +232,7 @@ namespace Google.Protobuf } }; - byte[] bytes = message.ToByteArray(); + MessageParsingHelpers.AssertWritingMessage(message); MessageParsingHelpers.AssertRoundtrip(TestMap.Parser, message); } @@ -246,6 +248,8 @@ namespace Google.Protobuf byte[] bytes = message.ToByteArray(); Assert.AreEqual(2, bytes.Length); // Tag for field entry (1 byte), length of entry (0; 1 byte) + MessageParsingHelpers.AssertWritingMessage(message); + MessageParsingHelpers.AssertReadingMessage( TestMap.Parser, bytes, @@ -660,6 +664,8 @@ namespace Google.Protobuf var bytes = message.ToByteArray(); Assert.AreEqual(3, bytes.Length); // 2 bytes for the tag + 1 for the value - no string! + MessageParsingHelpers.AssertWritingMessage(message); + MessageParsingHelpers.AssertRoundtrip(TestAllTypes.Parser, message, parsedMessage => { Assert.AreEqual(TestAllTypes.OneofFieldOneofCase.OneofUint32, parsedMessage.OneofFieldCase); @@ -675,6 +681,8 @@ namespace Google.Protobuf var bytes = message.ToByteArray(); Assert.AreEqual(3, bytes.Length); // 2 bytes for the tag + 1 for the value - it's still serialized + MessageParsingHelpers.AssertWritingMessage(message); + MessageParsingHelpers.AssertRoundtrip(TestAllTypes.Parser, message, parsedMessage => { Assert.AreEqual(TestAllTypes.OneofFieldOneofCase.OneofUint32, parsedMessage.OneofFieldCase); diff --git a/csharp/src/Google.Protobuf.Test/MessageParsingHelpers.cs b/csharp/src/Google.Protobuf.Test/MessageParsingHelpers.cs index ec5f13abc3..031319eb92 100644 --- a/csharp/src/Google.Protobuf.Test/MessageParsingHelpers.cs +++ b/csharp/src/Google.Protobuf.Test/MessageParsingHelpers.cs @@ -33,6 +33,7 @@ using NUnit.Framework; using System; using System.Buffers; +using Google.Protobuf.Buffers; namespace Google.Protobuf { @@ -81,6 +82,11 @@ namespace Google.Protobuf { var bytes = message.ToByteArray(); + // also serialize using IBufferWriter and check it leads to the same data + var bufferWriter = new ArrayBufferWriter(); + message.WriteTo(bufferWriter); + Assert.AreEqual(bytes, bufferWriter.WrittenSpan.ToArray(), "Both serialization approaches need to result in the same data."); + // Load content as single segment var parsedBuffer = parser.ParseFrom(new ReadOnlySequence(bytes)); Assert.AreEqual(message, parsedBuffer); @@ -96,5 +102,18 @@ namespace Google.Protobuf Assert.AreEqual(message, parsedStream); additionalAssert?.Invoke(parsedStream); } + + public static void AssertWritingMessage(IMessage message) + { + // serialize using CodedOutputStream + var bytes = message.ToByteArray(); + + // also serialize using IBufferWriter and check it leads to the same data + var bufferWriter = new ArrayBufferWriter(); + message.WriteTo(bufferWriter); + Assert.AreEqual(bytes, bufferWriter.WrittenSpan.ToArray(), "Both serialization approaches need to result in the same data."); + + Assert.AreEqual(message.CalculateSize(), bytes.Length); + } } } \ No newline at end of file