Imported Upstream version 5.0.0.42

Former-commit-id: fd56571888259555122d8a0f58c68838229cea2b
This commit is contained in:
Xamarin Public Jenkins (auto-signing)
2017-04-10 11:41:01 +00:00
parent 1190d13a04
commit 6bdd276d05
19939 changed files with 3099680 additions and 93811 deletions

View File

@@ -0,0 +1,62 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.22310.1
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "System.IO.UnmanagedMemoryStream.Tests", "tests\System.IO.UnmanagedMemoryStream.Tests.csproj", "{55F26FB1-D4AF-48CA-A470-83113AE7BFDB}"
ProjectSection(ProjectDependencies) = postProject
{BCF9255A-4321-4277-AD7D-F5094092C554} = {BCF9255A-4321-4277-AD7D-F5094092C554}
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "System.IO.UnmanagedMemoryStream", "src\System.IO.UnmanagedMemoryStream.csproj", "{BCF9255A-4321-4277-AD7D-F5094092C554}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{BC25A4FD-0A1D-4AD3-AFEB-5F179D34DF5B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
DebugNETCoreAppnet463-Windows_NTnetcoreapp|AnyCPU = DebugNETCoreAppnet463-Windows_NTnetcoreapp|AnyCPU
ReleaseNETCoreAppnet463-Windows_NTnetcoreapp|AnyCPU = ReleaseNETCoreAppnet463-Windows_NTnetcoreapp|AnyCPU
DebugNETCoreAppnet463-Windows_NTnetcoreapp|AnyCPU = DebugNETCoreAppnet463-Windows_NTnetcoreapp|AnyCPU
ReleaseNETCoreAppnet463-Windows_NTnetcoreapp|AnyCPU = ReleaseNETCoreAppnet463-Windows_NTnetcoreapp|AnyCPU
DebugNETCoreAppnetcoreappnetcoreapp|AnyCPU = DebugNETCoreAppnetcoreappnetcoreapp|AnyCPU
ReleaseNETCoreAppnetcoreappnetcoreapp|AnyCPU = ReleaseNETCoreAppnetcoreappnetcoreapp|AnyCPU
DebugNETCoreAppnetcoreappnetcoreapp|AnyCPU = DebugNETCoreAppnetcoreappnetcoreapp|AnyCPU
ReleaseNETCoreAppnetcoreappnetcoreapp|AnyCPU = ReleaseNETCoreAppnetcoreappnetcoreapp|AnyCPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{55F26FB1-D4AF-48CA-A470-83113AE7BFDB}.DebugNETCoreAppnet463-Windows_NTnetcoreapp|AnyCPU.ActiveCfg = netcoreapp-Debug|Any CPU
{55F26FB1-D4AF-48CA-A470-83113AE7BFDB}.DebugNETCoreAppnet463-Windows_NTnetcoreapp|AnyCPU.Build.0 = netcoreapp-Debug|Any CPU
{55F26FB1-D4AF-48CA-A470-83113AE7BFDB}.ReleaseNETCoreAppnet463-Windows_NTnetcoreapp|AnyCPU.ActiveCfg = netcoreapp-Release|Any CPU
{55F26FB1-D4AF-48CA-A470-83113AE7BFDB}.ReleaseNETCoreAppnet463-Windows_NTnetcoreapp|AnyCPU.Build.0 = netcoreapp-Release|Any CPU
{55F26FB1-D4AF-48CA-A470-83113AE7BFDB}.DebugNETCoreAppnet463-Windows_NTnetcoreapp|AnyCPU.ActiveCfg = netcoreapp-Debug|Any CPU
{55F26FB1-D4AF-48CA-A470-83113AE7BFDB}.DebugNETCoreAppnet463-Windows_NTnetcoreapp|AnyCPU.Build.0 = netcoreapp-Debug|Any CPU
{55F26FB1-D4AF-48CA-A470-83113AE7BFDB}.ReleaseNETCoreAppnet463-Windows_NTnetcoreapp|AnyCPU.ActiveCfg = netcoreapp-Release|Any CPU
{55F26FB1-D4AF-48CA-A470-83113AE7BFDB}.ReleaseNETCoreAppnet463-Windows_NTnetcoreapp|AnyCPU.Build.0 = netcoreapp-Release|Any CPU
{55F26FB1-D4AF-48CA-A470-83113AE7BFDB}.DebugNETCoreAppnetcoreappnetcoreapp|AnyCPU.ActiveCfg = netcoreapp-Debug|Any CPU
{55F26FB1-D4AF-48CA-A470-83113AE7BFDB}.DebugNETCoreAppnetcoreappnetcoreapp|AnyCPU.Build.0 = netcoreapp-Debug|Any CPU
{55F26FB1-D4AF-48CA-A470-83113AE7BFDB}.ReleaseNETCoreAppnetcoreappnetcoreapp|AnyCPU.ActiveCfg = netcoreapp-Release|Any CPU
{55F26FB1-D4AF-48CA-A470-83113AE7BFDB}.ReleaseNETCoreAppnetcoreappnetcoreapp|AnyCPU.Build.0 = netcoreapp-Release|Any CPU
{55F26FB1-D4AF-48CA-A470-83113AE7BFDB}.DebugNETCoreAppnetcoreappnetcoreapp|AnyCPU.ActiveCfg = netcoreapp-Debug|Any CPU
{55F26FB1-D4AF-48CA-A470-83113AE7BFDB}.DebugNETCoreAppnetcoreappnetcoreapp|AnyCPU.Build.0 = netcoreapp-Debug|Any CPU
{55F26FB1-D4AF-48CA-A470-83113AE7BFDB}.ReleaseNETCoreAppnetcoreappnetcoreapp|AnyCPU.ActiveCfg = netcoreapp-Release|Any CPU
{55F26FB1-D4AF-48CA-A470-83113AE7BFDB}.ReleaseNETCoreAppnetcoreappnetcoreapp|AnyCPU.Build.0 = netcoreapp-Release|Any CPU
{BCF9255A-4321-4277-AD7D-F5094092C554}.DebugNETCoreAppnet463-Windows_NTnetcoreapp|AnyCPU.ActiveCfg = net463-Windows_NT-Debug|Any CPU
{BCF9255A-4321-4277-AD7D-F5094092C554}.DebugNETCoreAppnet463-Windows_NTnetcoreapp|AnyCPU.Build.0 = net463-Windows_NT-Debug|Any CPU
{BCF9255A-4321-4277-AD7D-F5094092C554}.ReleaseNETCoreAppnet463-Windows_NTnetcoreapp|AnyCPU.ActiveCfg = net463-Windows_NT-Release|Any CPU
{BCF9255A-4321-4277-AD7D-F5094092C554}.ReleaseNETCoreAppnet463-Windows_NTnetcoreapp|AnyCPU.Build.0 = net463-Windows_NT-Release|Any CPU
{BCF9255A-4321-4277-AD7D-F5094092C554}.DebugNETCoreAppnet463-Windows_NTnetcoreapp|AnyCPU.ActiveCfg = net463-Windows_NT-Debug|Any CPU
{BCF9255A-4321-4277-AD7D-F5094092C554}.DebugNETCoreAppnet463-Windows_NTnetcoreapp|AnyCPU.Build.0 = net463-Windows_NT-Debug|Any CPU
{BCF9255A-4321-4277-AD7D-F5094092C554}.ReleaseNETCoreAppnet463-Windows_NTnetcoreapp|AnyCPU.ActiveCfg = net463-Windows_NT-Release|Any CPU
{BCF9255A-4321-4277-AD7D-F5094092C554}.ReleaseNETCoreAppnet463-Windows_NTnetcoreapp|AnyCPU.Build.0 = net463-Windows_NT-Release|Any CPU
{BCF9255A-4321-4277-AD7D-F5094092C554}.DebugNETCoreAppnetcoreappnetcoreapp|AnyCPU.ActiveCfg = netcoreapp-Debug|Any CPU
{BCF9255A-4321-4277-AD7D-F5094092C554}.DebugNETCoreAppnetcoreappnetcoreapp|AnyCPU.Build.0 = netcoreapp-Debug|Any CPU
{BCF9255A-4321-4277-AD7D-F5094092C554}.ReleaseNETCoreAppnetcoreappnetcoreapp|AnyCPU.ActiveCfg = netcoreapp-Release|Any CPU
{BCF9255A-4321-4277-AD7D-F5094092C554}.ReleaseNETCoreAppnetcoreappnetcoreapp|AnyCPU.Build.0 = netcoreapp-Release|Any CPU
{BCF9255A-4321-4277-AD7D-F5094092C554}.DebugNETCoreAppnetcoreappnetcoreapp|AnyCPU.ActiveCfg = netcoreapp-Debug|Any CPU
{BCF9255A-4321-4277-AD7D-F5094092C554}.DebugNETCoreAppnetcoreappnetcoreapp|AnyCPU.Build.0 = netcoreapp-Debug|Any CPU
{BCF9255A-4321-4277-AD7D-F5094092C554}.ReleaseNETCoreAppnetcoreappnetcoreapp|AnyCPU.ActiveCfg = netcoreapp-Release|Any CPU
{BCF9255A-4321-4277-AD7D-F5094092C554}.ReleaseNETCoreAppnetcoreappnetcoreapp|AnyCPU.Build.0 = netcoreapp-Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal

