Skip to content

Commit bbf343f

Browse files
Merge pull request #406 from DaZombieKiller/with-packing-tests
Adding tests for --with-packing output
2 parents 69391f1 + abc629a commit bbf343f

File tree

14 files changed

+571
-26
lines changed

14 files changed

+571
-26
lines changed

tests/ClangSharp.PInvokeGenerator.UnitTests/Base/StructDeclarationTest.cs

+5
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,9 @@ public abstract class StructDeclarationTest : PInvokeGeneratorTest
206206
[Test]
207207
public Task WithAccessSpecifierTest() => WithAccessSpecifierTestImpl();
208208

209+
[Test]
210+
public Task WithPackingTest() => WithPackingTestImpl();
211+
209212
[Test]
210213
public Task SourceLocationAttributeTest() => SourceLocationAttributeTestImpl();
211214

@@ -279,5 +282,7 @@ public abstract class StructDeclarationTest : PInvokeGeneratorTest
279282

280283
protected abstract Task WithAccessSpecifierTestImpl();
281284

285+
protected abstract Task WithPackingTestImpl();
286+
282287
protected abstract Task SourceLocationAttributeTestImpl();
283288
}

tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpCompatibleUnix/StructDeclarationTest.cs

+44
Original file line numberDiff line numberDiff line change
@@ -1724,6 +1724,50 @@ public partial struct MyStruct3
17241724
return ValidateGeneratedCSharpCompatibleUnixBindingsAsync(inputContents, expectedOutputContents, withAccessSpecifiers: withAccessSpecifiers);
17251725
}
17261726

1727+
protected override Task WithPackingTestImpl()
1728+
{
1729+
const string InputContents = @"struct MyStruct
1730+
{
1731+
size_t FixedBuffer[1];
1732+
};
1733+
";
1734+
1735+
const string ExpectedOutputContents = @"using System;
1736+
using System.Runtime.InteropServices;
1737+
1738+
namespace ClangSharp.Test
1739+
{
1740+
[StructLayout(LayoutKind.Sequential, Pack = CustomPackValue)]
1741+
public partial struct MyStruct
1742+
{
1743+
[NativeTypeName(""size_t[1]"")]
1744+
public _FixedBuffer_e__FixedBuffer FixedBuffer;
1745+
1746+
public partial struct _FixedBuffer_e__FixedBuffer
1747+
{
1748+
public UIntPtr e0;
1749+
1750+
public unsafe ref UIntPtr this[int index]
1751+
{
1752+
get
1753+
{
1754+
fixed (UIntPtr* pThis = &e0)
1755+
{
1756+
return ref pThis[index];
1757+
}
1758+
}
1759+
}
1760+
}
1761+
}
1762+
}
1763+
";
1764+
1765+
var withPackings = new Dictionary<string, string> {
1766+
["MyStruct"] = "CustomPackValue"
1767+
};
1768+
return ValidateGeneratedCSharpCompatibleUnixBindingsAsync(InputContents, ExpectedOutputContents, withPackings: withPackings);
1769+
}
1770+
17271771
protected override Task SourceLocationAttributeTestImpl()
17281772
{
17291773
const string InputContents = @"struct MyStruct

tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpCompatibleWindows/StructDeclarationTest.cs

+44
Original file line numberDiff line numberDiff line change
@@ -1728,6 +1728,50 @@ public partial struct MyStruct3
17281728
return ValidateGeneratedCSharpCompatibleWindowsBindingsAsync(inputContents, expectedOutputContents, withAccessSpecifiers: withAccessSpecifiers);
17291729
}
17301730

