public void AdvanceSingleBufferSkipsValues() { ByteBufferReader reader = new ByteBufferReader(SequenceFactory.Create(GetInputData(5))); Assert.Equal(5, reader.Length); Assert.Equal(5, reader.Remaining); Assert.Equal(0, reader.Consumed); Assert.Equal(0, reader.CurrentSpanIndex); // Advance 2 positions reader.Advance(2); Assert.Equal(5, reader.Length); Assert.Equal(3, reader.Remaining); Assert.Equal(2, reader.Consumed); Assert.Equal(2, reader.CurrentSpanIndex); Assert.Equal(InputData[2], reader.CurrentSpan[reader.CurrentSpanIndex]); Assert.True(reader.TryPeek(out byte value)); Assert.Equal(InputData[2], value); // Advance 2 positions reader.Advance(2); Assert.Equal(1, reader.Remaining); Assert.Equal(4, reader.Consumed); Assert.Equal(4, reader.CurrentSpanIndex); Assert.Equal(InputData[4], reader.CurrentSpan[reader.CurrentSpanIndex]); Assert.True(reader.TryPeek(out value)); Assert.Equal(InputData[4], value); }
public void AdvanceSingleBufferSkipsBytes() { var reader = new ByteBufferReader(BufferUtilities.CreateBuffer(GetInputData(5))); reader.Advance(2); Assert.Equal(2, reader.CurrentSpanIndex); Assert.Equal(InputData[2], reader.CurrentSpan[reader.CurrentSpanIndex]); Assert.True(reader.TryPeek(out byte value)); Assert.Equal(InputData[2], value); reader.Advance(2); Assert.True(reader.TryPeek(out value)); Assert.Equal(InputData[4], value); Assert.Equal(4, reader.CurrentSpanIndex); Assert.Equal(InputData[4], reader.CurrentSpan[reader.CurrentSpanIndex]); }
public void TryReadTo_SingleDelimiter() { ReadOnlySequence <byte> bytes = SequenceFactory.Create(new byte[][] { new byte[] { 1 }, new byte[] { 2, 3, 4, 5, 6 } }); ByteBufferReader baseReader = new ByteBufferReader(bytes); ByteBufferReader spanReader = baseReader; ByteBufferReader sequenceReader = baseReader; Span <byte> delimiter = new byte[] { 1 }; for (int i = 1; i < 6; i += 1) { // Also check scanning from the start. ByteBufferReader resetReader = baseReader; delimiter[0] = (byte)i; Assert.True(spanReader.TryReadTo(out ReadOnlySpan <byte> span, delimiter, advancePastDelimiter: true)); Assert.True(resetReader.TryReadTo(out span, delimiter, advancePastDelimiter: true)); Assert.True(spanReader.TryPeek(out byte value)); Assert.Equal(i + 1, value); Assert.True(resetReader.TryPeek(out value)); Assert.Equal(i + 1, value); // Also check scanning from the start. resetReader = baseReader; delimiter[0] = (byte)i; Assert.True(sequenceReader.TryReadTo(out ReadOnlySequence <byte> sequence, delimiter, advancePastDelimiter: true)); Assert.True(resetReader.TryReadTo(out sequence, delimiter, advancePastDelimiter: true)); Assert.True(sequenceReader.TryPeek(out value)); Assert.Equal(i + 1, value); Assert.True(resetReader.TryPeek(out value)); Assert.Equal(i + 1, value); } }