// check length and position (position will be in the truncated space. without a write, this leaves thile file trunaceted)
        public static void TestStreamWrite_SetLengthTruncate()
        {
            DebugStream stm = StmInit("0123456789");

            stm.SetLength(5);
            Assert.AreEqual(10, stm.Position);
            Assert.AreEqual(5, stm.Length);

            byte[] rgbExpected = Encoding.UTF8.GetBytes("01234");
            byte[] rgbRead     = new byte[5];
            // try to read from beyond the end
            Assert.AreEqual(0, stm.Read(rgbRead, 0, 5));
            //now reposition and read
            stm.Position = 0;
            Assert.AreEqual(5, stm.Read(rgbRead, 0, 5));

            Assert.AreEqual(rgbExpected, rgbRead);
        }
        public static void TestStreamSetLength_SeekIntoSpace_WriteIntoSpaceNoGrow()
        {
            DebugStream stm = new DebugStream();

            stm.SetLength(20);
            stm.Position = 10;
            Assert.AreEqual(10, stm.Position);
            Assert.AreEqual(20, stm.Length);

            byte[] rgbWrite = new byte[2] {
                65, 66
            };
            stm.Write(rgbWrite, 0, 2);

            Assert.AreEqual(12, stm.Position);
            Assert.AreEqual(20, stm.Length);
            stm.Position = 10;

            byte[] rgbRead = new byte[2];
            Assert.AreEqual(2, stm.Read(rgbRead, 0, 2));
            Assert.AreEqual(rgbWrite, rgbRead);
        }
        public static void TestStreamSetLength_SeekIntoSpace_WriteIntoSpaceGrow()
        {
            DebugStream stm = new DebugStream();

            stm.SetLength(20);
            stm.Position = 18;
            Assert.AreEqual(18, stm.Position);
            Assert.AreEqual(20, stm.Length);

            byte[] rgbWrite = new byte[6] {
                65, 66, 67, 68, 69, 70
            };
            stm.Write(rgbWrite, 0, 6);

            Assert.AreEqual(24, stm.Position);
            Assert.AreEqual(24, stm.Length);
            stm.Position = 18;

            byte[] rgbRead = new byte[6];
            Assert.AreEqual(6, stm.Read(rgbRead, 0, 6));
            Assert.AreEqual(rgbWrite, rgbRead);
        }
        public static void TestStreamWrite_SetLengthTruncate_Write()
        {
            DebugStream stm = StmInit("0123456789");

            stm.SetLength(5);
            Assert.AreEqual(10, stm.Position);
            Assert.AreEqual(5, stm.Length);

            byte[] rgbWrite = new byte[2] {
                65, 66
            };
            stm.Write(rgbWrite, 0, 2);

            Assert.AreEqual(12, stm.Position);
            Assert.AreEqual(12, stm.Length);
            stm.Position = 0;

            byte[] rgbRead = new byte[12];
            Assert.AreEqual(12, stm.Read(rgbRead, 0, 12));

            byte[] rgbExpected = new byte[] { 48, 49, 50, 51, 52, 0, 0, 0, 0, 0, 65, 66 };

            Assert.AreEqual(rgbExpected, rgbRead);
        }
        public static void TestStreamWrite_SetLengthGrowFile() // check length and position
        {
            DebugStream stm = StmInit("0123456789");

            stm.SetLength(20);
            Assert.AreEqual(10, stm.Position);
            Assert.AreEqual(20, stm.Length);

            stm.Seek(-4, SeekOrigin.Current);

            byte[] rgbWrite = new byte[2] {
                65, 66
            };
            stm.Write(rgbWrite, 0, 2);

            Assert.AreEqual(8, stm.Position);
            Assert.AreEqual(20, stm.Length);
            stm.Position = 0;

            byte[] rgbExpected = Encoding.UTF8.GetBytes("012345AB89");
            byte[] rgbRead     = new byte[10];
            Assert.AreEqual(10, stm.Read(rgbRead, 0, 10));
            Assert.AreEqual(rgbExpected, rgbRead);
        }