Skip to content

Commit

Permalink
Add Box matrix tests
Browse files Browse the repository at this point in the history
Thought we had but apparently not.
  • Loading branch information
metalgearsloth committed Aug 27, 2024
1 parent cd95929 commit a5cde7f
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
41 changes: 41 additions & 0 deletions Robust.UnitTesting/Shared/Maths/Box2Rotated_Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,47 @@ public void FullRotationTest([ValueSource(nameof(BoxRotations))] Box2[] boxes)
(new Box2(-1, 1, 1, 2), new Vector2(0, 0), -Math.PI/2, new Box2(1, -1, 2, 1)),
};

private static TestCaseData[] MatrixCases = new[]
{
new TestCaseData(Matrix3x2.Identity,
Box2Rotated.UnitCentered,
Box2Rotated.UnitCentered),
new TestCaseData(Matrix3x2.CreateRotation(MathF.PI),
Box2Rotated.UnitCentered,
new Box2Rotated(new Vector2(0.5f, 0.5f), new Vector2(-0.5f, -0.5f))),
new TestCaseData(Matrix3x2.CreateTranslation(Vector2.One),
Box2Rotated.UnitCentered,
new Box2Rotated(new Vector2(0.5f, 0.5f), new Vector2(1.5f, 1.5f))),
};

[Test, TestCaseSource(nameof(MatrixCases))]
public void TestBox2RotatedMatrices(Matrix3x2 matrix, Box2Rotated bounds, Box2Rotated result)
{
Assert.That(matrix.TransformBounds(bounds), Is.EqualTo(result));
}

private static TestCaseData[] MatrixBox2Cases = new[]
{
new TestCaseData(Matrix3x2.Identity,
Box2Rotated.UnitCentered,
Box2Rotated.UnitCentered.CalcBoundingBox()),
new TestCaseData(Matrix3x2.CreateRotation(MathF.PI),
Box2Rotated.UnitCentered,
new Box2Rotated(new Vector2(0.5f, 0.5f), new Vector2(-0.5f, -0.5f)).CalcBoundingBox()),
new TestCaseData(Matrix3x2.CreateTranslation(Vector2.One),
Box2Rotated.UnitCentered,
new Box2Rotated(new Vector2(0.5f, 0.5f), new Vector2(1.5f, 1.5f)).CalcBoundingBox()),
};

/// <summary>
/// Tests that transforming a Box2Rotated into a Box2 works.
/// </summary>
[Test, TestCaseSource(nameof(MatrixBox2Cases))]
public void TestBox2Matrices(Matrix3x2 matrix, Box2Rotated bounds, Box2 result)
{
Assert.That(matrix.TransformBox(bounds), Is.EqualTo(result));
}

[Test]
public void TestCalcBoundingBox([ValueSource(nameof(CalcBoundingBoxData))]
(Box2 baseBox, Vector2 origin, Angle rotation, Box2 expected) dat)
Expand Down
19 changes: 19 additions & 0 deletions Robust.UnitTesting/Shared/Maths/Box2_Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,25 @@ public sealed class Box2_Test
10.0f
};

private static TestCaseData[] MatrixCases = new[]
{
new TestCaseData(Matrix3x2.Identity,
Box2.UnitCentered,
Box2.UnitCentered),
new TestCaseData(Matrix3x2.CreateRotation(MathF.PI),
Box2.UnitCentered,
new Box2(new Vector2(-0.5f, -0.5f), new Vector2(0.5f, 0.5f))),
new TestCaseData(Matrix3x2.CreateTranslation(Vector2.One),
Box2.UnitCentered,
new Box2(new Vector2(0.5f, 0.5f), new Vector2(1.5f, 1.5f))),
};

[Test, TestCaseSource(nameof(MatrixCases))]
public void TestBox2Matrices(Matrix3x2 matrix, Box2 bounds, Box2 result)
{
Assert.That(matrix.TransformBox(bounds), Is.EqualTo(result));
}

/// <summary>
/// Check whether the sources list has correct data.
/// That is, no boxes where left > right or top > bottom.
Expand Down

0 comments on commit a5cde7f

Please sign in to comment.