View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\dir.props" />
<PropertyGroup>
<AssemblyVersion>4.1.0.0</AssemblyVersion>
<IsNETCoreApp>true</IsNETCoreApp>
</PropertyGroup>
</Project>

View File

@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<BuildConfigurations>
netcoreapp;
uap;
</BuildConfigurations>
</PropertyGroup>
</Project>

View File

@@ -0,0 +1,9 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
// ------------------------------------------------------------------------------
// Changes to this file must follow the http://aka.ms/api-review process.
// ------------------------------------------------------------------------------
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.IO.UnmanagedMemoryStream))]
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.IO.UnmanagedMemoryAccessor))]

View File

@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.props))\dir.props" />
<PropertyGroup>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'netcoreapp-Debug|AnyCPU'" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'netcoreapp-Release|AnyCPU'" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'uap-Debug|AnyCPU'" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'uap-Release|AnyCPU'" />
<ItemGroup>
<Compile Include="System.IO.UnmanagedMemoryStream.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\System.Runtime.InteropServices\ref\System.Runtime.InteropServices.csproj" />
<ProjectReference Include="..\..\System.Runtime\ref\System.Runtime.csproj" />
</ItemGroup>
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
</Project>

View File

@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<BuildConfigurations>
net463-Windows_NT;
netcoreapp;
</BuildConfigurations>
</PropertyGroup>
</Project>

View File

@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build">
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.props))\dir.props" />
<PropertyGroup>
<ProjectGuid>{BCF9255A-4321-4277-AD7D-F5094092C554}</ProjectGuid>
<AssemblyName>System.IO.UnmanagedMemoryStream</AssemblyName>
<IsPartialFacadeAssembly>true</IsPartialFacadeAssembly>
</PropertyGroup>
<!-- Default configurations to help VS understand the configurations -->
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='net463-Windows_NT-Debug|AnyCPU'" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='net463-Windows_NT-Release|AnyCPU'" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='netcoreapp-Debug|AnyCPU'" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='netcoreapp-Release|AnyCPU'" />
<ItemGroup Condition="'$(TargetGroup)' == 'netcoreapp'">
<TargetingPackReference Include="System.Private.CoreLib" />
</ItemGroup>
<ItemGroup Condition="'$(TargetGroup)' == 'net463'">
<TargetingPackReference Include="mscorlib" />
</ItemGroup>
<ItemGroup>
<ReferenceFromRuntime Include="System.Private.CoreLib" />
</ItemGroup>
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
</Project>

View File

@@ -0,0 +1,79 @@
// Licensed to the .NET Foundation under one or more agreements.
// 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.Collections.Generic;
namespace System.IO.Tests
{
public static class ArrayHelpers
{
public static IEqualityComparer<T[]> Comparer<T>()
{
return ArrayComparer<T>.Instance;
}
public static byte[] Flatten(this IEnumerable<byte[]> segments)
{
List<byte> bytes = new List<byte>();
foreach (var segment in segments)
{
bytes.AddRange(segment);
}
return bytes.ToArray();
}
public static byte[] CreateByteArray(long length)
{
var random = new Random(100);
var data = new byte[length];
random.NextBytes(data);
return data;
}
public static byte[] CreateByteArray(long length, byte value)
{
var data = new byte[length];
for (int index = 0; index < length; index++)
{
data[index] = value;
}
return data;
}
public static T[] Copy<T>(this T[] source)
{
return (T[])source.Clone();
}
private sealed class ArrayComparer<T> : IEqualityComparer<T[]>
{
public static readonly ArrayComparer<T> Instance = new ArrayComparer<T>();
private ArrayComparer() // use the static Instance singleton
{
}
public bool Equals(T[] x, T[] y)
{
if (x.Length != y.Length)
{
return false;
}
for (int i = 0; i < x.Length; i++)
{
if(!EqualityComparer<T>.Default.Equals(x[i], y[i]))
{
return false;
}
}
return true;
}
public int GetHashCode(T[] obj)
{
throw new NotSupportedException("Avoid using arrays as keys in hashtables. If you really have to do it, write your own comparer; I don't want to be responsible for your slow code.");
}
}
}
}

View File

@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<BuildConfigurations>
netstandard1.3;
netstandard;
</BuildConfigurations>
</PropertyGroup>
</Project>

View File

@@ -0,0 +1,24 @@
// Licensed to the .NET Foundation under one or more agreements.
// 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.Runtime.InteropServices;
using Xunit;
namespace System.IO.Tests
{
internal sealed class HGlobalSafeBuffer : SafeBuffer
{
internal HGlobalSafeBuffer(int capacity) : base(true)
{
SetHandle(Marshal.AllocHGlobal(capacity));
Initialize((ulong)capacity);
}
protected override bool ReleaseHandle()
{
Marshal.FreeHGlobal(handle);
return true;
}
}
}

View File

