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

            BufferReader <byte> reader = new BufferReader <byte>(bytes);

            reader.Advance(1);
            BufferReader <byte> copy = reader;

            for (int i = 1; i < bytes.Length; i++)
            {
                reader.Advance(i);
                for (int j = 0; j < i; j++)
                {
                    reader.Rewind(1);
                    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));
            }
        }
Beispiel #2
0
            public void AdvanceSingleBufferSkipsBytes()
            {
                var reader = new BufferReader(Factory.CreateWithContent(new byte[] { 1, 2, 3, 4, 5 }));

                reader.Advance(2);
                Assert.Equal(2, reader.CurrentSegmentIndex);
                Assert.Equal(3, reader.CurrentSegment[reader.CurrentSegmentIndex]);
                Assert.Equal(3, reader.Peek());
                reader.Advance(2);
                Assert.Equal(5, reader.Peek());
                Assert.Equal(4, reader.CurrentSegmentIndex);
                Assert.Equal(5, reader.CurrentSegment[reader.CurrentSegmentIndex]);
            }
Beispiel #3
0
            public void AdvanceSingleBufferSkipsBytes()
            {
                var reader = new BufferReader(BufferUtilities.CreateBuffer(new byte[] { 1, 2, 3, 4, 5 }));

                reader.Advance(2);
                Assert.Equal(2, reader.CurrentSpanIndex);
                Assert.Equal(3, reader.CurrentSpan[reader.CurrentSpanIndex]);
                Assert.Equal(3, reader.Peek());
                reader.Advance(2);
                Assert.Equal(5, reader.Peek());
                Assert.Equal(4, reader.CurrentSpanIndex);
                Assert.Equal(5, reader.CurrentSpan[reader.CurrentSpanIndex]);
            }
 public void AdvanceSingleBufferSkipsBytes()
 {
     var reader = new BufferReader<T>(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 T 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]);
 }
Beispiel #5
0
        public void AdvanceToEndThenPeekReturnsMinusOne()
        {
            var reader = new BufferReader(Factory.CreateWithContent(new byte[] { 1, 2, 3, 4, 5 }));

            reader.Advance(5);
            Assert.True(reader.End);
            Assert.Equal(-1, reader.Peek());
        }
Beispiel #6
0
        public void ParseInt32BufferReaderRaw()
        {
            BufferReader <byte> reader = new BufferReader <byte>(s_ros);

            while (Utf8Parser.TryParse(reader.CurrentSpan.Slice((int)reader.Consumed), out int value, out int consumed))
            {
                reader.Advance(consumed + 1);
            }
        }
Beispiel #7
0
        public void AdvanceTraversesSegments()
        {
            var buffer = Factory.CreateWithContent(new byte[] { 1, 2, 3 });
            var reader = new BufferReader(buffer);

            reader.Advance(2);
            Assert.Equal(3, reader.CurrentSegment[reader.CurrentSegmentIndex]);
            Assert.Equal(3, reader.Read());
        }
Beispiel #8
0
        public void EmptySegmentsAreSkippedOnMoveNext()
        {
            var buffer = Factory.CreateWithContent(new byte[] { 1, 2 });
            var reader = new BufferReader(buffer);

            Assert.Equal(1, reader.Peek());
            reader.Advance(1);
            Assert.Equal(2, reader.Peek());
        }
Beispiel #9
0
        public void ParseInt32BufferReader()
        {
            BufferReader <byte> reader = new BufferReader <byte>(s_ros);

            while (reader.TryParse(out int value))
            {
                reader.Advance(1); // advance past the delimiter
            }
        }
Beispiel #10
0
        public void TryParseRos()
        {
            var reader = new BufferReader <byte>(s_ros);

            while (reader.TryParse(out int value))
            {
                reader.Advance(1); // advance past the delimiter
                Assert.Equal(1234, value);
            }
        }
Beispiel #11
0
        public void TryReadTo_NotFound_Sequence(bool advancePastDelimiter)
        {
            ReadOnlySequence <byte> bytes = BufferFactory.Create(new byte[][] {
                new byte[] { 1 },
                new byte[] { 2, 3, 255 }
            });

            BufferReader <byte> reader = new BufferReader <byte>(bytes);

            reader.Advance(4);
            Assert.False(reader.TryReadTo(out ReadOnlySequence <byte> span, 255, 0, advancePastDelimiter));
        }
Beispiel #12
0
        public void AdvancingPastLengthThrows()
        {
            var reader = new BufferReader(Factory.CreateWithContent(new byte[] { 1, 2, 3, 4, 5 }));

            try
            {
                reader.Advance(6);
                Assert.True(false);
            }
            catch (Exception ex)
            {
                Assert.True(ex is ArgumentOutOfRangeException);
            }
        }
Beispiel #13
0
            static bool TryReadAddress(ref BufferReader <byte> reader, out IPAddress address)
            {
                Span <byte> addressBuffer = stackalloc byte[ADDRESS_SIZE];

                if (reader.TryCopyTo(addressBuffer))
                {
                    reader.Advance(ADDRESS_SIZE);
                    address = new IPAddress(addressBuffer);
                    return(true);
                }

                address = null !;
                return(false);
            }
