예제 #1
0
        // tests OwnedBuffer.AsSpan overloads
        static void AsSpan(OwnedBuffer <byte> buffer)
        {
            var span      = buffer.AsSpan();
            var fullSlice = buffer.AsSpan(0, buffer.Length);

            for (int i = 0; i < span.Length; i++)
            {
                span[i] = (byte)(i % 254 + 1);
                Assert.Equal(span[i], fullSlice[i]);
            }

            var slice = buffer.AsSpan(5, buffer.Length - 5);

            Assert.Equal(span.Length - 5, slice.Length);

            for (int i = 0; i < slice.Length; i++)
            {
                Assert.Equal(span[i + 5], slice[i]);
            }

            Assert.Throws <ArgumentOutOfRangeException>(() =>
            {
                buffer.AsSpan(buffer.Length, 1);
            });

            Assert.Throws <ArgumentOutOfRangeException>(() =>
            {
                buffer.AsSpan(1, buffer.Length);
            });
        }
예제 #2
0
        static void Dispose(OwnedBuffer <byte> buffer)
        {
            var length = buffer.Length;

            buffer.Dispose();
            Assert.True(buffer.IsDisposed);
            Assert.False(buffer.IsRetained);

            Assert.ThrowsAny <ObjectDisposedException>(() =>
            {
                buffer.AsSpan();
            });

            Assert.ThrowsAny <ObjectDisposedException>(() =>
            {
                buffer.AsSpan(0, length);
            });

            Assert.ThrowsAny <ObjectDisposedException>(() =>
            {
                buffer.Pin();
            });

            Assert.ThrowsAny <ObjectDisposedException>(() =>
            {
                var rwBuffer = buffer.Buffer;
            });

            Assert.ThrowsAny <ObjectDisposedException>(() =>
            {
                var roBuffer = buffer.ReadOnlyBuffer;
            });
        }
예제 #3
0
        public void ReleasedBlockWorks()
        {
            var pool  = new MemoryPool();
            var block = pool.Rent(1);

            block.Dispose();

            OwnedBuffer <byte> block2 = null;

            // Lease-return until we get same block
            do
            {
                block2?.Dispose();
                block2 = pool.Rent(1);
            } while (block != block2);

            Assert.True(block2.AsSpan().Length > 0);
        }
예제 #4
0
        public void ReleasedBlockWorks()
        {
            using (var pool = new MemoryPool())
            {
                var block1 = pool.Rent(1);
                block1.Retain();
                block1.Release();

                OwnedBuffer <byte> block2 = null;

                // Lease-return until we get same block
                while (block1 != block2)
                {
                    block2 = pool.Rent(1);
                    block2.Retain();
                    block2.Release();
                }

                Assert.True(block2.AsSpan().Length > 0);
            }
        }
예제 #5
0
        static void MemoryAccessBasics(OwnedBuffer <byte> buffer)
        {
            var span = buffer.AsSpan();

            span[10] = 10;

            Assert.Equal(buffer.Length, span.Length);
            Assert.Equal(10, span[10]);

            var memory = buffer.Buffer;

            Assert.Equal(buffer.Length, memory.Length);
            Assert.Equal(10, memory.Span[10]);

            var array = memory.ToArray();

            Assert.Equal(buffer.Length, array.Length);
            Assert.Equal(10, array[10]);

            Span <byte> copy = new byte[20];

            memory.Slice(10, 20).CopyTo(copy);
            Assert.Equal(10, copy[0]);
        }