Exemple #1
0
        public void Test_MemoryExtensions_IMemoryOwnerStream()
        {
            MemoryOwner <byte> buffer = MemoryOwner <byte> .Allocate(1024);

            Stream stream = buffer.AsStream();

            Assert.IsNotNull(stream);
            Assert.AreEqual(stream.Length, buffer.Length);
            Assert.IsTrue(stream.CanWrite);
        }
Exemple #2
0
        public void Test_IMemoryOwnerExtensions_EmptyIMemoryOwnerStream()
        {
            MemoryOwner <byte> buffer = MemoryOwner <byte> .Empty;

            Stream stream = buffer.AsStream();

            Assert.IsNotNull(stream);
            Assert.AreEqual(stream.Length, buffer.Length);
            Assert.IsTrue(stream.CanWrite);
        }
        public void Test_IMemoryOwnerStream_Lifecycle()
        {
            MemoryOwner <byte> buffer = MemoryOwner <byte> .Allocate(100);

            Stream stream = buffer.AsStream();

            Assert.IsTrue(stream.CanRead);
            Assert.IsTrue(stream.CanSeek);
            Assert.IsTrue(stream.CanWrite);
            Assert.AreEqual(stream.Length, buffer.Length);
            Assert.AreEqual(stream.Position, 0);

            stream.Dispose();

            Assert.ThrowsException <ObjectDisposedException>(() => buffer.Memory);
        }
Exemple #4
0
        public void Test_MemoryExtensions_IMemoryOwnerStream_DoesNotAlterExistingData()
        {
            MemoryOwner <byte> buffer = MemoryOwner <byte> .Allocate(1024);

            // Fill the buffer with sample data
            for (int i = 0; i < buffer.Length; i++)
            {
                buffer.Span[i] = unchecked ((byte)(i & byte.MaxValue));
            }

            Stream stream = buffer.AsStream();

            Assert.IsNotNull(stream);
            Assert.AreEqual(stream.Length, buffer.Length);
            Assert.IsTrue(stream.CanWrite);

            // Validate that creating the stream doesn't alter the underlying buffer
            for (int i = 0; i < buffer.Length; i++)
            {
                Assert.AreEqual(buffer.Span[i], unchecked ((byte)(i & byte.MaxValue)));
            }
        }