@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.props))\dir.props" />
<ItemGroup>
<Project Include="System.IO.UnmanagedMemoryStream.Tests.csproj" />
<Project Include="System.IO.UnmanagedMemoryStream.Tests.csproj">
<TargetGroup>netstandard1.3</TargetGroup>
<TestTFMs>netcoreapp1.0</TestTFMs>
</Project>
<Project Include="System.IO.UnmanagedMemoryStream.Tests.csproj">
<TestTFMs>netcore50;net463</TestTFMs>
<OSGroup>Windows_NT</OSGroup>
</Project>
</ItemGroup>
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.traversal.targets))\dir.traversal.targets" />
</Project>

View File

@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.props))\dir.props" />
<PropertyGroup>
<ProjectGuid>{55F26FB1-D4AF-48CA-A470-83113AE7BFDB}</ProjectGuid>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='netstandard1.3-Debug|AnyCPU'" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='netstandard1.3-Release|AnyCPU'" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='netstandard-Debug|AnyCPU'" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='netstandard-Release|AnyCPU'" />
<ItemGroup Condition="'$(TargetGroup)' == 'netstandard'">
<Compile Include="Uma.TestStructs.cs" />
<Compile Include="Uma.ReadWriteStruct.cs" />
<Compile Include="Uma.ReadWriteStructArray.cs" />
</ItemGroup>
<ItemGroup>
<Compile Include="ArrayHelpers.cs" />
<Compile Include="UmaTests.cs" />
<Compile Include="UmsCtorTests.cs" />
<Compile Include="HGlobalSafeBuffer.cs" />
<Compile Include="UmsRead.cs" />
<Compile Include="UmsSafeBuffer.cs" />
<Compile Include="UmaCtorTests.cs" />
<Compile Include="UmsFlush.cs" />
<Compile Include="UmsWrite.cs" />
<Compile Include="UmsReadWrite.cs" />
<Compile Include="UmsTests.cs" />
<Compile Include="UmsManager.cs" />
<Compile Include="UmsSecurityTest.cs" />
</ItemGroup>
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
</Project>

View File

@@ -0,0 +1,150 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using Xunit;
namespace System.IO.Tests
{
public class Uma_ReadWriteStruct : Uma_TestStructs
{
[Fact]
public static void UmaReadWriteStruct_NegativePosition()
{
const int capacity = 100;
UmaTestStruct inStruct = new UmaTestStruct();
using (TestSafeBuffer buffer = new TestSafeBuffer(capacity))
using (UnmanagedMemoryAccessor uma = new UnmanagedMemoryAccessor(buffer, 0, capacity, FileAccess.ReadWrite))
{
Assert.Throws<ArgumentOutOfRangeException>("position", () => uma.Write<UmaTestStruct>(-1, ref inStruct));
Assert.Throws<ArgumentOutOfRangeException>("position", () => uma.Read<UmaTestStruct>(-1, out inStruct));
}
}
[Fact]
public static void UmaReadWriteStruct_Closed()
{
const int capacity = 100;
UmaTestStruct inStruct = new UmaTestStruct();
using (var buffer = new TestSafeBuffer(capacity))
{
UnmanagedMemoryAccessor uma = new UnmanagedMemoryAccessor(buffer, 0, capacity, FileAccess.ReadWrite);
uma.Dispose();
Assert.Throws<ObjectDisposedException>(() => uma.Write<UmaTestStruct>(0, ref inStruct));
Assert.Throws<ObjectDisposedException>(() => uma.Read<UmaTestStruct>(0, out inStruct));
}
}
[Fact]
public static void UmaReadStruct_WriteMode()
{
const int capacity = 100;
UmaTestStruct inStruct = new UmaTestStruct();
using (TestSafeBuffer buffer = new TestSafeBuffer(capacity))
using (UnmanagedMemoryAccessor uma = new UnmanagedMemoryAccessor(buffer, 0, capacity, FileAccess.Write))
{
Assert.Throws<NotSupportedException>(() => uma.Read<UmaTestStruct>(0, out inStruct));
}
}
[Fact]
public static void UmaWriteStruct_ReadMode()
{
const int capacity = 100;
UmaTestStruct inStruct = new UmaTestStruct();
using (TestSafeBuffer buffer = new TestSafeBuffer(capacity))
using (UnmanagedMemoryAccessor uma = new UnmanagedMemoryAccessor(buffer, 0, capacity, FileAccess.Read))
{
Assert.Throws<NotSupportedException>(() => uma.Write<UmaTestStruct>(0, ref inStruct));
}
}
[Fact]
public static void UmaReadWriteStruct_PositionGreaterThanCapacity()
{
const int capacity = 100;
UmaTestStruct inStruct = new UmaTestStruct();
using (TestSafeBuffer buffer = new TestSafeBuffer(capacity))
using (UnmanagedMemoryAccessor uma = new UnmanagedMemoryAccessor(buffer, 0, capacity, FileAccess.ReadWrite))
{
Assert.Throws<ArgumentOutOfRangeException>("position", () => uma.Write<UmaTestStruct>(capacity, ref inStruct));
Assert.Throws<ArgumentOutOfRangeException>("position", () => uma.Read<UmaTestStruct>(capacity, out inStruct));
}
}
[Fact]
public static void UmaReadWriteStruct_InsufficientSpace()
{
const int capacity = 100;
UmaTestStruct inStruct = new UmaTestStruct();
using (TestSafeBuffer buffer = new TestSafeBuffer(capacity))
using (UnmanagedMemoryAccessor uma = new UnmanagedMemoryAccessor(buffer, 0, capacity, FileAccess.ReadWrite))
{
Assert.Throws<ArgumentException>(() => uma.Write<UmaTestStruct>(capacity - UmaTestStruct_UnalignedSize + 1, ref inStruct));
Assert.Throws<ArgumentException>(() => uma.Write<UmaTestStruct>(capacity - UmaTestStruct_AlignedSize + 1, ref inStruct));
Assert.Throws<ArgumentException>(() => uma.Read<UmaTestStruct>(capacity - UmaTestStruct_UnalignedSize + 1, out inStruct));
Assert.Throws<ArgumentException>(() => uma.Read<UmaTestStruct>(capacity - UmaTestStruct_AlignedSize + 1, out inStruct));
}
}
[Fact]
public static void UmaReadWriteStructWithReferenceType_ThrowsArgumentException()
{
const int capacity = 100;
UmaTestStruct_ContainsReferenceType inStruct = new UmaTestStruct_ContainsReferenceType { referenceType = new object() };
using (var buffer = new TestSafeBuffer(capacity))
using (var uma = new UnmanagedMemoryAccessor(buffer, 0, capacity, FileAccess.ReadWrite))
{
Assert.Throws<ArgumentException>("type", () => uma.Write<UmaTestStruct_ContainsReferenceType>(0, ref inStruct));
Assert.Throws<ArgumentException>("type", () => uma.Read<UmaTestStruct_ContainsReferenceType>(0, out inStruct));
}
}
[Fact]
public static void UmaReadWriteGenericIntStruct_Valid()
{
const int capacity = 100;
UmaTestStruct_Generic<int> inStruct = new UmaTestStruct_Generic<int> () { ofT = 158 };
UmaTestStruct_Generic<int> outStruct;
using (var buffer = new TestSafeBuffer(capacity))
using (var uma = new UnmanagedMemoryAccessor(buffer, 0, capacity, FileAccess.ReadWrite))
{
uma.Write<UmaTestStruct_Generic<int>>(0, ref inStruct);
uma.Read<UmaTestStruct_Generic<int>>(0, out outStruct);
Assert.Equal(inStruct.ofT, outStruct.ofT);
}
}
[Fact]
public static void UmaReadWriteGenericStringStruct_ThrowsArgumentException()
{
const int capacity = 100;
UmaTestStruct_Generic<string> inStruct = new UmaTestStruct_Generic<string>() { ofT = "Cats!" };
using (var buffer = new TestSafeBuffer(capacity))
using (var uma = new UnmanagedMemoryAccessor(buffer, 0, capacity, FileAccess.ReadWrite))
{
Assert.Throws<ArgumentException>("type", () => uma.Write<UmaTestStruct_Generic<string>>(0, ref inStruct));
Assert.Throws<ArgumentException>("type", () => uma.Read<UmaTestStruct_Generic<string>>(0, out inStruct));
}
}
[Fact]
public static void UmaReadWriteStruct_Valid()
{
const int capacity = 100;
UmaTestStruct inStruct = new UmaTestStruct(){ int1 = 1, int2 = 2, bool1 = false, char1 = 'p', bool2 = true};
UmaTestStruct outStruct;
using (TestSafeBuffer buffer = new TestSafeBuffer(capacity))
using (UnmanagedMemoryAccessor uma = new UnmanagedMemoryAccessor(buffer, 0, capacity, FileAccess.ReadWrite))
{
uma.Write<UmaTestStruct>(capacity - UmaTestStruct_AlignedSize, ref inStruct);
uma.Read<UmaTestStruct>(capacity - UmaTestStruct_AlignedSize, out outStruct);
Assert.Equal(inStruct.int1, outStruct.int1);
Assert.Equal(inStruct.int2, outStruct.int2);
Assert.Equal(inStruct.bool1, outStruct.bool1);
Assert.Equal(inStruct.char1, outStruct.char1);
Assert.Equal(inStruct.bool2, outStruct.bool2);
}
}
}
}

