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 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 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 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 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 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 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() { 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 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); } } }