public void InvalidateMakesSequenceUnusable()
        {
            var origBuffer = GetTestBuffer(100);

            var sliceBuffer = new ReusableSliceBuffer();
            var sequence    = sliceBuffer.PopulateFrom(fakeBufferReaderManager.CreateMultiSegmentBufferReader(new List <byte[]> {
                origBuffer
            }));

            Assert.AreEqual(origBuffer.Length, sequence.Length);

            sliceBuffer.Invalidate();

            // Invalidate with make the returned sequence completely unusable and broken, users must not use it beyond the deserializer functions.
            Assert.Throws(typeof(ArgumentOutOfRangeException), () => { var first = sequence.First; });
        }
        public void SegmentsAreCached()
        {
            var bufferSegments1 = Enumerable.Range(0, 100).Select((_) => GetTestBuffer(50)).ToList();
            var bufferSegments2 = Enumerable.Range(0, 100).Select((_) => GetTestBuffer(50)).ToList();

            var sliceBuffer = new ReusableSliceBuffer();

            var sequence1       = sliceBuffer.PopulateFrom(fakeBufferReaderManager.CreateMultiSegmentBufferReader(bufferSegments1));
            var memoryManagers1 = GetMemoryManagersForSequenceSegments(sequence1);

            sliceBuffer.Invalidate();

            var sequence2       = sliceBuffer.PopulateFrom(fakeBufferReaderManager.CreateMultiSegmentBufferReader(bufferSegments2));
            var memoryManagers2 = GetMemoryManagersForSequenceSegments(sequence2);

            // check memory managers are identical objects (i.e. they've been reused)
            CollectionAssert.AreEquivalent(memoryManagers1, memoryManagers2);
        }