Example #1
0
        public void EmptyBytesReader()
        {
            ReadOnlyBytes bytes  = Create("");
            var           reader = BufferReader.Create(bytes);

            Assert.False(BufferReaderExtensions.TryReadUntill(ref reader, out var range, (byte)' '));

            bytes  = Parse("|");
            reader = BufferReader.Create(bytes);
            Assert.False(BufferReaderExtensions.TryReadUntill(ref reader, out range, (byte)' '));
        }
Example #2
0
        public void BytesReaderParse()
        {
            ReadOnlySequence <byte> bytes = BufferFactory.Parse("12|3Tr|ue|456Tr|ue7|89False|");
            var reader = BufferReader.Create(bytes);

            Assert.True(BufferReaderExtensions.TryParse(ref reader, out ulong u64));
            Assert.Equal(123ul, u64);

            Assert.True(BufferReaderExtensions.TryParse(ref reader, out bool b));
            Assert.Equal(true, b);

            Assert.True(BufferReaderExtensions.TryParse(ref reader, out u64));
            Assert.Equal(456ul, u64);

            Assert.True(BufferReaderExtensions.TryParse(ref reader, out b));
            Assert.Equal(true, b);

            Assert.True(BufferReaderExtensions.TryParse(ref reader, out u64));
            Assert.Equal(789ul, u64);

            Assert.True(BufferReaderExtensions.TryParse(ref reader, out b));
            Assert.Equal(false, b);
        }
Example #3
0
        public void MultiSegmentBytesReaderNumbers()
        {
            var bytes = BufferFactory.Create(new byte[][] {
                new byte[] { 0 },
                new byte[] { 1, 2 },
                new byte[] { 3, 4 },
                new byte[] { 5, 6, 7, 8 },
                new byte[] { 8, 0 },
                new byte[] { 1, },
                new byte[] { 0, 2, },
                new byte[] { 1, 2, 3, 4 },
            });

            var reader = BufferReader.Create(bytes);

            Assert.True(BufferReaderExtensions.TryReadUntill(ref reader, out var bytesValue, 2));
            var span = bytesValue.ToSpan();

            Assert.Equal(0, span[0]);
            Assert.Equal(1, span[1]);

            Assert.True(BufferReaderExtensions.TryReadUntill(ref reader, out bytesValue, 5));
            span = bytesValue.ToSpan();
            Assert.Equal(3, span[0]);
            Assert.Equal(4, span[1]);

            Assert.True(BufferReaderExtensions.TryReadUntill(ref reader, out bytesValue, new byte[] { 8, 8 }));
            span = bytesValue.ToSpan();
            Assert.Equal(6, span[0]);
            Assert.Equal(7, span[1]);

            Assert.True(BufferReaderExtensions.TryRead(ref reader, out int value, true));
            Assert.Equal(BitConverter.ToInt32(new byte[] { 0, 1, 0, 2 }), value);

            Assert.True(BufferReaderExtensions.TryRead(ref reader, out value));
            Assert.Equal(BitConverter.ToInt32(new byte[] { 4, 3, 2, 1 }), value);
        }
Example #4
0
        public void CopyToSmallerBufferWorks()
        {
            var content = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

            Span <byte> buffer = new byte[content.Length];
            var         reader = BufferReader.Create(Factory.CreateWithContent(content));

            // this loop skips more and more items in the reader
            for (int i = 0; i < content.Length; i++)
            {
                // this loop makes the destination buffer smaller and smaller
                for (int j = 0; j < buffer.Length - i; j++)
                {
                    var bufferSlice = buffer.Slice(0, j);
                    bufferSlice.Clear();
                    int copied = BufferReaderExtensions.Peek(reader, bufferSlice);
                    Assert.Equal(Math.Min(bufferSlice.Length, content.Length - i), copied);

                    Assert.True(bufferSlice.Slice(0, copied).SequenceEqual(content.AsSpan().Slice(i, j)));
                }

                reader.Advance(1);
            }
        }
Example #5
0
        public void CopyToLargerBufferWorks()
        {
            var content = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

            Span <byte> buffer = new byte[content.Length + 1];
            var         reader = BufferReader.Create(Factory.CreateWithContent(content));

            // this loop skips more and more items in the reader
            for (int i = 0; i < content.Length; i++)
            {
                int copied = BufferReaderExtensions.Peek(reader, buffer);
                Assert.Equal(content.Length - i, copied);
                Assert.True(buffer.Slice(0, copied).SequenceEqual(content.AsSpan().Slice(i)));

                // make sure that nothing more got written, i.e. tail is empty
                for (int r = copied; r < buffer.Length; r++)
                {
                    Assert.Equal(0, buffer[r]);
                }

                reader.Advance(1);
                buffer.Clear();
            }
        }