From 783c4b0f8e87b5b05f969f8d78a5b77adf8cd383 Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Mon, 3 Apr 2023 01:28:01 +0100 Subject: [PATCH] test: add tests for integer Pack (#73) --- X10D.Tests/src/Core/SpanTest.cs | 154 ++++++++++++++++++++++++++++++-- X10D/src/Core/SpanExtensions.cs | 4 + 2 files changed, 153 insertions(+), 5 deletions(-) diff --git a/X10D.Tests/src/Core/SpanTest.cs b/X10D.Tests/src/Core/SpanTest.cs index c5e27a4..e242c12 100644 --- a/X10D.Tests/src/Core/SpanTest.cs +++ b/X10D.Tests/src/Core/SpanTest.cs @@ -146,6 +146,46 @@ public class SpanTest Assert.IsTrue(span.Contains(EnumInt64.B)); } + [TestMethod] + public void PackByte_ShouldThrowArgumentException_GivenSpanLengthGreaterThan8() + { + Assert.ThrowsException(() => + { + Span span = stackalloc bool[9]; + return span.PackByte(); + }); + } + + [TestMethod] + public void PackInt16_ShouldThrowArgumentException_GivenSpanLengthGreaterThan16() + { + Assert.ThrowsException(() => + { + Span span = stackalloc bool[17]; + return span.PackInt16(); + }); + } + + [TestMethod] + public void PackInt32_ShouldThrowArgumentException_GivenSpanLengthGreaterThan32() + { + Assert.ThrowsException(() => + { + Span span = stackalloc bool[33]; + return span.PackInt32(); + }); + } + + [TestMethod] + public void PackInt64_ShouldThrowArgumentException_GivenSpanLengthGreaterThan64() + { + Assert.ThrowsException(() => + { + Span span = stackalloc bool[65]; + return span.PackInt64(); + }); + } + [TestMethod] public void PackByteInternal_Fallback_ShouldReturnCorrectByte_GivenReadOnlySpan_Using() { @@ -315,7 +355,7 @@ public class SpanTest #endif [TestMethod] - public void PackInt32_ShouldReturnSameAsPackByte_WhenSpanHasLength8() + public void PackInt32_ShouldReturnSameAsPackByte_WhenSpanHasLength8_UsingReadOnlySpan() { ReadOnlySpan span = stackalloc bool[8] {true, true, false, false, true, true, false, false}; @@ -326,7 +366,18 @@ public class SpanTest } [TestMethod] - public void PackInt32_ShouldReturnSameAsPackInt16_WhenSpanHasLength16() + public void PackInt32_ShouldReturnSameAsPackByte_WhenSpanHasLength8_UsingSpan() + { + Span 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 span = stackalloc bool[16] { @@ -339,6 +390,20 @@ public class SpanTest Assert.AreEqual(expected, actual); } + [TestMethod] + public void PackInt32_ShouldReturnSameAsPackInt16_WhenSpanHasLength16_UsingSpan() + { + Span 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] public void PackInt64_ShouldReturnCorrectInt64_GivenReadOnlySpan() { @@ -357,7 +422,24 @@ public class SpanTest } [TestMethod] - public void PackInt64_ShouldReturnSameAsPackByte_WhenSpanHasLength8() + public void PackInt64_ShouldReturnCorrectInt64_GivenSpan() + { + const long expected = 0b01010101_11010110_01101001_11010110_00010010_10010111_00101100_10100101; + Span 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 span = stackalloc bool[8] {true, true, false, false, true, true, false, false}; @@ -368,7 +450,18 @@ public class SpanTest } [TestMethod] - public void PackInt64_ShouldReturnSameAsPackInt16_WhenSpanHasLength16() + public void PackInt64_ShouldReturnSameAsPackByte_WhenSpanHasLength8_UsingSpan() + { + Span 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 span = stackalloc bool[16] { @@ -382,7 +475,21 @@ public class SpanTest } [TestMethod] - public void PackInt64_ShouldReturnSameAsPackInt32_WhenSpanHasLength16() + public void PackInt64_ShouldReturnSameAsPackInt16_WhenSpanHasLength16_UsingSpan() + { + Span 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 span = stackalloc bool[32] { @@ -396,6 +503,43 @@ public class SpanTest Assert.AreEqual(expected, actual); } + [TestMethod] + public void PackInt64_ShouldReturnSameAsPackInt32_WhenSpanHasLength16_UsingSpan() + { + Span 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 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 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 { A, diff --git a/X10D/src/Core/SpanExtensions.cs b/X10D/src/Core/SpanExtensions.cs index 991b510..1ea3aaa 100644 --- a/X10D/src/Core/SpanExtensions.cs +++ b/X10D/src/Core/SpanExtensions.cs @@ -471,6 +471,8 @@ public static class SpanExtensions } } + // dotcover disable + //NOSONAR [Pure] [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] internal static int PackInt32Internal_AdvSimd(this ReadOnlySpan source) @@ -496,6 +498,8 @@ public static class SpanExtensions } } } + //NOSONAR + // dotcover enable [Pure] [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]