public void TestLength() { Random rnd = new Random(); Stream one = new MemoryStream(); Stream two = new MemoryStream(); int randomNum = rnd.Next(100000); for (int i = 0; i < randomNum; i++) { int number = rnd.Next(1000); one.Write(new byte[] { (byte)number }, 0, 1); two.Write(new byte[] { (byte)number }, 0, 1); } ConcatStream conStream = new ConcatStream(one, two); Assert.DoesNotThrow(delegate { conStream.SetLength(10); }); one = new MemoryStream(); NoSeekMemoryStream second = new NoSeekMemoryStream(new byte[1] { (byte)4 }); randomNum = rnd.Next(100000); for (int i = 0; i < randomNum; i++) { int number = rnd.Next(1000); one.Write(new byte[] { (byte)number }, 0, 1); } conStream = new ConcatStream(one, second); Assert.Throws <NotImplementedException> (delegate { conStream.SetLength(10); }); }
public void SetLength_WhenLengthLessThanFirstStreamLength_StreamIsTruncated() { byte[] aInitialExpected = System.Text.Encoding.Unicode.GetBytes(TEST_STRING_A); byte[] aFinalExpected = System.Text.Encoding.Unicode.GetBytes( TEST_STRING_A.Substring(0, TEST_STRING_A.Length / 2)); MemoryStream a = new MemoryStream(aInitialExpected); MemoryStream b = new MemoryStream(); ConcatStream stream = new ConcatStream(a, b, aInitialExpected.Length); int streamLength = Convert.ToInt32(stream.Length); stream.SetLength(streamLength / 2); Assert.AreEqual(a.Length, streamLength / 2); stream.Seek(0, SeekOrigin.Begin); byte[] aFinalActual = new byte[stream.Length]; int bytesRead = stream.Read(aFinalActual, 0, Convert.ToInt32(stream.Length)); Assert.AreEqual(aFinalExpected.Length, bytesRead); Assert.AreEqual(aFinalExpected, aFinalActual); }