1731+
protected override Task WithPackingTestImpl()
1732+
{
1733+
const string InputContents = @"struct MyStruct
1734+
{
1735+
size_t FixedBuffer[1];
1736+
};
1737+
";
1738+
1739+
const string ExpectedOutputContents = @"using System;
1740+
using System.Runtime.InteropServices;
1741+
1742+
namespace ClangSharp.Test
1743+
{
1744+
[StructLayout(LayoutKind.Sequential, Pack = CustomPackValue)]
1745+
public partial struct MyStruct
1746+
{
1747+
[NativeTypeName(""size_t[1]"")]
1748+
public _FixedBuffer_e__FixedBuffer FixedBuffer;
1749+
1750+
public partial struct _FixedBuffer_e__FixedBuffer
1751+
{
1752+
public UIntPtr e0;
1753+
1754+
public unsafe ref UIntPtr this[int index]
1755+
{
1756+
get
1757+
{
1758+
fixed (UIntPtr* pThis = &e0)
1759+
{
1760+
return ref pThis[index];
1761+
}
1762+
}
1763+
}
1764+
}
1765+
}
1766+
}
1767+
";
1768+
1769+
var withPackings = new Dictionary<string, string> {
1770+
["MyStruct"] = "CustomPackValue"
1771+
};
1772+
return ValidateGeneratedCSharpCompatibleWindowsBindingsAsync(InputContents, ExpectedOutputContents, withPackings: withPackings);
1773+
}
1774+
17311775
protected override Task SourceLocationAttributeTestImpl()
17321776
{
17331777
const string InputContents = @"struct MyStruct

tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestUnix/StructDeclarationTest.cs

+43
Original file line numberDiff line numberDiff line change
@@ -1702,6 +1702,49 @@ public partial struct MyStruct3
17021702
return ValidateGeneratedCSharpLatestUnixBindingsAsync(inputContents, expectedOutputContents, withAccessSpecifiers: withAccessSpecifiers);
17031703
}
17041704

1705+
protected override Task WithPackingTestImpl()
1706+
{
1707+
const string InputContents = @"struct MyStruct
1708+
{
1709+
size_t FixedBuffer[1];
1710+
};
1711+
";
1712+
1713+
const string ExpectedOutputContents = @"using System;
1714+
using System.Runtime.InteropServices;
1715+
1716+
namespace ClangSharp.Test
1717+
{
1718+
[StructLayout(LayoutKind.Sequential, Pack = CustomPackValue)]
1719+
public partial struct MyStruct
1720+
{
1721+
[NativeTypeName(""size_t[1]"")]
1722+
public _FixedBuffer_e__FixedBuffer FixedBuffer;
1723+
1724+
public partial struct _FixedBuffer_e__FixedBuffer
1725+
{
1726+
public nuint e0;
1727+
1728+
public ref nuint this[int index]
1729+
{
1730+
get
1731+
{
1732+
return ref AsSpan(int.MaxValue)[index];
1733+
}
1734+
}
1735+
1736+
public Span<nuint> AsSpan(int length) => MemoryMarshal.CreateSpan(ref e0, length);
1737+
}
1738+
}
1739+
}
1740+
";
1741+
1742+
var withPackings = new Dictionary<string, string> {
1743+
["MyStruct"] = "CustomPackValue"
1744+
};
1745+
return ValidateGeneratedCSharpLatestUnixBindingsAsync(InputContents, ExpectedOutputContents, withPackings: withPackings);
1746+
}
1747+
17051748
protected override Task SourceLocationAttributeTestImpl()
17061749
{
17071750
const string InputContents = @"struct MyStruct

tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestWindows/StructDeclarationTest.cs

+43
Original file line numberDiff line numberDiff line change
@@ -1706,6 +1706,49 @@ public partial struct MyStruct3
17061706
return ValidateGeneratedCSharpLatestWindowsBindingsAsync(inputContents, expectedOutputContents, withAccessSpecifiers: withAccessSpecifiers);
17071707
}
17081708

1709+
protected override Task WithPackingTestImpl()
1710+
{
1711+
const string InputContents = @"struct MyStruct
1712+
{
1713+
size_t FixedBuffer[1];
1714+
};
1715+
";
1716+
1717+
const string ExpectedOutputContents = @"using System;
1718+
using System.Runtime.InteropServices;
1719+
1720+
namespace ClangSharp.Test
1721+
{
1722+
[StructLayout(LayoutKind.Sequential, Pack = CustomPackValue)]
1723+
public partial struct MyStruct
1724+
{
1725+
[NativeTypeName(""size_t[1]"")]
1726+
public _FixedBuffer_e__FixedBuffer FixedBuffer;
1727+
1728+
public partial struct _FixedBuffer_e__FixedBuffer
1729+
{
1730+
public nuint e0;
1731+
1732+
public ref nuint this[int index]
1733+
{
1734+
get
1735+
{
1736+
return ref AsSpan(int.MaxValue)[index];
1737+
}
1738+
}
1739+
1740+
public Span<nuint> AsSpan(int length) => MemoryMarshal.CreateSpan(ref e0, length);
1741+
}
1742+
}
1743+
}
1744+
";
1745+
1746+
var withPackings = new Dictionary<string, string> {
1747+
["MyStruct"] = "CustomPackValue"
1748+
};
1749+
return ValidateGeneratedCSharpLatestWindowsBindingsAsync(InputContents, ExpectedOutputContents, withPackings: withPackings);
1750+
}
1751+
17091752
protected override Task SourceLocationAttributeTestImpl()
17101753
{
17111754
const string InputContents = @"struct MyStruct

tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpPreviewUnix/StructDeclarationTest.cs

+46
Original file line numberDiff line numberDiff line change
@@ -1728,6 +1728,52 @@ public partial struct MyStruct3
17281728
return ValidateGeneratedCSharpPreviewUnixBindingsAsync(inputContents, expectedOutputContents, withAccessSpecifiers: withAccessSpecifiers);
17291729
}
17301730

