diff --git a/X10D.Tests/src/Core/SpanTest.cs b/X10D.Tests/src/Core/SpanTest.cs index a92befd..1c86feb 100644 --- a/X10D.Tests/src/Core/SpanTest.cs +++ b/X10D.Tests/src/Core/SpanTest.cs @@ -1,4 +1,4 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; +using Microsoft.VisualStudio.TestTools.UnitTesting; using X10D.Collections; using X10D.Core; @@ -7,6 +7,142 @@ namespace X10D.Tests.Core; [TestClass] public class SpanTest { + [TestMethod] + public void Contains_ShouldReturnFalse_GivenReadOnlySpanWithNoMatchingElements_UsingByteEnum() + { + ReadOnlySpan span = stackalloc EnumByte[1] {EnumByte.B}; + + Assert.IsFalse(span.Contains(EnumByte.A)); + Assert.IsFalse(span.Contains(EnumByte.C)); + } + + [TestMethod] + public void Contains_ShouldReturnFalse_GivenReadOnlySpanWithNoMatchingElements_UsingInt16Enum() + { + ReadOnlySpan span = stackalloc EnumInt16[1] {EnumInt16.B}; + + Assert.IsFalse(span.Contains(EnumInt16.A)); + Assert.IsFalse(span.Contains(EnumInt16.C)); + } + + [TestMethod] + public void Contains_ShouldReturnFalse_GivenReadOnlySpanWithNoMatchingElements_UsingInt32Enum() + { + ReadOnlySpan span = stackalloc EnumInt32[1] {EnumInt32.B}; + + Assert.IsFalse(span.Contains(EnumInt32.A)); + Assert.IsFalse(span.Contains(EnumInt32.C)); + } + + [TestMethod] + public void Contains_ShouldReturnFalse_GivenReadOnlySpanWithNoMatchingElements_UsingInt64Enum() + { + ReadOnlySpan span = stackalloc EnumInt64[1] {EnumInt64.B}; + + Assert.IsFalse(span.Contains(EnumInt64.A)); + Assert.IsFalse(span.Contains(EnumInt64.C)); + } + + [TestMethod] + public void Contains_ShouldReturnTrue_GivenReadOnlySpanWithMatchingElements_UsingByteEnum() + { + ReadOnlySpan span = stackalloc EnumByte[1] {EnumByte.B}; + + Assert.IsTrue(span.Contains(EnumByte.B)); + } + + [TestMethod] + public void Contains_ShouldReturnTrue_GivenReadOnlySpanWithMatchingElements_UsingInt16Enum() + { + ReadOnlySpan span = stackalloc EnumInt16[1] {EnumInt16.B}; + + Assert.IsTrue(span.Contains(EnumInt16.B)); + } + + [TestMethod] + public void Contains_ShouldReturnTrue_GivenReadOnlySpanWithMatchingElements_UsingInt32Enum() + { + ReadOnlySpan span = stackalloc EnumInt32[1] {EnumInt32.B}; + + Assert.IsTrue(span.Contains(EnumInt32.B)); + } + + [TestMethod] + public void Contains_ShouldReturnTrue_GivenReadOnlySpanWithMatchingElements_UsingInt64Enum() + { + ReadOnlySpan span = stackalloc EnumInt64[1] {EnumInt64.B}; + + Assert.IsTrue(span.Contains(EnumInt64.B)); + } + + [TestMethod] + public void Contains_ShouldReturnFalse_GivenSpanWithNoMatchingElements_UsingByteEnum() + { + Span span = stackalloc EnumByte[1] {EnumByte.B}; + + Assert.IsFalse(span.Contains(EnumByte.A)); + Assert.IsFalse(span.Contains(EnumByte.C)); + } + + [TestMethod] + public void Contains_ShouldReturnFalse_GivenSpanWithNoMatchingElements_UsingInt16Enum() + { + Span span = stackalloc EnumInt16[1] {EnumInt16.B}; + + Assert.IsFalse(span.Contains(EnumInt16.A)); + Assert.IsFalse(span.Contains(EnumInt16.C)); + } + + [TestMethod] + public void Contains_ShouldReturnFalse_GivenSpanWithNoMatchingElements_UsingInt32Enum() + { + Span span = stackalloc EnumInt32[1] {EnumInt32.B}; + + Assert.IsFalse(span.Contains(EnumInt32.A)); + Assert.IsFalse(span.Contains(EnumInt32.C)); + } + + [TestMethod] + public void Contains_ShouldReturnFalse_GivenSpanWithNoMatchingElements_UsingInt64Enum() + { + Span span = stackalloc EnumInt64[1] {EnumInt64.B}; + + Assert.IsFalse(span.Contains(EnumInt64.A)); + Assert.IsFalse(span.Contains(EnumInt64.C)); + } + + [TestMethod] + public void Contains_ShouldReturnTrue_GivenSpanWithMatchingElements_UsingByteEnum() + { + Span span = stackalloc EnumByte[1] {EnumByte.B}; + + Assert.IsTrue(span.Contains(EnumByte.B)); + } + + [TestMethod] + public void Contains_ShouldReturnTrue_GivenSpanWithMatchingElements_UsingInt16Enum() + { + Span span = stackalloc EnumInt16[1] {EnumInt16.B}; + + Assert.IsTrue(span.Contains(EnumInt16.B)); + } + + [TestMethod] + public void Contains_ShouldReturnTrue_GivenSpanWithMatchingElements_UsingInt32Enum() + { + Span span = stackalloc EnumInt32[1] {EnumInt32.B}; + + Assert.IsTrue(span.Contains(EnumInt32.B)); + } + + [TestMethod] + public void Contains_ShouldReturnTrue_GivenSpanWithMatchingElements_UsingInt64Enum() + { + Span span = stackalloc EnumInt64[1] {EnumInt64.B}; + + Assert.IsTrue(span.Contains(EnumInt64.B)); + } + [TestMethod] public void Pack8Bit_Should_Pack_Correctly() { @@ -96,4 +232,32 @@ public class SpanTest Assert.AreEqual(value, unpacks.PackInt64()); } + + private enum EnumByte : byte + { + A, + B, + C + } + + private enum EnumInt16 : short + { + A, + B, + C + } + + private enum EnumInt32 + { + A, + B, + C + } + + private enum EnumInt64 : long + { + A, + B, + C + } } diff --git a/X10D/src/Core/SpanExtensions.cs b/X10D/src/Core/SpanExtensions.cs index 3d6133d..bc5cb1d 100644 --- a/X10D/src/Core/SpanExtensions.cs +++ b/X10D/src/Core/SpanExtensions.cs @@ -121,12 +121,16 @@ public static class SpanExtensions return MemoryMarshal.CreateSpan(ref enums, span.Length).Contains(Unsafe.As(ref value)); } + // dotcover disable + //NOSONAR default: #if NET7_0_OR_GREATER throw new UnreachableException($"Enum with the size of {Unsafe.SizeOf()} bytes is unexpected."); #else throw new ArgumentException($"Enum with the size of {Unsafe.SizeOf()} bytes is unexpected."); #endif + //NOSONAR + // dotcover enable } } #else