1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-10 16:49:09 +01:00
MLEM/Tests/NumberTests.cs
2022-06-17 18:23:47 +02:00

71 lines
3.5 KiB
C#

using Microsoft.Xna.Framework;
using MLEM.Extensions;
using MLEM.Misc;
using NUnit.Framework;
namespace Tests {
public class NumberTests {
[Test]
public void TestRounding() {
Assert.AreEqual(1.25F.Floor(), 1);
Assert.AreEqual(-1.25F.Floor(), -1);
Assert.AreEqual(1.25F.Ceil(), 2);
Assert.AreEqual(-1.25F.Ceil(), -2);
Assert.AreEqual(new Vector2(5, 2.5F).FloorCopy(), new Vector2(5, 2));
Assert.AreEqual(new Vector2(5, 2.5F).CeilCopy(), new Vector2(5, 3));
Assert.AreEqual(new Vector2(5.25F, 2).FloorCopy(), new Vector2(5, 2));
Assert.AreEqual(new Vector2(5.25F, 2).CeilCopy(), new Vector2(6, 2));
}
[Test]
public void TestEquals() {
Assert.IsTrue(0.25F.Equals(0.26F, 0.01F));
Assert.IsFalse(0.25F.Equals(0.26F, 0.009F));
Assert.IsTrue(new Vector2(0.25F, 0).Equals(new Vector2(0.3F, 0), 0.1F));
Assert.IsFalse(new Vector2(0.25F, 0.5F).Equals(new Vector2(0.3F, 0.25F), 0.1F));
Assert.IsTrue(new Vector3(0.25F, 0, 3.5F).Equals(new Vector3(0.3F, 0, 3.45F), 0.1F));
Assert.IsFalse(new Vector3(0.25F, 0.5F, 0).Equals(new Vector3(0.3F, 0.25F, 0), 0.1F));
Assert.IsTrue(new Vector4(0.25F, 0, 3.5F, 0.75F).Equals(new Vector4(0.3F, 0, 3.45F, 0.7F), 0.1F));
Assert.IsFalse(new Vector4(0.25F, 0.5F, 0, 1).Equals(new Vector4(0.3F, 0.25F, 0, 0.9F), 0.1F));
}
[Test]
public void TestPointExtensions() {
Assert.AreEqual(new Point(4, 3).Multiply(3), new Point(12, 9));
Assert.AreEqual(new Point(17, 12).Divide(3), new Point(5, 4));
Assert.AreEqual(new Point(4, 12).Transform(Matrix.CreateTranslation(2, 0, 0) * Matrix.CreateScale(2, 2, 2)), new Point(12, 24));
}
[Test]
public void TestMatrixOps([Range(0.5F, 2, 0.5F)] float scale, [Range(-0.5F, 0.5F, 1)] float rotationX, [Range(-0.5F, 0.5F, 1)] float rotationY, [Range(-0.5F, 0.5F, 1)] float rotationZ) {
var rotation = Matrix.CreateRotationX(rotationX) * Matrix.CreateRotationY(rotationY) * Matrix.CreateRotationZ(rotationZ);
var matrix = rotation * Matrix.CreateScale(scale, scale, scale);
Assert.IsTrue(matrix.Scale().Equals(new Vector3(scale), 0.001F), $"{matrix.Scale()} does not equal {new Vector2(scale)}");
Assert.IsTrue(matrix.Rotation().Equals(Quaternion.CreateFromRotationMatrix(rotation), 0.001F), $"{matrix.Rotation()} does not equal {Quaternion.CreateFromRotationMatrix(rotation)}");
Assert.IsTrue(matrix.RotationVector().Equals(new Vector3(rotationX, rotationY, rotationZ), 0.001F), $"{matrix.RotationVector()} does not equal {new Vector3(rotationX, rotationY, rotationZ)}");
// check against decomposed results
matrix.Decompose(out var sc, out var rot, out _);
Assert.AreEqual(matrix.Rotation(), rot);
Assert.AreEqual(matrix.Scale(), sc);
}
[Test]
public void TestPenetrate() {
new RectangleF(2, 2, 4, 4).Penetrate(new RectangleF(3, 2, 4, 4), out var normal, out var penetration);
Assert.AreEqual(normal, new Vector2(1, 0));
Assert.AreEqual(penetration, 3);
new RectangleF(-10, 10, 5, 5).Penetrate(new RectangleF(25, 25, 10, 10), out normal, out penetration);
Assert.AreEqual(normal, Vector2.Zero);
Assert.AreEqual(penetration, 0);
}
}
}