Exemple #1
0
        public static void ReadWrite()
        {
            var length = 1000;

            using (var manager = new UmsManager(FileAccess.ReadWrite, length))
            {
                UnmanagedMemoryStream stream = manager.Stream;
                UmsTests.ReadWriteUmsInvariants(stream);
                Assert.Equal(stream.Length, length);

                var bytes = ArrayHelpers.CreateByteArray(length);
                var copy  = bytes.Copy();
                stream.Write(copy, 0, length);

                var memory = manager.ToArray();
                Assert.True(ArrayHelpers.Comparer <byte>().Equals(bytes, memory));

                stream.Seek(0, SeekOrigin.Begin);
                byte[] read = UmsReadTests.ReadAllBytes(stream);
                Assert.Equal(stream.Position, read.Length);

                byte[] current = manager.ToArray();
                Assert.True(ArrayHelpers.Comparer <byte>().Equals(read, current));
                Assert.True(ArrayHelpers.Comparer <byte>().Equals(read, bytes));

                stream.Write(new byte[0], 0, 0);
            }
        }
Exemple #2
0
        public static void ReadWriteByte()
        {
            var length = 1000;

            using (var manager = new UmsManager(FileAccess.ReadWrite, length))
            {
                UnmanagedMemoryStream stream = manager.Stream;
                UmsTests.ReadWriteUmsInvariants(stream);

                var bytes = ArrayHelpers.CreateByteArray(length);

                for (int index = 0; index < bytes.Length; index++)
                {
                    byte byteToWrite = bytes[index];
                    stream.WriteByte(byteToWrite);
                    stream.Position--;
                    int read = stream.ReadByte();
                    Assert.Equal((byte)read, byteToWrite);
                }

                var memory = manager.ToArray();

                Assert.True(ArrayHelpers.Comparer <byte>().Equals(bytes, memory));
            }
        }
Exemple #3
0
        public static void ReadWriteByteSafeBuffer()
        {
            var length = 1000;

            using (var buffer = new TestSafeBuffer(length))
            {
                var stream = new UnmanagedMemoryStream(buffer, 0, (long)buffer.ByteLength, FileAccess.ReadWrite);
                Assert.Equal(stream.Length, length);

                var bytes = ArrayHelpers.CreateByteArray(length);
                for (int index = 0; index < length; index++)
                {
                    stream.WriteByte(bytes[index]);
                }

                stream.Position = 0;
                for (int index = 0; index < length; index++)
                {
                    int value = stream.ReadByte();
                    Assert.Equal(bytes[index], (byte)value);
                }

                var memory = buffer.ToArray();
                Assert.True(ArrayHelpers.Comparer <byte>().Equals(bytes, memory));
            }
        }
        public void WriteByte()
        {
            const int length = 1000;

            using (var manager = new UmsManager(FileAccess.Write, length))
            {
                UnmanagedMemoryStream stream = manager.Stream;
                UmsTests.WriteUmsInvariants(stream);

                var bytes = ArrayHelpers.CreateByteArray(length);
                for (int index = 0; index < bytes.Length; index++)
                {
                    stream.WriteByte(bytes[index]);
                }
                var memory = manager.ToArray();
                Assert.Equal(bytes, memory, ArrayHelpers.Comparer <byte>());

                stream.SetLength(0);
                stream.Position = 1;
                bytes           = ArrayHelpers.CreateByteArray(length);
                for (int index = 1; index < bytes.Length; index++)
                {
                    stream.WriteByte(bytes[index]);
                }
                stream.Position = 0;
                stream.WriteByte(bytes[0]);
                memory = manager.ToArray();
                Assert.Equal(bytes, memory, ArrayHelpers.Comparer <byte>());
            }
        }
        public void ReadWrite()
        {
            const int length = 1000;

            using (var manager = new UmsManager(FileAccess.ReadWrite, length))
            {
                UnmanagedMemoryStream stream = manager.Stream;
                UmsTests.ReadWriteUmsInvariants(stream);
                Assert.Equal(stream.Length, length);

                var bytes = ArrayHelpers.CreateByteArray(length);
                var copy  = bytes.Copy();
                Write(stream, copy, 0, length);

                var memory = manager.ToArray();
                Assert.Equal(bytes, memory, ArrayHelpers.Comparer <byte>());

                stream.Seek(0, SeekOrigin.Begin);
                byte[] read = ReadAllBytes(stream);
                Assert.Equal(stream.Position, read.Length);

                byte[] current = manager.ToArray();
                Assert.Equal(current, read, ArrayHelpers.Comparer <byte>());
                Assert.Equal(bytes, read, ArrayHelpers.Comparer <byte>());

                Write(stream, new byte[0], 0, 0);
            }
        }
