mirror of
https://github.com/oliverbooth/X10D
synced 2024-11-09 23:45:42 +00:00
test: add unit tests for Span.Contains(Enum) (#73)
This commit is contained in:
parent
4fc0d01670
commit
78cebbce8b
@ -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<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]
|
||||
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
|
||||
}
|
||||
}
|
||||
|
@ -121,12 +121,16 @@ public static class SpanExtensions
|
||||
return MemoryMarshal.CreateSpan(ref enums, span.Length).Contains(Unsafe.As<T, ulong>(ref value));
|
||||
}
|
||||
|
||||
// dotcover disable
|
||||
//NOSONAR
|
||||
default:
|
||||
#if NET7_0_OR_GREATER
|
||||
throw new UnreachableException($"Enum with the size of {Unsafe.SizeOf<T>()} bytes is unexpected.");
|
||||
#else
|
||||
throw new ArgumentException($"Enum with the size of {Unsafe.SizeOf<T>()} bytes is unexpected.");
|
||||
#endif
|
||||
//NOSONAR
|
||||
// dotcover enable
|
||||
}
|
||||
}
|
||||
#else
|
||||
|
Loading…
Reference in New Issue
Block a user