Exemplo n.º 1
0
        public void LengthPositionSlicing()
        {
            // single segment
            {
                var rob = new ReadOnlyBytes(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 });
                var c2  = rob.PositionOf(2).GetValueOrDefault();
                var c5  = rob.PositionOf(5).GetValueOrDefault();

                var slice = rob.Slice(c2, c5);

                Assert.Equal(2, slice.Memory.Span[0]);
                Assert.Equal(3, slice.Length);
            }

            // multi segment
            {
                var rob = ListHelper.CreateRob(new byte[] { 1, 2, 3 }, new byte[] { 4, 5, 6, 7, 8 });
                var c2  = rob.PositionOf(2).GetValueOrDefault();
                var c5  = rob.PositionOf(5).GetValueOrDefault();

                var slice = rob.Slice(c2, c5);

                Assert.Equal(2, slice.Memory.Span[0]);
                Assert.Equal(3, slice.Length);
            }
        }
Exemplo n.º 2
0
        public void PavelsScenarioCursorSlicing()
        {
            // single segment
            {
                var rob = ListHelper.CreateRob(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 });
                var c   = rob.PositionOf(5).GetValueOrDefault();

                var slice1 = rob.Slice(2).Slice(c);
                var slice2 = rob.Slice(c).Slice(2);

                Assert.NotEqual(slice1.Memory.Span[0], slice2.Memory.Span[0]);
                Assert.Equal(5, slice1.Memory.Span[0]);
                Assert.Equal(7, slice2.Memory.Span[0]);
            }

            // multi segment
            {
                var rob = ListHelper.CreateRob(new byte[] { 1, 2, 3 }, new byte[] { 4, 5, 6, 7, 8 });
                var c   = rob.PositionOf(5).GetValueOrDefault();

                var slice1 = rob.Slice(2).Slice(c);
                var slice2 = rob.Slice(c).Slice(2);

                Assert.NotEqual(slice1.Memory.Span[0], slice2.Memory.Span[0]);
                Assert.Equal(5, slice1.Memory.Span[0]);
                Assert.Equal(7, slice2.Memory.Span[0]);
            }
        }
Exemplo n.º 3
0
        public void MultiSegmentIndexOfSpan()
        {
            var bytes = ListHelper.CreateRob(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, new byte[] { 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 });

            Assert.Equal(10, bytes.Memory.Length);
            Assert.Equal(9, bytes.Memory.Span[9]);

            var index = bytes.IndexOf(new byte[] { 2, 3 });

            Assert.Equal(2, index);

            index = bytes.IndexOf(new byte[] { 8, 9, 10 });
            Assert.Equal(8, index);

            index = bytes.IndexOf(new byte[] { 11, 12, 13, 14 });
            Assert.Equal(11, index);

            index = bytes.IndexOf(new byte[] { 19 });
            Assert.Equal(19, index);

            index = bytes.IndexOf(new byte[] { 0 });
            Assert.Equal(0, index);

            index = bytes.IndexOf(new byte[] { 9 });
            Assert.Equal(9, index);

            index = bytes.IndexOf(new byte[] { 10 });
            Assert.Equal(10, index);
        }
