mirror of
https://github.com/oliverbooth/X10D
synced 2024-11-14 22:35:42 +00:00
88 lines
2.4 KiB
C#
88 lines
2.4 KiB
C#
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|
using X10D.Numerics;
|
|
|
|
namespace X10D.Tests.Numerics;
|
|
|
|
[TestClass]
|
|
public class Int16Tests
|
|
{
|
|
[TestMethod]
|
|
public void PopCount_ShouldBe0_Given0()
|
|
{
|
|
Assert.AreEqual(0, ((short)0).PopCount());
|
|
}
|
|
|
|
[TestMethod]
|
|
public void PopCount_ShouldBe5_Given11010101()
|
|
{
|
|
Assert.AreEqual(5, ((short)0b11010101).PopCount());
|
|
}
|
|
|
|
[TestMethod]
|
|
public void PopCount_ShouldBe15_Given0111111111111111()
|
|
{
|
|
Assert.AreEqual(15, ((short)0b0111111111111111).PopCount());
|
|
}
|
|
|
|
[TestMethod]
|
|
public void RotateLeft_ShouldRotateCorrectly()
|
|
{
|
|
const short value = 2896; // 00001011 01010000
|
|
const short expected = 27137; // 01101010 00000001
|
|
|
|
Assert.AreEqual(value, value.RotateLeft(0));
|
|
Assert.AreEqual(expected, value.RotateLeft(5));
|
|
Assert.AreEqual(value, value.RotateLeft(16));
|
|
}
|
|
|
|
[TestMethod]
|
|
public void RotateLeft_ShouldModForLargeCount()
|
|
{
|
|
const short value = 2896; // 00001011 01010000
|
|
Assert.AreEqual(value, value.RotateLeft(16));
|
|
}
|
|
|
|
[TestMethod]
|
|
public void RotateRight_ShouldRotateCorrectly()
|
|
{
|
|
const short value = 2896; // 00001011 01010000
|
|
const short expected = -32678; // 01111111 10100110
|
|
|
|
Assert.AreEqual(value, value.RotateRight(0));
|
|
Assert.AreEqual(expected, value.RotateRight(5));
|
|
Assert.AreEqual(value, value.RotateRight(16));
|
|
}
|
|
|
|
[TestMethod]
|
|
public void RotateRight_ShouldModForLargeCount()
|
|
{
|
|
const short value = 2896; // 00001011 01010000
|
|
Assert.AreEqual(value, value.RotateRight(16));
|
|
}
|
|
|
|
[TestMethod]
|
|
public void RoundUpToPowerOf2_ShouldReturnRoundedValue_GivenValue()
|
|
{
|
|
Assert.AreEqual(4, ((short)3).RoundUpToPowerOf2());
|
|
Assert.AreEqual(8, ((short)5).RoundUpToPowerOf2());
|
|
Assert.AreEqual(8, ((short)6).RoundUpToPowerOf2());
|
|
Assert.AreEqual(8, ((short)7).RoundUpToPowerOf2());
|
|
}
|
|
|
|
[TestMethod]
|
|
public void RoundUpToPowerOf2_ShouldReturnSameValue_GivenPowerOf2()
|
|
{
|
|
for (var i = 0; i < 8; i++)
|
|
{
|
|
var value = (short)System.Math.Pow(2, i);
|
|
Assert.AreEqual(value, value.RoundUpToPowerOf2());
|
|
}
|
|
}
|
|
|
|
[TestMethod]
|
|
public void RoundUpToPowerOf2_ShouldReturn0_Given0()
|
|
{
|
|
Assert.AreEqual(0, ((short)0).RoundUpToPowerOf2());
|
|
}
|
|
}
|