public void TestReadFromUnseekableStream() { MemoryStream firstStream = new MemoryStream(); // Now the second stream _does_ support seeking. If the stream chainer ignores // that, it would overwrite data in the second stream. MemoryStream secondStream = new MemoryStream(); secondStream.Write(new byte[] { 0, 9, 8, 7, 6 }, 0, 5); secondStream.Position = 3; TestStream testStream = new TestStream(firstStream, true, true, false); ChainStream chainer = new ChainStream(new Stream[] { testStream, secondStream }); Assert.IsFalse(chainer.CanSeek); byte[] buffer = new byte[5]; int readByteCount = chainer.Read(buffer, 0, 3); Assert.AreEqual(3, readByteCount); Assert.AreEqual(new byte[] { 0, 9, 8, 0, 0 }, buffer); readByteCount = chainer.Read(buffer, 0, 3); Assert.AreEqual(2, readByteCount); Assert.AreEqual(new byte[] { 7, 6, 8, 0, 0 }, buffer); }
public void TestThrowOnLengthChange() { ChainStream chainer = chainTwoStreamsOfTenBytes(); Assert.Throws <NotSupportedException>( delegate() { chainer.SetLength(123); } ); }
public void TestSeeking() { ChainStream chainer = chainTwoStreamsOfTenBytes(); Assert.AreEqual(7, chainer.Seek(-13, SeekOrigin.End)); Assert.AreEqual(14, chainer.Seek(7, SeekOrigin.Current)); Assert.AreEqual(11, chainer.Seek(11, SeekOrigin.Begin)); }
public void TestThrowOnInvalidSeekReferencePoint() { ChainStream chainer = chainTwoStreamsOfTenBytes(); Assert.Throws <ArgumentException>( delegate() { chainer.Seek(1, (SeekOrigin)12345); } ); }
public void TestPositionChange() { ChainStream chainer = chainTwoStreamsOfTenBytes(); chainer.Position = 7; Assert.AreEqual(chainer.Position, 7); chainer.Position = 14; Assert.AreEqual(chainer.Position, 14); }
public void TestThrowOnWriteToUnwriteableStream() { MemoryStream memoryStream = new MemoryStream(); TestStream testStream = new TestStream(memoryStream, true, false, true); ChainStream chainer = new ChainStream(new Stream[] { testStream }); Assert.Throws <NotSupportedException>( delegate() { chainer.Write(new byte[] { 1, 2, 3, 4, 5 }, 0, 5); } ); }
public void TestThrowOnReadFromUnreadableStream() { MemoryStream memoryStream = new MemoryStream(); TestStream testStream = new TestStream(memoryStream, false, true, true); ChainStream chainer = new ChainStream(new Stream[] { testStream }); Assert.Throws <NotSupportedException>( delegate() { chainer.Read(new byte[5], 0, 5); } ); }
public void TestThrowOnSetPositionWithUnseekableStream() { MemoryStream memoryStream = new MemoryStream(); TestStream testStream = new TestStream(memoryStream, true, true, false); ChainStream chainer = new ChainStream(new Stream[] { testStream }); Assert.Throws <NotSupportedException>( delegate() { chainer.Position = 123; } ); }
public void TestThrowOnGetLengthWithUnseekableStream() { MemoryStream memoryStream = new MemoryStream(); TestStream testStream = new TestStream(memoryStream, true, true, false); ChainStream chainer = new ChainStream(new Stream[] { testStream }); Assert.Throws <NotSupportedException>( delegate() { Assert.IsTrue(chainer.Length != chainer.Length); } ); }
public void TestReadBeyondEndOfStream() { ChainStream chainer = chainTwoStreamsOfTenBytes(); chainer.Seek(10, SeekOrigin.End); // This is how the MemoryStream behaves: it returns 0 bytes. int readByteCount = chainer.Read(new byte[1], 0, 1); Assert.AreEqual(0, readByteCount); }
public void TestPartitionedRead() { ChainStream chainer = chainTwoStreamsOfTenBytes(); ((MemoryStream)chainer.ChainedStreams[0]).Write( new byte[] { 1, 2, 3, 4, 5 }, 0, 5 ); ((MemoryStream)chainer.ChainedStreams[1]).Write( new byte[] { 6, 7, 8, 9, 10 }, 0, 5 ); chainer.Position = 3; byte[] buffer = new byte[15]; int bytesRead = chainer.Read(buffer, 0, 14); Assert.AreEqual(14, bytesRead); Assert.AreEqual(new byte[] { 4, 5, 0, 0, 0, 0, 0, 6, 7, 8, 9, 10, 0, 0, 0 }, buffer); }
public void TestWriteToUnseekableStream() { MemoryStream firstStream = new MemoryStream(); // Now the second stream _does_ support seeking. If the stream chainer ignores // that, it would overwrite data in the second stream. MemoryStream secondStream = new MemoryStream(); secondStream.Write(new byte[] { 0, 9, 8, 7, 6 }, 0, 5); secondStream.Position = 0; TestStream testStream = new TestStream(firstStream, true, true, false); ChainStream chainer = new ChainStream(new Stream[] { testStream, secondStream }); chainer.Write(new byte[] { 1, 2, 3, 4, 5 }, 0, 5); Assert.IsFalse(chainer.CanSeek); Assert.AreEqual(0, firstStream.Length); Assert.AreEqual(new byte[] { 0, 9, 8, 7, 6, 1, 2, 3, 4, 5 }, secondStream.ToArray()); }
public void TestFlush() { MemoryStream firstStream = new MemoryStream(); TestStream firstTestStream = new TestStream(firstStream, true, true, true); MemoryStream secondStream = new MemoryStream(); TestStream secondTestStream = new TestStream(secondStream, true, true, true); ChainStream chainer = new ChainStream( new Stream[] { firstTestStream, secondTestStream } ); Assert.AreEqual(0, firstTestStream.FlushCallCount); Assert.AreEqual(0, secondTestStream.FlushCallCount); chainer.Flush(); Assert.AreEqual(1, firstTestStream.FlushCallCount); Assert.AreEqual(1, secondTestStream.FlushCallCount); }
public void TestWriteAfterResize() { ChainStream chainer = chainTwoStreamsOfTenBytes(); // The first stream has a size of 10 bytes, so this goes into the second stream chainer.Position = 11; chainer.Write(new byte[] { 12, 34 }, 0, 2); // Now we resize the first stream to 15 bytes, so this goes into the first stream ((MemoryStream)chainer.ChainedStreams[0]).SetLength(15); chainer.Write(new byte[] { 56, 78, 11, 22 }, 0, 4); Assert.AreEqual( new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 56, 78 }, ((MemoryStream)chainer.ChainedStreams[0]).ToArray() ); Assert.AreEqual( new byte[] { 11, 22, 34, 0, 0, 0, 0, 0, 0, 0 }, ((MemoryStream)chainer.ChainedStreams[1]).ToArray() ); }
public void TestPartitionedWrite() { ChainStream chainer = chainTwoStreamsOfTenBytes(); byte[] testData = new byte[20]; for (int index = 0; index < testData.Length; ++index) { testData[index] = (byte)(index + 1); } chainer.Position = 5; chainer.Write(testData, 0, testData.Length); Assert.AreEqual( new byte[] { 0, 0, 0, 0, 0, 1, 2, 3, 4, 5 }, ((MemoryStream)chainer.ChainedStreams[0]).ToArray() ); Assert.AreEqual( new byte[] { 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 }, ((MemoryStream)chainer.ChainedStreams[1]).ToArray() ); }
public void TestThrowOnReadFromUnreadableStream() { MemoryStream memoryStream = new MemoryStream(); TestStream testStream = new TestStream(memoryStream, false, true, true); ChainStream chainer = new ChainStream(new Stream[] { testStream }); Assert.Throws<NotSupportedException>( delegate() { chainer.Read(new byte[5], 0, 5); } ); }
public void TestThrowOnWriteToUnwriteableStream() { MemoryStream memoryStream = new MemoryStream(); TestStream testStream = new TestStream(memoryStream, true, false, true); ChainStream chainer = new ChainStream(new Stream[] { testStream }); Assert.Throws<NotSupportedException>( delegate() { chainer.Write(new byte[] { 1, 2, 3, 4, 5 }, 0, 5); } ); }
public void TestThrowOnSetPositionWithUnseekableStream() { MemoryStream memoryStream = new MemoryStream(); TestStream testStream = new TestStream(memoryStream, true, true, false); ChainStream chainer = new ChainStream(new Stream[] { testStream }); Assert.Throws<NotSupportedException>( delegate() { chainer.Position = 123; } ); }
public void TestThrowOnGetLengthWithUnseekableStream() { MemoryStream memoryStream = new MemoryStream(); TestStream testStream = new TestStream(memoryStream, true, true, false); ChainStream chainer = new ChainStream(new Stream[] { testStream }); Assert.Throws<NotSupportedException>( delegate() { Assert.IsTrue(chainer.Length != chainer.Length); } ); }