示例#1
0
        public void Advance_Exception()
        {
            ReadOnlySequence <byte> bytes = SequenceFactory.Create(new byte[][] {
                new byte[] { 0 },
                new byte[] { 1, 2 },
                new byte[] { 3, 4 },
                new byte[] { 5, 6, 7, 8 }
            });

            Assert.Throws <ArgumentOutOfRangeException>(() => new ByteBufferReader(bytes).Advance(-1));
        }
示例#2
0
文件: ReadTo.cs 项目: dora-BYR/Fenix
        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));
        }
示例#3
0
文件: ReadTo.cs 项目: dora-BYR/Fenix
        public void TryReadTo_Sequence(bool advancePastDelimiter, bool useEscapeOverload)
        {
            ReadOnlySequence <byte> bytes = SequenceFactory.Create(new byte[][] {
                new byte[] { 0 },
                new byte[] { 1, 2 },
                new byte[] { },
                new byte[] { 3, 4, 5, 6 }
            });

            ByteBufferReader reader = new ByteBufferReader(bytes);

            // Read to 0-5
            for (byte i = 0; i < bytes.Length - 1; i++)
            {
                ByteBufferReader copy = reader;

                // Can read to the first integer (0-5)
                Assert.True(
                    useEscapeOverload
                        ? copy.TryReadTo(out ReadOnlySequence <byte> sequence, i, 255, advancePastDelimiter)
                        : copy.TryReadTo(out sequence, i, advancePastDelimiter));

                // Should never have a null Position object
                Assert.NotNull(copy.Position.GetObject());
                ReadOnlySequence <byte> .Enumerator enumerator = sequence.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    ;
                }

                // Should be able to read to final 6
                Assert.True(
                    useEscapeOverload
                        ? copy.TryReadTo(out sequence, 6, 255, advancePastDelimiter)
                        : copy.TryReadTo(out sequence, 6, advancePastDelimiter));

                Assert.NotNull(copy.Position.GetObject());
                enumerator = sequence.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    ;
                }

                // If we didn't advance, we should still be able to read to 6
                Assert.Equal(!advancePastDelimiter,
                             useEscapeOverload
                        ? copy.TryReadTo(out sequence, 6, 255, advancePastDelimiter)
                        : copy.TryReadTo(out sequence, 6, advancePastDelimiter));
            }
        }
示例#4
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());
        }
示例#5
0
文件: IsNext.cs 项目: dora-BYR/Fenix
        public void IsNext_Value()
        {
            ReadOnlySequence <byte> chars = SequenceFactory.Create(new byte[][] {
                new byte[] { (byte)'A' },
                new byte[] { (byte)'B', (byte)'C' },
            });

            var reader = new ByteBufferReader(chars);

            Assert.False(reader.IsNext((byte)'Z', advancePast: false));
            Assert.False(reader.IsNext((byte)'B', advancePast: false));
            Assert.True(reader.IsNext((byte)'A', advancePast: false));
            Assert.True(reader.IsNext((byte)'A', advancePast: true));
            Assert.True(reader.IsNext((byte)'B', advancePast: true));
            Assert.True(reader.IsNext((byte)'C', advancePast: true));
            Assert.False(reader.IsNext((byte)'C', advancePast: true));
            Assert.True(reader.End);
        }
示例#6
0
        public void TryReadToSpan_Sequence(bool advancePastDelimiter)
        {
            ReadOnlySequence <byte> bytes = SequenceFactory.Create(new byte[][] {
                new byte[] { 0, 0 },
                new byte[] { 1, 1, 2, 2 },
                new byte[] { },
                new byte[] { 3, 3, 4, 4, 5, 5, 6, 6 }
            });

            ByteBufferReader baseReader = new ByteBufferReader(bytes);

            for (byte i = 0; i < bytes.Length / 2 - 1; i++)
            {
                byte[] expected = new byte[i * 2 + 1];
                for (int j = 0; j < expected.Length - 1; j++)
                {
                    expected[j] = (byte)(j / 2);
                }
                expected[i * 2] = i;
                ReadOnlySpan <byte> searchFor = new byte[] { i, (byte)(i + 1) };
                ByteBufferReader    copy      = baseReader;

                Assert.True(copy.TryReadTo(out ReadOnlySpan <byte> sp, searchFor, advancePastDelimiter));
                Assert.True(sp.SequenceEqual(expected));

                copy = baseReader;
                Assert.True(copy.TryReadTo(out ReadOnlySequence <byte> seq, searchFor, advancePastDelimiter));
                Assert.True(seq.ToArray().AsSpan().SequenceEqual(expected));
            }

            bytes = SequenceFactory.Create(new byte[][] {
                new byte[] { 47, 42, 66, 32, 42, 32, 66, 42, 47 }   // /*b * b*/
            });

            baseReader = new ByteBufferReader(bytes);
            ByteBufferReader copyReader = baseReader;

            Assert.True(copyReader.TryReadTo(out ReadOnlySpan <byte> span, new byte[] { 42, 47 }, advancePastDelimiter));    //  */
            Assert.True(span.SequenceEqual(new byte[] { 47, 42, 66, 32, 42, 32, 66 }));

            copyReader = baseReader;
            Assert.True(copyReader.TryReadTo(out ReadOnlySequence <byte> sequence, new byte[] { 42, 47 }, advancePastDelimiter));    //  */
            Assert.True(sequence.ToArray().AsSpan().SequenceEqual(new byte[] { 47, 42, 66, 32, 42, 32, 66 }));
        }