View File

@@ -0,0 +1,191 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using Xunit;
namespace System.IO.Tests
{
public class Uma_ReadWriteStructArray : Uma_TestStructs
{
[Fact]
public static void UmaReadWriteStructArray_InvalidParameters()
{
const int capacity = 100;
UmaTestStruct[] structArr = new UmaTestStruct[1];
using (TestSafeBuffer buffer = new TestSafeBuffer(capacity))
using (UnmanagedMemoryAccessor uma = new UnmanagedMemoryAccessor(buffer, 0, capacity, FileAccess.ReadWrite))
{
Assert.Throws<ArgumentNullException>("array", () => uma.WriteArray<UmaTestStruct>(0, null, 0, 1));
Assert.Throws<ArgumentNullException>("array", () => uma.ReadArray<UmaTestStruct>(0, null, 0, 1));
Assert.Throws<ArgumentOutOfRangeException>("offset", () => uma.WriteArray<UmaTestStruct>(0, structArr, -1, 1));
Assert.Throws<ArgumentOutOfRangeException>("offset", () => uma.ReadArray<UmaTestStruct>(0, structArr, -1, 1));
Assert.Throws<ArgumentOutOfRangeException>("count", () => uma.WriteArray<UmaTestStruct>(0, structArr, 0, -1));
Assert.Throws<ArgumentOutOfRangeException>("count", () => uma.ReadArray<UmaTestStruct>(0, structArr, 0, -1));
Assert.Throws<ArgumentException>(() => uma.WriteArray<UmaTestStruct>(0, structArr, 2, 0));
Assert.Throws<ArgumentException>(() => uma.ReadArray<UmaTestStruct>(0, structArr, 2, 0));
Assert.Throws<ArgumentException>(() => uma.WriteArray<UmaTestStruct>(0, structArr, 0, 2));
Assert.Throws<ArgumentException>(() => uma.ReadArray<UmaTestStruct>(0, structArr, 0, 2));
Assert.Throws<ArgumentOutOfRangeException>("position", () => uma.WriteArray<UmaTestStruct>(-1, structArr, 0, 1));
Assert.Throws<ArgumentOutOfRangeException>("position", () => uma.ReadArray<UmaTestStruct>(-1, structArr, 0, 1));
Assert.Throws<ArgumentOutOfRangeException>("position", () => uma.WriteArray<UmaTestStruct>(capacity, structArr, 0, 1));
Assert.Throws<ArgumentOutOfRangeException>("position", () => uma.ReadArray<UmaTestStruct>(capacity, structArr, 0, 1));
}
}
[Fact]
public static void UmaReadWriteStructArray_Closed()
{
const int capacity = 100;
UmaTestStruct[] structArr = new UmaTestStruct[1];
using (var buffer = new TestSafeBuffer(capacity))
{
UnmanagedMemoryAccessor uma = new UnmanagedMemoryAccessor(buffer, 0, capacity, FileAccess.ReadWrite);
uma.Dispose();
Assert.Throws<ObjectDisposedException>(() => uma.WriteArray<UmaTestStruct>(0, structArr, 0, 1));
Assert.Throws<ObjectDisposedException>(() => uma.ReadArray<UmaTestStruct>(0, structArr, 0, 1));
}
}
[Fact]
public static void UmaReadStructArray_WriteMode()
{
const int capacity = 100;
UmaTestStruct[] structArr = new UmaTestStruct[1];
using (TestSafeBuffer buffer = new TestSafeBuffer(capacity))
using (UnmanagedMemoryAccessor uma = new UnmanagedMemoryAccessor(buffer, 0, capacity, FileAccess.Write))
{
Assert.Throws<NotSupportedException>(() => uma.ReadArray<UmaTestStruct>(0, structArr, 0, 1));
}
}
[Fact]
public static void UmaWriteStructArray_ReadMode()
{
const int capacity = 100;
UmaTestStruct[] structArr = new UmaTestStruct[1];
using (TestSafeBuffer buffer = new TestSafeBuffer(capacity))
using (UnmanagedMemoryAccessor uma = new UnmanagedMemoryAccessor(buffer, 0, capacity, FileAccess.Read))
{
Assert.Throws<NotSupportedException>(() => uma.WriteArray<UmaTestStruct>(0, structArr, 0, 1));
}
}
[Fact]
public static void UmaReadWriteStructArray_InsufficientSpace()
{
const int capacity = 100;
UmaTestStruct[] structArr = new UmaTestStruct[1];
using (TestSafeBuffer buffer = new TestSafeBuffer(capacity))
using (UnmanagedMemoryAccessor uma = new UnmanagedMemoryAccessor(buffer, 0, capacity, FileAccess.ReadWrite))
{
Assert.Throws<ArgumentException>(() => uma.WriteArray<UmaTestStruct>(capacity - UmaTestStruct_UnalignedSize + 1, structArr, 0, 1));
Assert.Throws<ArgumentException>(() => uma.WriteArray<UmaTestStruct>(capacity - UmaTestStruct_AlignedSize + 1, structArr, 0, 1));
Assert.Equal(0, uma.ReadArray<UmaTestStruct>(capacity - UmaTestStruct_UnalignedSize + 1, structArr, 0, 1));
Assert.Equal(0, uma.ReadArray<UmaTestStruct>(capacity - UmaTestStruct_AlignedSize + 1, structArr, 0, 1));
}
}
[Fact]
public static void UmaReadWriteStructArrayWithReferenceType_ThrowsArgumentException()
{
const int capacity = 100;
UmaTestStruct_ContainsReferenceType[] structArr = new UmaTestStruct_ContainsReferenceType[1] { new UmaTestStruct_ContainsReferenceType() { referenceType = new object() } };
using (var buffer = new TestSafeBuffer(capacity))
using (var uma = new UnmanagedMemoryAccessor(buffer, 0, capacity, FileAccess.ReadWrite))
{
Assert.Throws<ArgumentException>("type", () => uma.WriteArray<UmaTestStruct_ContainsReferenceType>(0, structArr, 0, 1));
Assert.Throws<ArgumentException>("type", () => uma.ReadArray<UmaTestStruct_ContainsReferenceType>(0, structArr, 0, 1));
}
}
[Fact]
public static void UmaReadWriteStructArrayGenericIntStruct_Valid()
{
const int capacity = 100;
UmaTestStruct_Generic<int>[] inStructArr = new UmaTestStruct_Generic<int>[1] { new UmaTestStruct_Generic<int>() { ofT = 190 } };
UmaTestStruct_Generic<int>[] outStructArr = new UmaTestStruct_Generic<int>[1];
using (var buffer = new TestSafeBuffer(capacity))
using (var uma = new UnmanagedMemoryAccessor(buffer, 0, capacity, FileAccess.ReadWrite))
{
uma.WriteArray<UmaTestStruct_Generic<int>>(0, inStructArr, 0, 1);
Assert.Equal(1, uma.ReadArray<UmaTestStruct_Generic<int>>(0, outStructArr, 0, 1));
Assert.Equal(inStructArr[0].ofT, outStructArr[0].ofT);
}
}
[Fact]
public static void UmaReadWriteGenericStringStructArray_ThrowsArgumentException()
{
const int capacity = 100;
UmaTestStruct_Generic<string>[] structArr = new UmaTestStruct_Generic<string>[1] { new UmaTestStruct_Generic<string>() { ofT = "Cats!" } };
using (var buffer = new TestSafeBuffer(capacity))
using (var uma = new UnmanagedMemoryAccessor(buffer, 0, capacity, FileAccess.ReadWrite))
{
Assert.Throws<ArgumentException>("type", () => uma.WriteArray<UmaTestStruct_Generic<string>>(0, structArr, 0, 1));
Assert.Throws<ArgumentException>("type", () => uma.ReadArray<UmaTestStruct_Generic<string>>(0, structArr, 0, 1));
}
}
[Fact]
public static void UmaReadWriteStructArray_OneItem()
{
const int capacity = 100;
UmaTestStruct[] inStructArr = new UmaTestStruct[1] { new UmaTestStruct() { int1 = 1, int2 = 2, bool1 = false, char1 = 'p', bool2 = true } };
UmaTestStruct[] outStructArr = new UmaTestStruct[1];
using (TestSafeBuffer buffer = new TestSafeBuffer(capacity))
using (UnmanagedMemoryAccessor uma = new UnmanagedMemoryAccessor(buffer, 0, capacity, FileAccess.ReadWrite))
{
uma.WriteArray<UmaTestStruct>(capacity - UmaTestStruct_AlignedSize, inStructArr, 0, 1);
Assert.Equal(1, uma.ReadArray<UmaTestStruct>(capacity - UmaTestStruct_AlignedSize, outStructArr, 0, 1));
Assert.Equal(inStructArr[0].int1, outStructArr[0].int1);
Assert.Equal(inStructArr[0].int2, outStructArr[0].int2);
Assert.Equal(inStructArr[0].bool1, outStructArr[0].bool1);
Assert.Equal(inStructArr[0].char1, outStructArr[0].char1);
Assert.Equal(inStructArr[0].bool2, outStructArr[0].bool2);
}
}
[Fact]
public static void UmaReadWriteStructArray_Multiples()
{
const int numberOfStructs = 12;
const int capacity = numberOfStructs * UmaTestStruct_AlignedSize;
UmaTestStruct[] inStructArr = new UmaTestStruct[numberOfStructs];
UmaTestStruct[] outStructArr = new UmaTestStruct[numberOfStructs];
for (int i = 0; i < numberOfStructs; i++)
{
inStructArr[i] = new UmaTestStruct() { bool1 = false, bool2 = true, int1 = i, int2 = i + 1, char1 = (char)i };
}
using (TestSafeBuffer buffer = new TestSafeBuffer(capacity))
using (UnmanagedMemoryAccessor uma = new UnmanagedMemoryAccessor(buffer, 0, capacity, FileAccess.ReadWrite))
{
uma.WriteArray<UmaTestStruct>(0, inStructArr, 0, numberOfStructs);
Assert.Equal(numberOfStructs, uma.ReadArray<UmaTestStruct>(0, outStructArr, 0, numberOfStructs));
for (int i = 0; i < numberOfStructs; i++)
{
Assert.Equal(i, outStructArr[i].int1);
Assert.Equal(i+1, outStructArr[i].int2);
Assert.Equal(false, outStructArr[i].bool1);
Assert.Equal((char)i, outStructArr[i].char1);
Assert.Equal(true, outStructArr[i].bool2);
}
}
}
[Fact]
public static void UmaReadWriteStructArray_TryToReadMoreThanAvailable()
{
const int capacity = 100;
UmaTestStruct[] inStructArr = new UmaTestStruct[1] { new UmaTestStruct() { int1 = 1, int2 = 2, bool1 = false, char1 = 'p', bool2 = true } };
UmaTestStruct[] outStructArr = new UmaTestStruct[5000];
using (TestSafeBuffer buffer = new TestSafeBuffer(capacity))
using (UnmanagedMemoryAccessor uma = new UnmanagedMemoryAccessor(buffer, 0, capacity, FileAccess.ReadWrite))
{
uma.WriteArray<UmaTestStruct>(0, inStructArr, 0, 1);
int readCount = uma.ReadArray<UmaTestStruct>(0, outStructArr, 0, 5000);
Assert.Equal(capacity / UmaTestStruct_AlignedSize, readCount);
}
}
}
}

