Skip to content

Commit

Permalink
Clean up math unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Tornado-Technology committed Jul 12, 2024
1 parent 761496b commit e981967
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 65 deletions.
1 change: 1 addition & 0 deletions Hypercube.UnitTests/Math/AngleTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public static void Degrees()
Assert.That(Angle.FromDegrees(45d), Is.EqualTo(new Angle(HyperMath.PIOver4)));
Assert.That(Angle.FromDegrees(30d), Is.EqualTo(new Angle(HyperMath.PIOver6)));
});

Assert.Pass($"{nameof(Angle)} degrees passed");
}

Expand Down
4 changes: 2 additions & 2 deletions Hypercube.UnitTests/Math/FloatingPointTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace Hypercube.UnitTests.Math;

public class FloatingPointTest
public static class FloatingPointTest
{
[Test]
public void Equals()
public static void Equals()
{
Assert.Multiple(() =>
{
Expand Down
36 changes: 8 additions & 28 deletions Hypercube.UnitTests/Math/Matrix4X4Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,21 @@

namespace Hypercube.UnitTests.Math;

public sealed class Matrix4X4Test
public static class Matrix4X4Test
{
[Test]
public void Equals()
{
var matrixA = new Matrix4X4(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16);
var matrixB = new Matrix4X4(16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1);

// ReSharper disable once EqualExpressionComparison
Assert.That(matrixA == matrixA);
Assert.That(matrixA.Equals(matrixA));
Assert.That(matrixA.Equals((object)matrixA));

var matrixAClone = new Matrix4X4(matrixA);
Assert.That(matrixA == matrixAClone);
Assert.That(matrixA.Equals(matrixAClone));
Assert.That(matrixA.Equals((object)matrixAClone));

Assert.That(matrixA != matrixB);
Assert.That(!matrixA.Equals(matrixB));
Assert.That(!matrixA.Equals((object)matrixB));

Assert.Pass($"{nameof(Matrix4X4)} equals passed");
}

[Test]
public void Multiplication()
public static void Multiplication()
{
var matrixA = new Matrix4X4(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16);
var matrixB = new Matrix4X4(16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1);
var vectorA = new Vector4(20, 60, 32, 7);

Assert.That(matrixA * matrixB == new Matrix4X4(80, 70, 60, 50, 240, 214, 188, 162, 400, 358, 316, 274, 560, 502, 444, 386));
Assert.That(matrixA * vectorA == new Vector4(264, 740, 1216, 1692));
Assert.That(matrixB * vectorA == new Vector4(1759, 1283, 807, 331));
Assert.Multiple(() =>
{
Assert.That(matrixA * matrixB, Is.EqualTo(new Matrix4X4(80, 70, 60, 50, 240, 214, 188, 162, 400, 358, 316, 274, 560, 502, 444, 386)));
Assert.That(matrixA * vectorA, Is.EqualTo(new Vector4(264, 740, 1216, 1692)));
Assert.That(matrixB * vectorA, Is.EqualTo(new Vector4(1759, 1283, 807, 331)));
});

Assert.Pass($"{nameof(Matrix4X4)} multiplication passed");
}
Expand Down
53 changes: 18 additions & 35 deletions Hypercube.UnitTests/Math/QuaternionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,69 +3,52 @@

namespace Hypercube.UnitTests.Math;

public class QuaternionTest
public static class QuaternionTest
{
[Test]
public void Equals()
{
var quaternionA = new Quaternion(1, 2, 3, 4);
var quaternionB = new Quaternion(10, 22, 40, 32);

// ReSharper disable once EqualExpressionComparison
Assert.That(quaternionA == quaternionA);
Assert.That(quaternionA.Equals(quaternionA));
Assert.That(quaternionA.Equals((object)quaternionA));

var quaternionAClone = new Quaternion(quaternionA);
Assert.That(quaternionA == quaternionAClone);
Assert.That(quaternionA.Equals(quaternionAClone));
Assert.That(quaternionA.Equals((object)quaternionAClone));

Assert.That(quaternionA != quaternionB);
Assert.That(!quaternionA.Equals(quaternionB));
Assert.That(!quaternionA.Equals((object)quaternionB));

Assert.Pass($"{nameof(Quaternion)} equals passed");
}

/// <summary>
/// Can ba useful, cite to 3d convert: https://www.andre-gaschler.com/rotationconverter/
/// </summary>
[Test]
public void ToEuler()
public static void ToEuler()
{
var eulerA = new Quaternion(1, 2, 3, 4).ToEuler();
var eulerB = new Quaternion(0, 0.6767456f, 0.4308296f, 0.5969936f).ToEuler();

Assert.That(eulerA == new Vector3(-0.19739556f, 0.8232120f, 1.3734008f));
Assert.That(eulerB == new Vector3(-1.42767680f, 0.9407929f, 2.0799970f));
Assert.Multiple(() =>
{
Assert.That(eulerA, Is.EqualTo(new Vector3(-0.19739556f, 0.8232120f, 1.3734008f)));
Assert.That(eulerB, Is.EqualTo(new Vector3(-1.42767680f, 0.9407929f, 2.0799970f)));
});

Assert.Pass($"{nameof(Quaternion)} to euler passed");
}

[Test]
public void FromEulerUnit()
public static void FromEulerUnit()
{
var quaternionUnitX = Quaternion.FromEuler(Vector3.UnitX);
var quaternionUnitY = Quaternion.FromEuler(Vector3.UnitY);
var quaternionUnitZ = Quaternion.FromEuler(Vector3.UnitZ);

Assert.That(quaternionUnitX == new Quaternion(0.47942555f, 0, 0, 0.87758255f));
Assert.That(quaternionUnitY == new Quaternion(0, 0.47942555f, 0, 0.87758255f));
Assert.That(quaternionUnitZ == new Quaternion(0, 0, 0.47942555f, 0.87758255f));

Assert.Multiple(() =>
{
Assert.That(quaternionUnitX, Is.EqualTo(new Quaternion(0.47942555f, 0, 0, 0.87758255f)));
Assert.That(quaternionUnitY, Is.EqualTo(new Quaternion(0, 0.47942555f, 0, 0.87758255f)));
Assert.That(quaternionUnitZ, Is.EqualTo(new Quaternion(0, 0, 0.47942555f, 0.87758255f)));
});

Assert.Pass($"{nameof(Quaternion)} from euler unit passed");
}

[Test]
public void FromEulerConvert()
public static void FromEulerConvert()
{
var quaternionA = new Quaternion(0.8232120f, 0.6767456f, 0.4308296f, 0.5969936f);
var eulerA = quaternionA.ToEuler();
var fromA = Quaternion.FromEuler(eulerA);

// The losses on this convert are fucked up.
Assert.That(fromA == new Quaternion(0.6355612f, 0.5224818f, 0.33262214f, 0.4609092f));
// The losses on this convert are fucked up
Assert.That(fromA, Is.EqualTo(new Quaternion(0.6355612f, 0.5224818f, 0.33262214f, 0.4609092f)));

Assert.Pass($"{nameof(Quaternion)} from euler convert passed");
}
Expand Down

0 comments on commit e981967

Please sign in to comment.