示例#7
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);
        }
示例#8
0
文件: ReadTo.cs 项目: dora-BYR/Fenix
        public void TryReadTo_SingleDelimiter()
        {
            ReadOnlySequence <byte> bytes = SequenceFactory.Create(new byte[][] {
                new byte[] { 1 },
                new byte[] { 2, 3, 4, 5, 6 }
            });

            ByteBufferReader reader    = new ByteBufferReader(bytes);
            Span <byte>      delimiter = new byte[] { 1 };

            for (int i = 1; i < 6; i += 1)
            {
                // Also check scanning from the start.
                ByteBufferReader resetReader = new ByteBufferReader(bytes);
                delimiter[0] = (byte)i;
                Assert.True(reader.TryReadTo(out ReadOnlySequence <byte> sequence, delimiter, advancePastDelimiter: true));
                Assert.True(resetReader.TryReadTo(out sequence, delimiter, advancePastDelimiter: true));
                Assert.True(reader.TryPeek(out byte value));
                Assert.Equal(i + 1, value);
                Assert.True(resetReader.TryPeek(out value));
                Assert.Equal(i + 1, value);
            }
        }
示例#9
0
        public void TryCopyTo_Multisegment()
        {
            ReadOnlySequence <byte> chars = SequenceFactory.Create(new byte[][] {
                new byte[] { (byte)'A' },
                new byte[] { (byte)'B', (byte)'C' },
                new byte[] { (byte)'D', (byte)'E', (byte)'F' }
            });

            ReadOnlySpan <byte> linear = new byte[] { (byte)'A', (byte)'B', (byte)'C', (byte)'D', (byte)'E', (byte)'F' };

            var reader = new ByteBufferReader(chars);

            // Something to nothing is always possible
            Assert.True(reader.TryCopyTo(Span <byte> .Empty));
            Span <byte> buffer;

            // Read out ABCDEF, ABCDE, etc.
            for (int i = linear.Length; i > 0; i--)
            {
                buffer = new byte[i];
                Assert.True(reader.TryCopyTo(buffer));
                Assert.True(buffer.SequenceEqual(linear.Slice(0, i)));
            }

            buffer = new byte[1];

            // Read out one at a time and move through
            for (int i = 0; i < linear.Length; i++)
            {
                Assert.True(reader.TryCopyTo(buffer));
                Assert.True(reader.TryRead(out byte value));
                Assert.Equal(buffer[0], value);
            }

            // Trying to get more data than there is will fail
            Assert.False(reader.TryCopyTo(new byte[reader.Remaining + 1]));
        }
示例#10
0
文件: IsNext.cs 项目: dora-BYR/Fenix
        public void IsNext_Span()
        {
            ReadOnlySequence <byte> bytes = SequenceFactory.Create(new byte[][] {
                new byte[] { 0 },
                new byte[] { 1, 2 },
                new byte[] { 3, 4 },
                new byte[] { 5, 6, 7, 8 }
            });

            var reader = new ByteBufferReader(bytes);

            Assert.True(reader.IsNext(ReadOnlySpan <byte> .Empty, advancePast: false));
            Assert.True(reader.IsNext(ReadOnlySpan <byte> .Empty, advancePast: true));
            Assert.True(reader.IsNext(new byte[] { 0 }, advancePast: false));
            Assert.False(reader.IsNext(new byte[] { 0, 2 }, advancePast: false));
            Assert.False(reader.IsNext(new byte[] { 0, 2 }, advancePast: true));
            Assert.True(reader.IsNext(new byte[] { 0, 1 }, advancePast: false));
            Assert.False(reader.IsNext(new byte[] { 0, 1, 3 }, advancePast: false));
            Assert.True(reader.IsNext(new byte[] { 0, 1, 2 }, advancePast: false));
            Assert.False(reader.IsNext(new byte[] { 0, 1, 2, 4 }, advancePast: false));
            Assert.True(reader.IsNext(new byte[] { 0, 1, 2, 3 }, advancePast: false));
            Assert.True(reader.IsNext(new byte[] { 0, 1, 2, 3, 4 }, advancePast: false));
            Assert.True(reader.IsNext(new byte[] { 0, 1, 2, 3, 4, 5 }, advancePast: false));
            Assert.True(reader.IsNext(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 }, advancePast: false));
            Assert.False(reader.IsNext(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, advancePast: false));
            Assert.False(reader.IsNext(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, advancePast: true));
            Assert.Equal(0, reader.Consumed);

            Assert.True(reader.IsNext(new byte[] { 0, 1, 2, 3 }, advancePast: true));
            Assert.True(reader.IsNext(new byte[] { 4, 5, 6 }, advancePast: true));
            Assert.True(reader.TryPeek(out byte value));
            Assert.Equal(7, value);

            Assert.True(reader.IsNext(new byte[] { 7, 8 }, advancePast: true));
            Assert.True(reader.End);
        }
示例#11
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);
        }