Esempio n. 1
0
        public static void MemoryBufferStream_AsyncPatternRead_SmallBuffer()
        {
            var truth  = BufferComparerTests.GenerateSegment(13, 203, 1).AsReadOnlyMemory();
            var buffer = new byte[100];

            using (var sut = new MemoryBufferStream(truth))
            {
                Assert.Equal(50, sut.SyncAsyncRead(buffer, 0, 50));
                Assert.Equal(buffer.AsReadOnlyMemory().Slice(0, 50), truth.Slice(0, 50), BufferComparer.Memory);

                Assert.Equal(100, sut.SyncAsyncRead(buffer, 0, 100));
                Assert.Equal(buffer.AsReadOnlyMemory(), truth.Slice(50, 100), BufferComparer.Memory);

                Assert.Equal(53, sut.SyncAsyncRead(buffer, 0, 100));
                Assert.Equal(buffer.AsReadOnlyMemory().Slice(0, 53), truth.Slice(150, 53), BufferComparer.Memory);

                // Ensure nothing was copied.

                Assert.Equal(0, sut.SyncAsyncRead(buffer, 0, 100));
                Assert.Equal(buffer.AsReadOnlyMemory().Slice(0, 53), truth.Slice(150, 53), BufferComparer.Memory);

                Assert.Equal(0, sut.SyncAsyncRead(buffer, 0, 100));
                Assert.Equal(buffer.AsReadOnlyMemory().Slice(0, 53), truth.Slice(150, 53), BufferComparer.Memory);
            }
        }
Esempio n. 2
0
        public static async Task MemoryBufferStream_ReadAsync_SmallBuffer()
        {
            var truth  = BufferComparerTests.GenerateSegment(13, 203, 1).AsReadOnlyMemory();
            var buffer = new byte[100];

            using (var sut = new MemoryBufferStream(truth))
            {
                Assert.Equal(50, await sut.ReadAsync(buffer, 0, 50));
                Assert.Equal(buffer.AsReadOnlyMemory().Slice(0, 50), truth.Slice(0, 50), BufferComparer.Memory);

                Assert.Equal(100, await sut.ReadAsync(buffer));
                Assert.Equal(buffer.AsReadOnlyMemory(), truth.Slice(50, 100), BufferComparer.Memory);

                Assert.Equal(53, await sut.ReadAsync(buffer));
                Assert.Equal(buffer.AsReadOnlyMemory().Slice(0, 53), truth.Slice(150, 53), BufferComparer.Memory);

                // Ensure nothing was copied.

                Assert.Equal(0, await sut.ReadAsync(buffer));
                Assert.Equal(buffer.AsReadOnlyMemory().Slice(0, 53), truth.Slice(150, 53), BufferComparer.Memory);

                Assert.Equal(0, await sut.ReadAsync(buffer));
                Assert.Equal(buffer.AsReadOnlyMemory().Slice(0, 53), truth.Slice(150, 53), BufferComparer.Memory);
            }
        }
Esempio n. 3
0
        public static void MemoryBufferStream_CopyAsync()
        {
            var truth = BufferComparerTests.GenerateSegment(13, 99834, 1).AsReadOnlyMemory();

            using (var sut = new MemoryBufferStream(truth))
                using (var ms = new MemoryStream())
                {
                    sut.CopyToAsync(ms, 500);

                    var buf = ms.ToArray();
                    Assert.Equal(truth, buf.AsReadOnlyMemory(), BufferComparer.Memory);
                    Assert.Equal(truth.Length, sut.Position);
                }
        }
Esempio n. 4
0
        public static void MemoryBufferStream_ReadByte()
        {
            var truth = BufferComparerTests.GenerateSegment(13, 107, 1).AsReadOnlyMemory();

            using (var sut = new MemoryBufferStream(truth))
            {
                for (var i = 0; i < truth.Length; i++)
                {
                    Assert.Equal(truth.Span[i], sut.ReadByte());
                }
                Assert.Equal(-1, sut.ReadByte());
                Assert.Equal(-1, sut.ReadByte());
            }
        }
Esempio n. 5
0
        public static void StreamExtensions_Write()
        {
            var buffer = BufferComparerTests.GenerateSegment(0, 64, 1);
            var memory = new ReadOnlyMemory <byte>(buffer.Array, buffer.Offset, buffer.Count);

            using (var specimen = new MemoryStream())
            {
                specimen.Write(memory);
                Assert.Equal((IEnumerable <byte>)buffer, specimen.ToArray());
            }

            using (var specimen = new MemoryStream())
            {
                specimen.Write(memory, 13);
                Assert.Equal((IEnumerable <byte>)buffer, specimen.ToArray());
            }
        }
Esempio n. 6
0
        public static void MemoryBufferStream_AsyncPatternRead_BigBuffer()
        {
            var truth  = BufferComparerTests.GenerateSegment(13, 107, 1).AsReadOnlyMemory();
            var buffer = new byte[1024];

            using (var sut = new MemoryBufferStream(truth))
            {
                Assert.Equal(107, sut.SyncAsyncRead(buffer, 0, 1024));
                Assert.Equal(buffer.AsReadOnlyMemory().Slice(0, 107), truth, BufferComparer.Memory);

                for (var i = 107; i < 1024; i++)
                {
                    Assert.Equal(0, buffer[i]);
                }

                Assert.Equal(0, sut.SyncAsyncRead(buffer, 0, 1024));
                Assert.Equal(buffer.AsReadOnlyMemory().Slice(0, 107), truth, BufferComparer.Memory);
            }
        }
Esempio n. 7
0
        public static async Task MemoryBufferStream_ReadAsync_BigBuffer()
        {
            var truth  = BufferComparerTests.GenerateSegment(13, 107, 1).AsReadOnlyMemory();
            var buffer = new byte[1024];

            using (var sut = new MemoryBufferStream(truth))
            {
                Assert.Equal(107, await sut.ReadAsync(buffer));
                Assert.Equal(buffer.AsReadOnlyMemory().Slice(0, 107), truth, BufferComparer.Memory);

                for (var i = 107; i < 1024; i++)
                {
                    Assert.Equal(0, buffer[i]);
                }

                Assert.Equal(0, await sut.ReadAsync(buffer));
                Assert.Equal(buffer.AsReadOnlyMemory().Slice(0, 107), truth, BufferComparer.Memory);
            }
        }