public void ConcatStreamReadTest(ConcatStream cs, BufferType bufferType, int offset, int length, ReadResult expectedResult) { ConcatStreamReadTestVerification(bufferType, offset, length, expectedResult); byte[] buffer = bufferType == BufferType.Null ? null : new byte[5]; switch (expectedResult) { case ReadResult.NullException: case ReadResult.ArgumentException: case ReadResult.OutOfRangeException: TestDelegate testDelegate = () => { cs.Read(buffer, offset, length); }; switch (expectedResult) { case ReadResult.NullException: Assert.Throws <System.ArgumentNullException>(testDelegate); break; case ReadResult.ArgumentException: Assert.Throws <System.ArgumentException>(testDelegate); break; case ReadResult.OutOfRangeException: Assert.Throws <System.ArgumentOutOfRangeException>(testDelegate); break; } break; case ReadResult.ZeroResult: var result = cs.Read(buffer, offset, length); Assert.That(result, Is.Zero); if (bufferType == BufferType.Instance) { Assert.That(buffer, Is.All.Zero); } break; case ReadResult.NonZeroResult: result = cs.Read(buffer, offset, length); Assert.That(result, Is.Not.Zero); if (bufferType == BufferType.Instance) { for (int i = offset; i < (result + offset); i++) { Assert.That(buffer[i], Is.EqualTo(ReadingSourceData[ReadingSourceNotEmpty][i - offset])); } } break; default: Assert.Fail($"Unknown {nameof(expectedResult)}: {expectedResult}"); break; } }
public void AttemptToReadMoreBytesThanAvailableFromConcatStream() { // Arrange byte[] buffer = new byte[1046]; byte[] buffer1 = Encoding.ASCII.GetBytes("123"); byte[] buffer2 = Encoding.ASCII.GetBytes("456"); byte[] expectedBuffer = new byte[buffer1.Length + buffer2.Length + 4]; System.Buffer.BlockCopy(buffer1, 0, expectedBuffer, 0, buffer1.Length); System.Buffer.BlockCopy(buffer2, 0, expectedBuffer, buffer1.Length, buffer2.Length); MemoryStream stream1 = new MemoryStream(buffer1); MemoryStream stream2 = new MemoryStream(buffer2); ConcatStream streamToTest = new ConcatStream(stream1, stream2); // Act int numberOfBytesRead = streamToTest.Read(buffer, 0, expectedBuffer.Length); //Assert Assert.IsTrue(streamToTest.CanSeek); Assert.AreEqual(streamToTest.Length, numberOfBytesRead); for (int i = 0; i < streamToTest.Length; i++) { Assert.AreEqual(expectedBuffer[i], buffer[i]); } }
public void basicConcatStreamSeek() { byte[] writeBuffer = new byte[14]; byte[] readBuffer = new byte[14]; MemoryStream s1 = new MemoryStream(); MemoryStream s2 = new MemoryStream(); ConcatStream testCS = new ConcatStream(s1, s2); for (int i = 0; i < 14; i++) { writeBuffer[i] = Convert.ToByte(i); } s1.Write(writeBuffer, 0, 6); s2.Write(writeBuffer, 6, 8); s1.Seek(0, SeekOrigin.Begin); s2.Seek(0, SeekOrigin.Begin); testCS.Read(readBuffer, 0, 14); Assert.AreEqual(14, testCS.Position); testCS.Seek(0, SeekOrigin.Begin); Assert.AreEqual(0, testCS.Position); Assert.AreEqual(0, s1.Position); Assert.AreEqual(0, s2.Position); }
public void ReadFromFirstStreamOnly() { // Arrange byte[] buffer = new byte[1046]; byte[] buffer1 = Encoding.ASCII.GetBytes("123"); byte[] buffer2 = Encoding.ASCII.GetBytes("456"); byte[] expectedBuffer = new byte[buffer1.Length + buffer2.Length]; System.Buffer.BlockCopy(buffer1, 0, expectedBuffer, 0, buffer1.Length); System.Buffer.BlockCopy(buffer2, 0, expectedBuffer, buffer1.Length, buffer2.Length); MemoryStream stream1 = new MemoryStream(buffer1); NoSeekMemoryStream stream2 = new NoSeekMemoryStream(buffer2); ConcatStream streamToTest = new ConcatStream(stream1, stream2); // Act int numberOfBytesRead = streamToTest.Read(buffer, 0, buffer1.Length); //Assert Assert.IsFalse(streamToTest.CanSeek); Assert.AreEqual(buffer1.Length, numberOfBytesRead); for (int i = 0; i < buffer1.Length; i++) { Assert.AreEqual(buffer1[i], buffer[i]); } }
public void ReadFromSecondStreamOnly() { // Arrange byte[] buffer = new byte[1046]; byte[] buffer1 = Encoding.ASCII.GetBytes("123"); byte[] buffer2 = Encoding.ASCII.GetBytes("456"); byte[] expectedBuffer = new byte[buffer1.Length + buffer2.Length]; System.Buffer.BlockCopy(buffer1, 0, expectedBuffer, 0, buffer1.Length); System.Buffer.BlockCopy(buffer2, 0, expectedBuffer, buffer1.Length, buffer2.Length); MemoryStream stream1 = new MemoryStream(buffer1); MemoryStream stream2 = new MemoryStream(buffer2); ConcatStream streamToTest = new ConcatStream(stream1, stream2); streamToTest.Position = buffer1.Length; Console.WriteLine(buffer1.Length.ToString()); Console.WriteLine("Position " + streamToTest.Position.ToString()); Console.WriteLine("Length " + streamToTest.Length.ToString()); // Act int numberOfBytesRead = streamToTest.Read(buffer, 0, buffer2.Length); //Assert Assert.AreEqual(3, numberOfBytesRead); for (int i = 0; i < buffer2.Length; i++) { Console.WriteLine("buffer[i]" + buffer[i].ToString()); Console.WriteLine("buffer2[i]" + buffer2[i].ToString()); Assert.AreEqual(buffer2[i], buffer[i]); } }
public void EmptyStreamReadZero() { var cs = new ConcatStream(); var buffer = new byte[0]; var res = cs.Read(buffer, 0, 0); Assert.That(res, Is.Zero); }
public void ConcatStreamTest() { ConcatStream cstr = new ConcatStream(new MemoryStream(Encoding.ASCII.GetBytes("newstream")), new NoSeekMemoryStream(Encoding.ASCII.GetBytes("newstream2"))); byte[] buf = new byte[19]; cstr.Read(buf, 0, 19); string str = Encoding.ASCII.GetString(buf); Assert.That("newstreamnewstream2" == str); }
//[Test()] public void ConcatStreamTest() { byte[] buffer1 = new byte[10]; byte[] buffer2 = new byte[10]; ConcatStream stream = new ConcatStream(new MemoryStream(buffer1), new NoSeekMemoryStream(buffer2)); stream.Read(new byte[20], 0, 20); stream.Seek(0, SeekOrigin.Begin); }
public void OnePopulatedStreamReadZero() { var cs = new ConcatStream(); cs.AppendSource(concatStreamSourceData); var buffer = new byte[0]; var res = cs.Read(buffer, 0, 0); Assert.That(res, Is.Zero); concatStreamSourceData.DidNotReceiveWithAnyArgs().Read(null, 0, 0); }
public void WriteTest() { ConcatStream cstr = new ConcatStream(new MemoryStream(Encoding.ASCII.GetBytes("newstream")), new MemoryStream()); string newStr = "stringthatshouldoverwriteeverythingelse"; cstr.Write(Encoding.ASCII.GetBytes(newStr), 0, newStr.Length); cstr.Seek(0, SeekOrigin.Begin); byte[] buf = new byte[newStr.Length]; cstr.Read(buf, 0, newStr.Length); Assert.That(newStr == Encoding.ASCII.GetString(buf)); }
public void randomConcatStreamRead() { Random rnd = new Random(); int read = 0; int ceiling = 14; int offset = 0; int floor = 1; read = rnd.Next(floor, ceiling - offset); //set up buffers byte[] writeBuffer = new byte[14]; byte[] readBuffer = new byte[14]; //setUp streams MemoryStream s1 = new MemoryStream(); MemoryStream s2 = new MemoryStream(); for (int i = 0; i < 14; i++) { writeBuffer [i] = Convert.ToByte(i); } s1.Write(writeBuffer, 0, 6); s2.Write(writeBuffer, 6, 8); s1.Seek(0, SeekOrigin.Begin); s2.Seek(0, SeekOrigin.Begin); ConcatStream testCS = new ConcatStream(s1, s2); while (ceiling != offset) { testCS.Read(readBuffer, offset, read); for (int i = 0; i < read; i++) { Assert.AreEqual(writeBuffer [offset + i], readBuffer [offset + i]); } offset += read; if (offset != ceiling) { read = rnd.Next(floor, ceiling - offset); } } }
public void boundaryRead() { byte[] writeBuffer = new byte[14]; byte[] readBuffer = new byte[1]; int read1; int read2; MemoryStream s1 = new MemoryStream(); MemoryStream s2 = new MemoryStream(); for (int i = 0; i < 14; i++) { writeBuffer[i] = Convert.ToByte(i); } s1.Write(writeBuffer, 0, 6); s2.Write(writeBuffer, 6, 8); s1.Seek(0, SeekOrigin.Begin); s2.Seek(0, SeekOrigin.Begin); ConcatStream testCS = new ConcatStream(s1, s2); long length = testCS.Length; testCS.Seek(length - 1, SeekOrigin.Begin); long position = testCS.Position; read1 = testCS.Read(readBuffer, 0, 1); read2 = testCS.Read(readBuffer, 0, 1); int x = 0; Assert.AreEqual(13, readBuffer [0]); }
public void EmptyStreamReadOne() { var cs = new ConcatStream(); var buffer = new byte[1]; Assert.That(buffer[0], Is.Zero); var res = cs.Read(buffer, 0, 1); Assert.That(res, Is.Zero); Assert.That(buffer[0], Is.Zero); }
public static void ReadAll(int bufferSize) { var buffer = new byte[bufferSize]; var sb = new StringBuilder(); while (true) { var read = Stream.Read(buffer, 0, buffer.Length); if (read == 0) { break; } sb.Append(Encoding.UTF8.GetChars(buffer, 0, read)); } Result = sb.ToString(); }
public void ReadTest() { MemoryStream str = new MemoryStream(Encoding.ASCII.GetBytes("newstreamnewstream2")); ConcatStream cstr = new ConcatStream(new MemoryStream(Encoding.ASCII.GetBytes("newstream")), new MemoryStream(Encoding.ASCII.GetBytes("newstream2")), 50); Random rand = new Random(); for (int i = 0; i < 19;) { int num = rand.Next() % 10; i += num; byte[] buf1 = new byte[10]; byte[] buf2 = new byte[10]; str.Read(buf1, 0, num); cstr.Read(buf2, 0, num); Assert.That(buf1.SequenceEqual(buf2)); } }
public static void Main(string[] args) { byte[] result = new byte[1046]; byte[] buffer1 = Encoding.ASCII.GetBytes("123"); byte[] buffer2 = Encoding.ASCII.GetBytes("456"); byte[] bufferToWrite = Encoding.ASCII.GetBytes("abcdef"); MemoryStream stream1 = new MemoryStream(buffer1); MemoryStream stream2 = new MemoryStream(buffer2); ConcatStream streamToTest = new ConcatStream(stream1, stream2); streamToTest.Position = 5; streamToTest.SetSeek = false; Assert.IsFalse(streamToTest.CanSeek); streamToTest.Write(bufferToWrite, 0, 2); streamToTest.SecondStream.Position = 2; int bytesRead = streamToTest.Read(result, 0, 2); }
public void OnePopulatedStreamReadOne() { concatStreamSourceData.Read(Arg.Is <byte[]>(x => x != null), 0, 1).Returns(1).AndDoes(i => i.ArgAt <byte[]>(0)[0] = 10); var cs = new ConcatStream(); cs.AppendSource(concatStreamSourceData); var buffer = new byte[1]; Assert.That(buffer[0], Is.Zero); var res = cs.Read(buffer, 0, 1); Assert.That(res, Is.EqualTo(1)); Assert.That(buffer[0], Is.EqualTo(10)); concatStreamSourceData.Received().Read(Arg.Any <byte[]>(), 0, 1); }
public void AttemptToReadPastEndOfNoSeekStreamShouldOnlyReadToEndOfStream() { byte[] result = new byte[100]; byte[] buffer = Encoding.ASCII.GetBytes("12345"); byte[] expected = Encoding.ASCII.GetBytes("1234512345"); MemoryStream stream1 = new MemoryStream(buffer); NoSeekMemoryStream stream2 = new NoSeekMemoryStream(buffer); ConcatStream streamToTest = new ConcatStream(stream1, stream2); // Act int bytesRead = streamToTest.Read(result, 0, expected.Length + 5); //Assert Assert.AreEqual(expected.Length, bytesRead); for (int i = 0; i < expected.Length; i++) { Assert.AreEqual(expected[i], result[i]); } Assert.AreEqual(expected.Length, bytesRead); }
public void SystemIOReadPassthrough() { // As calls are simply passed through to the stream, it's Stream dependent. So just make sure the stream was effected var data = new System.IO.MemoryStream(new byte[10]); var cs = new ConcatStream(); cs.Append(data); Assert.That(cs.Position, Is.Zero); Assert.That(data.Position, Is.Zero); var buffer = new byte[5]; var read = cs.Read(buffer, 0, 3); Assert.That(read, Is.EqualTo(3)); Assert.That(cs.Position, Is.EqualTo(3)); Assert.That(data.Position, Is.EqualTo(3)); }
public void ReadBackStreamDataInRandomChunks() { // Arrange byte[] result = new byte[1046]; byte[] buffer1 = Encoding.ASCII.GetBytes("123"); byte[] buffer2 = Encoding.ASCII.GetBytes("456"); byte[] expected = Encoding.ASCII.GetBytes("123456"); MemoryStream stream1 = new MemoryStream(buffer1); MemoryStream stream2 = new MemoryStream(buffer2); ConcatStream streamToTest = new ConcatStream(stream1, stream2); Random random = new Random(); int bytesRead = 0; int bytesToRead = 0; int totalBytesLeft = (int)(stream1.Length + stream2.Length); int i = 0; int offset = 0; // Act while (totalBytesLeft != 0) { bytesToRead = random.Next(1, totalBytesLeft); bytesRead = streamToTest.Read(result, offset, bytesToRead); Console.WriteLine("bytesRead " + bytesRead); for (i = 0; i < bytesRead; i++) { Console.WriteLine("expected[i]" + expected[i]); Console.WriteLine("result[i]" + result[i]); Assert.AreEqual(expected[i], result[i]); } totalBytesLeft -= bytesRead; offset += bytesRead; if (bytesRead == 0) { break; } } }
public void WritePastEndOfConcatStreamThatDoesNotSupportLengthShouldExpandStream() { // Arrange byte[] result = new byte[100]; byte[] bufferToWrite = Encoding.ASCII.GetBytes("12345"); MemoryStream stream1 = new MemoryStream(); NoSeekMemoryStream stream2 = new NoSeekMemoryStream(); ConcatStream streamToTest = new ConcatStream(stream1, stream2); // Act streamToTest.Write(bufferToWrite, 0, bufferToWrite.Length); streamToTest.Position = 0; int bytesRead = streamToTest.Read(result, 0, bufferToWrite.Length); //Assert Assert.AreEqual(bufferToWrite.Length, bytesRead); for (int i = 0; i < bufferToWrite.Length; i++) { Assert.AreEqual(bufferToWrite[i], result[i]); } }
public void TwoStreamsReadTwo() { concatStreamSourceData.Position.Returns(StreamSourceDefaultLength - 1); concatStreamSourceData.Read(Arg.Any <byte[]>(), 0, 2).Returns(1); concatStreamSourceData.Read(Arg.Any <byte[]>(), 1, 1).Returns(0); concatStreamSourceNoData.Length.Returns(StreamSourceDefaultLength); // Just to prevent making and setting up a new mock concatStreamSourceNoData.Read(Arg.Any <byte[]>(), 1, 1).Returns(1); var cs = new ConcatStream(); cs.AppendSource(concatStreamSourceData); cs.AppendSource(concatStreamSourceNoData); var buffer = new byte[2]; var count = cs.Read(buffer, 0, 2); Assert.That(count, Is.EqualTo(2)); concatStreamSourceData.ReceivedWithAnyArgs(2).Read(null, 0, 0); concatStreamSourceNoData.Received(1).Read(Arg.Any <byte[]>(), 1, 1); }
public void AttemptToReadPastEndStreamShouldOnlyReadToEndOfStream() { byte[] result = new byte[100]; byte[] bufferToWrite = Encoding.ASCII.GetBytes("12345"); MemoryStream stream1 = new MemoryStream(); MemoryStream stream2 = new MemoryStream(); ConcatStream streamToTest = new ConcatStream(stream1, stream2); // Act streamToTest.Write(bufferToWrite, 0, bufferToWrite.Length); streamToTest.Position = 0; int bytesRead = streamToTest.Read(result, 0, bufferToWrite.Length + 5); //Assert Assert.AreEqual(bufferToWrite.Length, bytesRead); for (int i = 0; i < bufferToWrite.Length; i++) { Assert.AreEqual(bufferToWrite[i], result[i]); } Assert.AreEqual(bufferToWrite.Length, streamToTest.Position); }
public void OnePopulatedStreamReadOnePositionIsLength() { concatStreamSourceData.Position.Returns(1); concatStreamSourceData.Length.Returns(1); concatStreamSourceData.Read(null, 0, 0).ReturnsForAnyArgs(0); var cs = new ConcatStream(); cs.AppendSource(concatStreamSourceData); var buffer = new byte[1]; Assert.That(buffer[0], Is.Zero); var res = cs.Read(buffer, 0, 1); Assert.That(res, Is.Zero); Assert.That(buffer[0], Is.Zero); concatStreamSourceData.Received().Read(Arg.Any <byte[]>(), 0, 1); }
public void basicNoSeekConcatStreamRead() { byte[] writeBuffer = new byte[14]; byte[] writeBuffer2 = new byte[8]; byte[] readBuffer = new byte[14]; MemoryStream s1 = new MemoryStream(); for (int i = 0; i < 14; i++) { writeBuffer[i] = Convert.ToByte(i); } s1.Write(writeBuffer, 0, 6); //s2.Write (writeBuffer, 6, 8); for (int i = 0; i < 8; i++) { writeBuffer2[i] = Convert.ToByte(i + 6); } NoSeekMemoryStream s2 = new NoSeekMemoryStream(writeBuffer2); ConcatStream testCS = new ConcatStream(s1, s2); s1.Seek(0, SeekOrigin.Begin); //s2.Seek (0, SeekOrigin.Begin); testCS.Read(readBuffer, 0, 14); s1.Seek(0, SeekOrigin.Begin); for (int i = 0; i < readBuffer.Length; i++) { Assert.AreEqual(readBuffer[i], writeBuffer [i]); } }
public static void Main(string[] args) { MemoryStream ms = new MemoryStream(10); MemoryStream ms2 = new MemoryStream(10); byte[] buffer = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; ms.Write(buffer, 0, 10); ms2.Write(buffer, 0, 10); //ConcatStream concatStream = new ConcatStream(ms, ms2); byte[] buffer2 = new byte[20]; ConcatStream concatStream = new ConcatStream(ms, ms2); concatStream.Read(buffer2, 0, 15); for (int i = 0; i < 15; i++) { Console.WriteLine(buffer2[i]); } }
public void WriteOverMiddleBoundryOfBothStreamsWithSeekEnabled() { // Arrange byte[] result = new byte[1046]; byte[] buffer1 = Encoding.ASCII.GetBytes("123"); byte[] buffer2 = Encoding.ASCII.GetBytes("456"); byte[] bufferToWrite = Encoding.ASCII.GetBytes("abcdef"); MemoryStream stream1 = new MemoryStream(buffer1); MemoryStream stream2 = new MemoryStream(buffer2); ConcatStream streamToTest = new ConcatStream(stream1, stream2); // Act streamToTest.Write(bufferToWrite, 0, bufferToWrite.Length); streamToTest.Position = 0; streamToTest.Read(result, 0, bufferToWrite.Length); for (int i = 0; i < bufferToWrite.Length; i++) { Console.WriteLine("Position " + streamToTest.Position); Console.WriteLine("result[i]: " + result[i]); Assert.AreEqual(bufferToWrite[i], result[i]); } }
public void ReadAllBytesFromConcatStreamThatHasNoSeekAsSecondStream() { // Arrange byte[] buffer = new byte[1046]; byte[] buffer1 = Encoding.ASCII.GetBytes("123"); byte[] buffer2 = Encoding.ASCII.GetBytes("456"); byte[] expectedBuffer = Encoding.ASCII.GetBytes("123456");; MemoryStream stream1 = new MemoryStream(buffer1); NoSeekMemoryStream stream2 = new NoSeekMemoryStream(buffer2); ConcatStream streamToTest = new ConcatStream(stream1, stream2); // Act int numberOfBytesRead = streamToTest.Read(buffer, 0, expectedBuffer.Length); //Assert Assert.IsFalse(streamToTest.CanSeek); Assert.AreEqual(expectedBuffer.Length, numberOfBytesRead); for (int i = 0; i < expectedBuffer.Length; i++) { Assert.AreEqual(expectedBuffer[i], buffer[i]); } }