예제 #1
0
        public void TestSetPositionReadWrite()
        {
            string filePath = Path.Combine(TestContext.CurrentContext.TestDirectory, "WinFileIOTests.TestSetPositionReadWrite");

            byte[]    buffer = new byte[1];
            WinFileIO wfio   = new WinFileIO(buffer);

            wfio.OpenForReadingWriting(filePath);

            Assert.AreEqual(0, wfio.Position);

            //Write stuff, move about writing more, then come back in & read it in etc...
            buffer[0] = 1;
            wfio.WriteBlocks(1);
            buffer[0] = 3;
            wfio.WriteBlocks(1);
            buffer[0] = 3;
            wfio.WriteBlocks(1);
            buffer[0] = 7;
            wfio.WriteBlocks(1);

            wfio.Position = 0;
            wfio.ReadBlocks(1);
            Assert.AreEqual(1, buffer[0]);
            wfio.ReadBlocks(1);
            Assert.AreEqual(3, buffer[0]);
            wfio.ReadBlocks(1);
            Assert.AreEqual(3, buffer[0]);
            wfio.ReadBlocks(1);
            Assert.AreEqual(7, buffer[0]);

            wfio.Position = 2;
            buffer[0]     = 24;
            wfio.WriteBlocks(1);
            wfio.Position = 0;
            buffer[0]     = 10;
            wfio.WriteBlocks(1);
            wfio.Position = 2;
            wfio.ReadBlocks(1);
            Assert.AreEqual(24, buffer[0]);
            wfio.Position = 0;
            wfio.ReadBlocks(1);
            Assert.AreEqual(10, buffer[0]);

            wfio.Close();

            //Now check that the final file is what we expected it to be
            byte[] expected = new byte[] { 10, 3, 24, 7 };
            byte[] actual   = readFile(filePath);

            CollectionAssert.AreEqual(expected, actual);
        }