1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-10 03:45:41 +00:00

test: add unit tests for Span.Contains(Enum) (#73)

This commit is contained in:
Oliver Booth 2023-04-02 22:11:33 +01:00
parent 4fc0d01670
commit 78cebbce8b
No known key found for this signature in database
GPG Key ID: 20BEB9DC87961025
2 changed files with 169 additions and 1 deletions

View File

@ -1,4 +1,4 @@
using Microsoft.VisualStudio.TestTools.UnitTesting; using Microsoft.VisualStudio.TestTools.UnitTesting;
using X10D.Collections; using X10D.Collections;
using X10D.Core; using X10D.Core;
@ -7,6 +7,142 @@ namespace X10D.Tests.Core;
[TestClass] [TestClass]
public class SpanTest public class SpanTest
{ {
[TestMethod]
public void Contains_ShouldReturnFalse_GivenReadOnlySpanWithNoMatchingElements_UsingByteEnum()
{
ReadOnlySpan<EnumByte> 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<EnumInt16> 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<EnumInt32> 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<EnumInt64> 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<EnumByte> span = stackalloc EnumByte[1] {EnumByte.B};
Assert.IsTrue(span.Contains(EnumByte.B));
}
[TestMethod]
public void Contains_ShouldReturnTrue_GivenReadOnlySpanWithMatchingElements_UsingInt16Enum()
{
ReadOnlySpan<EnumInt16> span = stackalloc EnumInt16[1] {EnumInt16.B};
Assert.IsTrue(span.Contains(EnumInt16.B));
}
[TestMethod]
public void Contains_ShouldReturnTrue_GivenReadOnlySpanWithMatchingElements_UsingInt32Enum()
{
ReadOnlySpan<EnumInt32> span = stackalloc EnumInt32[1] {EnumInt32.B};
Assert.IsTrue(span.Contains(EnumInt32.B));
}
[TestMethod]
public void Contains_ShouldReturnTrue_GivenReadOnlySpanWithMatchingElements_UsingInt64Enum()
{
ReadOnlySpan<EnumInt64> span = stackalloc EnumInt64[1] {EnumInt64.B};
Assert.IsTrue(span.Contains(EnumInt64.B));
}
[TestMethod]
public void Contains_ShouldReturnFalse_GivenSpanWithNoMatchingElements_UsingByteEnum()
{
Span<EnumByte> 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<EnumInt16> 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<EnumInt32> 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<EnumInt64> 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<EnumByte> span = stackalloc EnumByte[1] {EnumByte.B};
Assert.IsTrue(span.Contains(EnumByte.B));
}
[TestMethod]
public void Contains_ShouldReturnTrue_GivenSpanWithMatchingElements_UsingInt16Enum()
{
Span<EnumInt16> span = stackalloc EnumInt16[1] {EnumInt16.B};
Assert.IsTrue(span.Contains(EnumInt16.B));
}
[TestMethod]
public void Contains_ShouldReturnTrue_GivenSpanWithMatchingElements_UsingInt32Enum()
{
Span<EnumInt32> span = stackalloc EnumInt32[1] {EnumInt32.B};
Assert.IsTrue(span.Contains(EnumInt32.B));
}
[TestMethod]
public void Contains_ShouldReturnTrue_GivenSpanWithMatchingElements_UsingInt64Enum()
{
Span<EnumInt64> span = stackalloc EnumInt64[1] {EnumInt64.B};
Assert.IsTrue(span.Contains(EnumInt64.B));
}
[TestMethod] [TestMethod]
public void Pack8Bit_Should_Pack_Correctly() public void Pack8Bit_Should_Pack_Correctly()
{ {
@ -96,4 +232,32 @@ public class SpanTest
Assert.AreEqual(value, unpacks.PackInt64()); 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
}
} }

View File

@ -121,12 +121,16 @@ public static class SpanExtensions
return MemoryMarshal.CreateSpan(ref enums, span.Length).Contains(Unsafe.As<T, ulong>(ref value)); return MemoryMarshal.CreateSpan(ref enums, span.Length).Contains(Unsafe.As<T, ulong>(ref value));
} }
// dotcover disable
//NOSONAR
default: default:
#if NET7_0_OR_GREATER #if NET7_0_OR_GREATER
throw new UnreachableException($"Enum with the size of {Unsafe.SizeOf<T>()} bytes is unexpected."); throw new UnreachableException($"Enum with the size of {Unsafe.SizeOf<T>()} bytes is unexpected.");
#else #else
throw new ArgumentException($"Enum with the size of {Unsafe.SizeOf<T>()} bytes is unexpected."); throw new ArgumentException($"Enum with the size of {Unsafe.SizeOf<T>()} bytes is unexpected.");
#endif #endif
//NOSONAR
// dotcover enable
} }
} }
#else #else