public void GetOffset_SingleSegment_SequencePositionSegment()
        {
            var data     = new T[0];
            var sequence = new ReadOnlySequence <T>(data);

            Assert.Equal(0, sequence.GetOffset(new SequencePosition(data, 0)));

            // Invalid positions
            Assert.Throws <ArgumentOutOfRangeException>(() => sequence.GetOffset(new SequencePosition(data, 1)));
        }
        public void GetOffset_MultiSegment_SequencePositionSegment()
        {
            // [0] -> [0] -> [0] -> [50]
            var bufferSegment1 = new BufferSegment <T>(new T[0]);
            BufferSegment <T> bufferSegment2 = bufferSegment1.Append(new T[0]);
            BufferSegment <T> bufferSegment3 = bufferSegment2.Append(new T[0]);
            BufferSegment <T> bufferSegment4 = bufferSegment3.Append(new T[50]);

            var sequence = new ReadOnlySequence <T>(bufferSegment1, 0, bufferSegment4, 50);

            Assert.Equal(0, sequence.GetOffset(new SequencePosition(bufferSegment1, 0)));
            Assert.Equal(0, sequence.GetOffset(new SequencePosition(bufferSegment2, 0)));
            Assert.Equal(0, sequence.GetOffset(new SequencePosition(bufferSegment3, 0)));
            Assert.Equal(0, sequence.GetOffset(new SequencePosition(bufferSegment4, 0)));

            // Invalid positions
            Assert.Throws <ArgumentOutOfRangeException>(() => sequence.GetOffset(new SequencePosition(bufferSegment1, 1)));
            Assert.Throws <ArgumentOutOfRangeException>(() => sequence.GetOffset(new SequencePosition(bufferSegment2, 1)));
            Assert.Throws <ArgumentOutOfRangeException>(() => sequence.GetOffset(new SequencePosition(bufferSegment3, 1)));

            for (int i = 0; i <= bufferSegment4.Memory.Length; i++)
            {
                Assert.Equal(i, sequence.GetOffset(new SequencePosition(bufferSegment4, i)));
            }
        }
        public void GetOffset_MultiSegment_PositionOutOfRange_SegmentNotFound()
        {
            ReadOnlySequence <T> buffer  = GetFourSegmentsReadOnlySequence();
            ReadOnlySequence <T> buffer2 = GetFourSegmentsReadOnlySequence();

            Assert.Throws <ArgumentOutOfRangeException>("position", () => buffer.GetOffset(buffer2.GetPosition(25)));
        }
        public void GetOffset_MultiSegment_InvalidSequencePositionSegment()
        {
            ReadOnlySequence <T> buffer  = GetFourSegmentsReadOnlySequence();
            ReadOnlySequence <T> buffer2 = new ReadOnlySequence <T>(new T[50]);

            Assert.Throws <InvalidCastException>(() => buffer.GetOffset(buffer2.GetPosition(25)));
        }
        public void GetOffset_SingleSegment_PositionOutOfRange()
        {
            var positionObject = new T[50];
            var buffer         = new ReadOnlySequence <T>(positionObject);

            Assert.Throws <ArgumentOutOfRangeException>("position", () => buffer.GetOffset(new SequencePosition(positionObject, 75)));
        }
        public void GetOffset_MultiSegment_PositionOutOfRange()
        {
            (BufferSegment <T> bufferSegment1, BufferSegment <T> bufferSegment4) = GetBufferSegment();
            var buffer = new ReadOnlySequence <T>(bufferSegment1, 0, bufferSegment4, 50);

            Assert.Throws <ArgumentOutOfRangeException>("position", () => buffer.GetOffset(new SequencePosition(bufferSegment4, 200)));
        }
        public void GetOffset_MultiSegment_Slice()
        {
            ReadOnlySequence <T> buffer = GetFourSegmentsReadOnlySequence();

            for (int i = 0; i <= buffer.Length; i++)
            {
                Assert.Equal(buffer.Slice(0, i).Length, buffer.GetOffset(buffer.GetPosition(i)));
            }
        }
        public void GetOffset_SingleSegment_Slice()
        {
            ReadOnlySequence <T> buffer = new ReadOnlySequence <T>(new T[50]);

            for (int i = 0; i <= buffer.Length; i++)
            {
                Assert.Equal(buffer.Slice(0, i).Length, buffer.GetOffset(buffer.GetPosition(i)));
            }
        }
        public void GetOffset_MultiSegment_BoundaryConditions()
        {
            // [50] -> [50] -> [0] -> [50]
            var bufferSegment1 = new BufferSegment <T>(new T[50]);
            BufferSegment <T> bufferSegment2 = bufferSegment1.Append(new T[50]);
            BufferSegment <T> bufferSegment3 = bufferSegment2.Append(new T[0]);
            BufferSegment <T> bufferSegment4 = bufferSegment3.Append(new T[50]);
            var sequence = new ReadOnlySequence <T>(bufferSegment1, 0, bufferSegment4, 50);

            // Non empty adjacent segment
            Assert.Equal(50, sequence.GetOffset(new SequencePosition(bufferSegment1, 50)));
            Assert.Equal(50, sequence.GetOffset(new SequencePosition(bufferSegment2, 0)));
            Assert.Equal(51, sequence.GetOffset(new SequencePosition(bufferSegment2, 1)));

            // Empty adjacent segment
            Assert.Equal(100, sequence.GetOffset(new SequencePosition(bufferSegment2, 50)));
            Assert.Equal(100, sequence.GetOffset(new SequencePosition(bufferSegment3, 0)));
            Assert.Equal(101, sequence.GetOffset(new SequencePosition(bufferSegment4, 1)));

            // Cannot get 101 starting from empty adjacent segment
            Assert.Throws <ArgumentOutOfRangeException>(() => sequence.GetOffset(new SequencePosition(bufferSegment3, 1)));
        }
        public void GetOffset_MultiSegment_NullPositionObject()
        {
            ReadOnlySequence <T> buffer = GetFourSegmentsReadOnlySequence();

            Assert.Equal(0, buffer.GetOffset(new SequencePosition(null, 25)));
        }
        public void GetOffset_SingleSegment_NullPositionObject()
        {
            var buffer = new ReadOnlySequence <T>(new T[50]);

            Assert.Equal(0, buffer.GetOffset(new SequencePosition(null, 25)));
        }
        public void GetOffset_MultiSegment_MiddleSegment()
        {
            ReadOnlySequence <T> buffer = GetFourSegmentsReadOnlySequence();

            Assert.Equal(75, buffer.GetOffset(buffer.GetPosition(75)));
        }
        public void GetOffset_SingleSegment()
        {
            var buffer = new ReadOnlySequence <T>(new T[50]);

            Assert.Equal(25, buffer.GetOffset(buffer.GetPosition(25)));
        }