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 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 OneByteStreamRead() { using (var manager = new UmsManager(FileAccess.Read, new byte[] { 100 })) { UnmanagedMemoryStream stream = manager.Stream; UmsTests.ReadUmsInvariants(stream); var position = stream.Position; Assert.Equal(100, stream.ReadByte()); Assert.Equal(stream.Position, position + 1); position = stream.Position; Assert.Equal(stream.ReadByte(), -1); // end of stream Assert.Equal(stream.Position, position); } using (HGlobalSafeBuffer buffer = new HGlobalSafeBuffer(1)) using (var stream = new UnmanagedMemoryStream(buffer, 0, 1, FileAccess.ReadWrite)) { buffer.Write(0, (byte)100); var position = stream.Position; Assert.Equal(100, stream.ReadByte()); Assert.Equal(stream.Position, position + 1); Assert.Equal(stream.ReadByte(), -1); // end of stream Assert.Equal(stream.Position, position + 1); } }
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); } }
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); } }
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)); } }
public static void CannotWriteToReadStream() { using (var manager = new UmsManager(FileAccess.Read, 1000)) { UnmanagedMemoryStream stream = manager.Stream; UmsTests.ReadUmsInvariants(stream); Assert.Throws <NotSupportedException>(() => stream.WriteByte(1)); } }
public static void LengthTests() { using (var manager = new UmsManager(FileAccess.ReadWrite, 1000)) { UnmanagedMemoryStream stream = manager.Stream; UmsTests.ReadWriteUmsInvariants(stream); // TODO: uncomment once we run againts core CLR; currently type load issues with IOException // Assert.Throws<IOException>(() => stream.SetLength(1001)); Assert.Throws <ArgumentOutOfRangeException>(() => stream.SetLength(SByte.MinValue)); } }
public static void EmptyStreamRead() { using (var manager = new UmsManager(FileAccess.Read, 0)) { UnmanagedMemoryStream stream = manager.Stream; UmsTests.ReadUmsInvariants(stream); var position = stream.Position; Assert.Equal(manager.Stream.ReadByte(), -1); // end of stream Assert.Equal(stream.Position, position); } }
public void CannotWriteWithOverflow() { using (var manager = new UmsManager(FileAccess.Write, 1000)) { UnmanagedMemoryStream stream = manager.Stream; UmsTests.WriteUmsInvariants(stream); stream.Position = long.MaxValue; var bytes = new byte[3]; Assert.Throws <IOException>(() => Write(stream, bytes, 0, bytes.Length)); Assert.Throws <IOException>(() => stream.WriteByte(1)); } }
public static void LengthTests() { using (var manager = new UmsManager(FileAccess.ReadWrite, 1000)) { UnmanagedMemoryStream stream = manager.Stream; UmsTests.ReadWriteUmsInvariants(stream); Assert.Throws <IOException>(() => stream.SetLength(1001)); Assert.Throws <ArgumentOutOfRangeException>(() => stream.SetLength(sbyte.MinValue)); const long expectedLength = 500; stream.Position = 501; stream.SetLength(expectedLength); Assert.Equal(expectedLength, stream.Length); Assert.Equal(expectedLength, stream.Position); } }
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>()); } }
public void EmptyStreamRead() { using (var manager = new UmsManager(FileAccess.Read, 0)) { UnmanagedMemoryStream stream = manager.Stream; UmsTests.ReadUmsInvariants(stream); var position = stream.Position; Assert.Equal(manager.Stream.ReadByte(), -1); // end of stream Assert.Equal(stream.Position, position); } using (HGlobalSafeBuffer buffer = new HGlobalSafeBuffer(1)) using (var stream = new UnmanagedMemoryStream(buffer, 0, 0)) { var position = stream.Position; Assert.Equal(stream.ReadByte(), -1); // end of stream Assert.Equal(stream.Position, position); } }
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); } }
public static void CannotWriteWithOverflow() { using (var manager = new UmsManager(FileAccess.Write, 1000)) { UnmanagedMemoryStream stream = manager.Stream; UmsTests.WriteUmsInvariants(stream); if (IntPtr.Size == 4) { Assert.Throws <ArgumentOutOfRangeException>(() => stream.Position = long.MaxValue); stream.Position = int.MaxValue; } else { stream.Position = long.MaxValue; var bytes = new byte[3]; Assert.Throws <IOException>(() => stream.Write(bytes, 0, bytes.Length)); Assert.Throws <IOException>(() => stream.WriteByte(1)); } } }
public static void PositionTests() { using (var manager = new UmsManager(FileAccess.ReadWrite, 1000)) { UnmanagedMemoryStream stream = manager.Stream; UmsTests.ReadWriteUmsInvariants(stream); Assert.Throws <ArgumentOutOfRangeException>(() => { stream.Position = -1; }); // "Non-negative number required." Assert.Throws <ArgumentOutOfRangeException>(() => { stream.Position = unchecked (long.MaxValue + 1); }); Assert.Throws <ArgumentOutOfRangeException>(() => { stream.Position = int.MinValue; }); stream.Position = stream.Length; Assert.Equal(stream.Position, stream.Length); stream.Position = stream.Capacity; Assert.Equal(stream.Position, stream.Capacity); int mid = (int)stream.Length / 2; stream.Position = mid; Assert.Equal(stream.Position, mid); } }
public static void SeekTests() { int length = 1000; using (var manager = new UmsManager(FileAccess.ReadWrite, length)) { UnmanagedMemoryStream stream = manager.Stream; UmsTests.ReadWriteUmsInvariants(stream); // TODO: uncomment once we run againts core CLR; currently type load issues with IOException //Assert.Throws<IOException>(() => stream.Seek(unchecked(Int32.MaxValue + 1), SeekOrigin.Begin)); //Assert.Throws<IOException>(() => stream.Seek(Int64.MinValue, SeekOrigin.End)); Assert.Throws <ArgumentException>(() => stream.Seek(0, (SeekOrigin)7)); // Invalid seek origin stream.Seek(10, SeekOrigin.Begin); Assert.Equal(10, stream.Position); /* TODO: uncomment once we run againts core CLR; currently type load issues with IOException * Assert.Throws<IOException>(() => stream.Seek(-1, SeekOrigin.Begin)); // An attempt was made to move the position before the beginning of the stream * Assert.Equal(10, stream.Position); * * Assert.Throws<IOException>(() => stream.Seek(-(stream.Position + 1), SeekOrigin.Current)); // An attempt was made to move the position before the beginning of the stream * Assert.Equal(10, stream.Position); * * Assert.Throws<IOException>(() => stream.Seek(-(stream.Length + 1), SeekOrigin.End)); // "An attempt was made to move the position before the beginning of the stream." * Assert.Equal(10, stream.Position); */ // Seek from SeekOrigin.Begin stream.Seek(0, SeekOrigin.Begin); for (int position = 0; position < stream.Length; position++) { stream.Seek(position, SeekOrigin.Begin); Assert.Equal(position, stream.Position); } for (int position = (int)stream.Length; position >= 0; position--) { stream.Seek(position, SeekOrigin.Begin); Assert.Equal(position, stream.Position); } stream.Seek(0, SeekOrigin.Begin); // Seek from SeekOrigin.End for (int position = 0; position < stream.Length; position++) { stream.Seek(-position, SeekOrigin.End); Assert.Equal(length - position, stream.Position); } for (int position = (int)stream.Length; position >= 0; position--) { stream.Seek(-position, SeekOrigin.End); Assert.Equal(length - position, stream.Position); } // Seek from SeekOrigin.Current stream.Seek(0, SeekOrigin.Begin); for (int position = 0; position < stream.Length; position++) { stream.Seek(1, SeekOrigin.Current); Assert.Equal(position + 1, stream.Position); } for (int position = (int)stream.Length; position > 0; position--) { stream.Seek(-1, SeekOrigin.Current); Assert.Equal(position - 1, stream.Position); } } }
public static void SeekTests() { const int length = 1000; using (var manager = new UmsManager(FileAccess.ReadWrite, length)) { UnmanagedMemoryStream stream = manager.Stream; UmsTests.ReadWriteUmsInvariants(stream); Assert.Throws <IOException>(() => stream.Seek(unchecked (int.MaxValue + 1), SeekOrigin.Begin)); Assert.Throws <IOException>(() => stream.Seek(long.MinValue, SeekOrigin.End)); AssertExtensions.Throws <ArgumentException>(null, () => stream.Seek(0, (SeekOrigin)7)); // Invalid seek origin stream.Seek(10, SeekOrigin.Begin); Assert.Equal(10, stream.Position); Assert.Throws <IOException>(() => stream.Seek(-1, SeekOrigin.Begin)); // An attempt was made to move the position before the beginning of the stream Assert.Equal(10, stream.Position); Assert.Throws <IOException>(() => stream.Seek(-(stream.Position + 1), SeekOrigin.Current)); // An attempt was made to move the position before the beginning of the stream Assert.Equal(10, stream.Position); Assert.Throws <IOException>(() => stream.Seek(-(stream.Length + 1), SeekOrigin.End)); // "An attempt was made to move the position before the beginning of the stream." Assert.Equal(10, stream.Position); // Seek from SeekOrigin.Begin stream.Seek(0, SeekOrigin.Begin); for (int position = 0; position < stream.Length; position++) { stream.Seek(position, SeekOrigin.Begin); Assert.Equal(position, stream.Position); } for (int position = (int)stream.Length; position >= 0; position--) { stream.Seek(position, SeekOrigin.Begin); Assert.Equal(position, stream.Position); } stream.Seek(0, SeekOrigin.Begin); // Seek from SeekOrigin.End for (int position = 0; position < stream.Length; position++) { stream.Seek(-position, SeekOrigin.End); Assert.Equal(length - position, stream.Position); } for (int position = (int)stream.Length; position >= 0; position--) { stream.Seek(-position, SeekOrigin.End); Assert.Equal(length - position, stream.Position); } // Seek from SeekOrigin.Current stream.Seek(0, SeekOrigin.Begin); for (int position = 0; position < stream.Length; position++) { stream.Seek(1, SeekOrigin.Current); Assert.Equal(position + 1, stream.Position); } for (int position = (int)stream.Length; position > 0; position--) { stream.Seek(-1, SeekOrigin.Current); Assert.Equal(position - 1, stream.Position); } } }
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)); } }
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)); } }