View File

@@ -0,0 +1,30 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
namespace System.IO.Tests
{
public class Uma_TestStructs
{
public const int UmaTestStruct_UnalignedSize = 2 * sizeof(int) + 2 * sizeof(bool) + sizeof(char);
public const int UmaTestStruct_AlignedSize = 16; // potentially architecture dependent.
public struct UmaTestStruct
{
public int int1;
public bool bool1;
public int int2;
public char char1;
public bool bool2;
}
public struct UmaTestStruct_ContainsReferenceType
{
public object referenceType;
}
public struct UmaTestStruct_Generic<T>
{
public T ofT;
}
}
}

View File

@@ -0,0 +1,68 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using Xunit;
namespace System.IO.Tests
{
public class UmaCtorTests
{
[Fact]
public static void UmaCtors()
{
using (FakeSafeBuffer fakeBuffer = new FakeSafeBuffer(99))
using (var uma = new UnmanagedMemoryAccessor(fakeBuffer, 0, 0))
{
Assert.True(uma.CanRead);
Assert.False(uma.CanWrite);
Assert.Equal(0, uma.Capacity);
}
using (FakeSafeBuffer fakeBuffer = new FakeSafeBuffer(99))
using (var duma = new DerivedUnmanagedMemoryAccessor())
{
Assert.False(duma.CanRead);
Assert.False(duma.CanWrite);
Assert.Equal(0, duma.Capacity);
Assert.False(duma.IsOpen);
duma.Initialize(fakeBuffer, 0, (long)fakeBuffer.ByteLength, FileAccess.ReadWrite);
Assert.True(duma.IsOpen);
Assert.Throws<InvalidOperationException>(() => duma.Initialize(fakeBuffer, 0, (long)fakeBuffer.ByteLength, FileAccess.ReadWrite));
}
}
[Fact]
public static void UmaCtorsThatFail()
{
FakeSafeBuffer fakeBuffer = new FakeSafeBuffer(99);
FakeSafeBuffer nullBuffer = null;
Assert.Throws<ArgumentNullException>(() => new UnmanagedMemoryAccessor(nullBuffer, 0, 0));
Assert.Throws<ArgumentOutOfRangeException>(() => new UnmanagedMemoryAccessor(fakeBuffer, 2, -1));
Assert.Throws<ArgumentOutOfRangeException>(() => new UnmanagedMemoryAccessor(fakeBuffer, -1, 1));
Assert.Throws<ArgumentOutOfRangeException>(() => new UnmanagedMemoryAccessor(fakeBuffer, 1, 2, (FileAccess)(-1)));
Assert.Throws<ArgumentOutOfRangeException>(() => new UnmanagedMemoryAccessor(fakeBuffer, 1, 2, (FileAccess)42));
Assert.Throws<ArgumentException>(() => new UnmanagedMemoryAccessor(fakeBuffer, 2, 999));
Assert.Throws<ArgumentException>(() => new UnmanagedMemoryAccessor(fakeBuffer, 999, 9));
Assert.Throws<ArgumentException>(() => new UnmanagedMemoryAccessor(fakeBuffer, 1, 100));
Assert.Throws<ArgumentException>(() => new UnmanagedMemoryAccessor(fakeBuffer, Int32.MaxValue, 1));
}
// Derived class used to exercise protected members and to test behaviors before and after initialization
private sealed class DerivedUnmanagedMemoryAccessor : UnmanagedMemoryAccessor
{
internal DerivedUnmanagedMemoryAccessor() { }
internal void Initialize(FakeSafeBuffer buffer, long offset, long capacity, FileAccess access)
{
base.Initialize(buffer, offset, capacity, access);
}
internal new bool IsOpen { get { return base.IsOpen; } }
}
}
}

