Skip to content

Commit

Permalink
Extract test helper InvokeUnsafeSizeOf
Browse files Browse the repository at this point in the history
  • Loading branch information
sharwell committed Apr 24, 2023
1 parent 241c373 commit b0d211d
Showing 1 changed file with 10 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,42 +64,41 @@ public static IEnumerable<object[]> ExplicitSizeTypes
[MemberData(nameof(ExplicitSizeTypes))]
public void ExplicitSizesAreCorrect(Type type)
{
var unsafeSizeOfMethod = typeof(Unsafe).GetMethod(nameof(Unsafe.SizeOf)).MakeGenericMethod(type);
var size = (int)unsafeSizeOfMethod.Invoke(null, null);
Assert.Equal(int.Parse(type.Name[4..]), size);
Assert.Equal(int.Parse(type.Name[4..]), InvokeUnsafeSizeOf(type));
}

[Theory]
[MemberData(nameof(ExplicitSizeTypes))]
public void GetSegmentSize(Type type)
{
var getSegmentSizeMethod = typeof(SegmentedArrayHelper).GetMethod(nameof(SegmentedArrayHelper.GetSegmentSize), BindingFlags.Static | BindingFlags.NonPublic).MakeGenericMethod(type);
var unsafeSizeOfMethod = typeof(Unsafe).GetMethod(nameof(Unsafe.SizeOf)).MakeGenericMethod(type);
Assert.Equal(SegmentedArrayHelper.TestAccessor.CalculateSegmentSize((int)unsafeSizeOfMethod.Invoke(null, null)), (int)getSegmentSizeMethod.Invoke(null, null));
Assert.Equal(SegmentedArrayHelper.TestAccessor.CalculateSegmentSize(InvokeUnsafeSizeOf(type)), (int)getSegmentSizeMethod.Invoke(null, null));
}

[Theory]
[MemberData(nameof(ExplicitSizeTypes))]
public void GetSegmentShift(Type type)
{
var getSegmentSizeMethod = typeof(SegmentedArrayHelper).GetMethod(nameof(SegmentedArrayHelper.GetSegmentSize), BindingFlags.Static | BindingFlags.NonPublic).MakeGenericMethod(type);
var getSegmentShiftMethod = typeof(SegmentedArrayHelper).GetMethod(nameof(SegmentedArrayHelper.GetSegmentShift), BindingFlags.Static | BindingFlags.NonPublic).MakeGenericMethod(type);
var unsafeSizeOfMethod = typeof(Unsafe).GetMethod(nameof(Unsafe.SizeOf)).MakeGenericMethod(type);
var segmentSize = SegmentedArrayHelper.TestAccessor.CalculateSegmentSize((int)unsafeSizeOfMethod.Invoke(null, null));
var segmentSize = SegmentedArrayHelper.TestAccessor.CalculateSegmentSize(InvokeUnsafeSizeOf(type));
Assert.Equal(SegmentedArrayHelper.TestAccessor.CalculateSegmentShift(segmentSize), (int)getSegmentShiftMethod.Invoke(null, null));
}

[Theory]
[MemberData(nameof(ExplicitSizeTypes))]
public void GetOffsetMask(Type type)
{
var getSegmentSizeMethod = typeof(SegmentedArrayHelper).GetMethod(nameof(SegmentedArrayHelper.GetSegmentSize), BindingFlags.Static | BindingFlags.NonPublic).MakeGenericMethod(type);
var getOffsetMaskMethod = typeof(SegmentedArrayHelper).GetMethod(nameof(SegmentedArrayHelper.GetOffsetMask), BindingFlags.Static | BindingFlags.NonPublic).MakeGenericMethod(type);
var unsafeSizeOfMethod = typeof(Unsafe).GetMethod(nameof(Unsafe.SizeOf)).MakeGenericMethod(type);
var segmentSize = SegmentedArrayHelper.TestAccessor.CalculateSegmentSize((int)unsafeSizeOfMethod.Invoke(null, null));
var segmentSize = SegmentedArrayHelper.TestAccessor.CalculateSegmentSize(InvokeUnsafeSizeOf(type));
Assert.Equal(SegmentedArrayHelper.TestAccessor.CalculateOffsetMask(segmentSize), (int)getOffsetMaskMethod.Invoke(null, null));
}

private static int InvokeUnsafeSizeOf(Type type)
{
var unsafeSizeOfMethod = typeof(Unsafe).GetMethod(nameof(Unsafe.SizeOf)).MakeGenericMethod(type);
return (int)unsafeSizeOfMethod.Invoke(null, null);
}

[Theory]
[InlineData(1)]
[InlineData(2)]
Expand Down

0 comments on commit b0d211d

Please sign in to comment.