Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mono/corefx.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorStephen Toub <stoub@microsoft.com>2018-02-10 15:11:12 +0300
committerGitHub <noreply@github.com>2018-02-10 15:11:12 +0300
commit7130581c8bd7114143628679624f7ae29cb56818 (patch)
treebe4da0d79851c16f69b963141ab5c92944c20d9f /src
parent57ba13cbfa203c8bc5003dd9b3ab4aaa796c22f8 (diff)
Add FileStream tests for custom OwnedMemory with Read/WriteAsync (#26931)
Diffstat (limited to 'src')
-rw-r--r--src/System.IO.FileSystem/tests/FileStream/ReadWriteSpan.netcoreapp.cs48
-rw-r--r--src/System.IO.FileSystem/tests/System.IO.FileSystem.Tests.csproj3
2 files changed, 47 insertions, 4 deletions
diff --git a/src/System.IO.FileSystem/tests/FileStream/ReadWriteSpan.netcoreapp.cs b/src/System.IO.FileSystem/tests/FileStream/ReadWriteSpan.netcoreapp.cs
index 272b19769e..0b1898d1f5 100644
--- a/src/System.IO.FileSystem/tests/FileStream/ReadWriteSpan.netcoreapp.cs
+++ b/src/System.IO.FileSystem/tests/FileStream/ReadWriteSpan.netcoreapp.cs
@@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
+using System.Buffers;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
@@ -188,6 +189,20 @@ namespace System.IO.Tests
}
[Fact]
+ public async Task NonEmptyFile_CustomOwnedMemory_ReadAsync_GetsExpectedData()
+ {
+ string fileName = GetTestFilePath();
+ File.WriteAllBytes(fileName, TestBuffer);
+
+ using (var fs = CreateFileStream(fileName, FileMode.Open))
+ using (var buffer = new NativeOwnedMemory(TestBuffer.Length))
+ {
+ Assert.Equal(TestBuffer.Length, await fs.ReadAsync(buffer.Memory));
+ Assert.Equal<byte>(TestBuffer, buffer.Memory.ToArray());
+ }
+ }
+
+ [Fact]
public void ReadOnly_WriteAsync_Throws()
{
string fileName = GetTestFilePath();
@@ -238,24 +253,49 @@ namespace System.IO.Tests
Assert.Equal(TestBuffer, buffer);
}
}
+
+ [Fact]
+ public async Task NonEmptyWriteAsync_CustomOwnedMemory_WritesExpectedData()
+ {
+ using (var mem = new NativeOwnedMemory(TestBuffer.Length))
+ using (var fs = CreateFileStream(GetTestFilePath(), FileMode.Create))
+ {
+ new Memory<byte>(TestBuffer).CopyTo(mem.Memory);
+
+ await fs.WriteAsync(mem.Memory);
+ Assert.Equal(TestBuffer.Length, fs.Length);
+ Assert.Equal(TestBuffer.Length, fs.Position);
+
+ fs.Position = 0;
+ var buffer = new byte[TestBuffer.Length];
+ Assert.Equal(TestBuffer.Length, await fs.ReadAsync(new Memory<byte>(buffer)));
+ Assert.Equal(TestBuffer, buffer);
+ }
+ }
}
public class Sync_FileStream_ReadWrite_Span : FileStream_ReadWrite_Span
{
protected override FileStream CreateFileStream(string path, FileMode mode, FileAccess access) =>
- new FileStream(path, mode, access, FileShare.None, 0x1000, FileOptions.None);
+ new FileStream(path, mode, access, FileShare.None, bufferSize: 0x1000, FileOptions.None);
}
public class Async_FileStream_ReadWrite_Span : FileStream_ReadWrite_Span
{
protected override FileStream CreateFileStream(string path, FileMode mode, FileAccess access) =>
- new FileStream(path, mode, access, FileShare.None, 0x1000, FileOptions.Asynchronous);
+ new FileStream(path, mode, access, FileShare.None, bufferSize: 0x1000, FileOptions.Asynchronous);
+ }
+
+ public class Async_NoBuffer_FileStream_ReadWrite_Span : FileStream_ReadWrite_Span
+ {
+ protected override FileStream CreateFileStream(string path, FileMode mode, FileAccess access) =>
+ new FileStream(path, mode, access, FileShare.None, bufferSize: 1, FileOptions.Asynchronous);
}
public sealed class Sync_DerivedFileStream_ReadWrite_Span : Sync_FileStream_ReadWrite_Span
{
protected override FileStream CreateFileStream(string path, FileMode mode, FileAccess access) =>
- new DerivedFileStream(path, mode, access, FileShare.None, 0x1000, FileOptions.None);
+ new DerivedFileStream(path, mode, access, FileShare.None, bufferSize: 0x1000, FileOptions.None);
[Fact]
public void CallSpanReadWriteOnDerivedFileStream_ArrayMethodsUsed()
@@ -299,7 +339,7 @@ namespace System.IO.Tests
public sealed class Async_DerivedFileStream_ReadWrite_Span : Async_FileStream_ReadWrite_Span
{
protected override FileStream CreateFileStream(string path, FileMode mode, FileAccess access) =>
- new DerivedFileStream(path, mode, access, FileShare.None, 0x1000, FileOptions.Asynchronous);
+ new DerivedFileStream(path, mode, access, FileShare.None, bufferSize: 0x1000, FileOptions.Asynchronous);
}
internal sealed class DerivedFileStream : FileStream
diff --git a/src/System.IO.FileSystem/tests/System.IO.FileSystem.Tests.csproj b/src/System.IO.FileSystem/tests/System.IO.FileSystem.Tests.csproj
index cdbe5259f6..471bf4cd52 100644
--- a/src/System.IO.FileSystem/tests/System.IO.FileSystem.Tests.csproj
+++ b/src/System.IO.FileSystem/tests/System.IO.FileSystem.Tests.csproj
@@ -169,6 +169,9 @@
<Compile Include="FileInfo\AppendText.cs" />
<Compile Include="FileInfo\CopyTo.cs" />
<!-- Helpers -->
+ <Compile Include="$(CommonTestPath)\System\Buffers\NativeOwnedMemory.cs">
+ <Link>Common\System\Buffers\NativeOwnedMemory.cs</Link>
+ </Compile>
<Compile Include="$(CommonTestPath)\System\IO\TempFile.cs">
<Link>Common\System\IO\TempFile.cs</Link>
</Compile>