2022-04-28 08:47:48 +00:00
|
|
|
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
2023-04-01 20:56:45 +00:00
|
|
|
|
using Moq;
|
2022-04-28 08:47:48 +00:00
|
|
|
|
using X10D.Collections;
|
|
|
|
|
|
|
|
|
|
namespace X10D.Tests.Collections;
|
|
|
|
|
|
|
|
|
|
[TestClass]
|
|
|
|
|
public class Int16Tests
|
|
|
|
|
{
|
|
|
|
|
[TestMethod]
|
2023-04-01 20:56:45 +00:00
|
|
|
|
public void Unpack_ShouldUnpackToArrayCorrectly()
|
2022-04-28 08:47:48 +00:00
|
|
|
|
{
|
2022-04-28 21:58:58 +00:00
|
|
|
|
bool[] bits = ((short)0b11010100).Unpack();
|
2022-06-01 14:35:00 +00:00
|
|
|
|
|
2022-04-28 08:47:48 +00:00
|
|
|
|
Assert.AreEqual(16, bits.Length);
|
|
|
|
|
|
|
|
|
|
Assert.IsFalse(bits[0]);
|
|
|
|
|
Assert.IsFalse(bits[1]);
|
|
|
|
|
Assert.IsTrue(bits[2]);
|
|
|
|
|
Assert.IsFalse(bits[3]);
|
|
|
|
|
Assert.IsTrue(bits[4]);
|
|
|
|
|
Assert.IsFalse(bits[5]);
|
|
|
|
|
Assert.IsTrue(bits[6]);
|
|
|
|
|
Assert.IsTrue(bits[7]);
|
|
|
|
|
|
|
|
|
|
for (var index = 8; index < 16; index++)
|
|
|
|
|
{
|
|
|
|
|
Assert.IsFalse(bits[index]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
2023-04-01 20:56:45 +00:00
|
|
|
|
public void Unpack_ShouldUnpackToSpanCorrectly()
|
2022-04-28 08:47:48 +00:00
|
|
|
|
{
|
|
|
|
|
Span<bool> bits = stackalloc bool[16];
|
2022-04-28 21:58:58 +00:00
|
|
|
|
((short)0b11010100).Unpack(bits);
|
2022-04-28 08:47:48 +00:00
|
|
|
|
|
|
|
|
|
Assert.IsFalse(bits[0]);
|
|
|
|
|
Assert.IsFalse(bits[1]);
|
|
|
|
|
Assert.IsTrue(bits[2]);
|
|
|
|
|
Assert.IsFalse(bits[3]);
|
|
|
|
|
Assert.IsTrue(bits[4]);
|
|
|
|
|
Assert.IsFalse(bits[5]);
|
|
|
|
|
Assert.IsTrue(bits[6]);
|
|
|
|
|
Assert.IsTrue(bits[7]);
|
|
|
|
|
|
|
|
|
|
for (var index = 8; index < 16; index++)
|
|
|
|
|
{
|
|
|
|
|
Assert.IsFalse(bits[index]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-01 20:56:45 +00:00
|
|
|
|
#if NET5_0_OR_GREATER
|
2022-04-28 08:47:48 +00:00
|
|
|
|
[TestMethod]
|
2023-04-01 20:56:45 +00:00
|
|
|
|
public void Unpack_ShouldUnpackToSpanCorrectly_GivenFallbackImplementation()
|
|
|
|
|
{
|
|
|
|
|
var mock = new Mock<ISsse3SupportProvider>();
|
|
|
|
|
mock.Setup(provider => provider.IsSupported).Returns(false);
|
|
|
|
|
|
|
|
|
|
Span<bool> bits = stackalloc bool[16];
|
|
|
|
|
((short)0b11010100).UnpackInternal(bits, mock.Object);
|
|
|
|
|
|
|
|
|
|
Assert.IsFalse(bits[0]);
|
|
|
|
|
Assert.IsFalse(bits[1]);
|
|
|
|
|
Assert.IsTrue(bits[2]);
|
|
|
|
|
Assert.IsFalse(bits[3]);
|
|
|
|
|
Assert.IsTrue(bits[4]);
|
|
|
|
|
Assert.IsFalse(bits[5]);
|
|
|
|
|
Assert.IsTrue(bits[6]);
|
|
|
|
|
Assert.IsTrue(bits[7]);
|
|
|
|
|
|
|
|
|
|
for (var index = 8; index < 16; index++)
|
|
|
|
|
{
|
|
|
|
|
Assert.IsFalse(bits[index]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void Unpack_ShouldRepackEqually()
|
2022-04-28 08:47:48 +00:00
|
|
|
|
{
|
2022-04-28 21:58:58 +00:00
|
|
|
|
Assert.AreEqual(0b11010100, ((short)0b11010100).Unpack().PackInt16());
|
2022-04-28 08:47:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
2023-04-01 20:56:45 +00:00
|
|
|
|
public void Unpack_ShouldThrow_GivenTooSmallSpan()
|
2022-04-28 08:47:48 +00:00
|
|
|
|
{
|
|
|
|
|
Assert.ThrowsException<ArgumentException>(() =>
|
|
|
|
|
{
|
|
|
|
|
Span<bool> bits = stackalloc bool[0];
|
2022-04-28 21:58:58 +00:00
|
|
|
|
((short)0b11010100).Unpack(bits);
|
2022-04-28 08:47:48 +00:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|