Skip to content

Adding tests for --with-packing output #406

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,9 @@ public abstract class StructDeclarationTest : PInvokeGeneratorTest
[Test]
public Task WithAccessSpecifierTest() => WithAccessSpecifierTestImpl();

[Test]
public Task WithPackingTest() => WithPackingTestImpl();

[Test]
public Task SourceLocationAttributeTest() => SourceLocationAttributeTestImpl();

Expand Down Expand Up @@ -279,5 +282,7 @@ public abstract class StructDeclarationTest : PInvokeGeneratorTest

protected abstract Task WithAccessSpecifierTestImpl();

protected abstract Task WithPackingTestImpl();

protected abstract Task SourceLocationAttributeTestImpl();
}
Original file line number Diff line number Diff line change
Expand Up @@ -1724,6 +1724,50 @@ public partial struct MyStruct3
return ValidateGeneratedCSharpCompatibleUnixBindingsAsync(inputContents, expectedOutputContents, withAccessSpecifiers: withAccessSpecifiers);
}

protected override Task WithPackingTestImpl()
{
const string InputContents = @"struct MyStruct
{
size_t FixedBuffer[1];
};
";

const string ExpectedOutputContents = @"using System;
using System.Runtime.InteropServices;

namespace ClangSharp.Test
{
[StructLayout(LayoutKind.Sequential, Pack = CustomPackValue)]
public partial struct MyStruct
{
[NativeTypeName(""size_t[1]"")]
public _FixedBuffer_e__FixedBuffer FixedBuffer;

public partial struct _FixedBuffer_e__FixedBuffer
{
public UIntPtr e0;

public unsafe ref UIntPtr this[int index]
{
get
{
fixed (UIntPtr* pThis = &e0)
{
return ref pThis[index];
}
}
}
}
}
}
";

var withPackings = new Dictionary<string, string> {
["MyStruct"] = "CustomPackValue"
};
return ValidateGeneratedCSharpCompatibleUnixBindingsAsync(InputContents, ExpectedOutputContents, withPackings: withPackings);
}

protected override Task SourceLocationAttributeTestImpl()
{
const string InputContents = @"struct MyStruct
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1728,6 +1728,50 @@ public partial struct MyStruct3
return ValidateGeneratedCSharpCompatibleWindowsBindingsAsync(inputContents, expectedOutputContents, withAccessSpecifiers: withAccessSpecifiers);
}

protected override Task WithPackingTestImpl()
{
const string InputContents = @"struct MyStruct
{
size_t FixedBuffer[1];
};
";

const string ExpectedOutputContents = @"using System;
using System.Runtime.InteropServices;

namespace ClangSharp.Test
{
[StructLayout(LayoutKind.Sequential, Pack = CustomPackValue)]
public partial struct MyStruct
{
[NativeTypeName(""size_t[1]"")]
public _FixedBuffer_e__FixedBuffer FixedBuffer;

public partial struct _FixedBuffer_e__FixedBuffer
{
public UIntPtr e0;

public unsafe ref UIntPtr this[int index]
{
get
{
fixed (UIntPtr* pThis = &e0)
{
return ref pThis[index];
}
}
}
}
}
}
";

var withPackings = new Dictionary<string, string> {
["MyStruct"] = "CustomPackValue"
};
return ValidateGeneratedCSharpCompatibleWindowsBindingsAsync(InputContents, ExpectedOutputContents, withPackings: withPackings);
}

protected override Task SourceLocationAttributeTestImpl()
{
const string InputContents = @"struct MyStruct
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1702,6 +1702,49 @@ public partial struct MyStruct3
return ValidateGeneratedCSharpLatestUnixBindingsAsync(inputContents, expectedOutputContents, withAccessSpecifiers: withAccessSpecifiers);
}

protected override Task WithPackingTestImpl()
{
const string InputContents = @"struct MyStruct
{
size_t FixedBuffer[1];
};
";

const string ExpectedOutputContents = @"using System;
using System.Runtime.InteropServices;

namespace ClangSharp.Test
{
[StructLayout(LayoutKind.Sequential, Pack = CustomPackValue)]
public partial struct MyStruct
{
[NativeTypeName(""size_t[1]"")]
public _FixedBuffer_e__FixedBuffer FixedBuffer;

public partial struct _FixedBuffer_e__FixedBuffer
{
public nuint e0;

public ref nuint this[int index]
{
get
{
return ref AsSpan(int.MaxValue)[index];
}
}

public Span<nuint> AsSpan(int length) => MemoryMarshal.CreateSpan(ref e0, length);
}
}
}
";

var withPackings = new Dictionary<string, string> {
["MyStruct"] = "CustomPackValue"
};
return ValidateGeneratedCSharpLatestUnixBindingsAsync(InputContents, ExpectedOutputContents, withPackings: withPackings);
}