View File

@@ -0,0 +1,273 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using Xunit;
namespace System.IO.Tests
{
public class UmaTests
{
[Fact]
public static void UmaInvalidReadWrite()
{
const int capacity = 99;
FakeSafeBuffer sbuf = new FakeSafeBuffer((ulong)capacity);
using (var uma = new UnmanagedMemoryAccessor(sbuf, 0, capacity, FileAccess.ReadWrite))
{
Assert.Throws<ArgumentOutOfRangeException>(() => uma.ReadChar(-1));
Assert.Throws<ArgumentOutOfRangeException>(() => uma.ReadDecimal(capacity));
Assert.Throws<ArgumentException>(() => uma.ReadSingle(capacity - 1));
Assert.Throws<ArgumentOutOfRangeException>(() => uma.Write(-1, true));
Assert.Throws<ArgumentOutOfRangeException>(() => uma.Write(capacity, 12345));
Assert.Throws<ArgumentException>(() => uma.Write(capacity - 1, 0.123));
uma.Dispose();
Assert.Throws<ObjectDisposedException>(() => uma.ReadByte(0));
Assert.Throws<ObjectDisposedException>(() => uma.Write(0, (byte)123));
}
using (var uma = new UnmanagedMemoryAccessor(sbuf, 0, capacity, FileAccess.Write))
{
Assert.Throws<NotSupportedException>(() => uma.ReadInt16(0));
}
using (var uma = new UnmanagedMemoryAccessor(sbuf, 0, capacity, FileAccess.Read))
{
Assert.Throws<NotSupportedException>(() => uma.Write(0, (int)123));
}
}
[Fact]
public static void UmaInvalidReadDecimal()
{
const int capacity = 16; // sizeof(decimal)
using (var buffer = new TestSafeBuffer(capacity))
using (var uma = new UnmanagedMemoryAccessor(buffer, 0, capacity, FileAccess.ReadWrite))
{
// UMA should throw when reading bad decimal values (some bits of flags are reserved and must be 0)
uma.Write(0, 0); // lo
uma.Write(4, 0); // mid
uma.Write(8, 0); // hi
uma.Write(12, -1); // flags (all bits are set, so this should raise an exception)
Assert.Throws<ArgumentException>(() => uma.ReadDecimal(0)); // Should throw same exception as decimal(int[]) ctor for compat
}
}
[Fact]
public static void UmaReadWrite()
{
const int capacity = 199;
const bool expectedBool = true; // 1
const byte expectedByte = 123; // 1
const sbyte expectedSByte = -128; // 1
const char expectedChar = (char)255; // 2
const ushort expectedUInt16 = ushort.MinValue; // 2
const short expectedInt16 = short.MaxValue - 1; // 2
const uint expectedUInt32 = 67890; // 4
const int expectedInt32 = int.MinValue; // 4
const float expectedSingle = 0.000123f; // 4
const ulong expectedUInt64 = ulong.MaxValue - 12345; // 8
const long expectedInt64 = 12345; // 8
const double expectedDouble = 1234567.890; // 8
const decimal expectedDecimal = 1.1m; //123.456m; // 16
using (var buffer = new TestSafeBuffer(capacity))
using (var uma = new UnmanagedMemoryAccessor(buffer, 0, capacity, FileAccess.ReadWrite))
{
long pos = 0;
uma.Write(pos, expectedBool);
pos += sizeof(bool);
uma.Write(pos, expectedByte);
pos += sizeof(byte);
uma.Write(pos, expectedSByte);
pos += sizeof(sbyte);
pos = EnsureAligned(pos, sizeof(char));
uma.Write(pos, expectedChar);
pos += sizeof(char);
pos = EnsureNotAligned(pos, sizeof(char));
uma.Write(pos, expectedChar);
pos += sizeof(char);
pos = EnsureAligned(pos, sizeof(ushort));
uma.Write(pos, expectedUInt16);
pos += sizeof(ushort);
pos = EnsureNotAligned(pos, sizeof(ushort));
uma.Write(pos, expectedUInt16);
pos += sizeof(ushort);
pos = EnsureAligned(pos, sizeof(short));
uma.Write(pos, expectedInt16);
pos += sizeof(short);
pos = EnsureNotAligned(pos, sizeof(short));
uma.Write(pos, expectedInt16);
pos += sizeof(short);
pos = EnsureAligned(pos, sizeof(uint));
uma.Write(pos, expectedUInt32);
pos += sizeof(uint);
pos = EnsureNotAligned(pos, sizeof(uint));
uma.Write(pos, expectedUInt32);
pos += sizeof(uint);
pos = EnsureAligned(pos, sizeof(int));
uma.Write(pos, expectedInt32);
pos += sizeof(int);
pos = EnsureNotAligned(pos, sizeof(int));
uma.Write(pos, expectedInt32);
pos += sizeof(int);
pos = EnsureAligned(pos, sizeof(float));
uma.Write(pos, expectedSingle);
pos += sizeof(float);
pos = EnsureNotAligned(pos, sizeof(float));
uma.Write(pos, expectedSingle);
pos += sizeof(float);
pos = EnsureAligned(pos, sizeof(ulong));
uma.Write(pos, expectedUInt64);
pos += sizeof(ulong);
pos = EnsureNotAligned(pos, sizeof(ulong));
uma.Write(pos, expectedUInt64);
pos += sizeof(ulong);
pos = EnsureAligned(pos, sizeof(long));
uma.Write(pos, expectedInt64);
pos += sizeof(long);
pos = EnsureNotAligned(pos, sizeof(long));
uma.Write(pos, expectedInt64);
pos += sizeof(long);
pos = EnsureAligned(pos, sizeof(double));
uma.Write(pos, expectedDouble);
pos += sizeof(double);
pos = EnsureNotAligned(pos, sizeof(double));
uma.Write(pos, expectedDouble);
pos += sizeof(double);
pos = EnsureAligned(pos, sizeof(decimal));
uma.Write(pos, expectedDecimal);
pos += sizeof(decimal);
pos = EnsureNotAligned(pos, sizeof(decimal));
uma.Write(pos, expectedDecimal);
pos = 0;
Assert.Equal(expectedBool, uma.ReadBoolean(pos));
pos += sizeof(bool);
Assert.Equal(expectedByte, uma.ReadByte(pos));
pos += sizeof(byte);
Assert.Equal(expectedSByte, uma.ReadSByte(pos));
pos += sizeof(sbyte);
pos = EnsureAligned(pos, sizeof(char));
Assert.Equal(expectedChar, uma.ReadChar(pos));
pos += sizeof(char);
pos = EnsureNotAligned(pos, sizeof(char));
Assert.Equal(expectedChar, uma.ReadChar(pos));
pos += sizeof(char);
pos = EnsureAligned(pos, sizeof(ushort));
Assert.Equal(expectedUInt16, uma.ReadUInt16(pos));
pos += sizeof(ushort);
pos = EnsureNotAligned(pos, sizeof(ushort));
Assert.Equal(expectedUInt16, uma.ReadUInt16(pos));
pos += sizeof(ushort);
pos = EnsureAligned(pos, sizeof(short));
Assert.Equal(expectedInt16, uma.ReadInt16(pos));
pos += sizeof(short);
pos = EnsureNotAligned(pos, sizeof(short));
Assert.Equal(expectedInt16, uma.ReadInt16(pos));
pos += sizeof(short);
pos = EnsureAligned(pos, sizeof(uint));
Assert.Equal(expectedUInt32, uma.ReadUInt32(pos));
pos += sizeof(uint);
pos = EnsureNotAligned(pos, sizeof(uint));
Assert.Equal(expectedUInt32, uma.ReadUInt32(pos));
pos += sizeof(uint);
pos = EnsureAligned(pos, sizeof(int));
Assert.Equal(expectedInt32, uma.ReadInt32(pos));
pos += sizeof(int);
pos = EnsureNotAligned(pos, sizeof(int));
Assert.Equal(expectedInt32, uma.ReadInt32(pos));
pos += sizeof(int);
pos = EnsureAligned(pos, sizeof(float));
Assert.Equal(expectedSingle, uma.ReadSingle(pos));
pos += sizeof(float);
pos = EnsureNotAligned(pos, sizeof(float));
Assert.Equal(expectedSingle, uma.ReadSingle(pos));
pos += sizeof(float);
pos = EnsureAligned(pos, sizeof(ulong));
Assert.Equal(expectedUInt64, uma.ReadUInt64(pos));
pos += sizeof(ulong);
pos = EnsureNotAligned(pos, sizeof(ulong));
Assert.Equal(expectedUInt64, uma.ReadUInt64(pos));
pos += sizeof(ulong);
pos = EnsureAligned(pos, sizeof(long));
Assert.Equal(expectedInt64, uma.ReadInt64(pos));
pos += sizeof(long);
pos = EnsureNotAligned(pos, sizeof(long));
Assert.Equal(expectedInt64, uma.ReadInt64(pos));
pos += sizeof(long);
pos = EnsureAligned(pos, sizeof(double));
Assert.Equal(expectedDouble, uma.ReadDouble(pos));
pos += sizeof(double);
pos = EnsureNotAligned(pos, sizeof(double));
Assert.Equal(expectedDouble, uma.ReadDouble(pos));
pos += sizeof(double);
pos = EnsureAligned(pos, sizeof(decimal));
Assert.Equal(expectedDecimal, uma.ReadDecimal(pos));
pos += sizeof(decimal);
pos = EnsureNotAligned(pos, sizeof(decimal));
Assert.Equal(expectedDecimal, uma.ReadDecimal(pos));
}
}
private static long EnsureAligned(long position, int n)
{
return ((position & (n - 1)) == 0) ? position : (position / n + 1) * n;
}
private static long EnsureNotAligned(long position, int n)
{
return ((position & (n - 1)) != 0) ? position : ++position;
}
}
}