Exemplo n.º 4
0
        public void MultiSegmentBytesReaderNumbers()
        {
            ReadOnlyBytes bytes = ListHelper.CreateRob(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 = BytesReader.Create(bytes);

            var span = bytes.Slice(reader.ReadRange(2)).ToSpan();

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

            span = bytes.Slice(reader.ReadRange(5)).ToSpan();
            Assert.Equal(3, span[0]);
            Assert.Equal(4, span[1]);

            span = bytes.Slice(reader.ReadRange(new byte[] { 8, 8 })).ToSpan();
            Assert.Equal(6, span[0]);
            Assert.Equal(7, span[1]);

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

            Assert.True(reader.TryRead(out value));
            Assert.Equal(BitConverter.ToInt32(new byte[] { 4, 3, 2, 1 }), value);
        }
Exemplo n.º 5
0
        public void MultiSegmentBytesReaderNumbers()
        {
            ReadOnlyBytes bytes = ListHelper.CreateRob(new byte[][] {
                new byte[] { 0 },
                new byte[] { 1, 2 },
                new byte[] { 3, 4 },
                new byte[] { 5, 6, 7, 8 },
                new byte[] { 8 }
            });

            var reader = new BytesReader(bytes);

            var value = reader.ReadBytesUntil(2).Value.ToSpan();

            Assert.Equal(0, value[0]);
            Assert.Equal(1, value[1]);
            reader.Advance(1);

            value = reader.ReadBytesUntil(5).Value.ToSpan();
            Assert.Equal(3, value[0]);
            Assert.Equal(4, value[1]);
            reader.Advance(1);

            value = reader.ReadBytesUntil(new byte[] { 8, 8 }).Value.ToSpan();
            Assert.Equal(6, value[0]);
            Assert.Equal(7, value[1]);
            reader.Advance(2);

            //Assert.True(reader.IsEmpty);
        }
Exemplo n.º 6
0
        private ReadOnlyBytes Create(params string[] segments)
        {
            var buffers = new List <byte[]>();

            foreach (var segment in segments)
            {
                buffers.Add(Encoding.UTF8.GetBytes(segment));
            }
            return(ListHelper.CreateRob(buffers.ToArray()));
        }
Exemplo n.º 7
0
        private ReadOnlyBytes Parse(string text)
        {
            var segments = text.Split('|');
            var buffers  = new List <byte[]>();

            foreach (var segment in segments)
            {
                buffers.Add(Encoding.UTF8.GetBytes(segment));
            }
            return(ListHelper.CreateRob(buffers.ToArray()));
        }
Exemplo n.º 8
0
        public void MultiSegmentIndexOfByte()
        {
            var bytes = ListHelper.CreateRob(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, new byte[] { 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 });

            Assert.Equal(10, bytes.Memory.Length);
            Assert.Equal(9, bytes.Memory.Span[9]);

            for (int i = 0; i < 20; i++)
            {
                var index = Sequence.IndexOf(bytes, (byte)i);
                Assert.Equal(i, index);
            }
        }
Exemplo n.º 9
0
        public void SequenceIndexOfMultiSegment()
        {
            ReadOnlyBytes bytes = ListHelper.CreateRob(
                new byte[] { 1, 2 },
                new byte[] { 3, 4 }
                );

            Assert.Equal(4, bytes.Length);

            // Static method call to avoid calling ReadOnlyBytes.IndexOf
            Assert.Equal(-1, Sequence.IndexOf(bytes, 0));

            for (int i = 0; i < bytes.Length; i++)
            {
                Assert.Equal(i, Sequence.IndexOf(bytes, (byte)(i + 1)));
            }
        }
Exemplo n.º 10
0
        public void MultiSegmentSlicing()
        {
            var           array1      = new byte[] { 0, 1 };
            var           array2      = new byte[] { 2, 3 };
            var           totalLength = array1.Length + array2.Length;
            ReadOnlyBytes bytes       = ListHelper.CreateRob(array1, array2);

            ReadOnlyBytes       sliced = bytes;
            ReadOnlySpan <byte> span;

            for (int i = 1; i <= totalLength; i++)
            {
                sliced = bytes.Slice(i);
                span   = sliced.Memory.Span;
                Assert.Equal(totalLength - i, sliced.Length);
                if (i != totalLength)
                {
                    Assert.Equal(i, span[0]);
                }
            }
            Assert.Equal(0, span.Length);
        }
Exemplo n.º 11
0
        public void MultiSegmentCopyToUnknownLength()
        {
            var           array1      = new byte[] { 0, 1 };
            var           array2      = new byte[] { 2, 3 };
            var           totalLength = array1.Length + array2.Length;
            ReadOnlyBytes bytes       = ListHelper.CreateRob(array1, array2);

            { // copy to equal
                var copy   = new byte[totalLength];
                var copied = bytes.CopyTo(copy);
                Assert.Equal(totalLength, copied);
                for (int i = 0; i < totalLength; i++)
                {
                    Assert.Equal(i, copy[i]);
                }
            }

            { // copy to smaller
                var copy   = new byte[totalLength - 1];
                var copied = bytes.CopyTo(copy);
                Assert.Equal(copy.Length, copied);
                for (int i = 0; i < copied; i++)
                {
                    Assert.Equal(i, copy[i]);
                }
            }

            { // copy to larger
                var copy   = new byte[totalLength + 1];
                var copied = bytes.CopyTo(copy);
                Assert.Equal(totalLength, copied);
                for (int i = 0; i < totalLength; i++)
                {
                    Assert.Equal(i, copy[i]);
                }
            }
        }
Exemplo n.º 12
0
        public void MultiSegmentBytesReaderNumbers()
        {
            ReadOnlyBytes bytes = ListHelper.CreateRob(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);
        }
Exemplo n.º 13
0
        public void MultiSegmentPositionSlicing()
        {
            var           array1   = new byte[] { 0, 1 };
            var           array2   = new byte[] { 2, 3 };
            ReadOnlyBytes allBytes = ListHelper.CreateRob(array1, array2);

            ReadOnlyBytes allBytesSlice1 = allBytes.Slice(1);
            ReadOnlyBytes allBytesSlice2 = allBytes.Slice(2);
            ReadOnlyBytes allBytesSlice3 = allBytes.Slice(3);

            var positionOf3 = allBytes.PositionOf(3).GetValueOrDefault();
            var positionOf1 = allBytes.PositionOf(1).GetValueOrDefault();

            // all bytes
            {
                var slice = allBytes.Slice(positionOf3);
                Assert.Equal(1, slice.Length);
                Assert.Equal(3, slice.Memory.Span[0]);
            }

            {
                var slice = allBytes.Slice(positionOf1);
                Assert.Equal(3, slice.Length);
                Assert.Equal(1, slice.Memory.Span[0]);
            }

            // allBytesSlice1
            {
                var slice = allBytesSlice1.Slice(positionOf3);
                Assert.Equal(1, slice.Length);
                Assert.Equal(3, slice.Memory.Span[0]);
            }

            {
                var slice = allBytesSlice1.Slice(positionOf1);
                Assert.Equal(3, slice.Length);
                Assert.Equal(1, slice.Memory.Span[0]);
            }

            // allBytesSlice2
            {
                var slice = allBytesSlice2.Slice(positionOf3);
                Assert.Equal(1, slice.Length);
                Assert.Equal(3, slice.Memory.Span[0]);
            }

            {
                var slice = allBytesSlice2.Slice(positionOf1);
                Assert.Equal(3, slice.Length);
                Assert.Equal(1, slice.Memory.Span[0]);
            }

            // allBytesSlice3
            {
                var slice = allBytesSlice3.Slice(positionOf3);
                Assert.Equal(1, slice.Length);
                Assert.Equal(3, slice.Memory.Span[0]);
            }

            {
                var slice = allBytesSlice3.Slice(positionOf1);
                Assert.Equal(3, slice.Length);
                Assert.Equal(1, slice.Memory.Span[0]);
            }
        }