2022-04-28 09:47:48 +01:00
|
|
|
|
using System.Diagnostics;
|
2023-04-05 22:26:31 +01:00
|
|
|
|
using NUnit.Framework;
|
2022-04-28 09:47:48 +01:00
|
|
|
|
using X10D.Collections;
|
|
|
|
|
|
|
|
|
|
namespace X10D.Tests.Collections;
|
|
|
|
|
|
2023-04-05 22:26:31 +01:00
|
|
|
|
[TestFixture]
|
2022-04-28 09:47:48 +01:00
|
|
|
|
public class Int64Tests
|
|
|
|
|
{
|
2023-04-05 22:26:31 +01:00
|
|
|
|
[Test]
|
2022-04-28 09:47:48 +01:00
|
|
|
|
public void UnpackBits_ShouldUnpackToArrayCorrectly()
|
|
|
|
|
{
|
2022-04-28 22:58:58 +01:00
|
|
|
|
bool[] bits = 0b11010100L.Unpack();
|
2022-04-28 09:47:48 +01:00
|
|
|
|
|
2023-04-05 22:26:31 +01:00
|
|
|
|
Assert.That(bits, Has.Length.EqualTo(64));
|
2022-04-28 09:47:48 +01:00
|
|
|
|
|
|
|
|
|
Trace.WriteLine(Convert.ToString(0b11010100L, 2));
|
|
|
|
|
Trace.WriteLine(string.Join("", bits.Select(b => b ? "1" : "0")));
|
2023-04-05 22:26:31 +01:00
|
|
|
|
Assert.Multiple(() =>
|
2022-04-28 09:47:48 +01:00
|
|
|
|
{
|
2023-04-05 22:26:31 +01:00
|
|
|
|
Assert.That(bits[0], Is.False);
|
|
|
|
|
Assert.That(bits[1], Is.False);
|
|
|
|
|
Assert.That(bits[2], Is.True);
|
|
|
|
|
Assert.That(bits[3], Is.False);
|
|
|
|
|
Assert.That(bits[4], Is.True);
|
|
|
|
|
Assert.That(bits[5], Is.False);
|
|
|
|
|
Assert.That(bits[6], Is.True);
|
|
|
|
|
Assert.That(bits[7], Is.True);
|
|
|
|
|
|
|
|
|
|
for (var index = 8; index < 64; index++)
|
|
|
|
|
{
|
|
|
|
|
Assert.That(bits[index], Is.False, index.ToString());
|
|
|
|
|
}
|
|
|
|
|
});
|
2022-04-28 09:47:48 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-05 22:26:31 +01:00
|
|
|
|
[Test]
|
2022-04-28 09:47:48 +01:00
|
|
|
|
public void UnpackBits_ShouldUnpackToSpanCorrectly()
|
|
|
|
|
{
|
2023-04-05 22:26:31 +01:00
|
|
|
|
Assert.Multiple(() =>
|
|
|
|
|
{
|
|
|
|
|
Span<bool> bits = stackalloc bool[64];
|
|
|
|
|
0b11010100L.Unpack(bits);
|
2022-04-28 09:47:48 +01:00
|
|
|
|
|
2023-04-05 22:26:31 +01:00
|
|
|
|
Assert.That(bits[0], Is.False);
|
|
|
|
|
Assert.That(bits[1], Is.False);
|
|
|
|
|
Assert.That(bits[2], Is.True);
|
|
|
|
|
Assert.That(bits[3], Is.False);
|
|
|
|
|
Assert.That(bits[4], Is.True);
|
|
|
|
|
Assert.That(bits[5], Is.False);
|
|
|
|
|
Assert.That(bits[6], Is.True);
|
|
|
|
|
Assert.That(bits[7], Is.True);
|
2022-04-28 09:47:48 +01:00
|
|
|
|
|
2023-04-05 22:26:31 +01:00
|
|
|
|
for (var index = 8; index < 64; index++)
|
|
|
|
|
{
|
|
|
|
|
Assert.That(bits[index], Is.False, index.ToString());
|
|
|
|
|
}
|
|
|
|
|
});
|
2022-04-28 09:47:48 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-05 22:26:31 +01:00
|
|
|
|
[Test]
|
2022-04-28 09:47:48 +01:00
|
|
|
|
public void UnpackBits_ShouldRepackEqually()
|
|
|
|
|
{
|
2023-04-05 22:26:31 +01:00
|
|
|
|
Assert.That(0b11010100L.Unpack().PackInt64(), Is.EqualTo(0b11010100L));
|
2022-04-28 09:47:48 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-05 22:26:31 +01:00
|
|
|
|
[Test]
|
2022-04-28 09:47:48 +01:00
|
|
|
|
public void UnpackBits_ShouldThrow_GivenTooSmallSpan()
|
|
|
|
|
{
|
2023-04-05 22:26:31 +01:00
|
|
|
|
Assert.Throws<ArgumentException>(() =>
|
2022-04-28 09:47:48 +01:00
|
|
|
|
{
|
|
|
|
|
Span<bool> bits = stackalloc bool[0];
|
2022-04-28 22:58:58 +01:00
|
|
|
|
0b11010100L.Unpack(bits);
|
2022-04-28 09:47:48 +01:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|