Exemple #1
0
        public static async Task CopyToAsyncTest()
        {
            byte[] testData = ArrayHelpers.CreateByteArray(8192);

            using (var manager = new UmsManager(FileAccess.Read, testData))
            {
                UnmanagedMemoryStream ums = manager.Stream;
                UmsTests.ReadUmsInvariants(ums);
                MemoryStream destination = new MemoryStream();

                destination.Position = 0;
                await ums.CopyToAsync(destination);

                Assert.Equal(testData, destination.ToArray());

                destination.Position = 0;
                await ums.CopyToAsync(destination, 2);

                Assert.Equal(testData, destination.ToArray());

                destination.Position = 0;
                await ums.CopyToAsync(destination, 0x1000, new CancellationTokenSource().Token);

                Assert.Equal(testData, destination.ToArray());

                await Assert.ThrowsAnyAsync <OperationCanceledException>(() => ums.CopyToAsync(destination, 0x1000, new CancellationToken(true)));
            }

            // copy to disposed stream should throw
            using (var manager = new UmsManager(FileAccess.Read, testData))
            {
                UnmanagedMemoryStream ums = manager.Stream;
                UmsTests.ReadUmsInvariants(ums);

                MemoryStream destination = new MemoryStream();
                destination.Dispose();

                await Assert.ThrowsAsync <ObjectDisposedException>(() => ums.CopyToAsync(destination));
            }

            // copy from disposed stream should throw
            using (var manager = new UmsManager(FileAccess.Read, testData))
            {
                UnmanagedMemoryStream ums = manager.Stream;
                UmsTests.ReadUmsInvariants(ums);
                ums.Dispose();

                MemoryStream destination = new MemoryStream();

                await Assert.ThrowsAsync <ObjectDisposedException>(() => ums.CopyToAsync(destination));
            }

            // cpoying to non-writeable stream should throw
            using (var manager = new UmsManager(FileAccess.Read, testData))
            {
                UnmanagedMemoryStream ums = manager.Stream;
                UmsTests.ReadUmsInvariants(ums);

                MemoryStream destination = new MemoryStream(new byte[0], false);

                await Assert.ThrowsAsync <NotSupportedException>(() => ums.CopyToAsync(destination));
            }

            // copying from non-readable stream should throw
            using (var manager = new UmsManager(FileAccess.Write, testData))
            {
                UnmanagedMemoryStream ums = manager.Stream;
                UmsTests.WriteUmsInvariants(ums);

                MemoryStream destination = new MemoryStream(new byte[0], false);

                await Assert.ThrowsAsync <NotSupportedException>(() => ums.CopyToAsync(destination));
            }
        }
Exemple #2
0
        public static void CopyToTest()
        {
            byte[] testData = ArrayHelpers.CreateByteArray(8192);

            using (var manager = new UmsManager(FileAccess.Read, testData))
            {
                UnmanagedMemoryStream ums = manager.Stream;
                UmsTests.ReadUmsInvariants(ums);
                MemoryStream destination = new MemoryStream();

                destination.Position = 0;
                ums.CopyTo(destination);
                Assert.Equal(testData, destination.ToArray());

                destination.Position = 0;
                ums.CopyTo(destination, 1);
                Assert.Equal(testData, destination.ToArray());
            }

            // copy to disposed stream should throw
            using (var manager = new UmsManager(FileAccess.Read, testData))
            {
                UnmanagedMemoryStream ums = manager.Stream;
                UmsTests.ReadUmsInvariants(ums);

                MemoryStream destination = new MemoryStream();
                destination.Dispose();

                Assert.Throws <ObjectDisposedException>(() => ums.CopyTo(destination));
            }

            // copy from disposed stream should throw
            using (var manager = new UmsManager(FileAccess.Read, testData))
            {
                UnmanagedMemoryStream ums = manager.Stream;
                UmsTests.ReadUmsInvariants(ums);
                ums.Dispose();

                MemoryStream destination = new MemoryStream();

                Assert.Throws <ObjectDisposedException>(() => ums.CopyTo(destination));
            }

            // copying to non-writeable stream should throw
            using (var manager = new UmsManager(FileAccess.Read, testData))
            {
                UnmanagedMemoryStream ums = manager.Stream;
                UmsTests.ReadUmsInvariants(ums);

                MemoryStream destination = new MemoryStream(new byte[0], false);

                Assert.Throws <NotSupportedException>(() => ums.CopyTo(destination));
            }

            // copying from non-readable stream should throw
            using (var manager = new UmsManager(FileAccess.Write, testData))
            {
                UnmanagedMemoryStream ums = manager.Stream;
                UmsTests.WriteUmsInvariants(ums);

                MemoryStream destination = new MemoryStream(new byte[0], false);

                Assert.Throws <NotSupportedException>(() => ums.CopyTo(destination));
            }
        }