Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update integration tests
  • Loading branch information
elinor-fung committed Jun 22, 2022
commit c22961d34541c4de2dcdb4503876c4eee295951e
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public partial class Arrays

[LibraryImport(NativeExportsNE_Binary, EntryPoint = "and_all_members")]
[return:MarshalAs(UnmanagedType.U1)]
public static partial bool AndAllMembers(BoolStruct[] pArray, int length);
public static partial bool AndAllMembers(BoolStruct_V1[] pArray, int length);

[LibraryImport(NativeExportsNE_Binary, EntryPoint = "transpose_matrix")]
[return: MarshalUsing(CountElementName = "numColumns")]
Expand Down Expand Up @@ -400,19 +400,19 @@ public void ArrayWithSimpleNonBlittableTypeMarshalling(bool result)
{
var boolValues = new[]
{
new BoolStruct
new BoolStruct_V1
{
b1 = true,
b2 = true,
b3 = true,
},
new BoolStruct
new BoolStruct_V1
{
b1 = true,
b2 = true,
b3 = true,
},
new BoolStruct
new BoolStruct_V1
{
b1 = true,
b2 = true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public static partial void ReverseStrings_Out(

[LibraryImport(NativeExportsNE_Binary, EntryPoint = "and_all_members")]
[return: MarshalAs(UnmanagedType.U1)]
public static partial bool AndAllMembers([MarshalUsing(typeof(ListMarshaller<BoolStruct>))] List<BoolStruct> pArray, int length);
public static partial bool AndAllMembers([MarshalUsing(typeof(ListMarshaller<BoolStruct_V1>))] List<BoolStruct_V1> pArray, int length);
}
}

Expand Down Expand Up @@ -208,21 +208,21 @@ public void ConstantSizeCollection()
[InlineData(false)]
public void CollectionWithSimpleNonBlittableTypeMarshalling(bool result)
{
var boolValues = new List<BoolStruct>
var boolValues = new List<BoolStruct_V1>
{
new BoolStruct
new BoolStruct_V1
{
b1 = true,
b2 = true,
b3 = true,
},
new BoolStruct
new BoolStruct_V1
{
b1 = true,
b2 = true,
b3 = true,
},
new BoolStruct
new BoolStruct_V1
{
b1 = true,
b2 = true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,53 @@ namespace LibraryImportGenerator.IntegrationTests
{
partial class NativeExportsNE
{
[LibraryImport(NativeExportsNE_Binary, EntryPoint = "stringcontainer_deepduplicate")]
public static partial void DeepDuplicateStrings(StringContainer strings, out StringContainer pStringsOut);
internal partial class V1
{
[LibraryImport(NativeExportsNE_Binary, EntryPoint = "stringcontainer_deepduplicate")]
public static partial void DeepDuplicateStrings(StringContainer_V1 strings, out StringContainer_V1 pStringsOut);

[LibraryImport(NativeExportsNE_Binary, EntryPoint = "stringcontainer_reverse_strings")]
public static partial void ReverseStrings(ref StringContainer_V1 strings);

[LibraryImport(NativeExportsNE_Binary, EntryPoint = "get_long_bytes_as_double")]
public static partial double GetLongBytesAsDouble([MarshalUsing(typeof(DoubleToLongMarshaller_V1))] double d);

[LibraryImport(NativeExportsNE_Binary, EntryPoint = "negate_bools")]
public static partial void NegateBools(
BoolStruct_V1 boolStruct,
out BoolStruct_V1 pBoolStructOut);

[LibraryImport(NativeExportsNE_Binary, EntryPoint = "and_bools_ref")]
[return: MarshalAs(UnmanagedType.U1)]
public static partial bool AndBoolsRef(in BoolStruct_V1 boolStruct);

[LibraryImport(NativeExportsNE_Binary, EntryPoint = "double_int_ref")]
public static partial IntWrapper_V1 DoubleIntRef(IntWrapper_V1 pInt);
}

[LibraryImport(NativeExportsNE_Binary, EntryPoint = "stringcontainer_reverse_strings")]
public static partial void ReverseStrings(ref StringContainer strings);
internal partial class Stateless
{
[LibraryImport(NativeExportsNE_Binary, EntryPoint = "stringcontainer_deepduplicate")]
public static partial void DeepDuplicateStrings(StringContainer strings, out StringContainer pStringsOut);

[LibraryImport(NativeExportsNE_Binary, EntryPoint = "stringcontainer_reverse_strings")]
public static partial void ReverseStrings(ref StringContainer strings);

[LibraryImport(NativeExportsNE_Binary, EntryPoint = "get_long_bytes_as_double")]
public static partial double GetLongBytesAsDouble([MarshalUsing(typeof(DoubleToLongMarshaler))] double d);
[LibraryImport(NativeExportsNE_Binary, EntryPoint = "get_long_bytes_as_double")]
public static partial double GetLongBytesAsDouble([MarshalUsing(typeof(DoubleToLongMarshaller))] double d);

[LibraryImport(NativeExportsNE_Binary, EntryPoint = "negate_bools")]
public static partial void NegateBools(
BoolStruct boolStruct,
out BoolStruct pBoolStructOut);
[LibraryImport(NativeExportsNE_Binary, EntryPoint = "negate_bools")]
public static partial void NegateBools(
BoolStruct boolStruct,
out BoolStruct pBoolStructOut);

[LibraryImport(NativeExportsNE_Binary, EntryPoint = "and_bools_ref")]
[return: MarshalAs(UnmanagedType.U1)]
public static partial bool AndBoolsRef(in BoolStruct boolStruct);
[LibraryImport(NativeExportsNE_Binary, EntryPoint = "and_bools_ref")]
[return: MarshalAs(UnmanagedType.U1)]
public static partial bool AndBoolsRef(in BoolStruct boolStruct);

[LibraryImport(NativeExportsNE_Binary, EntryPoint = "double_int_ref")]
public static partial IntWrapper DoubleIntRef(IntWrapper pInt);
[LibraryImport(NativeExportsNE_Binary, EntryPoint = "double_int_ref")]
public static partial IntWrapper DoubleIntRef(IntWrapper pInt);
}

[LibraryImport(NativeExportsNE_Binary, EntryPoint = "reverse_replace_ref_ushort")]
public static partial void ReverseReplaceString([MarshalUsing(typeof(Utf16StringMarshaller))] ref string s);
Expand All @@ -52,7 +79,7 @@ public void NonBlittableStructWithFree()
str2 = "Bar"
};

NativeExportsNE.DeepDuplicateStrings(stringContainer, out var stringContainer2);
NativeExportsNE.Stateless.DeepDuplicateStrings(stringContainer, out var stringContainer2);

Assert.Equal(stringContainer, stringContainer2);
}
Expand All @@ -62,7 +89,7 @@ public void MarshalUsing()
{
double d = 1234.56789;

Assert.Equal(d, NativeExportsNE.GetLongBytesAsDouble(d));
Assert.Equal(d, NativeExportsNE.Stateless.GetLongBytesAsDouble(d));
}

[Fact]
Expand All @@ -75,7 +102,7 @@ public void NonBlittableStructWithoutAllocation()
b3 = true
};

NativeExportsNE.NegateBools(boolStruct, out BoolStruct boolStructNegated);
NativeExportsNE.Stateless.NegateBools(boolStruct, out BoolStruct boolStructNegated);

Assert.Equal(!boolStruct.b1, boolStructNegated.b1);
Assert.Equal(!boolStruct.b2, boolStructNegated.b2);
Expand All @@ -88,7 +115,7 @@ public void GetPinnableReferenceMarshalling()
int originalValue = 42;
var wrapper = new IntWrapper { i = originalValue };

var retVal = NativeExportsNE.DoubleIntRef(wrapper);
var retVal = NativeExportsNE.Stateless.DoubleIntRef(wrapper);

Assert.Equal(originalValue * 2, wrapper.i);
Assert.Equal(originalValue * 2, retVal.i);
Expand All @@ -111,7 +138,7 @@ public void NonBlittableStructRef()

var stringContainerCopy = stringContainer;

NativeExportsNE.ReverseStrings(ref stringContainerCopy);
NativeExportsNE.Stateless.ReverseStrings(ref stringContainerCopy);

Assert.Equal(expected, stringContainerCopy);
}
Expand All @@ -134,7 +161,104 @@ public void NonBlittableStructIn(bool b1, bool b2, bool b3)
b3 = b3
};

Assert.Equal(b1 && b2 && b3, NativeExportsNE.AndBoolsRef(container));
Assert.Equal(b1 && b2 && b3, NativeExportsNE.Stateless.AndBoolsRef(container));
}

public class V1
{
[Fact]
public void NonBlittableStructWithFree()
{
var stringContainer = new StringContainer_V1
{
str1 = "Foo",
str2 = "Bar"
};

NativeExportsNE.V1.DeepDuplicateStrings(stringContainer, out var stringContainer2);

Assert.Equal(stringContainer, stringContainer2);
}

[Fact]
public void MarshalUsing()
{
double d = 1234.56789;

Assert.Equal(d, NativeExportsNE.V1.GetLongBytesAsDouble(d));
}

[Fact]
public void NonBlittableStructWithoutAllocation()
{
var boolStruct = new BoolStruct_V1
{
b1 = true,
b2 = false,
b3 = true
};

NativeExportsNE.V1.NegateBools(boolStruct, out BoolStruct_V1 boolStructNegated);

Assert.Equal(!boolStruct.b1, boolStructNegated.b1);
Assert.Equal(!boolStruct.b2, boolStructNegated.b2);
Assert.Equal(!boolStruct.b3, boolStructNegated.b3);
}

[Fact]
public void GetPinnableReferenceMarshalling()
{
int originalValue = 42;
var wrapper = new IntWrapper_V1 { i = originalValue };

var retVal = NativeExportsNE.V1.DoubleIntRef(wrapper);

Assert.Equal(originalValue * 2, wrapper.i);
Assert.Equal(originalValue * 2, retVal.i);
}

[Fact]
public void NonBlittableStructRef()
{
var stringContainer = new StringContainer_V1
{
str1 = "Foo",
str2 = "Bar"
};

var expected = new StringContainer_V1
{
str1 = ReverseUTF8Bytes(stringContainer.str1),
str2 = ReverseUTF8Bytes(stringContainer.str2)
};

var stringContainerCopy = stringContainer;

NativeExportsNE.V1.ReverseStrings(ref stringContainerCopy);

Assert.Equal(expected, stringContainerCopy);
}

[Theory]
[InlineData(true, true, true)]
[InlineData(true, true, false)]
[InlineData(true, false, true)]
[InlineData(true, false, false)]
[InlineData(false, true, true)]
[InlineData(false, true, false)]
[InlineData(false, false, true)]
[InlineData(false, false, false)]
public void NonBlittableStructIn(bool b1, bool b2, bool b3)
{
var container = new BoolStruct_V1
{
b1 = b1,
b2 = b2,
b3 = b3
};

Assert.Equal(b1 && b2 && b3, NativeExportsNE.V1.AndBoolsRef(container));
}
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public partial class Span

[LibraryImport(NativeExportsNE_Binary, EntryPoint = "and_all_members")]
[return: MarshalAs(UnmanagedType.U1)]
public static partial bool AndAllMembers([MarshalUsing(typeof(SpanMarshaller<BoolStruct>))] Span<BoolStruct> pArray, int length);
public static partial bool AndAllMembers([MarshalUsing(typeof(SpanMarshaller<BoolStruct_V1>))] Span<BoolStruct_V1> pArray, int length);
}
}

