1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-09 23:25:43 +00:00

test: add tests for integer Pack (#73)

This commit is contained in:
Oliver Booth 2023-04-03 01:28:01 +01:00
parent cbbfa6d947
commit 783c4b0f8e
No known key found for this signature in database
GPG Key ID: 20BEB9DC87961025
2 changed files with 153 additions and 5 deletions

View File

@ -146,6 +146,46 @@ public class SpanTest
Assert.IsTrue(span.Contains(EnumInt64.B)); Assert.IsTrue(span.Contains(EnumInt64.B));
} }
[TestMethod]
public void PackByte_ShouldThrowArgumentException_GivenSpanLengthGreaterThan8()
{
Assert.ThrowsException<ArgumentException>(() =>
{
Span<bool> span = stackalloc bool[9];
return span.PackByte();
});
}
[TestMethod]
public void PackInt16_ShouldThrowArgumentException_GivenSpanLengthGreaterThan16()
{
Assert.ThrowsException<ArgumentException>(() =>
{
Span<bool> span = stackalloc bool[17];
return span.PackInt16();
});
}
[TestMethod]
public void PackInt32_ShouldThrowArgumentException_GivenSpanLengthGreaterThan32()
{
Assert.ThrowsException<ArgumentException>(() =>
{
Span<bool> span = stackalloc bool[33];
return span.PackInt32();
});
}
[TestMethod]
public void PackInt64_ShouldThrowArgumentException_GivenSpanLengthGreaterThan64()
{
Assert.ThrowsException<ArgumentException>(() =>
{
Span<bool> span = stackalloc bool[65];
return span.PackInt64();
});
}
[TestMethod] [TestMethod]
public void PackByteInternal_Fallback_ShouldReturnCorrectByte_GivenReadOnlySpan_Using() public void PackByteInternal_Fallback_ShouldReturnCorrectByte_GivenReadOnlySpan_Using()
{ {
@ -315,7 +355,7 @@ public class SpanTest
#endif #endif
[TestMethod] [TestMethod]
public void PackInt32_ShouldReturnSameAsPackByte_WhenSpanHasLength8() public void PackInt32_ShouldReturnSameAsPackByte_WhenSpanHasLength8_UsingReadOnlySpan()
{ {
ReadOnlySpan<bool> span = stackalloc bool[8] {true, true, false, false, true, true, false, false}; ReadOnlySpan<bool> span = stackalloc bool[8] {true, true, false, false, true, true, false, false};
@ -326,7 +366,18 @@ public class SpanTest
} }
[TestMethod] [TestMethod]
public void PackInt32_ShouldReturnSameAsPackInt16_WhenSpanHasLength16() public void PackInt32_ShouldReturnSameAsPackByte_WhenSpanHasLength8_UsingSpan()
{
Span<bool> span = stackalloc bool[8] {true, true, false, false, true, true, false, false};
int expected = span.PackByte();
int actual = span.PackInt32();
Assert.AreEqual(expected, actual);
}
[TestMethod]
public void PackInt32_ShouldReturnSameAsPackInt16_WhenSpanHasLength16_UsingReadOnlySpan()
{ {
ReadOnlySpan<bool> span = stackalloc bool[16] ReadOnlySpan<bool> span = stackalloc bool[16]
{ {
@ -339,6 +390,20 @@ public class SpanTest
Assert.AreEqual(expected, actual); Assert.AreEqual(expected, actual);
} }
[TestMethod]
public void PackInt32_ShouldReturnSameAsPackInt16_WhenSpanHasLength16_UsingSpan()
{
Span<bool> span = stackalloc bool[16]
{
false, false, true, false, true, false, true, true, true, false, true, true, false, true, false, false,
};
int expected = span.PackInt16();
int actual = span.PackInt32();
Assert.AreEqual(expected, actual);
}
[TestMethod] [TestMethod]
public void PackInt64_ShouldReturnCorrectInt64_GivenReadOnlySpan() public void PackInt64_ShouldReturnCorrectInt64_GivenReadOnlySpan()
{ {
@ -357,7 +422,24 @@ public class SpanTest
} }
[TestMethod] [TestMethod]
public void PackInt64_ShouldReturnSameAsPackByte_WhenSpanHasLength8() public void PackInt64_ShouldReturnCorrectInt64_GivenSpan()
{
const long expected = 0b01010101_11010110_01101001_11010110_00010010_10010111_00101100_10100101;
Span<bool> span = stackalloc bool[64]
{
true, false, true, false, false, true, false, true, false, false, true, true, false, true, false, false, true,
true, true, false, true, false, false, true, false, true, false, false, true, false, false, false, false, true,
true, false, true, false, true, true, true, false, false, true, false, true, true, false, false, true, true,
false, true, false, true, true, true, false, true, false, true, false, true, false,
};
long actual = span.PackInt64();
Assert.AreEqual(expected, actual);
}
[TestMethod]
public void PackInt64_ShouldReturnSameAsPackByte_WhenSpanHasLength8_UsingReadOnlySpan()
{ {
ReadOnlySpan<bool> span = stackalloc bool[8] {true, true, false, false, true, true, false, false}; ReadOnlySpan<bool> span = stackalloc bool[8] {true, true, false, false, true, true, false, false};
@ -368,7 +450,18 @@ public class SpanTest
} }
[TestMethod] [TestMethod]
public void PackInt64_ShouldReturnSameAsPackInt16_WhenSpanHasLength16() public void PackInt64_ShouldReturnSameAsPackByte_WhenSpanHasLength8_UsingSpan()
{
Span<bool> span = stackalloc bool[8] {true, true, false, false, true, true, false, false};
long expected = span.PackByte();
long actual = span.PackInt64();
Assert.AreEqual(expected, actual);
}
[TestMethod]
public void PackInt64_ShouldReturnSameAsPackInt16_WhenSpanHasLength16_UsingReadOnlySpan()
{ {
ReadOnlySpan<bool> span = stackalloc bool[16] ReadOnlySpan<bool> span = stackalloc bool[16]
{ {
@ -382,7 +475,21 @@ public class SpanTest
} }
[TestMethod] [TestMethod]
public void PackInt64_ShouldReturnSameAsPackInt32_WhenSpanHasLength16() public void PackInt64_ShouldReturnSameAsPackInt16_WhenSpanHasLength16_UsingSpan()
{
Span<bool> span = stackalloc bool[16]
{
false, false, true, false, true, false, true, true, true, false, true, true, false, true, false, false,
};
long expected = span.PackInt16();
long actual = span.PackInt64();
Assert.AreEqual(expected, actual);
}
[TestMethod]
public void PackInt64_ShouldReturnSameAsPackInt32_WhenSpanHasLength16_UsingReadOnlySpan()
{ {
ReadOnlySpan<bool> span = stackalloc bool[32] ReadOnlySpan<bool> span = stackalloc bool[32]
{ {
@ -396,6 +503,43 @@ public class SpanTest
Assert.AreEqual(expected, actual); Assert.AreEqual(expected, actual);
} }
[TestMethod]
public void PackInt64_ShouldReturnSameAsPackInt32_WhenSpanHasLength16_UsingSpan()
{
Span<bool> span = stackalloc bool[32]
{
false, true, false, true, false, true, false, true, true, false, true, false, true, false, true, false, false,
true, false, true, false, true, false, true, true, false, true, false, true, false, true, false,
};
long expected = span.PackInt32();
long actual = span.PackInt64();
Assert.AreEqual(expected, actual);
}
[TestMethod]
public void PackInt64_ShouldFallbackAndReturnCorrectValue_GivenNonPowerOfTwoLength_UsingReadOnlySpan()
{
const long expected = 0b00000000_00000000_00000000_00000000_00000000_00000000_00000001_01010011;
ReadOnlySpan<bool> span = stackalloc bool[10] {true, true, false, false, true, false, true, false, true, false};
long actual = span.PackInt64();
Assert.AreEqual(expected, actual);
}
[TestMethod]
public void PackInt64_ShouldFallbackAndReturnCorrectValue_GivenNonPowerOfTwoLength_UsingSpan()
{
const long expected = 0b00000000_00000000_00000000_00000000_00000000_00000000_00000001_01010011;
Span<bool> span = stackalloc bool[10] {true, true, false, false, true, false, true, false, true, false};
long actual = span.PackInt64();
Assert.AreEqual(expected, actual);
}
private enum EnumByte : byte private enum EnumByte : byte
{ {
A, A,

View File

@ -471,6 +471,8 @@ public static class SpanExtensions
} }
} }
// dotcover disable
//NOSONAR
[Pure] [Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
internal static int PackInt32Internal_AdvSimd(this ReadOnlySpan<bool> source) internal static int PackInt32Internal_AdvSimd(this ReadOnlySpan<bool> source)
@ -496,6 +498,8 @@ public static class SpanExtensions
} }
} }
} }
//NOSONAR
// dotcover enable
[Pure] [Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]