Example #1
0
        public void EmptyConcatStreamWrite()
        {
            byte[] writeBuffer = 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);
            }

            testCS.Write(writeBuffer, 0, writeBuffer.Length);

            long length = testCS.Length;

            Assert.AreEqual(14, length);

            s2.Seek(0, SeekOrigin.Begin);

            for (int i = 0; i < 14; i++)
            {
                byte[] buffer = new byte[1];
                s2.Read(buffer, 0, 1);
                Assert.AreEqual(i, buffer [0]);
            }
        }
Example #2
0
        public void WriteOverMiddleBoundryOfBothStreamsWithSeekDisabled()
        {
            // 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);

            //Assert
            string actual = streamToTest.ToString();

            Assert.IsFalse(streamToTest.CanSeek);

            for (int i = 0; i < bufferToWrite.Length; i++)
            {
                Assert.AreEqual(actual[i], bufferToWrite[i]);
            }
        }
Example #3
0
        public void ConcatStreamExpanded()
        {
            byte[] writeBuffer = new byte[14];

            MemoryStream s1 = new MemoryStream();

            for (int i = 0; i < 14; i++)
            {
                writeBuffer[i] = Convert.ToByte(i);
            }

            s1.Write(writeBuffer, 0, 6);

            MemoryStream s2 = new MemoryStream();


            ConcatStream testCS = new ConcatStream(s1, s2);

            testCS.Position = 6;

            long pre_s2_length = s2.Length;

            testCS.Write(writeBuffer, 6, 8);

            long post_s2_length = s2.Length;


            //Assert.AreNotEqual (s2_length, s2.Length);
            Assert.AreEqual(pre_s2_length, 0);

            Assert.AreEqual(post_s2_length, 8);
        }
Example #4
0
        public void basicConcatStreamWrite()
        {
            //set up buffers
            byte[] writeBuffer = new byte[14];
            byte[] readBuffer1 = new byte[14];
            byte[] readBuffer2 = new byte[14];


            //setUp streams
            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);

            for (int i = 14; i < 28; i++)
            {
                writeBuffer[i - 14] = Convert.ToByte(i);
            }


            testCS.Write(writeBuffer, 0, 14);

            s1.Seek(0, SeekOrigin.Begin);
            s2.Seek(0, SeekOrigin.Begin);

            s1.Read(readBuffer1, 0, 6);
            s2.Read(readBuffer2, 6, 8);



            for (int i = 0; i < writeBuffer.Length; i++)
            {
                if (i <= 5)
                {
                    Assert.AreEqual(readBuffer1 [i], writeBuffer [i]);
                }
                else
                {
                    Assert.AreEqual(readBuffer2 [i], writeBuffer [i]);
                }
            }
        }
Example #5
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));
        }
Example #6
0
        public void WritePastEndOfCurrentStreamShouldThrowException()
        {
            // Arrange
            byte[] buffer1       = Encoding.ASCII.GetBytes("123");
            byte[] buffer2       = Encoding.ASCII.GetBytes("456");
            byte[] bufferToWrite = Encoding.ASCII.GetBytes("123456789");

            MemoryStream stream1      = new MemoryStream(buffer1);
            MemoryStream stream2      = new MemoryStream(buffer2);
            ConcatStream streamToTest = new ConcatStream(stream1, stream2);

            // Act
            Assert.Throws <NotSupportedException>(() => streamToTest.Write(bufferToWrite, 0, bufferToWrite.Length));

            // streamToTest.Write(bufferToWrite, 0, bufferToWrite.Length)
        }
Example #7
0
        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);
        }
Example #8
0
        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);
        }
Example #9
0
        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]);
            }
        }
Example #10
0
        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]);
            }
        }