From b2874023c9adb13ff41fae6fc3e6ee354d534709 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Strehovsk=C3=BD?= Date: Mon, 19 Mar 2018 07:55:04 +0100 Subject: [PATCH] Add more test coverage for System.Reflection.Pointer (#28143) * Test that null object reference is properly converted to a null pointer value (this requires special handling) * Test `Delegate.DynamicInvoke`. This one also uses `System.Reflection.Pointer` and on .NET Native is handled by separate code paths. --- .../tests/System/Reflection/PointerTests.cs | 78 +++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/src/System.Runtime/tests/System/Reflection/PointerTests.cs b/src/System.Runtime/tests/System/Reflection/PointerTests.cs index 5856e745c3e0..68f26b7673c9 100644 --- a/src/System.Runtime/tests/System/Reflection/PointerTests.cs +++ b/src/System.Runtime/tests/System/Reflection/PointerTests.cs @@ -23,6 +23,10 @@ public void Method(byte* ptr, int expected) } } + unsafe delegate void MethodDelegate(byte* ptr, int expected); + + unsafe delegate bool* ReturnDelegate(int expected); + public unsafe class PointerTests { [Fact] @@ -90,6 +94,16 @@ public void PointerFieldSetValue(int value) Assert.Equal(value, unchecked((int)obj.field)); } + [Fact] + [SkipOnTargetFramework(TargetFrameworkMonikers.UapAot, "Bug that will be fixed along with the rest of UAPAOT issues here")] + public void PointerFieldSetNullValue() + { + var obj = new PointerHolder(); + FieldInfo field = typeof(PointerHolder).GetField("field"); + field.SetValue(obj, null); + Assert.Equal(0, unchecked((int)obj.field)); + } + [Theory] [MemberData(nameof(Pointers))] public void IntPtrFieldSetValue(int value) @@ -184,6 +198,15 @@ public void PointerMethodParameter(int value) method.Invoke(obj, new[] { Pointer.Box(unchecked((void*)value), typeof(byte*)), value }); } + [Fact] + [SkipOnTargetFramework(TargetFrameworkMonikers.UapAot, "Pointers through Invoke not implemented: https://github.com/dotnet/corert/issues/2113")] + public void PointerNullMethodParameter() + { + var obj = new PointerHolder(); + MethodInfo method = typeof(PointerHolder).GetMethod("Method"); + method.Invoke(obj, new object[] { null, 0 }); + } + [Theory] [MemberData(nameof(Pointers))] [SkipOnTargetFramework(TargetFrameworkMonikers.UapAot, "Pointers through Invoke not implemented: https://github.com/dotnet/corert/issues/2113")] @@ -219,5 +242,60 @@ public void PointerMethodReturn(int value) void* actualPointer = Pointer.Unbox(actualValue); Assert.Equal(value, unchecked((int)actualPointer)); } + + [Theory] + [MemberData(nameof(Pointers))] + [SkipOnTargetFramework(TargetFrameworkMonikers.UapAot, "Pointers through Invoke not implemented: https://github.com/dotnet/corert/issues/2113")] + public void PointerMethodDelegateParameter(int value) + { + var obj = new PointerHolder(); + MethodDelegate d = obj.Method; + d.DynamicInvoke(Pointer.Box(unchecked((void*)value), typeof(byte*)), value); + } + + [Fact] + [SkipOnTargetFramework(TargetFrameworkMonikers.UapAot, "Pointers through Invoke not implemented: https://github.com/dotnet/corert/issues/2113")] + public void PointerNullMethodDelegateParameter() + { + var obj = new PointerHolder(); + MethodDelegate d = obj.Method; + d.DynamicInvoke(null, 0); + } + + [Theory] + [MemberData(nameof(Pointers))] + [SkipOnTargetFramework(TargetFrameworkMonikers.UapAot, "Pointers through Invoke not implemented: https://github.com/dotnet/corert/issues/2113")] + public void IntPtrMethodDelegateParameter(int value) + { + var obj = new PointerHolder(); + MethodDelegate d = obj.Method; + d.DynamicInvoke((IntPtr)value, value); + } + + [Theory] + [MemberData(nameof(Pointers))] + [SkipOnTargetFramework(TargetFrameworkMonikers.UapAot, "Pointers through Invoke not implemented: https://github.com/dotnet/corert/issues/2113")] + public void PointerMethodDelegateParameter_InvalidType(int value) + { + var obj = new PointerHolder(); + MethodDelegate d = obj.Method; + AssertExtensions.Throws(null, () => + { + d.DynamicInvoke(Pointer.Box(unchecked((void*)value), typeof(long*)), value); + }); + } + + [Theory] + [MemberData(nameof(Pointers))] + [SkipOnTargetFramework(TargetFrameworkMonikers.UapAot, "Pointers through Invoke not implemented: https://github.com/dotnet/corert/issues/2113")] + public void PointerMethodDelegateReturn(int value) + { + var obj = new PointerHolder(); + ReturnDelegate d = obj.Return; + object actualValue = d.DynamicInvoke(value); + Assert.IsType(actualValue); + void* actualPointer = Pointer.Unbox(actualValue); + Assert.Equal(value, unchecked((int)actualPointer)); + } } }