Exemple #6
0
        public static void CannotUseStreamAfterDispose()
        {
            using (var manager = new UmsManager(FileAccess.ReadWrite, 1000))
            {
                UnmanagedMemoryStream stream = manager.Stream;
                UmsTests.ReadWriteUmsInvariants(stream);

                stream.Dispose();
                Assert.False(stream.CanRead);
                Assert.False(stream.CanWrite);
                Assert.False(stream.CanSeek);

                Assert.Throws <ObjectDisposedException>(() => { long x = stream.Capacity; });

                Assert.Throws <ObjectDisposedException>(() => { long y = stream.Length; });
                Assert.Throws <ObjectDisposedException>(() => { stream.SetLength(2); });

                Assert.Throws <ObjectDisposedException>(() => { long z = stream.Position; });
                Assert.Throws <ObjectDisposedException>(() => { stream.Position = 2; });

                Assert.Throws <ObjectDisposedException>(() => stream.Seek(0, SeekOrigin.Begin));
                Assert.Throws <ObjectDisposedException>(() => stream.Seek(0, SeekOrigin.Current));
                Assert.Throws <ObjectDisposedException>(() => stream.Seek(0, SeekOrigin.End));

                byte[] buffer = ArrayHelpers.CreateByteArray(10);
                Assert.Throws <ObjectDisposedException>(() => stream.Read(buffer, 0, buffer.Length));
                Assert.Throws <ObjectDisposedException>(() => stream.ReadByte());

                Assert.Throws <ObjectDisposedException>(() => stream.WriteByte(0));
                Assert.Throws <ObjectDisposedException>(() => stream.Write(buffer, 0, buffer.Length));
            }
        }
        public void Write()
        {
            const int length = 1000;

            using (var manager = new UmsManager(FileAccess.Write, length))
            {
                UnmanagedMemoryStream stream = manager.Stream;
                UmsTests.WriteUmsInvariants(stream);
                Assert.Equal(stream.Length, length);

                var bytes = ArrayHelpers.CreateByteArray(length);
                Write(stream, bytes.Copy(), 0, length);
                var memory = manager.ToArray();
                Assert.Equal(bytes, memory, ArrayHelpers.Comparer <byte>());

                Write(stream, new byte[0], 0, 0);

                stream.SetLength(1);
                Assert.Equal(1, stream.Length);
                stream.SetLength(4);
                Assert.Equal(4, stream.Length);
                stream.SetLength(0);
                Assert.Equal(0, stream.Length);

                stream.Position = 1;
                bytes           = ArrayHelpers.CreateByteArray(length - 1);
                Write(stream, bytes, 0, length - 1);
                memory = manager.ToArray();
                for (int i = 0; i < bytes.Length; i++)
                {
                    Assert.Equal(bytes[i], memory[i + 1]);
                }

                Assert.True(stream.WriteAsync(bytes, 0, bytes.Length, new CancellationToken(true)).IsCanceled);

                stream.Position = 0;
                bytes           = ArrayHelpers.CreateByteArray(length);
                for (int i = 0; i < 4; i++)
                {
                    Task t = stream.WriteAsync(bytes, i * (bytes.Length / 4), bytes.Length / 4);
                    Assert.True(t.Status == TaskStatus.RanToCompletion);
                }
                Assert.Equal(bytes, manager.ToArray(), ArrayHelpers.Comparer <byte>());
            }
        }
Exemple #8
0
        public static void WriteSafeBuffer()
        {
            var length = 1000;

            using (var buffer = new TestSafeBuffer(length))
            {
                var stream = new UnmanagedMemoryStream(buffer, 0, (long)buffer.ByteLength, FileAccess.Write);
                Assert.Equal(stream.Length, length);

                var bytes = ArrayHelpers.CreateByteArray(length);
                var copy  = bytes.Copy();
                stream.Write(copy, 0, length);

                var memory = buffer.ToArray();

                Assert.True(ArrayHelpers.Comparer <byte>().Equals(bytes, memory));

                stream.Write(new byte[0], 0, 0);
            }
        }
Exemple #9
0
        public static void Write()
        {
            const int length = 1000;

            using (var manager = new UmsManager(FileAccess.Write, length))
            {
                UnmanagedMemoryStream stream = manager.Stream;
                UmsTests.WriteUmsInvariants(stream);
                Assert.Equal(stream.Length, length);

                var bytes = ArrayHelpers.CreateByteArray(length);
                stream.Write(bytes.Copy(), 0, length);

                var memory = manager.ToArray();

                Assert.Equal(bytes, memory, ArrayHelpers.Comparer <byte>());

                stream.Write(new byte[0], 0, 0);
            }
        }
Exemple #10
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));
            }

            // copying to non-writable 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 #11
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-writable 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));
            }
        }