コード例 #1
0
 public void TestAccessBackingBytes()
 {
     var backingBytes = new byte[100];
     using (var buffer = new ByteArrayBuffer(backingBytes, 10, 80, false))
     {
         var segment = buffer.AccessBackingBytes(20);
         Assert.AreSame(backingBytes, segment.Array);
         Assert.AreEqual(30, segment.Offset);
         Assert.AreEqual(60, segment.Count);
     }
 }
コード例 #2
0
        public async Task ReadBytesAsync_with_byte_buffer_should_have_expected_effect_for_partial_reads(int testCase, int[] partition)
        {
            var bytes = new byte[] { 1, 2, 3 };
            var stream = Substitute.For<Stream>();
            var destination = new ByteArrayBuffer(new byte[3], 3);
            var n = 0;
            var p = 0;
            stream.ReadAsync(Arg.Any<byte[]>(), Arg.Any<int>(), Arg.Any<int>(), Arg.Any<CancellationToken>()).Returns(x =>
            {
                var l = partition[n++];
                var b = (byte[])x[0];
                var o = (int)x[1];
                Buffer.BlockCopy(bytes, p, b, o, l);
                p += l;
                return Task.FromResult(l);
            });

            await stream.ReadBytesAsync(destination, 0, 3, CancellationToken.None);

            destination.AccessBackingBytes(0).Array.Should().Equal(bytes);
        }
コード例 #3
0
        public async Task ReadBytesAsync_with_byte_buffer_should_have_expected_effect_for_offset(int offset, byte[] expectedBytes)
        {
            var bytes = new byte[] { 1 };
            var stream = new MemoryStream(bytes);
            var destination = new ByteArrayBuffer(new byte[3]);

            await stream.ReadBytesAsync(destination, offset, 1, CancellationToken.None);

            destination.AccessBackingBytes(0).Array.Should().Equal(expectedBytes);
        }
コード例 #4
0
        public async Task ReadBytesAsync_with_byte_buffer_should_have_expected_effect_for_partial_reads(int testCase, int[] partition)
        {
            var bytes = new byte[] { 1, 2, 3 };
            var mockStream = new Mock<Stream>();
            var destination = new ByteArrayBuffer(new byte[3], 3);
            var n = 0;
            var position = 0;
            mockStream.Setup(s => s.ReadAsync(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<int>(), It.IsAny<CancellationToken>()))
                .Returns((byte[] buffer, int offset, int count, CancellationToken cancellationToken) =>
                {
                    var length = partition[n++];
                    Buffer.BlockCopy(bytes, position, buffer, offset, length);
                    position += length;
                    return Task.FromResult(length);
                });

            await mockStream.Object.ReadBytesAsync(destination, 0, 3, CancellationToken.None);

            destination.AccessBackingBytes(0).Array.Should().Equal(bytes);
        }