Example #1
0
        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);
        }
Example #2
0
        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");
            }
        }
Example #3
0
        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);
        }
Example #4
0
        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);
            }
        }
Example #5
0
        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);
        }
Example #6
0
        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);
        }
Example #7
0
        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);
        }
Example #8
0
        public void SeekTest()
        {
            ConcatStream stream = new ConcatStream(
                new MemoryStream(new byte[10]), new MemoryStream(new byte[10]));

            Assert.DoesNotThrow(() => stream.Seek(5, SeekOrigin.Begin));
        }
Example #9
0
        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);
        }
Example #10
0
        public void Write_ExpandsProperly()
        {
            String a = RandomString(1000);

            byte[] aBytes = bytes(a);


            MemoryStream First  = new MemoryStream(aBytes);
            MemoryStream Second = new MemoryStream();

            ConcatStream C = new ConcatStream(First, Second);

            First.Seek(20, SeekOrigin.End);
            Second.Seek(22, SeekOrigin.Begin);

            C.Write(aBytes, 0, aBytes.Length);

            Assert.AreEqual(aBytes.Length, C.Length);


            C.Seek(aBytes.Length - 1, SeekOrigin.Begin);

            String strToWrite = RandomString(1000);

            byte[] bytesToWrite = bytes(strToWrite);
        }
Example #11
0
        static bool SeekTesting()
        {
            Console.WriteLine("\nTesting Seeking Functionality: ");

            Console.Write("\tTesting 100 random seeks from beginning: ");
            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);
            Random       rn      = new Random();
            bool         success = true;
            int          offset  = 0;

            for (int i = 0; i < 100; i++)
            {
                offset = rn.Next(0, (int)cs1.Length);
                cs1.Seek(offset, SeekOrigin.Begin);
                if (offset != cs1.Position)
                {
                    Console.WriteLine("FAILED"); break; success = false;
                }
            }
            if (success == true)
            {
                Console.WriteLine("SUCCESS");
            }

            /***********************/
            Console.Write("\tTesting random seeks from current: ");
            success = true;
            offset  = -100;// rn.Next((int)cs1.Position, (int)cs1.Length);
            long oldPosition = cs1.Position;

            cs1.Seek(offset, SeekOrigin.Current);
            if (offset + oldPosition != cs1.Position)
            {
                Console.WriteLine("FAILED"); success = false;
            }
            if (success == true)
            {
                Console.WriteLine("SUCCESS");
            }


            return(true);
        }
Example #12
0
        public void Seek_OnSeek_DifferentOffsetsWork()
        {
            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);

            stream.Seek(0, SeekOrigin.End);
            Assert.AreEqual(stream.Length, stream.Position);

            stream.Seek(-1 * stream.Length, SeekOrigin.Current);
            Assert.AreEqual(0, stream.Position);


            stream.Seek(stream.Length, SeekOrigin.Begin);
            Assert.AreEqual(stream.Length, stream.Position);
        }
Example #13
0
        public void Seek_OnSeekBeyondLengthNegative_PositionIsTruncated()
        {
            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);

            stream.Seek(-20, SeekOrigin.Begin);
            Assert.AreEqual(0, stream.Position);
        }
Example #14
0
        public void Seek_OnSeekBeyondLengthPositive_PositionIsNotTruncated()
        {
            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);

            int position = Convert.ToInt32(stream.Length) + r.Next(0, 2000);

            stream.Seek(position, SeekOrigin.Begin);
            Assert.AreEqual(position, stream.Position);
        }
Example #15
0
        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]);
            }
        }
Example #16
0
        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);
        }
Example #17
0
        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);
        }
Example #18
0
        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);
        }
Example #19
0
        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;
            }

            // Failed test that curreupt at posistion 0
            // Seeking then writing to posistion0, seek there and read from position0.
            // I do not know why it is failing that test. This passes.
            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);
        }
Example #20
0
        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);
        }
Example #21
0
        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);
        }
Example #22
0
        public void SetLength_OnSetLengthAndWrite_DataIsCorrectlyWritten()
        {
            const int N_TEST_CASES      = 1;
            const int MAX_STRING_LENGTH = int.MaxValue / 100;

            for (int i = 0; i < N_TEST_CASES; i++)
            {
                Console.WriteLine("SetLength_OnSetLengthAndWrite_DataIsCorrectlyWritten i = {0}", i);

                String randomString = RandomString(r.Next(0, MAX_STRING_LENGTH));
                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);

                    if (r.Next(1, int.MaxValue) % 2 == 0)
                    {
                        // change both streams position to messs wwith concatstream
                        A.Position = r.Next(0, int.MaxValue);
                        B.Position = r.Next(0, int.MaxValue);
                    }
                    else
                    {
                        A.Seek(r.Next(0, int.MaxValue), SeekOrigin.Begin);
                        B.Seek(r.Next(0, int.MaxValue), SeekOrigin.Begin);
                    }

                    concat.Write(bBuf, bBuf.Length - bytesToWrite, nBytesToWrite);
                    bytesToWrite -= nBytesToWrite;
                }

                concat.Seek(0, SeekOrigin.Begin);

                byte[] actual = new byte[concat.Length];

                if (r.Next(1, int.MaxValue) % 2 == 0)
                {
                    // change both streams position to messs wwith concatstream
                    A.Position = r.Next(0, int.MaxValue);
                    B.Position = r.Next(0, int.MaxValue);
                }
                else
                {
                    A.Seek(r.Next(0, int.MaxValue), SeekOrigin.Current);
                    B.Seek(r.Next(0, int.MaxValue), SeekOrigin.Current);
                }

                int nBytesRead = concat.Read(actual, 0, actual.Length);


                Assert.AreEqual(completeBuf.Length, nBytesRead);
                Assert.AreEqual(completeBuf, actual);
            }
        }