public static unsafe void BufferCtor() { const int length = 99; using (FakeSafeBuffer buffer = new FakeSafeBuffer(length)) using (var stream = new DerivedUnmanagedMemoryStream(buffer, length, FileAccess.Read)) { Assert.True(stream.CanRead); Assert.True(stream.CanSeek); Assert.False(stream.CanWrite); Assert.Equal(length, stream.Length); Assert.Equal(length, stream.Capacity); Assert.Equal(0, stream.Position); } using (FakeSafeBuffer buffer = new FakeSafeBuffer(length)) using (var stream = new DerivedUnmanagedMemoryStream()) { Assert.False(stream.CanRead); Assert.False(stream.CanSeek); Assert.False(stream.CanWrite); stream.Initialize(buffer, 0, length, FileAccess.Write); Assert.Throws <InvalidOperationException>(() => stream.Initialize(buffer, 0, length, FileAccess.Write)); Assert.False(stream.CanRead); Assert.True(stream.CanSeek); Assert.True(stream.CanWrite); Assert.Equal(length, stream.Length); Assert.Equal(length, stream.Capacity); Assert.Equal(0, stream.Position); Assert.Throws <NotSupportedException>(() => stream.SetLength(1)); } }
public static unsafe void PointerCtor() { int someInt32 = 42; int * pInt32 = &someInt32; byte *pByte = (byte *)pInt32; using (var stream = new UnmanagedMemoryStream(pByte, 0)) { Assert.True(stream.CanRead); Assert.True(stream.CanSeek); Assert.False(stream.CanWrite); Assert.False(stream.CanTimeout); Assert.Equal(0, stream.Length); Assert.Equal(0, stream.Capacity); Assert.Equal(0, stream.Position); Assert.Throws <InvalidOperationException>(() => stream.ReadTimeout); Assert.Throws <InvalidOperationException>(() => stream.ReadTimeout = 42); Assert.Throws <InvalidOperationException>(() => stream.WriteTimeout); Assert.Throws <InvalidOperationException>(() => stream.WriteTimeout = 42); Assert.True(stream.PositionPointer == pByte); } using (var stream = new DerivedUnmanagedMemoryStream()) { Assert.False(stream.CanRead); Assert.False(stream.CanSeek); Assert.False(stream.CanWrite); Assert.False(stream.CanTimeout); Assert.Throws <ArgumentOutOfRangeException>(() => stream.Initialize(pByte, -1, 4, FileAccess.Read)); Assert.Throws <ArgumentOutOfRangeException>(() => stream.Initialize(pByte, 1, -4, FileAccess.Read)); Assert.Throws <ArgumentOutOfRangeException>(() => stream.Initialize(pByte, 5, 4, FileAccess.Read)); Assert.Throws <ArgumentOutOfRangeException>(() => stream.Initialize(pByte, 1, 4, (FileAccess)12345)); stream.Initialize(pByte, 1, 4, FileAccess.ReadWrite); Assert.Throws <InvalidOperationException>(() => stream.Initialize(pByte, 1, 4, FileAccess.ReadWrite)); Assert.True(stream.CanRead); Assert.True(stream.CanSeek); Assert.True(stream.CanWrite); Assert.Equal(1, stream.Length); Assert.Equal(4, stream.Capacity); Assert.Equal(0, stream.Position); Assert.Throws <InvalidOperationException>(() => stream.ReadTimeout); Assert.Throws <InvalidOperationException>(() => stream.ReadTimeout = 42); Assert.Throws <InvalidOperationException>(() => stream.WriteTimeout); Assert.Throws <InvalidOperationException>(() => stream.WriteTimeout = 42); Assert.True(stream.PositionPointer == pByte); } }
public static unsafe void PointerCtor() { int someInt32 = 42; int* pInt32 = &someInt32; byte* pByte = (byte*)pInt32; using (var stream = new UnmanagedMemoryStream(pByte, 0)) { Assert.True(stream.CanRead); Assert.True(stream.CanSeek); Assert.False(stream.CanWrite); Assert.False(stream.CanTimeout); Assert.Equal(0, stream.Length); Assert.Equal(0, stream.Capacity); Assert.Equal(0, stream.Position); Assert.Throws<InvalidOperationException>(() => stream.ReadTimeout); Assert.Throws<InvalidOperationException>(() => stream.ReadTimeout = 42); Assert.Throws<InvalidOperationException>(() => stream.WriteTimeout); Assert.Throws<InvalidOperationException>(() => stream.WriteTimeout = 42); Assert.True(stream.PositionPointer == pByte); } using (var stream = new DerivedUnmanagedMemoryStream()) { Assert.False(stream.CanRead); Assert.False(stream.CanSeek); Assert.False(stream.CanWrite); Assert.False(stream.CanTimeout); Assert.Throws<ArgumentOutOfRangeException>(() => stream.Initialize(pByte, -1, 4, FileAccess.Read)); Assert.Throws<ArgumentOutOfRangeException>(() => stream.Initialize(pByte, 1, -4, FileAccess.Read)); Assert.Throws<ArgumentOutOfRangeException>(() => stream.Initialize(pByte, 5, 4, FileAccess.Read)); Assert.Throws<ArgumentOutOfRangeException>(() => stream.Initialize(pByte, 1, 4, (FileAccess)12345)); stream.Initialize(pByte, 1, 4, FileAccess.ReadWrite); Assert.Throws<InvalidOperationException>(() => stream.Initialize(pByte, 1, 4, FileAccess.ReadWrite)); Assert.True(stream.CanRead); Assert.True(stream.CanSeek); Assert.True(stream.CanWrite); Assert.Equal(1, stream.Length); Assert.Equal(4, stream.Capacity); Assert.Equal(0, stream.Position); Assert.Throws<InvalidOperationException>(() => stream.ReadTimeout); Assert.Throws<InvalidOperationException>(() => stream.ReadTimeout = 42); Assert.Throws<InvalidOperationException>(() => stream.WriteTimeout); Assert.Throws<InvalidOperationException>(() => stream.WriteTimeout = 42); Assert.True(stream.PositionPointer == pByte); } }
public static unsafe void BufferCtor() { const int length = 99; using (FakeSafeBuffer buffer = new FakeSafeBuffer(length)) using (var stream = new DerivedUnmanagedMemoryStream(buffer, length, FileAccess.Read)) { Assert.True(stream.CanRead); Assert.True(stream.CanSeek); Assert.False(stream.CanWrite); Assert.Equal(length, stream.Length); Assert.Equal(length, stream.Capacity); Assert.Equal(0, stream.Position); } using (FakeSafeBuffer buffer = new FakeSafeBuffer(length)) using (var stream = new DerivedUnmanagedMemoryStream()) { Assert.False(stream.CanRead); Assert.False(stream.CanSeek); Assert.False(stream.CanWrite); stream.Initialize(buffer, 0, length, FileAccess.Write); Assert.Throws<InvalidOperationException>(() => stream.Initialize(buffer, 0, length, FileAccess.Write)); Assert.False(stream.CanRead); Assert.True(stream.CanSeek); Assert.True(stream.CanWrite); Assert.Equal(length, stream.Length); Assert.Equal(length, stream.Capacity); Assert.Equal(0, stream.Position); Assert.Throws<NotSupportedException>(() => stream.SetLength(1)); } }