Ejemplo n.º 1
0
        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);
        }
Ejemplo n.º 2
0
        public void Rewind_ByOne()
        {
            ReadOnlySequence <byte> bytes = SequenceFactory.Create(new byte[][] {
                new byte[] { 0 },
                new byte[] { 1, 2 },
                new byte[] { 3, 4 },
                new byte[] { 5, 6, 7, 8 }
            });

            ByteBufferReader reader = new ByteBufferReader(bytes);

            reader.Advance(1);
            ByteBufferReader copy = reader;

            for (int i = 1; i < bytes.Length; i++)
            {
                reader.Advance(i);
                Assert.Equal(i + 1, reader.Consumed);

                for (int j = 0; j < i; j++)
                {
                    reader.Rewind(1);
                    Assert.Equal(i - j, reader.Consumed);
                    Assert.False(reader.End);
                }

                Assert.Equal(copy.Position, reader.Position);
                Assert.Equal(copy.Consumed, reader.Consumed);
                Assert.Equal(copy.CurrentSpanIndex, reader.CurrentSpanIndex);
                Assert.Equal(copy.End, reader.End);
                Assert.True(copy.CurrentSpan.SequenceEqual(reader.CurrentSpan));
            }
        }
Ejemplo n.º 3
0
        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]);
        }
Ejemplo n.º 4
0
        public void TryReadTo_NotFound_Sequence(bool advancePastDelimiter)
        {
            ReadOnlySequence <byte> bytes = SequenceFactory.Create(new byte[][] {
                new byte[] { 1 },
                new byte[] { 2, 3, 255 }
            });

            ByteBufferReader reader = new ByteBufferReader(bytes);

            reader.Advance(4);
            Assert.False(reader.TryReadTo(out ReadOnlySequence <byte> span, 255, 0, advancePastDelimiter));
        }
Ejemplo n.º 5
0
        public void RewindEmptyFirstSpan()
        {
            // This is to hit the "if (memory.Length == 0)" branch in ResetReader.
            ReadOnlySequence <byte> bytes = SequenceFactory.Create(new byte[][] {
                new byte[0],
                new byte[] { 1, 2 },
                new byte[] { 3, 4 }
            });

            var reader = new ByteBufferReader(bytes);

            reader.Advance(3);
            Assert.True(reader.IsNext(4));
            reader.Rewind(2);
            Assert.Equal(new byte[] { 1, 2 }, reader.CurrentSpan.ToArray());
        }
Ejemplo n.º 6
0
        public void PastEmptySegments()
        {
            ReadOnlySequence <byte> bytes = SequenceFactory.Create(new byte[][] {
                new byte[] { 0 },
                new byte[] { },
                new byte[] { },
                new byte[] { }
            });

            ByteBufferReader reader = new ByteBufferReader(bytes);

            reader.Advance(1);
            Assert.Equal(0, reader.CurrentSpanIndex);
            Assert.Equal(0, reader.CurrentSpan.Length);
            Assert.False(reader.TryPeek(out byte value));
            ReadOnlySequence <byte> sequence = reader.Sequence.Slice(reader.Position);

            Assert.Equal(0, sequence.Length);
        }
Ejemplo n.º 7
0
        public void Rewind_Exception()
        {
            ReadOnlySequence <byte> bytes = SequenceFactory.Create(new byte[][] {
                new byte[] { 0 },
                new byte[] { 1, 2 }
            });

            // Can't go negative
            Assert.Throws <ArgumentOutOfRangeException>(() => new ByteBufferReader(bytes).Rewind(-1));

            // Can't pull more than we consumed
            ByteBufferReader reader = new ByteBufferReader(bytes);

            try
            {
                reader.Rewind(1);
                Assert.True(false, "no exception thrown");
            }
            catch (ArgumentOutOfRangeException)
            {
                // Expected
            }
            Assert.Equal(0, reader.Consumed);

            reader.Advance(1);
            try
            {
                reader.Rewind(2);
                Assert.True(false, "no exception thrown");
            }
            catch (ArgumentOutOfRangeException)
            {
                // Expected
            }
            Assert.Equal(1, reader.Consumed);
        }