コード例 #1
0
        public void PerformWriteOpOnReadOnlyWrapper()
        {
            byte[] buf = new byte[10];

            using (FileStreamWrapper fsw = new FileStreamWrapper())
            {
                // If:
                // ... I have a readonly file stream wrapper
                // Then:
                // ... Attempting to perform any write operation should result in an exception
                Assert.Throws <InvalidOperationException>(() => fsw.WriteData(buf, 1));
                Assert.Throws <InvalidOperationException>(() => fsw.Flush());
            }
        }
コード例 #2
0
        public void PerformOpWithoutInit()
        {
            byte[] buf = new byte[10];

            using (FileStreamWrapper fsw = new FileStreamWrapper())
            {
                // If:
                // ... I have a file stream wrapper that hasn't been initialized
                // Then:
                // ... Attempting to perform any operation will result in an exception
                Assert.Throws <InvalidOperationException>(() => fsw.ReadData(buf, 1));
                Assert.Throws <InvalidOperationException>(() => fsw.ReadData(buf, 1, 0));
                Assert.Throws <InvalidOperationException>(() => fsw.WriteData(buf, 1));
                Assert.Throws <InvalidOperationException>(() => fsw.Flush());
            }
        }
コード例 #3
0
        [InlineData(10)]    // Internal buffer too small, forces a flush
        public void WriteData(int internalBufferLength)
        {
            string fileName = Path.GetTempFileName();

            byte[] bytesToWrite = Encoding.Unicode.GetBytes("hello");

            try
            {
                // If:
                // ... I have a file stream that has been initialized
                // ... And I write some bytes to it
                using (FileStreamWrapper fsw = new FileStreamWrapper())
                {
                    fsw.Init(fileName, internalBufferLength, FileAccess.ReadWrite);
                    int bytesWritten = fsw.WriteData(bytesToWrite, bytesToWrite.Length);

                    Assert.Equal(bytesToWrite.Length, bytesWritten);
                }

                // Then:
                // ... The file I wrote to should contain only the bytes I wrote out
                using (FileStream fs = File.OpenRead(fileName))
                {
                    byte[] readBackBytes = new byte[1024];
                    int    bytesRead     = fs.Read(readBackBytes, 0, readBackBytes.Length);

                    Assert.Equal(bytesToWrite.Length, bytesRead);   // If bytes read is not equal, then more or less of the original string was written to the file
                    Assert.True(bytesToWrite.SequenceEqual(readBackBytes.Take(bytesRead)));
                }
            }
            finally
            {
                // Cleanup:
                // ... Delete the test file
                CleanupTestFile(fileName);
            }
        }