protected override Task SourceLocationAttributeTestImpl()
{
const string InputContents = @"struct MyStruct
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1706,6 +1706,49 @@ public partial struct MyStruct3
return ValidateGeneratedCSharpLatestWindowsBindingsAsync(inputContents, expectedOutputContents, withAccessSpecifiers: withAccessSpecifiers);
}

protected override Task WithPackingTestImpl()
{
const string InputContents = @"struct MyStruct
{
size_t FixedBuffer[1];
};
";

const string ExpectedOutputContents = @"using System;
using System.Runtime.InteropServices;

namespace ClangSharp.Test
{
[StructLayout(LayoutKind.Sequential, Pack = CustomPackValue)]
public partial struct MyStruct
{
[NativeTypeName(""size_t[1]"")]
public _FixedBuffer_e__FixedBuffer FixedBuffer;

public partial struct _FixedBuffer_e__FixedBuffer
{
public nuint e0;

public ref nuint this[int index]
{
get
{
return ref AsSpan(int.MaxValue)[index];
}
}

public Span<nuint> AsSpan(int length) => MemoryMarshal.CreateSpan(ref e0, length);
}
}
}
";

var withPackings = new Dictionary<string, string> {
["MyStruct"] = "CustomPackValue"
};
return ValidateGeneratedCSharpLatestWindowsBindingsAsync(InputContents, ExpectedOutputContents, withPackings: withPackings);
}

protected override Task SourceLocationAttributeTestImpl()
{
const string InputContents = @"struct MyStruct
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1728,6 +1728,52 @@ public partial struct MyStruct3
return ValidateGeneratedCSharpPreviewUnixBindingsAsync(inputContents, expectedOutputContents, withAccessSpecifiers: withAccessSpecifiers);
}

protected override Task WithPackingTestImpl()
{
const string InputContents = @"struct MyStruct
{
size_t FixedBuffer[1];
};
";

const string ExpectedOutputContents = @"using System;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;

namespace ClangSharp.Test
{
[StructLayout(LayoutKind.Sequential, Pack = CustomPackValue)]
public partial struct MyStruct
{
[NativeTypeName(""size_t[1]"")]
public _FixedBuffer_e__FixedBuffer FixedBuffer;

public partial struct _FixedBuffer_e__FixedBuffer
{
public nuint e0;

[UnscopedRef]
public ref nuint this[int index]
{
get
{
return ref AsSpan(int.MaxValue)[index];
}
}

[UnscopedRef]
public Span<nuint> AsSpan(int length) => MemoryMarshal.CreateSpan(ref e0, length);
}
}
}
";

var withPackings = new Dictionary<string, string> {
["MyStruct"] = "CustomPackValue"
};
return ValidateGeneratedCSharpPreviewUnixBindingsAsync(InputContents, ExpectedOutputContents, withPackings: withPackings);
}

protected override Task SourceLocationAttributeTestImpl()
{
const string InputContents = @"struct MyStruct
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1732,6 +1732,52 @@ public partial struct MyStruct3
return ValidateGeneratedCSharpPreviewWindowsBindingsAsync(inputContents, expectedOutputContents, withAccessSpecifiers: withAccessSpecifiers);
}

protected override Task WithPackingTestImpl()
{
const string InputContents = @"struct MyStruct
{
size_t FixedBuffer[1];
};
";

const string ExpectedOutputContents = @"using System;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;

namespace ClangSharp.Test
{
[StructLayout(LayoutKind.Sequential, Pack = CustomPackValue)]
public partial struct MyStruct
{
[NativeTypeName(""size_t[1]"")]
public _FixedBuffer_e__FixedBuffer FixedBuffer;

public partial struct _FixedBuffer_e__FixedBuffer
{
public nuint e0;

[UnscopedRef]
public ref nuint this[int index]
{
get
{
return ref AsSpan(int.MaxValue)[index];
}
}

[UnscopedRef]
public Span<nuint> AsSpan(int length) => MemoryMarshal.CreateSpan(ref e0, length);
}
}
}
";

var withPackings = new Dictionary<string, string> {
["MyStruct"] = "CustomPackValue"
};
return ValidateGeneratedCSharpPreviewWindowsBindingsAsync(InputContents, ExpectedOutputContents, withPackings: withPackings);
}

protected override Task SourceLocationAttributeTestImpl()
{
const string InputContents = @"struct MyStruct
Expand Down
Loading