public void MiddleDuplicateCanBeRemoved() { VerifySupportsExceptionsInDelegates(); var dotnet = new MemoryStream(new byte[] { 1, 2, 3, 4, 5 }); var stream = new SKManagedStream(dotnet, true); Assert.Equal(1, stream.ReadByte()); Assert.Equal(2, stream.ReadByte()); var dupe1 = stream.Duplicate(); Assert.Equal(1, dupe1.ReadByte()); Assert.Equal(2, dupe1.ReadByte()); Assert.Throws <InvalidOperationException>(() => stream.Duplicate()); var dupe2 = dupe1.Duplicate(); Assert.Equal(1, dupe2.ReadByte()); Assert.Equal(2, dupe2.ReadByte()); Assert.Throws <InvalidOperationException>(() => stream.Duplicate()); Assert.Throws <InvalidOperationException>(() => dupe1.Duplicate()); dupe1.Dispose(); Assert.Throws <InvalidOperationException>(() => stream.Duplicate()); dupe2.Dispose(); var dupe3 = stream.Duplicate(); Assert.Equal(1, dupe3.ReadByte()); Assert.Equal(2, dupe3.ReadByte()); Assert.Throws <InvalidOperationException>(() => stream.Duplicate()); }
public void DuplicateStreamIsCollected() { VerifySupportsExceptionsInDelegates(); var handle = DoWork(); CollectGarbage(); Assert.False(SKObject.GetInstance <SKManagedStream>(handle, out _)); IntPtr DoWork() { var dotnet = new MemoryStream(new byte[] { 1, 2, 3, 4, 5 }); var stream = new SKManagedStream(dotnet, true); Assert.Equal(1, stream.ReadByte()); Assert.Equal(2, stream.ReadByte()); var dupe1 = stream.Duplicate(); Assert.Equal(1, dupe1.ReadByte()); Assert.Equal(2, dupe1.ReadByte()); Assert.Throws <InvalidOperationException>(() => stream.Duplicate()); stream.Dispose(); var dupe2 = dupe1.Duplicate(); Assert.Equal(1, dupe2.ReadByte()); Assert.Equal(2, dupe2.ReadByte()); return(dupe2.Handle); } }
public void FullOwnershipIsTransferredToTheChildIfTheParentIsDisposed() { VerifySupportsExceptionsInDelegates(); var dotnet = new MemoryStream(new byte[] { 1, 2, 3, 4, 5 }); var stream = new SKManagedStream(dotnet, true); Assert.Equal(1, stream.ReadByte()); Assert.Equal(2, stream.ReadByte()); var dupe1 = stream.Duplicate(); Assert.Equal(1, dupe1.ReadByte()); Assert.Equal(2, dupe1.ReadByte()); Assert.Throws <InvalidOperationException>(() => stream.Duplicate()); stream.Dispose(); var dupe2 = dupe1.Duplicate(); Assert.Equal(1, dupe2.ReadByte()); Assert.Equal(2, dupe2.ReadByte()); Assert.Throws <InvalidOperationException>(() => dupe1.Duplicate()); dupe1.Dispose(); dupe2.Dispose(); Assert.Throws <ObjectDisposedException>(() => dotnet.Position); }
public void ManagedStreamReadsByteCorrectly() { var data = new byte[1024]; for (int i = 0; i < data.Length; i++) { data[i] = (byte)(i % byte.MaxValue); } var stream = new MemoryStream(data); var skManagedStream = new SKManagedStream(stream); skManagedStream.Rewind(); Assert.AreEqual(0, stream.Position); Assert.AreEqual(0, skManagedStream.Position); for (int i = 0; i < data.Length; i++) { skManagedStream.Position = i; Assert.AreEqual(i, stream.Position); Assert.AreEqual(i, skManagedStream.Position); Assert.AreEqual((byte)(i % byte.MaxValue), data[i]); Assert.AreEqual((byte)(i % byte.MaxValue), skManagedStream.ReadByte()); Assert.AreEqual(i + 1, stream.Position); Assert.AreEqual(i + 1, skManagedStream.Position); } }
public void StreamCannotBeDuplicatedMultipleTimes() { VerifySupportsExceptionsInDelegates(); var dotnet = new MemoryStream(new byte[] { 1, 2, 3, 4, 5 }); var stream = new SKManagedStream(dotnet, true); Assert.Equal(1, stream.ReadByte()); Assert.Equal(2, stream.ReadByte()); var dupe = stream.Duplicate(); Assert.Throws <InvalidOperationException>(() => stream.Duplicate()); Assert.Equal(1, dupe.ReadByte()); }
public void StreamCanBeForkedButTheOriginalCannotBeRead() { VerifySupportsExceptionsInDelegates(); var dotnet = new MemoryStream(new byte[] { 1, 2, 3, 4, 5 }); var stream = new SKManagedStream(dotnet, true); Assert.Equal(1, stream.ReadByte()); Assert.Equal(2, stream.ReadByte()); var dupe = stream.Fork(); Assert.NotSame(stream, dupe); Assert.IsType <SKManagedStream>(dupe); Assert.Equal(3, dupe.ReadByte()); Assert.Equal(4, dupe.ReadByte()); Assert.Throws <InvalidOperationException>(() => stream.ReadByte()); }