diff options
author | Yi Zhang (CLR) <yizhang82@users.noreply.github.com> | 2017-04-29 09:51:03 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-04-29 09:51:03 -0700 |
commit | 88779dc83763dc199ad1666282316d393473609d (patch) | |
tree | 613150640bd23f808063a55486dfdcc594e52ec0 /tests | |
parent | bd0a833845cc334eaf06d95329d00f9c4cb067b2 (diff) | |
download | coreclr-88779dc83763dc199ad1666282316d393473609d.tar.gz coreclr-88779dc83763dc199ad1666282316d393473609d.tar.bz2 coreclr-88779dc83763dc199ad1666282316d393473609d.zip |
Bring back Marshal.Read/Write overloads that takes object (#11251)
* Bring back desktop version of Marshal.Read/Write APIs that takes object and add a test
Diffstat (limited to 'tests')
-rw-r--r-- | tests/src/Interop/MarshalAPI/ReadWrite/ReadWriteObject.cs | 116 | ||||
-rw-r--r-- | tests/src/Interop/MarshalAPI/ReadWrite/ReadWriteObject.csproj | 40 |
2 files changed, 156 insertions, 0 deletions
diff --git a/tests/src/Interop/MarshalAPI/ReadWrite/ReadWriteObject.cs b/tests/src/Interop/MarshalAPI/ReadWrite/ReadWriteObject.cs new file mode 100644 index 0000000000..a11c10b01b --- /dev/null +++ b/tests/src/Interop/MarshalAPI/ReadWrite/ReadWriteObject.cs @@ -0,0 +1,116 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.InteropServices; +using System.Text; +using System.Threading.Tasks; + +using CoreFXTestLibrary; + +internal struct BlittableStruct +{ + internal int a; + internal int b; + internal byte c; + internal short d; + internal IntPtr p; +} + +internal struct StructWithReferenceTypes +{ + internal IntPtr ptr; + internal string str; + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)] + internal int[] byValArr; +} + +class Test +{ + static int Main(string[] args) + { + TestNegativeCases(); + TestBlittableStruct(); + TestStructWithReferenceType(); + + return 100; + } + + static void TestNegativeCases() + { + Assert.Throws<ArgumentNullException>(() => { Marshal.WriteByte(null, 0, 0); }); + Assert.Throws<ArgumentNullException>(() => { Marshal.WriteInt16(null, 0, 0); }); + Assert.Throws<ArgumentNullException>(() => { Marshal.WriteInt32(null, 0, 0); }); + Assert.Throws<ArgumentNullException>(() => { Marshal.WriteInt64(null, 0, 0); }); + Assert.Throws<ArgumentNullException>(() => { Marshal.WriteIntPtr(null, 0, IntPtr.Zero); }); + Assert.Throws<ArgumentNullException>(() => { Marshal.ReadByte(null, 0); }); + Assert.Throws<ArgumentNullException>(() => { Marshal.ReadInt16(null, 0); }); + Assert.Throws<ArgumentNullException>(() => { Marshal.ReadInt32(null, 0); }); + Assert.Throws<ArgumentNullException>(() => { Marshal.ReadIntPtr(null, 0); }); + } + + static void TestBlittableStruct() + { + Console.WriteLine("TestBlittableStruct"); + + BlittableStruct blittableStruct = new BlittableStruct(); + blittableStruct.a = 200; + blittableStruct.b = 300; + blittableStruct.c = 10; + blittableStruct.d = 123; + blittableStruct.p = new IntPtr(100); + + object boxedBlittableStruct = (object)blittableStruct; + + int offsetOfB = Marshal.OffsetOf<BlittableStruct>("b").ToInt32(); + int offsetOfC = Marshal.OffsetOf<BlittableStruct>("c").ToInt32(); + int offsetOfD = Marshal.OffsetOf<BlittableStruct>("d").ToInt32(); + int offsetOfP = Marshal.OffsetOf<BlittableStruct>("p").ToInt32(); + + Assert.AreEqual(Marshal.ReadInt32(boxedBlittableStruct, 0), 200); + Assert.AreEqual(Marshal.ReadInt32(boxedBlittableStruct, offsetOfB), 300); + Assert.AreEqual(Marshal.ReadByte(boxedBlittableStruct, offsetOfC), 10); + Assert.AreEqual(Marshal.ReadInt16(boxedBlittableStruct, offsetOfD), 123); + Assert.AreEqual(Marshal.ReadIntPtr(boxedBlittableStruct, offsetOfP), new IntPtr(100)); + + Marshal.WriteInt32(boxedBlittableStruct, 0, 300); + Marshal.WriteInt32(boxedBlittableStruct, offsetOfB, 400); + Marshal.WriteByte(boxedBlittableStruct, offsetOfC, 20); + Marshal.WriteInt16(boxedBlittableStruct, offsetOfD, 144); + + Marshal.WriteIntPtr(boxedBlittableStruct, offsetOfP, new IntPtr(500)); + + Assert.AreEqual(((BlittableStruct)boxedBlittableStruct).a, 300); + Assert.AreEqual(((BlittableStruct)boxedBlittableStruct).b, 400); + Assert.AreEqual(((BlittableStruct)boxedBlittableStruct).c, 20); + Assert.AreEqual(((BlittableStruct)boxedBlittableStruct).d, 144); + Assert.AreEqual(((BlittableStruct)boxedBlittableStruct).p, new IntPtr(500)); + } + + static void TestStructWithReferenceType() + { + Console.WriteLine("TestStructWithReferenceType"); + + StructWithReferenceTypes structWithReferenceTypes = new StructWithReferenceTypes(); + structWithReferenceTypes.ptr = new IntPtr(100); + structWithReferenceTypes.str = "ABC"; + structWithReferenceTypes.byValArr = new int[10] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; + + object boxedStruct = (object)structWithReferenceTypes; + + int offsetOfStr = Marshal.OffsetOf<StructWithReferenceTypes>("str").ToInt32(); + int offsetOfByValArr = Marshal.OffsetOf<StructWithReferenceTypes>("byValArr").ToInt32(); + + Assert.AreEqual(Marshal.ReadInt32(boxedStruct, 0), 100); + Assert.AreNotEqual(Marshal.ReadIntPtr(boxedStruct, offsetOfStr), IntPtr.Zero); + Assert.AreEqual(Marshal.ReadInt32(boxedStruct, offsetOfByValArr + sizeof(int) * 2), 3); + + Marshal.WriteInt32(boxedStruct, 0, 200); + Marshal.WriteInt32(boxedStruct, offsetOfByValArr + sizeof(int) * 9, 100); + + Assert.AreEqual(((StructWithReferenceTypes)boxedStruct).ptr, new IntPtr(200)); + Assert.AreEqual(((StructWithReferenceTypes)boxedStruct).byValArr[9], 100); + Assert.AreEqual(((StructWithReferenceTypes)boxedStruct).byValArr[8], 9); + Assert.AreEqual(((StructWithReferenceTypes)boxedStruct).str, "ABC"); + } +} + diff --git a/tests/src/Interop/MarshalAPI/ReadWrite/ReadWriteObject.csproj b/tests/src/Interop/MarshalAPI/ReadWrite/ReadWriteObject.csproj new file mode 100644 index 0000000000..14fcd2c97d --- /dev/null +++ b/tests/src/Interop/MarshalAPI/ReadWrite/ReadWriteObject.csproj @@ -0,0 +1,40 @@ +<?xml version="1.0" encoding="utf-8"?> +<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> + <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.props))\dir.props" /> + <PropertyGroup> + <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> + <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> + <AssemblyName>ReadWriteObject</AssemblyName> + <SchemaVersion>2.0</SchemaVersion> + <ProjectGuid>{F1E66554-8C8E-4141-85CF-D0CD6A0CD0B0}</ProjectGuid> + <OutputType>Exe</OutputType> + <ProjectTypeGuids>{786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids> + <SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\</SolutionDir> + + <DefineConstants>$(DefineConstants);STATIC</DefineConstants> + </PropertyGroup> + <!-- Default configurations to help VS understand the configurations --> + <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'"> + </PropertyGroup> + <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'"> + </PropertyGroup> + <ItemGroup> + <CodeAnalysisDependentAssemblyPaths Condition=" '$(VS100COMNTOOLS)' != '' " Include="$(VS100COMNTOOLS)..\IDE\PrivateAssemblies"> + <Visible>False</Visible> + </CodeAnalysisDependentAssemblyPaths> + </ItemGroup> + <ItemGroup> + <Compile Include="ReadWriteObject.cs" /> + <Compile Include="..\..\common\Assertion.cs" /> + </ItemGroup> + <ItemGroup> + <Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" /> + </ItemGroup> + <ItemGroup> + <ProjectReference Include="..\..\..\Common\CoreCLRTestLibrary\CoreCLRTestLibrary.csproj"> + <Project>{c8c0dc74-fac4-45b1-81fe-70c4808366e0}</Project> + <Name>CoreCLRTestLibrary</Name> + </ProjectReference> + </ItemGroup> + <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" /> +</Project> |