Add an AlignOf<T> function for use in struct layout tests

This commit is contained in:
Alex Barney 2022-04-23 12:17:46 -07:00
parent cac796ac19
commit bc7fea5714
3 changed files with 17 additions and 34 deletions

View file

@ -16,4 +16,17 @@ public class Layout
return offset;
}
public static int AlignOf<T>() where T : unmanaged
{
return Unsafe.SizeOf<AlignOfHelper<T>>() - Unsafe.SizeOf<T>();
}
private readonly struct AlignOfHelper<T> where T : unmanaged
{
#pragma warning disable CS0169 // Field is never used
private readonly byte _padding;
private readonly T _value;
#pragma warning restore CS0169
}
}

View file

@ -1,5 +1,4 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using LibHac.Fs;
using LibHac.Fs.Impl;
using Xunit;
@ -476,21 +475,10 @@ public class TypeLayoutTests
Assert.Equal(0, GetOffset(in s, in s.Value));
}
[StructLayout(LayoutKind.Sequential)]
private struct Int64AlignmentTest
{
public byte A;
public Int64 B;
}
[Fact]
public static void Int64Test_Layout()
public static void Int64_Layout()
{
var s = new Int64AlignmentTest();
Assert.Equal(12, Unsafe.SizeOf<Int64AlignmentTest>());
Assert.Equal(0, GetOffset(in s, in s.A));
Assert.Equal(4, GetOffset(in s, in s.B));
Assert.Equal(8, Unsafe.SizeOf<Int64>());
Assert.Equal(4, AlignOf<Int64>());
}
}

View file

@ -1,5 +1,4 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using LibHac.FsSystem;
using Xunit;
using static LibHac.Tests.Common.Layout;
@ -287,6 +286,7 @@ public class TypeLayoutTests
HierarchicalIntegrityVerificationLevelInformation s = default;
Assert.Equal(0x18, Unsafe.SizeOf<HierarchicalIntegrityVerificationLevelInformation>());
Assert.Equal(0x04, AlignOf<HierarchicalIntegrityVerificationLevelInformation>());
Assert.Equal(0x00, GetOffset(in s, in s.Offset));
Assert.Equal(0x08, GetOffset(in s, in s.Size));
@ -294,24 +294,6 @@ public class TypeLayoutTests
Assert.Equal(0x14, GetOffset(in s, in s.Reserved));
}
[StructLayout(LayoutKind.Sequential)]
private struct HierarchicalIntegrityVerificationLevelInformationAlignmentTest
{
public byte A;
public HierarchicalIntegrityVerificationLevelInformation B;
}
[Fact]
public static void HierarchicalIntegrityVerificationLevelInformation_Alignment()
{
var s = new HierarchicalIntegrityVerificationLevelInformationAlignmentTest();
Assert.Equal(0x1C, Unsafe.SizeOf<HierarchicalIntegrityVerificationLevelInformationAlignmentTest>());
Assert.Equal(0, GetOffset(in s, in s.A));
Assert.Equal(4, GetOffset(in s, in s.B));
}
[Fact]
public static void HierarchicalIntegrityVerificationInformation_Layout()
{