Exemplo n.º 1
0
        public void ReadTWorksAgainstSimpleBuffers()
        {
            var readable = BufferUtilities.CreateBuffer(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7 });
            var span     = readable.First.Span;

            Assert.True(readable.IsSingleSegment);
            Assert.Equal(ReadMachineEndian <byte>(span), readable.ReadLittleEndian <byte>());
            Assert.Equal(ReadMachineEndian <sbyte>(span), readable.ReadLittleEndian <sbyte>());
            Assert.Equal(ReadMachineEndian <short>(span), readable.ReadLittleEndian <short>());
            Assert.Equal(ReadMachineEndian <ushort>(span), readable.ReadLittleEndian <ushort>());
            Assert.Equal(ReadMachineEndian <int>(span), readable.ReadLittleEndian <int>());
            Assert.Equal(ReadMachineEndian <uint>(span), readable.ReadLittleEndian <uint>());
            Assert.Equal(ReadMachineEndian <long>(span), readable.ReadLittleEndian <long>());
            Assert.Equal(ReadMachineEndian <ulong>(span), readable.ReadLittleEndian <ulong>());
            Assert.Equal(ReadMachineEndian <float>(span), readable.ReadLittleEndian <float>());
            Assert.Equal(ReadMachineEndian <double>(span), readable.ReadLittleEndian <double>());
        }
        public void PeekTraversesSegments()
        {
            var buffer = BufferUtilities.CreateBuffer(new[] { new byte[] { 1 }, new byte[] { 2 } });
            var reader = new ReadableBufferReader(buffer);

            Assert.Equal(0, reader.Index);
            Assert.Equal(1, reader.Span.Length);
            Assert.Equal(1, reader.Span[0]);
            Assert.Equal(1, reader.Take());
            Assert.Equal(0, reader.Index);
            Assert.Equal(1, reader.Span.Length);
            Assert.Equal(2, reader.Span[0]);
            Assert.Equal(2, reader.Peek());
            Assert.Equal(2, reader.Take());
            Assert.Equal(-1, reader.Peek());
            Assert.Equal(-1, reader.Take());
        }
Exemplo n.º 3
0
        public void ReaderIndexIsCorrect()
        {
            var buffer = BufferUtilities.CreateBuffer(new[] { new byte[] { 1, 2, 3, 4 }, new byte[] { 5, 6, 7 }, new byte[] { 8, 9, 10 } });
            var reader = new ReadableBufferReader(buffer);

            var counter = 1;

            while (!reader.End)
            {
                var span = reader.Span;
                for (int i = reader.Index; i < span.Length; i++)
                {
                    Assert.Equal(counter++, reader.Span[i]);
                }
                reader.Skip(span.Length);
            }
            Assert.Equal(buffer.Length, reader.ConsumedBytes);
        }
Exemplo n.º 4
0
        public void TestSeekByteLimitWithinSameBlock(string input, char seek, int limit, int expectedBytesScanned, int expectedReturnValue)
        {
            // Arrange
            var buffer = BufferUtilities.CreateBuffer(input);

            // Act
            var end           = limit > input.Length ? buffer.End : buffer.Slice(0, limit).End;
            var returnValue   = ReadCursorOperations.Seek(buffer.Start, end, out ReadCursor result, (byte)seek);
            var returnValue_1 = ReadCursorOperations.Seek(buffer.Start, end, out result, (byte)seek, (byte)seek);
            var returnValue_2 = ReadCursorOperations.Seek(buffer.Start, end, out result, (byte)seek, (byte)seek, (byte)seek);

            // Assert
            Assert.Equal(expectedReturnValue, returnValue);
            Assert.Equal(expectedReturnValue, returnValue_1);
            Assert.Equal(expectedReturnValue, returnValue_2);

            if (expectedReturnValue != -1)
            {
                Assert.Same(buffer.Start.Segment, result.Segment);
                Assert.Equal(result.Segment.Start + input.IndexOf(seek), result.Index);
            }
        }
Exemplo n.º 5
0
 public override ReadOnlySequence <byte> CreateWithContent(byte[] data)
 {
     return(BufferUtilities.CreateBuffer(data));
 }
Exemplo n.º 6
0
 public override ReadOnlySequence <byte> CreateOfSize(int size)
 {
     return(BufferUtilities.CreateBuffer(size));
 }
