Kaynağa Gözat

also test with ParseContext in CodedInputStream test

Jan Tattermusch 5 yıl önce
ebeveyn
işleme
f1d12ac768

+ 79 - 0
csharp/src/Google.Protobuf.Test/CodedInputStreamTest.cs

@@ -31,6 +31,7 @@
 #endregion
 
 using System;
+using System.Buffers;
 using System.IO;
 using Google.Protobuf.TestProtos;
 using Proto2 = Google.Protobuf.TestProtos.Proto2;
@@ -68,6 +69,16 @@ namespace Google.Protobuf
             Assert.AreEqual(value, input.ReadRawVarint64());
             Assert.IsTrue(input.IsAtEnd);
 
+            AssertReadFromParseContext(new ReadOnlySequence<byte>(data), (ref ParseContext ctx) =>
+            {
+                Assert.AreEqual((uint) value, ctx.ReadUInt32());
+            }, true);
+
+            AssertReadFromParseContext(new ReadOnlySequence<byte>(data), (ref ParseContext ctx) =>
+            {
+                Assert.AreEqual(value, ctx.ReadUInt64());
+            }, true);
+
             // Try different block sizes.
             for (int bufferSize = 1; bufferSize <= 16; bufferSize *= 2)
             {
@@ -77,6 +88,16 @@ namespace Google.Protobuf
                 input = new CodedInputStream(new SmallBlockInputStream(data, bufferSize));
                 Assert.AreEqual(value, input.ReadRawVarint64());
                 Assert.IsTrue(input.IsAtEnd);
+
+                AssertReadFromParseContext(ReadOnlySequenceFactory.CreateWithContent(data, bufferSize), (ref ParseContext ctx) =>
+                {
+                    Assert.AreEqual((uint) value, ctx.ReadUInt32());
+                }, true);
+
+                AssertReadFromParseContext(ReadOnlySequenceFactory.CreateWithContent(data, bufferSize), (ref ParseContext ctx) =>
+                {
+                    Assert.AreEqual(value, ctx.ReadUInt64());
+                }, true);
             }
 
             // Try reading directly from a MemoryStream. We want to verify that it
@@ -105,11 +126,49 @@ namespace Google.Protobuf
             exception = Assert.Throws<InvalidProtocolBufferException>(() => input.ReadRawVarint64());
             Assert.AreEqual(expected.Message, exception.Message);
 
+            AssertReadFromParseContext(new ReadOnlySequence<byte>(data), (ref ParseContext ctx) =>
+            {
+                try
+                {
+                    ctx.ReadUInt32();
+                    Assert.Fail();
+                }
+                catch (InvalidProtocolBufferException ex)
+                {
+                    Assert.AreEqual(expected.Message, ex.Message);
+                }
+            }, false);
+
+            AssertReadFromParseContext(new ReadOnlySequence<byte>(data), (ref ParseContext ctx) =>
+            {
+                try
+                {
+                    ctx.ReadUInt64();
+                    Assert.Fail();
+                }
+                catch (InvalidProtocolBufferException ex)
+                {
+                    Assert.AreEqual(expected.Message, ex.Message);
+                }
+            }, false);
+
             // Make sure we get the same error when reading directly from a Stream.
             exception = Assert.Throws<InvalidProtocolBufferException>(() => CodedInputStream.ReadRawVarint32(new MemoryStream(data)));
             Assert.AreEqual(expected.Message, exception.Message);
         }
 
+        private delegate void ParseContextAssertAction(ref ParseContext ctx);
+
+        private static void AssertReadFromParseContext(ReadOnlySequence<byte> input, ParseContextAssertAction assertAction, bool assertIsAtEnd)
+        {
+            ParseContext.Initialize(input, out ParseContext parseCtx);
+            assertAction(ref parseCtx);
+            if (assertIsAtEnd)
+            {
+                Assert.IsTrue(SegmentedBufferHelper.IsAtEnd(ref parseCtx.buffer, ref parseCtx.state));
+            }
+        }
+
         [Test]
         public void ReadVarint()
         {
@@ -158,6 +217,11 @@ namespace Google.Protobuf
             Assert.AreEqual(value, input.ReadRawLittleEndian32());
             Assert.IsTrue(input.IsAtEnd);
 
+            AssertReadFromParseContext(new ReadOnlySequence<byte>(data), (ref ParseContext ctx) =>
+            {
+                Assert.AreEqual(value, ctx.ReadFixed32());
+            }, true);
+
             // Try different block sizes.
             for (int blockSize = 1; blockSize <= 16; blockSize *= 2)
             {
@@ -165,6 +229,11 @@ namespace Google.Protobuf
                     new SmallBlockInputStream(data, blockSize));
                 Assert.AreEqual(value, input.ReadRawLittleEndian32());
                 Assert.IsTrue(input.IsAtEnd);
+
+                AssertReadFromParseContext(ReadOnlySequenceFactory.CreateWithContent(data, blockSize), (ref ParseContext ctx) =>
+                {
+                    Assert.AreEqual(value, ctx.ReadFixed32());
+                }, true);
             }
         }
 
@@ -178,6 +247,11 @@ namespace Google.Protobuf
             Assert.AreEqual(value, input.ReadRawLittleEndian64());
             Assert.IsTrue(input.IsAtEnd);
 
+            AssertReadFromParseContext(new ReadOnlySequence<byte>(data), (ref ParseContext ctx) =>
+            {
+                Assert.AreEqual(value, ctx.ReadFixed64());
+            }, true);
+
             // Try different block sizes.
             for (int blockSize = 1; blockSize <= 16; blockSize *= 2)
             {
@@ -185,6 +259,11 @@ namespace Google.Protobuf
                     new SmallBlockInputStream(data, blockSize));
                 Assert.AreEqual(value, input.ReadRawLittleEndian64());
                 Assert.IsTrue(input.IsAtEnd);
+
+                AssertReadFromParseContext(ReadOnlySequenceFactory.CreateWithContent(data, blockSize), (ref ParseContext ctx) =>
+                {
+                    Assert.AreEqual(value, ctx.ReadFixed64());
+                }, true);
             }
         }
 

+ 1 - 2
csharp/src/Google.Protobuf.Test/MessageParsingHelpers.cs

@@ -32,7 +32,6 @@
 
 using NUnit.Framework;
 using System;
-using Google.Protobuf.Buffers;
 using System.Buffers;
 
 namespace Google.Protobuf
@@ -65,7 +64,7 @@ namespace Google.Protobuf
             // Load content as multiple segments
             parsedBuffer = parser.ParseFrom(ReadOnlySequenceFactory.CreateWithContent(bytes));
             assert(parsedBuffer);
-            
+
             assert(parsedStream);
         }
 

+ 1 - 1
csharp/src/Google.Protobuf.Test/ReadOnlySequenceFactory.cs

@@ -37,7 +37,7 @@ using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 
-namespace Google.Protobuf.Buffers
+namespace Google.Protobuf
 {
     internal static class ReadOnlySequenceFactory
     {