public void EmptyIsCorrect() { var buffer = BufferUtilities.CreateBuffer(0, 0); Assert.Equal(0, buffer.Length); Assert.True(buffer.IsEmpty); }
public void TestSeekByteLimitAcrossBlocks(string input, char seek, int limit, int expectedBytesScanned, int expectedReturnValue) { // Arrange var input1 = input.Substring(0, input.Length / 2); var input2 = input.Substring(input.Length / 2); var buffer = BufferUtilities.CreateBuffer(input1, string.Empty, input2); var block1 = buffer.Start; var block2 = buffer.End; // Act ReadCursor result; var end = limit > input.Length ? buffer.End : buffer.Slice(0, limit).End; var returnValue = ReadCursorOperations.Seek(buffer.Start, end, out 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) { var seekCharIndex = input.IndexOf(seek); var expectedEndBlock = limit <= input.Length / 2 ? block1.Segment : (seekCharIndex != -1 && seekCharIndex < input.Length / 2 ? block1.Segment : block2.Segment); Assert.Same(expectedEndBlock, result.Segment); var expectedEndIndex = expectedEndBlock.Start + (expectedEndBlock == block1.Segment ? input1.IndexOf(seek) : input2.IndexOf(seek)); Assert.Equal(expectedEndIndex, result.Index); } }
public async Task Split(string input, char delimiter) { // note: different expectation to string.Split; empty has 0 outputs var expected = input == "" ? new string[0] : input.Split(delimiter); var output = _pipe.Writer.Alloc(); output.AsOutput().Append(input, SymbolTable.InvariantUtf8); var readable = BufferUtilities.CreateBuffer(input); // via struct API var iter = readable.Split((byte)delimiter); Assert.Equal(expected.Length, iter.Count()); int i = 0; foreach (var item in iter) { Assert.Equal(expected[i++], item.GetUtf8Span()); } Assert.Equal(expected.Length, i); // via objects/LINQ etc IEnumerable <ReadOnlyBuffer> asObject = iter; Assert.Equal(expected.Length, asObject.Count()); i = 0; foreach (var item in asObject) { Assert.Equal(expected[i++], item.GetUtf8Span()); } Assert.Equal(expected.Length, i); await output.FlushAsync(); }
public void ReadCursorSeekChecksEndIfNotTrustingEnd() { var buffer = BufferUtilities.CreateBuffer(1, 1, 1); var buffer2 = BufferUtilities.CreateBuffer(1, 1, 1); Assert.Throws <InvalidOperationException>(() => buffer.Start.Seek(2, buffer2.End, true)); }
public void MemorySeek(string raw, string search, char expectResult, int expectIndex) { var cursors = BufferUtilities.CreateBuffer(raw); ReadCursor start = cursors.Start; ReadCursor end = cursors.End; ReadCursor result = default(ReadCursor); var searchFor = search.ToCharArray(); int found = -1; if (searchFor.Length == 1) { found = ReadCursorOperations.Seek(start, end, out result, (byte)searchFor[0]); } else if (searchFor.Length == 2) { found = ReadCursorOperations.Seek(start, end, out result, (byte)searchFor[0], (byte)searchFor[1]); } else if (searchFor.Length == 3) { found = ReadCursorOperations.Seek(start, end, out result, (byte)searchFor[0], (byte)searchFor[1], (byte)searchFor[2]); } else { Assert.False(true, "Invalid test sample."); } Assert.Equal(expectResult, found); Assert.Equal(expectIndex, result.Index - start.Index); }
public void ReadCursorSeekDoesNotCheckEndIfTrustingEnd() { var buffer = BufferUtilities.CreateBuffer(1, 1, 1); var buffer2 = BufferUtilities.CreateBuffer(1, 1, 1); buffer.Start.Seek(2, buffer2.End, false); }
public void ReadableBufferSequenceWorks() { using (var factory = new PipeFactory()) { var readerWriter = factory.Create(); var output = readerWriter.Writer.Alloc(); { // empty buffer var readable = output.AsReadableBuffer() as ISequence <ReadOnlyMemory <byte> >; var position = Position.First; ReadOnlyMemory <byte> memory; int spanCount = 0; while (readable.TryGet(ref position, out memory)) { spanCount++; Assert.Equal(0, memory.Length); } Assert.Equal(1, spanCount); } { var readable = BufferUtilities.CreateBuffer(new byte[] { 1 }, new byte[] { 2, 2 }, new byte[] { 3, 3, 3 }) as ISequence <ReadOnlyMemory <byte> >; var position = Position.First; ReadOnlyMemory <byte> memory; int spanCount = 0; while (readable.TryGet(ref position, out memory)) { spanCount++; Assert.Equal(spanCount, memory.Length); } Assert.Equal(3, spanCount); } } }
public void CtorFindsFirstNonEmptySegment() { var buffer = BufferUtilities.CreateBuffer(new[] { new byte[] { }, new byte[] { 1 } }); var reader = new ReadableBufferReader(buffer); Assert.Equal(1, reader.Peek()); }
public void ReadTWorksAgainstMultipleBuffers() { var readable = BufferUtilities.CreateBuffer(new byte[] { 0, 1, 2 }, new byte[] { 3, 4, 5 }, new byte[] { 6, 7, 9 }); Assert.Equal(9, readable.Length); int spanCount = 0; foreach (var _ in readable) { spanCount++; } Assert.Equal(3, spanCount); Assert.False(readable.IsSingleSegment); Span <byte> span = readable.ToArray(); 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 SkipThrowsPastLengthMultipleSegments() { var buffer = BufferUtilities.CreateBuffer(new[] { new byte[] { 1 }, new byte[] { 2 }, new byte[] { 3 } }); var reader = new ReadableBufferReader(buffer); Assert.Throws <ArgumentOutOfRangeException>(() => reader.Skip(4)); }
public void PeekGoesToEndIfAllEmptySegments() { var buffer = BufferUtilities.CreateBuffer(new[] { new byte[] { }, new byte[] { }, new byte[] { }, new byte[] { } }); var reader = BufferReader.Create(buffer); Assert.Equal(-1, reader.Peek()); Assert.True(reader.End); }
public void SkipTraversesSegments() { var buffer = BufferUtilities.CreateBuffer(new[] { new byte[] { 1 }, new byte[] { 2 }, new byte[] { 3 } }); var reader = new ReadableBufferReader(buffer); reader.Skip(2); Assert.Equal(3, reader.Take()); }
public void TestSeekIteratorLimitWithinSameBlock(string input, char seek, char limitAfter, int expectedReturnValue) { // Arrange var afterSeek = (byte)'B'; var buffer = BufferUtilities.CreateBuffer(input); 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)limitAfter); end = buffer.Slice(end, 1).End; 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(limitAfter) ? limitAfter : -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 block = start.Segment; Assert.Same(block, scan1.Segment); Assert.Same(block, scan2_1.Segment); Assert.Same(block, scan2_2.Segment); Assert.Same(block, scan3_1.Segment); Assert.Same(block, scan3_2.Segment); Assert.Same(block, scan3_3.Segment); var expectedEndIndex = expectedReturnValue != -1 ? start.Index + input.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 void EmptySegmentsAreSkippedOnMoveNext() { var buffer = BufferUtilities.CreateBuffer(new[] { new byte[] { 1 }, new byte[] { }, new byte[] { }, new byte[] { 2 } }); var reader = new ReadableBufferReader(buffer); Assert.Equal(1, reader.Peek()); reader.Skip(1); Assert.Equal(2, reader.Peek()); }
public void LengthIncreasesAfterAppend() { var writableBuffer = _pipe.Writer.Alloc(); writableBuffer.Append(BufferUtilities.CreateBuffer(1, 2, 3)); Assert.Equal(0, _pipe.Length); writableBuffer.Commit(); Assert.Equal(6, _pipe.Length); }
public void PeekWorkesWithEmptySegments() { var buffer = BufferUtilities.CreateBuffer(new[] { new byte[] { }, new byte[] { 1 } }); var reader = new ReadableBufferReader(buffer); Assert.Equal(1, reader.Peek()); Assert.Equal(1, reader.Take()); Assert.Equal(-1, reader.Peek()); Assert.Equal(-1, reader.Take()); }
public void PeekTraversesSegments() { var buffer = BufferUtilities.CreateBuffer(new[] { new byte[] { 1 }, new byte[] { 2 } }); var reader = new ReadableBufferReader(buffer); Assert.Equal(1, reader.Take()); Assert.Equal(2, reader.Peek()); Assert.Equal(2, reader.Take()); Assert.Equal(-1, reader.Peek()); Assert.Equal(-1, reader.Take()); }
public void SkipSingleBufferSkipsBytes() { var reader = new ReadableBufferReader(BufferUtilities.CreateBuffer(new byte[] { 1, 2, 3, 4, 5 })); reader.Skip(2); Assert.Equal(2, reader.Index); Assert.Equal(3, reader.Span[reader.Index]); Assert.Equal(3, reader.Peek()); reader.Skip(2); Assert.Equal(5, reader.Peek()); Assert.Equal(4, reader.Index); Assert.Equal(5, reader.Span[reader.Index]); }
public void ReadableBufferSequenceWorks() { var readable = BufferUtilities.CreateBuffer(new byte[] { 1 }, new byte[] { 2, 2 }, new byte[] { 3, 3, 3 }); Position position = readable.Start; int spanCount = 0; while (readable.TryGet(ref position, out ReadOnlyMemory <byte> memory)) { spanCount++; Assert.Equal(spanCount, memory.Length); } Assert.Equal(3, spanCount); }
public void AdvanceSingleBufferSkipsBytes() { var reader = BufferReader.Create(BufferUtilities.CreateBuffer(new byte[] { 1, 2, 3, 4, 5 })); reader.Advance(2); Assert.Equal(2, reader.Index); Assert.Equal(3, reader.CurrentSegment[reader.Index]); Assert.Equal(3, reader.Peek()); reader.Advance(2); Assert.Equal(5, reader.Peek()); Assert.Equal(4, reader.Index); Assert.Equal(5, reader.CurrentSegment[reader.Index]); }
public override ReadOnlySequence <byte> CreateWithContent(byte[] data) { var segments = new List <byte[]>(); segments.Add(System.Array.Empty <byte>()); foreach (var b in data) { segments.Add(new[] { b }); segments.Add(System.Array.Empty <byte>()); } return(BufferUtilities.CreateBuffer(segments.ToArray())); }
public void SkipThrowsPastLengthMultipleSegments() { var buffer = BufferUtilities.CreateBuffer(new[] { new byte[] { 1 }, new byte[] { 2 }, new byte[] { 3 } }); var reader = new ReadableBufferReader(buffer); try { reader.Skip(4); Assert.True(false); } catch (Exception ex) { Assert.True(ex is ArgumentOutOfRangeException); } }
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.IsSingleSpan); Assert.Equal(span.Read <byte>(), readable.ReadLittleEndian <byte>()); Assert.Equal(span.Read <sbyte>(), readable.ReadLittleEndian <sbyte>()); Assert.Equal(span.Read <short>(), readable.ReadLittleEndian <short>()); Assert.Equal(span.Read <ushort>(), readable.ReadLittleEndian <ushort>()); Assert.Equal(span.Read <int>(), readable.ReadLittleEndian <int>()); Assert.Equal(span.Read <uint>(), readable.ReadLittleEndian <uint>()); Assert.Equal(span.Read <long>(), readable.ReadLittleEndian <long>()); Assert.Equal(span.Read <ulong>(), readable.ReadLittleEndian <ulong>()); Assert.Equal(span.Read <float>(), readable.ReadLittleEndian <float>()); Assert.Equal(span.Read <double>(), readable.ReadLittleEndian <double>()); }
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); }
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); } }
public override ReadableBuffer CreateOfSize(int size) { return(BufferUtilities.CreateBuffer(Enumerable.Repeat(1, size).ToArray())); }
public override ReadOnlySequence <byte> CreateWithContent(byte[] data) { return(BufferUtilities.CreateBuffer(data)); }
public override ReadOnlyBuffer CreateOfSize(int size) { return(BufferUtilities.CreateBuffer(size)); }
public override ReadOnlySequence <byte> CreateOfSize(int size) { return(BufferUtilities.CreateBuffer(size)); }
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); } }