-
Notifications
You must be signed in to change notification settings - Fork 198
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Boolean and Delegate tests Updated to DNNE 1.0.12 with support for C# function pointers. Renamed CBoolMarshaller to ByteBoolMarshaller. Added integration tests for all bool marshaller types. Added integration tests for delegates. Style nits.
- Loading branch information
1 parent
778ecfa
commit 6c980df
Showing
10 changed files
with
301 additions
and
44 deletions.
There are no files selected for viewing
140 changes: 140 additions & 0 deletions
140
DllImportGenerator/DllImportGenerator.IntegrationTests/BooleanTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
using System.Collections.Generic; | ||
using System.Runtime.InteropServices; | ||
|
||
using Xunit; | ||
|
||
namespace DllImportGenerator.IntegrationTests | ||
{ | ||
partial class NativeExportsNE | ||
{ | ||
[GeneratedDllImport(nameof(NativeExportsNE), EntryPoint = "bytebool_return_as_uint")] | ||
public static partial uint ReturnByteBoolAsUInt([MarshalAs(UnmanagedType.U1)] bool input); | ||
|
||
[GeneratedDllImport(nameof(NativeExportsNE), EntryPoint = "bytebool_return_as_uint")] | ||
public static partial uint ReturnSByteBoolAsUInt([MarshalAs(UnmanagedType.I1)] bool input); | ||
|
||
[GeneratedDllImport(nameof(NativeExportsNE), EntryPoint = "variantbool_return_as_uint")] | ||
public static partial uint ReturnVariantBoolAsUInt([MarshalAs(UnmanagedType.VariantBool)] bool input); | ||
|
||
[GeneratedDllImport(nameof(NativeExportsNE), EntryPoint = "bool_return_as_uint")] | ||
public static partial uint ReturnIntBoolAsUInt([MarshalAs(UnmanagedType.I4)] bool input); | ||
|
||
[GeneratedDllImport(nameof(NativeExportsNE), EntryPoint = "bool_return_as_uint")] | ||
public static partial uint ReturnUIntBoolAsUInt([MarshalAs(UnmanagedType.U4)] bool input); | ||
|
||
[GeneratedDllImport(nameof(NativeExportsNE), EntryPoint = "bool_return_as_uint")] | ||
public static partial uint ReturnWinBoolAsUInt([MarshalAs(UnmanagedType.Bool)] bool input); | ||
|
||
[GeneratedDllImport(nameof(NativeExportsNE), EntryPoint = "bool_return_as_uint")] | ||
[return: MarshalAs(UnmanagedType.U1)] | ||
public static partial bool ReturnUIntAsByteBool(uint input); | ||
|
||
[GeneratedDllImport(nameof(NativeExportsNE), EntryPoint = "bool_return_as_uint")] | ||
[return: MarshalAs(UnmanagedType.VariantBool)] | ||
public static partial bool ReturnUIntAsVariantBool(uint input); | ||
|
||
[GeneratedDllImport(nameof(NativeExportsNE), EntryPoint = "bool_return_as_uint")] | ||
[return: MarshalAs(UnmanagedType.Bool)] | ||
public static partial bool ReturnUIntAsWinBool(uint input); | ||
|
||
[GeneratedDllImport(nameof(NativeExportsNE), EntryPoint = "bool_return_as_refuint")] | ||
public static partial void ReturnUIntAsRefByteBool(uint input, [MarshalAs(UnmanagedType.U1)] ref bool res); | ||
|
||
[GeneratedDllImport(nameof(NativeExportsNE), EntryPoint = "bool_return_as_refuint")] | ||
public static partial void ReturnUIntAsOutByteBool(uint input, [MarshalAs(UnmanagedType.U1)] out bool res); | ||
|
||
[GeneratedDllImport(nameof(NativeExportsNE), EntryPoint = "bool_return_as_refuint")] | ||
public static partial void ReturnUIntAsRefVariantBool(uint input, [MarshalAs(UnmanagedType.VariantBool)] ref bool res); | ||
|
||
[GeneratedDllImport(nameof(NativeExportsNE), EntryPoint = "bool_return_as_refuint")] | ||
public static partial void ReturnUIntAsOutVariantBool(uint input, [MarshalAs(UnmanagedType.VariantBool)] out bool res); | ||
|
||
[GeneratedDllImport(nameof(NativeExportsNE), EntryPoint = "bool_return_as_refuint")] | ||
public static partial void ReturnUIntAsRefWinBool(uint input, [MarshalAs(UnmanagedType.Bool)] ref bool res); | ||
|
||
[GeneratedDllImport(nameof(NativeExportsNE), EntryPoint = "bool_return_as_refuint")] | ||
public static partial void ReturnUIntAsOutWinBool(uint input, [MarshalAs(UnmanagedType.Bool)] out bool res); | ||
} | ||
|
||
public class BooleanTests | ||
{ | ||
// See definition of Windows' VARIANT_BOOL | ||
const ushort VARIANT_TRUE = unchecked((ushort)-1); | ||
const ushort VARIANT_FALSE = 0; | ||
|
||
[Fact] | ||
public void ValidateBoolIsMarshalledAsExpected() | ||
{ | ||
Assert.Equal((uint)1, NativeExportsNE.ReturnByteBoolAsUInt(true)); | ||
Assert.Equal((uint)0, NativeExportsNE.ReturnByteBoolAsUInt(false)); | ||
Assert.Equal((uint)1, NativeExportsNE.ReturnSByteBoolAsUInt(true)); | ||
Assert.Equal((uint)0, NativeExportsNE.ReturnSByteBoolAsUInt(false)); | ||
Assert.Equal(VARIANT_TRUE, NativeExportsNE.ReturnVariantBoolAsUInt(true)); | ||
Assert.Equal(VARIANT_FALSE, NativeExportsNE.ReturnVariantBoolAsUInt(false)); | ||
Assert.Equal((uint)1, NativeExportsNE.ReturnIntBoolAsUInt(true)); | ||
Assert.Equal((uint)0, NativeExportsNE.ReturnIntBoolAsUInt(false)); | ||
Assert.Equal((uint)1, NativeExportsNE.ReturnUIntBoolAsUInt(true)); | ||
Assert.Equal((uint)0, NativeExportsNE.ReturnUIntBoolAsUInt(false)); | ||
Assert.Equal((uint)1, NativeExportsNE.ReturnWinBoolAsUInt(true)); | ||
Assert.Equal((uint)0, NativeExportsNE.ReturnWinBoolAsUInt(false)); | ||
} | ||
|
||
[Theory] | ||
[InlineData(new object[] { 0, false })] | ||
[InlineData(new object[] { 1, true })] | ||
[InlineData(new object[] { 37, true })] | ||
[InlineData(new object[] { 0xff, true })] | ||
[InlineData(new object[] { 0xffffff00, false })] | ||
public void ValidateByteBoolReturns(uint value, bool expected) | ||
{ | ||
Assert.Equal(expected, NativeExportsNE.ReturnUIntAsByteBool(value)); | ||
|
||
bool result = !expected; | ||
NativeExportsNE.ReturnUIntAsRefByteBool(value, ref result); | ||
Assert.Equal(expected, result); | ||
|
||
result = !expected; | ||
NativeExportsNE.ReturnUIntAsOutByteBool(value, out result); | ||
Assert.Equal(expected, result); | ||
} | ||
|
||
[Theory] | ||
[InlineData(new object[] { 0, false })] | ||
[InlineData(new object[] { 1, false })] | ||
[InlineData(new object[] { 0xff, false })] | ||
[InlineData(new object[] { VARIANT_TRUE, true })] | ||
[InlineData(new object[] { 0xffffffff, true })] | ||
[InlineData(new object[] { 0xffff0000, false })] | ||
public void ValidateVariantBoolReturns(uint value, bool expected) | ||
{ | ||
Assert.Equal(expected, NativeExportsNE.ReturnUIntAsVariantBool(value)); | ||
|
||
bool result = !expected; | ||
NativeExportsNE.ReturnUIntAsRefVariantBool(value, ref result); | ||
Assert.Equal(expected, result); | ||
|
||
result = !expected; | ||
NativeExportsNE.ReturnUIntAsOutVariantBool(value, out result); | ||
Assert.Equal(expected, result); | ||
} | ||
|
||
[Theory] | ||
[InlineData(new object[] { 0, false })] | ||
[InlineData(new object[] { 1, true})] | ||
[InlineData(new object[] { 37, true })] | ||
[InlineData(new object[] { 0xffffffff, true })] | ||
[InlineData(new object[] { 0x80000000, true })] | ||
public void ValidateWinBoolReturns(uint value, bool expected) | ||
{ | ||
Assert.Equal(expected, NativeExportsNE.ReturnUIntAsWinBool(value)); | ||
|
||
bool result = !expected; | ||
NativeExportsNE.ReturnUIntAsRefWinBool(value, ref result); | ||
Assert.Equal(expected, result); | ||
|
||
result = !expected; | ||
NativeExportsNE.ReturnUIntAsOutWinBool(value, out result); | ||
Assert.Equal(expected, result); | ||
} | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
DllImportGenerator/DllImportGenerator.IntegrationTests/DelegateTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
using System.Runtime.InteropServices; | ||
|
||
using Xunit; | ||
|
||
namespace DllImportGenerator.IntegrationTests | ||
{ | ||
partial class NativeExportsNE | ||
{ | ||
public delegate void VoidVoid(); | ||
|
||
[GeneratedDllImport(nameof(NativeExportsNE), EntryPoint = "invoke_callback_after_gc")] | ||
public static partial void InvokeAfterGC(VoidVoid cb); | ||
|
||
public delegate int IntIntInt(int a, int b); | ||
|
||
[GeneratedDllImport(nameof(NativeExportsNE), EntryPoint = "invoke_callback_blittable_args")] | ||
public static partial int InvokeWithBlittableArgument(IntIntInt cb, int a, int b); | ||
} | ||
|
||
public class DelegateTests | ||
{ | ||
[Fact] | ||
public void DelegateIsKeptAliveDuringCall() | ||
{ | ||
bool wasCalled = false; | ||
NativeExportsNE.InvokeAfterGC(new NativeExportsNE.VoidVoid(Callback)); | ||
Assert.True(wasCalled); | ||
|
||
void Callback() | ||
{ | ||
wasCalled = true; | ||
} | ||
} | ||
|
||
[Fact] | ||
public void DelegateIsCalledWithArgumentsInOrder() | ||
{ | ||
const int a = 100; | ||
const int b = 50; | ||
int result; | ||
|
||
result = NativeExportsNE.InvokeWithBlittableArgument(new NativeExportsNE.IntIntInt(Callback), a, b); | ||
Assert.Equal(Callback(a, b), result); | ||
|
||
result = NativeExportsNE.InvokeWithBlittableArgument(new NativeExportsNE.IntIntInt(Callback), b, a); | ||
Assert.Equal(Callback(b, a), result); | ||
|
||
static int Callback(int a, int b) | ||
{ | ||
// Use a noncommutative operation to validate passed in order. | ||
return a - b; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 2 additions & 3 deletions
5
DllImportGenerator/DllImportGenerator/Marshalling/DelegateMarshaller.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using System.Runtime.InteropServices; | ||
|
||
namespace NativeExports | ||
{ | ||
public static unsafe class Booleans | ||
{ | ||
[UnmanagedCallersOnly(EntryPoint = "bytebool_return_as_uint")] | ||
public static uint ReturnByteAsUInt(byte input) | ||
{ | ||
return input; | ||
} | ||
|
||
[UnmanagedCallersOnly(EntryPoint = "variantbool_return_as_uint")] | ||
public static uint ReturnUShortAsUInt(ushort input) | ||
{ | ||
return input; | ||
} | ||
|
||
[UnmanagedCallersOnly(EntryPoint = "bool_return_as_uint")] | ||
public static uint ReturnUIntAsUInt(uint input) | ||
{ | ||
return input; | ||
} | ||
|
||
[UnmanagedCallersOnly(EntryPoint = "bool_return_as_refuint")] | ||
public static void ReturnUIntAsRefUInt(uint input, uint* res) | ||
{ | ||
*res = input; | ||
} | ||
} | ||
} |
Oops, something went wrong.