Expand Down Expand Up @@ -135,21 +135,21 @@ public void NullBlittableElementSpanReturnedFromNative()
[InlineData(false)]
public void SpanWithSimpleNonBlittableTypeMarshalling(bool result)
{
var boolValues = new BoolStruct[]
var boolValues = new BoolStruct_V1[]
{
new BoolStruct
new BoolStruct_V1
{
b1 = true,
b2 = true,
b3 = true,
},
new BoolStruct
new BoolStruct_V1
{
b1 = true,
b2 = true,
b3 = true,
},
new BoolStruct
new BoolStruct_V1
{
b1 = true,
b2 = true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,12 +236,12 @@ public static void Append(int** values, int numOriginalValues, int newValue)

[UnmanagedCallersOnly(EntryPoint = "and_all_members")]
[DNNE.C99DeclCode("struct bool_struct;")]
public static byte AndAllMembers([DNNE.C99Type("struct bool_struct*")] BoolStructNative* pArray, int length)
public static byte AndAllMembers([DNNE.C99Type("struct bool_struct*")] BoolStructNative_V1* pArray, int length)
{
bool result = true;
for (int i = 0; i < length; i++)
{
BoolStruct managed = pArray[i].ToManaged();
BoolStruct_V1 managed = pArray[i].ToManaged();
result &= managed.b1 && managed.b2 && managed.b3;
}
return (byte)(result ? 1 : 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ public static unsafe class CustomMarshalling
[UnmanagedCallersOnly(EntryPoint = "stringcontainer_deepduplicate")]
[DNNE.C99DeclCode("struct string_container { char* str1; char* str2; };")]
public static void DeepDuplicateStrings(
[DNNE.C99Type("struct string_container")] StringContainerNative strings,
[DNNE.C99Type("struct string_container*")] StringContainerNative* pStringsOut)
[DNNE.C99Type("struct string_container")] StringContainerNative_V1 strings,
[DNNE.C99Type("struct string_container*")] StringContainerNative_V1* pStringsOut)
{
// Round trip through the managed view to allocate a new native instance.
*pStringsOut = new StringContainerNative(strings.ToManaged());
*pStringsOut = new StringContainerNative_V1(strings.ToManaged());
}

[UnmanagedCallersOnly(EntryPoint = "stringcontainer_reverse_strings")]
public static void ReverseStrings(
[DNNE.C99Type("struct string_container*")] StringContainerNative* strings)
[DNNE.C99Type("struct string_container*")] StringContainerNative_V1* strings)
{
strings->str1 = (IntPtr)Strings.Reverse((byte*)strings->str1);
strings->str2 = (IntPtr)Strings.Reverse((byte*)strings->str2);
Expand All @@ -36,10 +36,10 @@ public static double GetLongBytesAsDouble(long l)
[UnmanagedCallersOnly(EntryPoint = "negate_bools")]
[DNNE.C99DeclCode("struct bool_struct { int8_t b1; int8_t b2; int8_t b3; };")]
public static void NegateBools(
[DNNE.C99Type("struct bool_struct")] BoolStructNative boolStruct,
[DNNE.C99Type("struct bool_struct*")] BoolStructNative* pBoolStructOut)
[DNNE.C99Type("struct bool_struct")] BoolStructNative_V1 boolStruct,
[DNNE.C99Type("struct bool_struct*")] BoolStructNative_V1* pBoolStructOut)
{
*pBoolStructOut = new BoolStructNative
*pBoolStructOut = new BoolStructNative_V1
{
b1 = (byte)(boolStruct.b1 != 0 ? 0 : 1),
b2 = (byte)(boolStruct.b2 != 0 ? 0 : 1),
Expand All @@ -49,7 +49,7 @@ public static void NegateBools(

[UnmanagedCallersOnly(EntryPoint = "and_bools_ref")]
public static byte AndBoolsRef(
[DNNE.C99Type("struct bool_struct*")] BoolStructNative* boolStruct)
[DNNE.C99Type("struct bool_struct*")] BoolStructNative_V1* boolStruct)
{
return (byte)(boolStruct->b1 != 0 && boolStruct->b2 != 0 && boolStruct->b3 != 0 ? 1 : 0);
}
Expand Down
Loading