View File

@@ -0,0 +1,149 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using Xunit;
namespace System.IO.Tests
{
public class UmsCtorTests
{
[Fact]
public static unsafe void CtorsThatFail()
{
Assert.Throws<ArgumentNullException>(() => { var ums = new UnmanagedMemoryStream(null, 0); });
TestSafeBuffer nullBuffer = null;
FakeSafeBuffer fakeBuffer = new FakeSafeBuffer(99);
Assert.Throws<ArgumentNullException>(() => new UnmanagedMemoryStream(nullBuffer, 0, 1));
Assert.Throws<ArgumentOutOfRangeException>(() => new UnmanagedMemoryStream(fakeBuffer, 2, -1));
Assert.Throws<ArgumentOutOfRangeException>(() => new UnmanagedMemoryStream(fakeBuffer, -1, 1));
Assert.Throws<ArgumentOutOfRangeException>(() => new UnmanagedMemoryStream(fakeBuffer, 1, 2, (FileAccess)(-1)));
Assert.Throws<ArgumentOutOfRangeException>(() => new UnmanagedMemoryStream(fakeBuffer, 1, 2, (FileAccess)42));
Assert.Throws<ArgumentException>(() => new UnmanagedMemoryStream(fakeBuffer, 2, 999));
Assert.Throws<ArgumentException>(() => new UnmanagedMemoryStream(fakeBuffer, 999, 9));
Assert.Throws<ArgumentException>(() => new UnmanagedMemoryStream(fakeBuffer, 1, 100));
Assert.Throws<ArgumentException>(() => new UnmanagedMemoryStream(fakeBuffer, Int32.MaxValue, 1));
}
[Fact]
public static unsafe void PointerCtor()
{
int someInt32 = 42;
int* pInt32 = &someInt32;
byte* pByte = (byte*)pInt32;
using (var stream = new UnmanagedMemoryStream(pByte, 0))
{
Assert.True(stream.CanRead);
Assert.True(stream.CanSeek);
Assert.False(stream.CanWrite);
Assert.False(stream.CanTimeout);
Assert.Equal(0, stream.Length);
Assert.Equal(0, stream.Capacity);
Assert.Equal(0, stream.Position);
Assert.Throws<InvalidOperationException>(() => stream.ReadTimeout);
Assert.Throws<InvalidOperationException>(() => stream.ReadTimeout = 42);
Assert.Throws<InvalidOperationException>(() => stream.WriteTimeout);
Assert.Throws<InvalidOperationException>(() => stream.WriteTimeout = 42);
Assert.True(stream.PositionPointer == pByte);
}
using (var stream = new DerivedUnmanagedMemoryStream())
{
Assert.False(stream.CanRead);
Assert.False(stream.CanSeek);
Assert.False(stream.CanWrite);
Assert.False(stream.CanTimeout);
Assert.Throws<ArgumentOutOfRangeException>(() => stream.Initialize(pByte, -1, 4, FileAccess.Read));
Assert.Throws<ArgumentOutOfRangeException>(() => stream.Initialize(pByte, 1, -4, FileAccess.Read));
Assert.Throws<ArgumentOutOfRangeException>(() => stream.Initialize(pByte, 5, 4, FileAccess.Read));
Assert.Throws<ArgumentOutOfRangeException>(() => stream.Initialize(pByte, 1, 4, (FileAccess)12345));
stream.Initialize(pByte, 1, 4, FileAccess.ReadWrite);
Assert.Throws<InvalidOperationException>(() => stream.Initialize(pByte, 1, 4, FileAccess.ReadWrite));
Assert.True(stream.CanRead);
Assert.True(stream.CanSeek);
Assert.True(stream.CanWrite);
Assert.Equal(1, stream.Length);
Assert.Equal(4, stream.Capacity);
Assert.Equal(0, stream.Position);
Assert.Throws<InvalidOperationException>(() => stream.ReadTimeout);
Assert.Throws<InvalidOperationException>(() => stream.ReadTimeout = 42);
Assert.Throws<InvalidOperationException>(() => stream.WriteTimeout);
Assert.Throws<InvalidOperationException>(() => stream.WriteTimeout = 42);
Assert.True(stream.PositionPointer == pByte);
}
}
[Fact]
public static unsafe void BufferCtor()
{
const int length = 99;
using (FakeSafeBuffer buffer = new FakeSafeBuffer(length))
using (var stream = new DerivedUnmanagedMemoryStream(buffer, length, FileAccess.Read))
{
Assert.True(stream.CanRead);
Assert.True(stream.CanSeek);
Assert.False(stream.CanWrite);
Assert.Equal(length, stream.Length);
Assert.Equal(length, stream.Capacity);
Assert.Equal(0, stream.Position);
}
using (FakeSafeBuffer buffer = new FakeSafeBuffer(length))
using (var stream = new DerivedUnmanagedMemoryStream())
{
Assert.False(stream.CanRead);
Assert.False(stream.CanSeek);
Assert.False(stream.CanWrite);
stream.Initialize(buffer, 0, length, FileAccess.Write);
Assert.Throws<InvalidOperationException>(() => stream.Initialize(buffer, 0, length, FileAccess.Write));
Assert.False(stream.CanRead);
Assert.True(stream.CanSeek);
Assert.True(stream.CanWrite);
Assert.Equal(length, stream.Length);
Assert.Equal(length, stream.Capacity);
Assert.Equal(0, stream.Position);
Assert.Throws<NotSupportedException>(() => stream.SetLength(1));
}
}
}
// Derived class used to exercise protected members and to test behaviors before and after initialization
internal sealed unsafe class DerivedUnmanagedMemoryStream : UnmanagedMemoryStream
{
internal DerivedUnmanagedMemoryStream()
{
}
internal DerivedUnmanagedMemoryStream(FakeSafeBuffer buffer, long length, FileAccess access) : base(buffer, 0, length, access)
{
}
internal new void Initialize(byte* pointer, long length, long capacity, FileAccess access)
{
base.Initialize(pointer, length, capacity, access);
}
internal void Initialize(FakeSafeBuffer buffer, long offset, long capacity, FileAccess access)
{
base.Initialize(buffer, offset, capacity, access);
}
}
}