Exemplo n.º 7
0
        public void TestSeekIteratorLimitAcrossBlocks(string input, char seek, char limitAt, int expectedReturnValue)
        {
            // Arrange
            var afterSeek = (byte)'B';

            var input1 = input.Substring(0, input.Length / 2);
            var input2 = input.Substring(input.Length / 2);
            var buffer = BufferUtilities.CreateBuffer(input1, string.Empty, input2);

            var start   = buffer.Start;
            var scan1   = buffer.Start;
            var veryEnd = buffer.End;
            var scan2_1 = scan1;
            var scan2_2 = scan1;
            var scan3_1 = scan1;
            var scan3_2 = scan1;
            var scan3_3 = scan1;
            var end     = buffer.End;

            // Act
            var endReturnValue = ReadCursorOperations.Seek(start, veryEnd, out end, (byte)limitAt);

            end = buffer.Move(end, 1);
            var returnValue1   = ReadCursorOperations.Seek(start, end, out scan1, (byte)seek);
            var returnValue2_1 = ReadCursorOperations.Seek(start, end, out scan2_1, (byte)seek, afterSeek);
            var returnValue2_2 = ReadCursorOperations.Seek(start, end, out scan2_2, afterSeek, (byte)seek);
            var returnValue3_1 = ReadCursorOperations.Seek(start, end, out scan3_1, (byte)seek, afterSeek, afterSeek);
            var returnValue3_2 = ReadCursorOperations.Seek(start, end, out scan3_2, afterSeek, (byte)seek, afterSeek);
            var returnValue3_3 = ReadCursorOperations.Seek(start, end, out scan3_3, afterSeek, afterSeek, (byte)seek);

            // Assert
            Assert.Equal(input.Contains(limitAt) ? limitAt : -1, endReturnValue);
            Assert.Equal(expectedReturnValue, returnValue1);
            Assert.Equal(expectedReturnValue, returnValue2_1);
            Assert.Equal(expectedReturnValue, returnValue2_2);
            Assert.Equal(expectedReturnValue, returnValue3_1);
            Assert.Equal(expectedReturnValue, returnValue3_2);
            Assert.Equal(expectedReturnValue, returnValue3_3);

            if (expectedReturnValue != -1)
            {
                var seekCharIndex    = input.IndexOf(seek);
                var limitAtIndex     = input.IndexOf(limitAt);
                var expectedEndBlock = seekCharIndex != -1 && seekCharIndex < input.Length / 2 ?
                                       start.Segment :
                                       (limitAtIndex != -1 && limitAtIndex < input.Length / 2 ? start.Segment : end.Segment);
                Assert.Same(expectedEndBlock, scan1.Segment);
                Assert.Same(expectedEndBlock, scan2_1.Segment);
                Assert.Same(expectedEndBlock, scan2_2.Segment);
                Assert.Same(expectedEndBlock, scan3_1.Segment);
                Assert.Same(expectedEndBlock, scan3_2.Segment);
                Assert.Same(expectedEndBlock, scan3_3.Segment);

                var expectedEndIndex = expectedReturnValue != -1 ?
                                       expectedEndBlock.Start + (expectedEndBlock == start.Segment ? input1.IndexOf(seek) : input2.IndexOf(seek)) :
                                       end.Index;
                Assert.Equal(expectedEndIndex, scan1.Index);
                Assert.Equal(expectedEndIndex, scan2_1.Index);
                Assert.Equal(expectedEndIndex, scan2_2.Index);
                Assert.Equal(expectedEndIndex, scan3_1.Index);
                Assert.Equal(expectedEndIndex, scan3_2.Index);
                Assert.Equal(expectedEndIndex, scan3_3.Index);
            }
        }
 public override ReadOnlyBuffer CreateOfSize(int size)
 {
     return(BufferUtilities.CreateBuffer(size));
 }
Exemplo n.º 9
0
 public override ReadableBuffer CreateOfSize(int size)
 {
     return(BufferUtilities.CreateBuffer(Enumerable.Repeat(1, size).ToArray()));
 }
Exemplo n.º 10
0
 public override ReadableBuffer CreateWithContent(byte[] data)
 {
     return(BufferUtilities.CreateBuffer(data));
 }
Exemplo n.º 11
0
 public override ReadOnlySequence <T> CreateWithContent(T[] data)
 {
     return(BufferUtilities.CreateBuffer <T>(data));
 }