public void Write_OnOverWriteWithFixedLength_ExceptionThrown() { byte[] aInitialExpected = System.Text.Encoding.Unicode.GetBytes(TEST_STRING_A); MemoryStream a = new MemoryStream(aInitialExpected); MemoryStream b = new MemoryStream(); ConcatStream stream = new ConcatStream(a, b, aInitialExpected.Length); byte[] actual = new byte[a.Length]; stream.Read(actual, 0, actual.Length); Assert.AreEqual(actual, aInitialExpected); stream.Seek(0, SeekOrigin.Begin); Assert.AreEqual(0, stream.Position); byte[] aFinalExpected = System.Text.Encoding.Unicode.GetBytes(TEST_STRING_B); stream.Write(aFinalExpected, 0, aFinalExpected.Length); Assert.AreEqual(aFinalExpected.Length, stream.Position); actual = new byte[aFinalExpected.Length]; stream.Seek(0, SeekOrigin.Begin); stream.Read(actual, 0, actual.Length); Assert.AreEqual(aFinalExpected, actual); }
public static void Main(string[] args) { byte[] buffer = new byte[10] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; ConcatStream stream = new ConcatStream( new MemoryStream(new byte[1] { 10 }), new MemoryStream(buffer), 10); byte[] b = new byte[11]; long amountRead = 0; amountRead += stream.Read(b, 0, 1); stream.Seek(0, SeekOrigin.Begin); amountRead += stream.Read(b, 1, 1); stream.Seek(0, SeekOrigin.Begin); amountRead += stream.Read(b, 2, 1); stream.Seek(0, SeekOrigin.Begin); foreach (var i in b) { Console.Write(i + " "); } Console.WriteLine("Amount read: " + amountRead); if (amountRead == 11) { Console.WriteLine("We read in everything"); } else { Console.WriteLine("We didn't read everything"); } }
public void TestReadTwoMemStream() { Random rnd = new Random(); Stream defaultStream = new MemoryStream(); Stream one = new MemoryStream(); Stream two = new MemoryStream(); for (int i = 0; i < 920955; i++) { int number = rnd.Next(1000); defaultStream.Write(new byte[] { (byte)number }, 0, 1); one.Write(new byte[] { (byte)number }, 0, 1); } for (int i = 0; i < 2000; i++) { int number = rnd.Next(1000); defaultStream.Write(new byte[] { (byte)number }, 0, 1); two.Write(new byte[] { (byte)number }, 0, 1); } // Now defualtStream is what we expect our concat stream to be defaultStream.Seek(0, SeekOrigin.Begin); one.Seek(0, SeekOrigin.Begin); two.Seek(0, SeekOrigin.Begin); ConcatStream conStream = new ConcatStream(one, two); int bytesRead = 0; for (int i = 0; i <= defaultStream.Length;) { int randomRead = rnd.Next((int)defaultStream.Length); byte[] readBuf = new byte[randomRead]; byte[] expectetBuf = new byte[randomRead]; int amountRead = conStream.Read(readBuf, 0, randomRead); int expectedRead = defaultStream.Read(expectetBuf, 0, randomRead); Assert.AreEqual(expectetBuf, readBuf); Assert.AreEqual(expectedRead, amountRead); bytesRead += amountRead; i += randomRead; } // Trying to read past the end of the stream. // bytes Read is exacty the length of the stream long size = conStream.Length; bytesRead += conStream.Read(new byte[1000], 0, 1000); Assert.AreEqual(conStream.Length, bytesRead); return; }
public void Write_DoesExpandWhenSecondStreamIsExpandable() { String a = RandomString(1000); byte[] aBytes = bytes(a); String b = RandomString(1000); //byte[] bBytes = bytes(); String c = RandomString(4000); byte[] cBytes = bytes(c); MemoryStream First = new MemoryStream(aBytes); MemoryStream Second = new MemoryStream(); ConcatStream C = new ConcatStream(First, Second); // double original buffer and try to expand byte[] bytesToWrite = bytes(c); C.Write(bytesToWrite, 0, bytesToWrite.Length); byte[] actual = new byte[C.Length]; C.Seek(0, SeekOrigin.Begin); C.Read(actual, 0, actual.Length); Assert.AreEqual(cBytes.Length, C.Length); Assert.AreEqual(cBytes, actual); }
public void ExpandWriteTest() { MemoryStream expandable = new MemoryStream(100); expandable.Write(new byte[10], 0, 10); ConcatStream stream = new ConcatStream(new MemoryStream(new byte[10]), expandable); byte[] expect = new byte[100]; for (byte i = 0; i < 100; i++) { expect[i] = i; } stream.Write(expect, 0, expect.Length); stream.Seek(0, SeekOrigin.Begin); byte[] got = new byte[100]; stream.Read(got, 0, got.Length); Assert.AreEqual(got, expect); Assert.AreEqual(stream.Length, 100); }
public void ExpandableWriteTest() { MemoryStream ms1 = new MemoryStream(new byte[5]); MemoryStream ms2 = new MemoryStream(); ConcatStream concatStream = new ConcatStream(ms1, ms2); Console.WriteLine("Length = {0}", concatStream.Length); byte[] buffer = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; concatStream.Write(buffer, 0, 10); concatStream.Seek(0, SeekOrigin.Begin); byte[] buffer2 = new byte[10]; concatStream.Read(buffer2, 0, 10); for (int i = 0; i < 10; i++) { Console.WriteLine(buffer2[i]); Assert.AreEqual(buffer[i], buffer2[i]); } Console.WriteLine("Length = {0}", concatStream.Length); }
public void TestReadBasic() { Stream a = new MemoryStream(), b = new MemoryStream(); string toWrite = "Asdasd"; byte[] buffer = System.Text.Encoding.Unicode.GetBytes(toWrite); a.Write(buffer, 0, buffer.Length); buffer = System.Text.Encoding.Unicode.GetBytes("Secondsasdasd"); b.Write(buffer, 0, buffer.Length); a.Seek(0, SeekOrigin.Begin); b.Seek(0, SeekOrigin.Begin); ConcatStream concatStream = new ConcatStream(a, b); Assert.AreEqual(true, concatStream.CanRead); Assert.AreEqual(true, concatStream.CanWrite); Assert.AreEqual(true, concatStream.CanSeek); Assert.AreEqual(0, concatStream.Position); buffer = new byte[a.Length + b.Length]; concatStream.Read(buffer, 0, Convert.ToInt32(a.Length + b.Length)); string actual = System.Text.Encoding.Unicode.GetString(buffer); Assert.AreEqual(a.Length + b.Length, concatStream.Position); Assert.AreEqual("AsdasdSecondsasdasd", actual); }
public void Write_FragmentedWriting_BytesReadInCorrectOrder() { MemoryStream a = new MemoryStream(); MemoryStream b = new MemoryStream(); ConcatStream c = new ConcatStream(a, b); string test = ""; for (int i = 0; i < 10; i++) { test += TEST_STRING_B; } byte[] expected = System.Text.Encoding.Unicode.GetBytes(test); byte[] actual = new byte[expected.Length]; int count = Convert.ToInt32(expected.Length); int offset = 0; while (count > 0) { int randCount = r.Next(0, count + 1); c.Write(expected, offset, randCount); offset += randCount; count -= randCount; Assert.AreEqual(offset, c.Position); } c.Seek(0, SeekOrigin.Begin); //c.Position = 0; c.Read(actual, 0, expected.Length); Console.WriteLine("actual: " + System.Text.Encoding.Unicode.GetString(actual)); Assert.AreEqual(expected, actual); }
public void Read_FragmentedReading_BytesReadInCorrectOrder() { MemoryStream a = new MemoryStream(System.Text.Encoding.Unicode.GetBytes(TEST_STRING_A)); MemoryStream b = new MemoryStream(System.Text.Encoding.Unicode.GetBytes(TEST_STRING_B)); ConcatStream c = new ConcatStream(a, b); byte[] expected = System.Text.Encoding.Unicode.GetBytes(TEST_STRING_A + TEST_STRING_B); byte[] actual = new byte[a.Length + b.Length]; int count = Convert.ToInt32(a.Length + b.Length); int offset = 0; while (count > 0) { int randCount = r.Next(0, count + 1); if (r.Next(1, count + 1) % 2 == 0) { // change both streams position to messs wwith concatstream a.Position = r.Next(0, count + 1); b.Position = r.Next(0, count + 1); } else { a.Seek(r.Next(0, count + 1), SeekOrigin.Current); b.Seek(r.Next(0, count + 1), SeekOrigin.Current); } int bytesRead = c.Read(actual, offset, randCount); offset += bytesRead; count -= randCount; } Assert.AreEqual(expected, actual); }
public bool randomReadTest(ConcatStream c) { byte[] buffer = new byte[100]; for (int i = 0; i < 20; i++) { c.Read(buffer, 0, 1); } return(true); }
public bool readAllDataSequenciallyTest(ConcatStream c1) { int OUTPUT_ARRAY_LENGTH = 100; byte[] output = new byte[OUTPUT_ARRAY_LENGTH]; c1.Read(output, 0, OUTPUT_ARRAY_LENGTH); foreach (var i in output) { Console.Out.WriteLine(i); } return(true); }
public void Write_TestWriteOnOffset_DataIsCorrectlyWritten() { const int MAX_STRING_LENGTH = int.MaxValue / 100; String randomString = RandomString(10000 + MAX_STRING_LENGTH); byte[] completeBuf = System.Text.Encoding.Unicode.GetBytes(randomString); // now create two streams int strALength = r.Next(0, randomString.Length); string strBase = randomString.Substring(0, 10000); byte[] baseBuf = System.Text.Encoding.Unicode.GetBytes(strBase); String strA = randomString.Substring(10000, strALength); byte[] aBuf = System.Text.Encoding.Unicode.GetBytes(strA); String strB = randomString.Substring(strALength); byte[] bBuf = System.Text.Encoding.Unicode.GetBytes(strB); Stream A = new MemoryStream(baseBuf); Stream B = new MemoryStream(aBuf); ConcatStream concat = new ConcatStream(A, B); concat.Seek(aBuf.Length, SeekOrigin.Begin); int bytesToWrite = bBuf.Length; while (bytesToWrite > 0) { int nBytesToWrite = r.Next(0, bytesToWrite + 1); concat.Write(bBuf, bBuf.Length - bytesToWrite, nBytesToWrite); bytesToWrite -= nBytesToWrite; } concat.Seek(0, SeekOrigin.Begin); byte[] actual = new byte[concat.Length]; int nBytesRead = concat.Read(actual, 0, actual.Length); Assert.AreEqual(completeBuf.Length, nBytesRead); Assert.AreEqual(completeBuf, actual); }
public static void Main(string[] args) { int nTestCases = 1; for (int i = 0; i < nTestCases; i++) { Console.WriteLine("SetLength_OnSetLengthAndWrite_DataIsCorrectlyWritten i = {0}", i); String randomString = RandomString(r.Next(0, int.MaxValue / 50)); byte[] completeBuf = System.Text.Encoding.Unicode.GetBytes(randomString); // now create two streams int strALength = r.Next(0, randomString.Length); String strA = randomString.Substring(0, strALength); byte[] aBuf = System.Text.Encoding.Unicode.GetBytes(strA); String strB = randomString.Substring(strALength); byte[] bBuf = System.Text.Encoding.Unicode.GetBytes(strB); Stream A = new MemoryStream(aBuf); Stream B = new MemoryStream(); ConcatStream concat = new ConcatStream(A, B); concat.Seek(A.Length, SeekOrigin.Begin); int bytesToWrite = bBuf.Length; while (bytesToWrite > 0) { int nBytesToWrite = r.Next(0, bytesToWrite + 1); concat.Write(bBuf, bBuf.Length - bytesToWrite, nBytesToWrite); Console.WriteLine("Writing {0}", bytesToWrite); bytesToWrite -= nBytesToWrite; } Console.WriteLine("Done"); concat.Seek(0, SeekOrigin.Begin); byte[] actual = new byte[concat.Length]; int nBytesRead = concat.Read(actual, 0, actual.Length); Assert.AreEqual(completeBuf.Length, nBytesRead); Assert.AreEqual(completeBuf, actual); } }
public void MsWithNoSeekMsTest() { MemoryStream ms1 = new MemoryStream(new byte[] { 0, 1, 2, 3 }); NoSeekMemoryStream ms2 = new NoSeekMemoryStream(new byte[] { 4, 5, 6, 7, 8, 9 }); ConcatStream concatStream = new ConcatStream(ms1, ms2); byte[] buffer = new byte[10]; int read = 0; int count = 1; int bufferPosition = 0; byte[] originalData = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; /****NOTE: The count changing is the "Reading data in random chunks"*********/ Console.Error.WriteLine("bufferPosition = {0}, count = {1}", bufferPosition, count); while ((read = concatStream.Read(buffer, bufferPosition, count)) > 0) { if (bufferPosition < 9) { bufferPosition += read; } if (count + 1 + bufferPosition < buffer.Length) { count++; } else { count = 1; } Console.Error.WriteLine("bufferPosition = {0}, count = {1}, read = {2}", bufferPosition, count, read); } for (int i = 0; i < 10; i++) { Console.Error.WriteLine(buffer[i]); if (buffer[i] != originalData[i]) { Assert.Fail(); } } }
public void TestReadMemAndNoSeek() { Random rnd = new Random(); Stream defaultStream = new MemoryStream(); byte[] buf1 = new byte[1000]; byte[] buf2 = new byte[1500]; for (int i = 0; i < 1000; i++) { int number = rnd.Next(1000); defaultStream.Write(new byte[] { (byte)number }, 0, 1); buf1 [i] = (byte)number; } for (int i = 0; i < 1500; i++) { int number = rnd.Next(1000); defaultStream.Write(new byte[] { (byte)number }, 0, 1); buf2 [i] = (byte)number; } // Now defualtStream is what we expect our concat stream to be defaultStream.Seek(0, SeekOrigin.Begin); Stream one = new MemoryStream(buf1); Stream two = new NoSeekMemoryStream(buf2); ConcatStream conStream = new ConcatStream(one, two); for (int i = 0; i <= defaultStream.Length;) { int randomRead = rnd.Next((int)defaultStream.Length); byte[] readBuf = new byte[randomRead]; byte[] expectetBuf = new byte[randomRead]; int amountRead = conStream.Read(readBuf, 0, randomRead); int expectedRead = defaultStream.Read(expectetBuf, 0, randomRead); Assert.AreEqual(expectetBuf, readBuf); Assert.AreEqual(expectedRead, amountRead); i += randomRead; } }
public void ReadTest() { byte[] expected1 = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, expected2 = { 10, 11, 12, 13, 14 }; byte[] expected = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 }; ConcatStream stream = new ConcatStream( new MemoryStream(expected1), new MemoryStream(expected2)); byte[] buffer = new byte[15]; stream.Read(buffer, 0, buffer.Length); Assert.AreEqual(expected, buffer); }
static public bool canReadTest() { bool val = true; FileStream fs1 = new FileStream("testFile1.txt", FileMode.Open, FileAccess.Read); FileStream fs2 = new FileStream("testFile1.txt", FileMode.Open, FileAccess.Read); FileStream fs3 = new FileStream("testFile2.txt", FileMode.Open, FileAccess.ReadWrite); ConcatStream cs1 = new ConcatStream(fs1, fs2); ConcatStream cs2 = new ConcatStream(fs2, fs3); if (!cs1.CanRead) { Console.Out.WriteLine("CanRead failed - 2 readonly. Expected Output: True | Output: False "); val = false; } if (!cs2.CanRead) { Console.Out.WriteLine("CanRead failed - 1 readonly, 1 write only. Expected Output: true | Output: false "); val = false; } try { byte[] toBytes = new byte[8000]; cs1.Read(toBytes, 0, toBytes.Length); } catch { Console.WriteLine("2 readonly stream : Read from Concatstream failed | Expected Output: Success"); val = false; }; try { byte[] toBytes = new byte[8000]; cs2.Read(toBytes, 0, toBytes.Length); Console.WriteLine("1 readonly, 1 Write stream : Read from Concatstream succeeded | Expected Output: Fail"); val = false; } catch { }; fs1.Close(); fs2.Close(); fs3.Close(); cs1.Close(); cs2.Close(); return(val); }
public void TestMethod9() { byte[] buf1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; byte[] buf2 = { 11, 12, 13, 14, 15, 16, 17, 18 }; MemoryStream mem1 = new MemoryStream(buf1); MemoryStream mem2 = new MemoryStream(buf2); ConcatStream cs = new ConcatStream(mem1, mem2); byte[] buf3 = { 19, 20, 21 }; cs.Write(buf3, 0, 3); byte[] expected = { 19, 20, 21, 4, 5, 6, 7, 8, 9, 10 }; byte[] result = new byte[10]; cs.Position = 0; cs.Read(result, 0, 10); Assert.AreEqual(expected, result); }
public void TestMethod10() { byte[] buf1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; byte[] buf2 = { 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 }; MemoryStream mem1 = new MemoryStream(buf1); MemoryStream mem2 = new MemoryStream(buf2); ConcatStream cs = new ConcatStream(mem1, mem2); byte[] buf3 = { 21, 22, 23, 24, 25, 26, 27, 28, 29, 30 }; cs.Write(buf3, 0, buf3.Length); byte[] expected = { 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 }; byte[] result = new byte[expected.Length]; cs.Position = 0; cs.Read(result, 0, expected.Length); Assert.AreEqual(expected, result); }
public void TestMethod1() { byte[] buf1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; byte[] buf2 = { 11, 12, 13, 14, 15, 16, 17, 18 }; MemoryStream mem1 = new MemoryStream(buf1); MemoryStream mem2 = new MemoryStream(buf2); ConcatStream cs = new ConcatStream(mem1, mem2); byte[] buf = new byte[512]; int nbytes = 11; cs.Read(buf, 0, nbytes); for (int i = 1; i <= nbytes; i++) { Assert.AreEqual(buf[i - 1], i); } }
public void WriteSecondConstructorTest() { MemoryStream ms1 = new MemoryStream(new byte[5]); MemoryStream ms2 = new MemoryStream(new byte[5]); ConcatStream concatStream = new ConcatStream(ms1, ms2, 5); byte[] buffer = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; concatStream.Write(buffer, 0, 10); concatStream.Seek(0, SeekOrigin.Begin); byte[] buffer2 = new byte[10]; concatStream.Read(buffer2, 0, 10); for (int i = 0; i < 10; i++) { Console.WriteLine(buffer2[i]); Assert.AreEqual(buffer[i], buffer2[i]); } }
public void WriteTest() { ConcatStream stream = new ConcatStream( new MemoryStream(new byte[10]), new MemoryStream(new byte[20])); byte[] expected = new byte[stream.Length]; for (int i = 0; i < stream.Length; i++) { expected[i] = (byte)i; } stream.Write(expected, 0, expected.Length); stream.Seek(0, SeekOrigin.Begin); byte[] got = new byte[stream.Length]; stream.Read(got, 0, got.Length); Assert.AreEqual(got, expected); }
public void Write_OnMultipleFragmentedWritesWithFirstStreamOfZeroLength_DataIsSavedProperly() { string test = new string('x', 20000); MemoryStream a = new MemoryStream(System.Text.Encoding.Unicode.GetBytes(test)); MemoryStream b = new MemoryStream(); ConcatStream c = new ConcatStream(a, b); byte[] expected = System.Text.Encoding.Unicode.GetBytes(test); byte[] actual = new byte[expected.Length]; uint count = 0; while (count < int.MaxValue) { c.Write(expected, 0, expected.Length); count += Convert.ToUInt32(expected.Length); Assert.AreEqual(count, c.Position); } c.Seek(0, SeekOrigin.Begin); c.Read(actual, 0, expected.Length); Assert.AreEqual(expected, actual); }
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); }
public void Read_NoSeekAndMemoryStreamConcat_DataIsPreserved() { MemoryStream a = new MemoryStream(System.Text.Encoding.Unicode.GetBytes(TEST_STRING_A)); byte[] bBuf = System.Text.Encoding.Unicode.GetBytes(TEST_STRING_B); NoSeekMemoryStream b = new NoSeekMemoryStream(bBuf); ConcatStream c = new ConcatStream(a, b); byte[] expected = System.Text.Encoding.Unicode.GetBytes(TEST_STRING_A + TEST_STRING_B); byte[] actual = new byte[a.Length + bBuf.Length]; int count = Convert.ToInt32(a.Length + bBuf.Length); int offset = 0; while (count > 0) { int randCount = r.Next(0, count + 1); int bytesRead = c.Read(actual, offset, randCount); offset += bytesRead; count -= randCount; } Assert.AreEqual(expected, actual); }
static bool WriteTests() { Console.WriteLine("\nTesting Concat Stream Write Functionality: "); //Can we write all the way through both buffers Console.Write("\tTesting write all the way through both buffers: "); FileStream fs1 = new FileStream("testFile11.txt", FileMode.Create, FileAccess.ReadWrite); string testString = "Checking 123"; byte[] toBytes = Encoding.ASCII.GetBytes(testString); fs1.Write(toBytes, 0, toBytes.Length); FileStream fs2 = new FileStream("testFile12.txt", FileMode.Create, FileAccess.ReadWrite); testString = "IT's working!!!!"; toBytes = Encoding.ASCII.GetBytes(testString); fs2.Write(toBytes, 0, toBytes.Length); ConcatStream cs1 = new ConcatStream(fs1, fs2); string msg = ""; for (int i = 0; i < fs1.Length + fs2.Length + 20; i++) { msg += "a"; } toBytes = Encoding.ASCII.GetBytes(msg); cs1.Write(toBytes, 0, toBytes.Length); cs1.Position = 0; toBytes = new byte[cs1.Length]; int bytesRead = cs1.Read(toBytes, 0, toBytes.Length); string msg2 = System.Text.Encoding.ASCII.GetString(toBytes, 0, bytesRead); if (msg == msg2) { Console.WriteLine("SUCCESS"); } else { Console.WriteLine("FAILED"); } /********************************/ Console.Write("\tTesting writing NULL buffer, excetion expected: "); try { cs1.Write(null, 0, 10); Console.WriteLine("FAILED"); } catch { Console.WriteLine("SUCCESS"); } cs1.Close(); /**************************/ //Can we write all the way through both buffers Console.Write("\tTesting writing passed a fixed length, expception expected: "); fs1 = new FileStream("testFile11.txt", FileMode.Open, FileAccess.ReadWrite); fs2 = new FileStream("testFile12.txt", FileMode.Open, FileAccess.ReadWrite); cs1 = new ConcatStream(fs1, fs2, 10); for (int i = 0; i < fs1.Length + fs2.Length + 200; i++) { msg += "a"; } toBytes = Encoding.ASCII.GetBytes(msg); try { cs1.Write(toBytes, 0, toBytes.Length); Console.WriteLine("FAILED"); } catch { Console.WriteLine("SUCCESS"); } cs1.Close(); return(true); }
static bool ReadStreamTests() { Console.WriteLine("\nTesting Read Functionality:"); Console.Write("\tTesting reading all content from both streams (FileStream, FileStream): "); FileStream fs1 = new FileStream("testFile1.txt", FileMode.Open, FileAccess.Read); byte[] buffer1 = new byte[fs1.Length]; int bytesRead = fs1.Read(buffer1, 0, buffer1.Length); string msg1 = System.Text.Encoding.ASCII.GetString(buffer1, 0, bytesRead); FileStream fs2 = new FileStream("testFile1.txt", FileMode.Open, FileAccess.Read); byte[] buffer2 = new byte[fs2.Length]; bytesRead = fs2.Read(buffer2, 0, buffer2.Length); string msg2 = System.Text.Encoding.ASCII.GetString(buffer2, 0, bytesRead); ConcatStream cs1 = new ConcatStream(fs1, fs2); byte[] buffer3 = new byte[cs1.Length]; bytesRead = cs1.Read(buffer3, 0, buffer3.Length); string msg3 = System.Text.Encoding.ASCII.GetString(buffer3, 0, bytesRead); if (msg3 != msg1 + msg2 || msg3.Length <= 0 || msg2.Length <= 0 || msg1.Length <= 0) { Console.WriteLine("FAILED"); } else { Console.WriteLine("SUCCESS"); } cs1.Close(); /***************************************/ Console.Write("\tTesting reading all content from both streams, random amounts (MemoryStream, MemoryStream): "); MemoryStream ms1 = new MemoryStream(buffer1); MemoryStream ms2 = new MemoryStream(buffer2); cs1 = new ConcatStream(ms1, ms2); byte[] buffer4 = new byte[cs1.Length]; Random rnd = new Random(); msg3 = ""; bytesRead = 0; int totalRead = 0; bytesRead = cs1.Read(buffer4, 0, rnd.Next(1, buffer4.Length - totalRead)); msg3 += System.Text.Encoding.ASCII.GetString(buffer4, 0, bytesRead); totalRead += bytesRead; while (bytesRead != 0) { int max = Math.Max(buffer4.Length - totalRead, 1); bytesRead = cs1.Read(buffer4, 0, rnd.Next(1, max)); msg3 += System.Text.Encoding.ASCII.GetString(buffer4, 0, bytesRead); buffer4 = new byte[cs1.Length]; totalRead += bytesRead; } if (msg3 == msg1 + msg2 && msg3.Length > 0 && msg2.Length > 0 && msg1.Length > 0) { Console.WriteLine("SUCCESS"); } else { Console.WriteLine("FAILED"); } ms1.Close(); ms2.Close(); cs1.Close(); /*****************************/ Console.Write("\tTesting read all from (memoryStream,NoSeekMemoryStream): "); ms1 = new MemoryStream(buffer1); ms2 = new NoSeekMemoryStream(buffer2); cs1 = new ConcatStream(ms1, ms2); msg1 = System.Text.Encoding.ASCII.GetString(buffer1, 0, buffer1.Length); msg2 = System.Text.Encoding.ASCII.GetString(buffer2, 0, buffer2.Length); buffer3 = new byte[cs1.Length]; bytesRead = cs1.Read(buffer3, 0, buffer3.Length); msg3 = System.Text.Encoding.ASCII.GetString(buffer3, 0, buffer3.Length); if (msg3 == msg1 + msg2 && msg3.Length > 0 && msg2.Length > 0 && msg1.Length > 0) { Console.WriteLine("SUCCESS"); } else { Console.WriteLine("FAILED"); } /*********************************/ Console.Write("\tTesting if position resets when stream is passed to CS: "); fs1 = new FileStream("testFile1.txt", FileMode.Open, FileAccess.Read); int amountToRead = 10; buffer1 = new byte[amountToRead]; bytesRead = fs1.Read(buffer1, 0, buffer1.Length); msg1 = System.Text.Encoding.ASCII.GetString(buffer1, 0, bytesRead); fs2 = new FileStream("testFile1.txt", FileMode.Open, FileAccess.Read); buffer2 = new byte[amountToRead]; bytesRead = fs2.Read(buffer2, 0, buffer2.Length); msg2 = System.Text.Encoding.ASCII.GetString(buffer2, 0, bytesRead); cs1 = new ConcatStream(fs1, fs2); buffer3 = new byte[amountToRead]; bytesRead = cs1.Read(buffer3, 0, buffer3.Length); msg3 = System.Text.Encoding.ASCII.GetString(buffer3, 0, bytesRead); if (msg3 != msg1 || msg3 != msg2 || msg3.Length <= 0) { Console.WriteLine("FAILED"); } else { Console.WriteLine("SUCCESS"); } fs1.Close(); fs2.Close(); cs1.Close(); /*********************************************************************/ Console.Write("\tTesting reading from stream no length property (FileStream, NetworkStream): "); string urlAddress = "https://www.facebook.com"; HttpWebRequest request = (HttpWebRequest)System.Net.WebRequest.Create(urlAddress); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); fs2 = new FileStream("testFile2.txt", FileMode.Open, FileAccess.Read); byte[] buffer = new byte[fs2.Length + 2]; if (response.StatusCode == HttpStatusCode.OK) { Stream receiveStream = response.GetResponseStream(); cs1 = new ConcatStream(fs2, receiveStream); bytesRead = cs1.Read(buffer, 0, buffer.Length); msg1 = System.Text.Encoding.ASCII.GetString(buffer, 0, bytesRead); if (msg1.Length > fs2.Length + 1) { Console.WriteLine("SUCCESS"); } else { Console.WriteLine("FAILED"); } } else { Console.Write("!!!!!!!!!!!!!!!!!!!SOCKET CONNNECTION FAILED, cannot run test."); throw new ArgumentException(); } fs2.Close(); cs1.Close(); return(true); }
static bool PositionTests() { Console.WriteLine("\nTesting Concat Stream Position Property: "); Console.Write("\tTesting Position after Read (FileStream, FileStream): "); FileStream fs1 = new FileStream("testFile1.txt", FileMode.Open, FileAccess.Read); FileStream fs2 = new FileStream("testFile1.txt", FileMode.Open, FileAccess.Read); ConcatStream cs1 = new ConcatStream(fs1, fs2); long initialPosition = cs1.Position; byte[] buffer1 = new byte[10]; int bytesRead = cs1.Read(buffer1, 0, buffer1.Length); long currentPosition = cs1.Position; if (currentPosition - bytesRead != initialPosition || currentPosition == 0) { Console.WriteLine("FAILED"); } else { Console.WriteLine("SUCCESS"); } fs1.Close(); fs2.Close(); cs1.Close(); /******************************************/ Console.Write("\tTesting Position after Write (FileStream, FileStream): "); FileStream fs3 = new FileStream("testFile3.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite); string testString = "Checking"; byte[] toBytes = Encoding.ASCII.GetBytes(testString); fs3.Write(toBytes, 0, toBytes.Length); FileStream fs4 = new FileStream("testFile4.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite); ConcatStream cs2 = new ConcatStream(fs3, fs4); initialPosition = cs2.Position; testString = "Testing, 1 2 3, Testing..."; toBytes = Encoding.ASCII.GetBytes(testString); cs2.Write(toBytes, 0, toBytes.Length); currentPosition = cs2.Position; if (currentPosition - toBytes.Length != initialPosition || currentPosition == 0) { Console.WriteLine("FAILED"); } else { Console.WriteLine("SUCCESS"); } fs3.Close(); fs4.Close(); cs2.Close(); /*********************************************/ Console.Write("\tTesting Position after Seek (FileStream, FileStream): "); fs1 = new FileStream("testFile1.txt", FileMode.Open, FileAccess.Read); fs2 = new FileStream("testFile1.txt", FileMode.Open, FileAccess.Read); cs1 = new ConcatStream(fs1, fs2); int offset = 20; cs1.Seek(offset, SeekOrigin.Begin); if (cs1.Position == offset) { Console.WriteLine("SUCCESS"); } else { Console.WriteLine("FAILED"); } fs1.Close(); fs2.Close(); cs1.Close(); /******************************************/ Console.Write("\tTesting set Position after length max (FileStream, FileStream, Length): "); fs1 = new FileStream("testFile1.txt", FileMode.Open, FileAccess.Read); fs2 = new FileStream("testFile1.txt", FileMode.Open, FileAccess.Read); cs1 = new ConcatStream(fs1, fs2, 10); try { cs1.Position = 12; Console.WriteLine("FAILED"); } catch { Console.WriteLine("SUCCESS"); } fs1.Close(); fs2.Close(); cs1.Close(); /******************************************/ Console.Write("\tTesting exception for passing the same instance for both streams (FileStream, FileStream): "); fs3 = new FileStream("testFile10.txt", FileMode.Create, FileAccess.ReadWrite); try { cs2 = new ConcatStream(fs3, fs3); Console.WriteLine("FAILED"); } catch { Console.WriteLine("SUCCESS"); } cs1.Close(); return(true); }
public void TestWrite() { Stream one = new MemoryStream(); Stream two = new MemoryStream(); Random rnd = new Random(); one.Write(new byte[] { 1, 2, 3, 4, 5 }, 0, 5); two.Write(new byte[] { 6, 7, 8, 9, 10 }, 0, 5); one.Seek(0, SeekOrigin.Begin); two.Seek(0, SeekOrigin.Begin); Stream conStream = new ConcatStream(one, two); conStream.Seek(0, SeekOrigin.Begin); conStream.Seek(4, SeekOrigin.Begin); conStream.Seek(7, SeekOrigin.Begin); conStream.Seek(10, SeekOrigin.Begin); conStream.Seek(0, SeekOrigin.End); conStream.Seek(4, SeekOrigin.End); conStream.Seek(8, SeekOrigin.End); one = new MemoryStream(new byte[] { 5, 6, 7, 8 }); two = new MemoryStream(new byte[] { 5, 6, 7, 8 }); conStream = new ConcatStream(one, two); conStream.Write(new byte[5] { 1, 2, 3, 4, 5 }, 0, 5); byte[] buf = new byte[5]; conStream.Seek(0, SeekOrigin.Begin); conStream.Read(buf, 0, 5); Assert.AreEqual(new byte[5] { 1, 2, 3, 4, 5 }, buf); one = new MemoryStream(); two = new MemoryStream(); Stream defaultStream = new MemoryStream(); for (int i = 0; i < 920955; i++) { int number = rnd.Next(1000); one.Write(new byte[] { (byte)number }, 0, 1); } for (int i = 0; i < 2000; i++) { int number = rnd.Next(1000); two.Write(new byte[] { (byte)number }, 0, 1); } //byte[] writeBuf = new byte[10000000]; one.Seek(0, SeekOrigin.Begin); two.Seek(0, SeekOrigin.Begin); conStream = new ConcatStream(one, two); for (int i = 0; i < 10000000; i++) { int num = rnd.Next(); defaultStream.Write(new byte[] { (byte)num }, 0, 1); conStream.Write(new byte[] { (byte)num }, 0, 1); //writeBuf[i] = (byte)num; } //conStream.Write (writeBuf, 0, 10000000); //Assert.AreEqual (10000000, conStream.Length); conStream.Seek(0, SeekOrigin.Begin); defaultStream.Seek(0, SeekOrigin.Begin); for (int i = 0; i <= defaultStream.Length;) { int randomRead = rnd.Next((int)defaultStream.Length); byte[] readBuf = new byte[randomRead]; byte[] expectetBuf = new byte[randomRead]; int amountRead = conStream.Read(readBuf, 0, randomRead); int expectedRead = defaultStream.Read(expectetBuf, 0, randomRead); Assert.AreEqual(expectetBuf, readBuf); Assert.AreEqual(expectedRead, amountRead); i += randomRead; } conStream.Seek(0, SeekOrigin.Begin); conStream.Write(new byte[] { (byte)5 }, 0, 1); conStream.Seek(0, SeekOrigin.Begin); byte[] expectedBuf = new byte[1]; conStream.Read(expectedBuf, 0, 1); Assert.AreEqual(new byte[] { (byte)5 }, expectedBuf); // Using position setting instead of seeking conStream.Position = 0; conStream.Write(new byte[] { (byte)15 }, 0, 1); Assert.AreEqual(1, conStream.Position); conStream.Position = 0; expectedBuf = new byte[1]; conStream.Read(expectedBuf, 0, 1); Assert.AreEqual(new byte[] { (byte)15 }, expectedBuf); }
public void TestSecondConstructor() { Random rnd = new Random(); Stream defaultStream = new MemoryStream(); Stream one = new MemoryStream(); byte[] b2 = new byte[2000]; for (int i = 0; i < 1000; i++) { int number = rnd.Next(1000); defaultStream.Write(new byte[] { (byte)number }, 0, 1); one.Write(new byte[] { (byte)number }, 0, 1); } for (int i = 0; i < 2000; i++) { int number = rnd.Next(1000); defaultStream.Write(new byte[] { (byte)number }, 0, 1); b2 [i] = (byte)number; } Stream two = new NoSeekMemoryStream(b2); // Now defualtStream is what we expect our concat stream to be defaultStream.Seek(0, SeekOrigin.Begin); one.Seek(0, SeekOrigin.Begin); ConcatStream conStream = new ConcatStream(one, two, 300); Assert.AreEqual(300, conStream.Length); conStream = new ConcatStream(one, two, 3000); Assert.AreEqual(3000, conStream.Length); int bytesRead = 0; for (int i = 0; i <= defaultStream.Length;) { int randomRead = rnd.Next((int)defaultStream.Length); byte[] readBuf = new byte[randomRead]; byte[] expectetBuf = new byte[randomRead]; int amountRead = conStream.Read(readBuf, 0, randomRead); int expectedRead = defaultStream.Read(expectetBuf, 0, randomRead); Assert.AreEqual(expectetBuf, readBuf); Assert.AreEqual(expectedRead, amountRead); bytesRead += amountRead; i += randomRead; } Assert.Throws <NotImplementedException> (delegate { conStream.Position = conStream.Length + 5; }); Assert.AreEqual(0, conStream.Read(new byte[10], 0, 5)); Assert.Throws <ArgumentException> (delegate { conStream.Write(b2, 0, 300); }); }