View File

@@ -0,0 +1,37 @@
// Licensed to the .NET Foundation under one or more agreements.
// 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.Threading;
using System.Threading.Tasks;
using Xunit;
namespace System.IO.Tests
{
public class FlushTests
{
[Fact]
public static void Flush()
{
const int length = 1000;
using (var manager = new UmsManager(FileAccess.Write, length))
{
UnmanagedMemoryStream stream = manager.Stream;
stream.Flush();
Assert.True(stream.FlushAsync(new CancellationToken(true)).IsCanceled);
Assert.True(stream.FlushAsync().Status == TaskStatus.RanToCompletion);
}
using (var stream = new DerivedUnmanagedMemoryStream())
{
Assert.Throws<ObjectDisposedException>(() => stream.Flush());
Task t = stream.FlushAsync();
Assert.True(t.IsFaulted);
Assert.IsType<ObjectDisposedException>(t.Exception.InnerException);
}
}
}
}

View File

@@ -0,0 +1,89 @@
// Licensed to the .NET Foundation under one or more agreements.
// 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.Runtime.InteropServices;
using Xunit;
namespace System.IO.Tests
{
// this class holds managed native memory used by UnmanagedMemoryStream
internal class UmsManager : IDisposable
{
private readonly UnmanagedMemoryStream _stream;
private IntPtr _memory;
private bool _isDisposed;
public unsafe UmsManager(FileAccess access, int capacity)
{
int memorySizeInBytes = capacity;
_memory = Marshal.AllocHGlobal(memorySizeInBytes);
byte* bytes = (byte*)_memory.ToPointer();
byte* currentByte = bytes;
for (int index = 0; index < memorySizeInBytes; index++)
{
*currentByte = 0;
currentByte++;
}
_stream = new UnmanagedMemoryStream(bytes, memorySizeInBytes, memorySizeInBytes, access);
}
public unsafe UmsManager(FileAccess access, byte[] seedData)
{
int memorySizeInBytes = seedData.Length;
_memory = Marshal.AllocHGlobal(memorySizeInBytes);
byte* destination = (byte*)_memory.ToPointer();
fixed (byte* source = seedData)
{
Buffer.MemoryCopy(source, destination, memorySizeInBytes, memorySizeInBytes);
}
_stream = new UnmanagedMemoryStream(destination, memorySizeInBytes, memorySizeInBytes, access);
}
public UnmanagedMemoryStream Stream
{
get
{
return _stream;
}
}
public byte[] ToArray()
{
long count = _stream.Length;
var array = new byte[count];
unsafe
{
byte* source = (byte*)_memory.ToPointer();
fixed (byte* destination = array)
{
Buffer.MemoryCopy(source, destination, array.Length, count);
}
}
return array;
}
#region IDisposable
protected void Dispose(bool disposing)
{
if (disposing)
{
_stream.Dispose();
}
Marshal.FreeHGlobal(_memory);
}
public void Dispose()
{
Dispose(true);
_isDisposed = true;
}
~UmsManager()
{
Assert.True(_isDisposed); // please dispose the context
}
#endregion
}
}

Some files were not shown because too many files have changed in this diff Show More