Beispiel #14
0
        public void AdvanceThrowsPastLengthMultipleSegments()
        {
            var buffer = Factory.CreateWithContent(new byte[] { 1, 2, 3 });
            var reader = new BufferReader(buffer);

            try
            {
                reader.Advance(4);
                Assert.True(false);
            }
            catch (Exception ex)
            {
                Assert.True(ex is ArgumentOutOfRangeException);
            }
        }
Beispiel #15
0
            static bool TryReadCommandString(ref BufferReader <byte> reader, out string command)
            {
                Span <byte> commandBytes = stackalloc byte[CommandSize];

                if (reader.TryCopyTo(commandBytes))
                {
                    reader.Advance(CommandSize);
                    var index = commandBytes.IndexOf((byte)0);
                    command = index == -1
                        ? Encoding.ASCII.GetString(commandBytes)
                        : Encoding.ASCII.GetString(commandBytes.Slice(0, index));
                    return(true);
                }

                command = null !;
                return(false);
            }
Beispiel #16
0
        public void ReaderIndexIsCorrect()
        {
            var buffer = Factory.CreateWithContent(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
            var reader = new BufferReader(buffer);

            var counter = 1;

            while (!reader.End)
            {
                var span = reader.CurrentSegment;
                for (int i = reader.CurrentSegmentIndex; i < span.Length; i++)
                {
                    Assert.Equal(counter++, reader.CurrentSegment[i]);
                }
                reader.Advance(span.Length);
            }
            Assert.Equal(buffer.Length, reader.ConsumedBytes);
        }
Beispiel #17
0
        // StorageKey.Key uses an atypical storage pattern relative to other models in Neo.
        // The byte array is written in blocks of BLOCK_SIZE (aka 16) bytes  followed by a byte
        // indicating how many bytes of the previous block were padding. Only the last block is
        // allowed to have padding. Read blocks of BLOCK_SIZE + 1 until padding indication byte
        // is greater than zero.

        public static bool TryRead(ref BufferReader <byte> reader, out StorageKey value)
        {
            const int READ_BLOCK_SIZE = BLOCK_SIZE + 1;

            if (UInt160.TryRead(ref reader, out var scriptHash))
            {
                using var bufferOwner = MemoryPool <byte> .Shared.Rent(READ_BLOCK_SIZE);

                var buffer = bufferOwner.Memory.Slice(0, READ_BLOCK_SIZE).Span;
                var writer = new ArrayBufferWriter <byte>();

                while (true)
                {
                    if (!reader.TryCopyTo(buffer))
                    {
                        value = default;
                        return(false);
                    }
                    reader.Advance(READ_BLOCK_SIZE);

                    var dataSize = BLOCK_SIZE - buffer[BLOCK_SIZE];
                    buffer.Slice(0, dataSize).CopyTo(writer.GetSpan(dataSize));
                    writer.Advance(dataSize);

                    if (dataSize < BLOCK_SIZE)
                    {
                        break;
                    }
                }

                // unfortunately, since we don't know a priori how many blocks there will be
                // or how much padding the last block will have, we have to make another copy
                // of the key array. However, we can use Unsafe.As to cast the mutable key array
                // into an ImmutableArray
                var keyArray = writer.WrittenSpan.ToArray();
                value = new StorageKey(scriptHash, Unsafe.As <byte[], ImmutableArray <byte> >(ref keyArray));

                return(true);
            }

            value = default;
            return(false);
        }
Beispiel #18
0
        public void AdvancePastEmptySegments()
        {
            ReadOnlySequence <byte> bytes = BufferFactory.Create(new byte[][] {
                new byte[] { 0 },
                new byte[] { },
                new byte[] { },
                new byte[] { }
            });

            var reader = new BufferReader <byte>(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);
        }
Beispiel #19
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 = new BufferReader(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++)
                {
                    Span <byte> bufferSlice = buffer.Slice(0, j);
                    bufferSlice.Clear();
                    ReadOnlySpan <byte> peeked = reader.Peek(bufferSlice);
                    Assert.Equal(Math.Min(bufferSlice.Length, content.Length - i), peeked.Length);

                    Assert.True(peeked.SequenceEqual(content.AsSpan(i, j)));
                }

                reader.Advance(1);
            }
        }
Beispiel #20
0
        private static void BytesReaderBenchmarkBaseline()
        {
            int           sections = 10;
            StringBuilder sb       = new StringBuilder();

            for (int i = 0; i < sections; i++)
            {
                sb.Append("1234 ");
            }
            var data = Encoding.UTF8.GetBytes(sb.ToString());

            var readOnlyBytes = new ReadOnlySequence <byte>(data);
            var bytesRange    = new ReadOnlySequence <byte>(data);

            var robReader = new BufferReader <byte>(readOnlyBytes);

            long robSum = 0;

            while (robReader.TryParse(out int value))
            {
                robSum += value;
                robReader.Advance(1);
            }

            var  brReader = new BufferReader <byte>(bytesRange);
            long brSum    = 0;

            while (brReader.TryParse(out int value))
            {
                brSum += value;
                brReader.Advance(1);
            }

            Assert.Equal(robSum, brSum);
            Assert.NotEqual(brSum, 0);
        }
Beispiel #21
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 = new BufferReader(Factory.CreateWithContent(content));

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