1731+
protected override Task WithPackingTestImpl()
1732+
{
1733+
const string InputContents = @"struct MyStruct
1734+
{
1735+
size_t FixedBuffer[1];
1736+
};
1737+
";
1738+
1739+
const string ExpectedOutputContents = @"using System;
1740+
using System.Diagnostics.CodeAnalysis;
1741+
using System.Runtime.InteropServices;
1742+
1743+
namespace ClangSharp.Test
1744+
{
1745+
[StructLayout(LayoutKind.Sequential, Pack = CustomPackValue)]
1746+
public partial struct MyStruct
1747+
{
1748+
[NativeTypeName(""size_t[1]"")]
1749+
public _FixedBuffer_e__FixedBuffer FixedBuffer;
1750+
1751+
public partial struct _FixedBuffer_e__FixedBuffer
1752+
{
1753+
public nuint e0;
1754+
1755+
[UnscopedRef]
1756+
public ref nuint this[int index]
1757+
{
1758+
get
1759+
{
1760+
return ref AsSpan(int.MaxValue)[index];
1761+
}
1762+
}
1763+
1764+
[UnscopedRef]
1765+
public Span<nuint> AsSpan(int length) => MemoryMarshal.CreateSpan(ref e0, length);
1766+
}
1767+
}
1768+
}
1769+
";
1770+
1771+
var withPackings = new Dictionary<string, string> {
1772+
["MyStruct"] = "CustomPackValue"
1773+
};
1774+
return ValidateGeneratedCSharpPreviewUnixBindingsAsync(InputContents, ExpectedOutputContents, withPackings: withPackings);
1775+
}
1776+
17311777
protected override Task SourceLocationAttributeTestImpl()
17321778
{
17331779
const string InputContents = @"struct MyStruct

tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpPreviewWindows/StructDeclarationTest.cs

+46
Original file line numberDiff line numberDiff line change
@@ -1732,6 +1732,52 @@ public partial struct MyStruct3
17321732
return ValidateGeneratedCSharpPreviewWindowsBindingsAsync(inputContents, expectedOutputContents, withAccessSpecifiers: withAccessSpecifiers);
17331733
}
17341734

1735+
protected override Task WithPackingTestImpl()
1736+
{
1737+
const string InputContents = @"struct MyStruct
1738+
{
1739+
size_t FixedBuffer[1];
1740+
};
1741+
";
1742+
1743+
const string ExpectedOutputContents = @"using System;
1744+
using System.Diagnostics.CodeAnalysis;
1745+
using System.Runtime.InteropServices;
1746+
1747+
namespace ClangSharp.Test
1748+
{
1749+
[StructLayout(LayoutKind.Sequential, Pack = CustomPackValue)]
1750+
public partial struct MyStruct
1751+
{
1752+
[NativeTypeName(""size_t[1]"")]
1753+
public _FixedBuffer_e__FixedBuffer FixedBuffer;
1754+
1755+
public partial struct _FixedBuffer_e__FixedBuffer
1756+
{
1757+
public nuint e0;
1758+
1759+
[UnscopedRef]
1760+
public ref nuint this[int index]
1761+
{
1762+
get
1763+
{
1764+
return ref AsSpan(int.MaxValue)[index];
1765+
}
1766+
}
1767+
1768+
[UnscopedRef]
1769+
public Span<nuint> AsSpan(int length) => MemoryMarshal.CreateSpan(ref e0, length);
1770+
}
1771+
}
1772+
}
1773+
";
1774+
1775+
var withPackings = new Dictionary<string, string> {
1776+
["MyStruct"] = "CustomPackValue"
1777+
};
1778+
return ValidateGeneratedCSharpPreviewWindowsBindingsAsync(InputContents, ExpectedOutputContents, withPackings: withPackings);
1779+
}
1780+
17351781
protected override Task SourceLocationAttributeTestImpl()
17361782
{
17371783
const string InputContents = @"struct MyStruct

0 commit comments

Comments
 (0)