diff options
Diffstat (limited to 'Plugins/MonoGame.Extended/tests/MonoGame.Extended.Tests/Primitives')
10 files changed, 2531 insertions, 0 deletions
diff --git a/Plugins/MonoGame.Extended/tests/MonoGame.Extended.Tests/Primitives/BoundingRectangleTests.cs b/Plugins/MonoGame.Extended/tests/MonoGame.Extended.Tests/Primitives/BoundingRectangleTests.cs new file mode 100644 index 0000000..6f95cf5 --- /dev/null +++ b/Plugins/MonoGame.Extended/tests/MonoGame.Extended.Tests/Primitives/BoundingRectangleTests.cs @@ -0,0 +1,396 @@ +//using System.Collections.Generic; +//using System.Globalization; +//using Microsoft.Xna.Framework; +//using Xunit; + +//namespace MonoGame.Extended.Tests.Primitives +//{ +// +// public class BoundingRectangleTests +// { +// public IEnumerable<TestCaseData> ConstructorTestCases +// { +// get +// { +// yield return +// new TestCaseData(new Point2(), new Vector2()).SetName( +// "The empty bounding rectangle has the expected position and radii."); +// yield return +// new TestCaseData(new Point2(5, 5), new Vector2(15, 15)).SetName( +// "A non-empty bounding rectangle has the expected position and radii."); +// } +// } + +// [Fact] +// [TestCaseSource(nameof(ConstructorTestCases))] +// public void Constructor(Point2 centre, Vector2 radii) +// { +// var boundingRectangle = new BoundingRectangle(centre, radii); +// Assert.Equal(centre, boundingRectangle.Center); +// Assert.Equal(radii, boundingRectangle.HalfExtents); +// } + +// public IEnumerable<TestCaseData> CreateFromMinimumMaximumTestCases +// { +// get +// { +// yield return +// new TestCaseData(new Point2(), new Point2(), new BoundingRectangle()).SetName( +// "The bounding rectangle created from the zero minimum point and zero maximum point is the empty bounding rectangle.") +// ; +// yield return +// new TestCaseData(new Point2(5, 5), new Point2(15, 15), +// new BoundingRectangle(new Point2(10, 10), new Size2(5, 5))).SetName( +// "The bounding rectangle created from the non-zero minimum point and the non-zero maximum point is the expected bounding rectangle.") +// ; +// } +// } + +// [Fact] +// [TestCaseSource(nameof(CreateFromMinimumMaximumTestCases))] +// public void CreateFromMinimumMaximum(Point2 minimum, Point2 maximum, BoundingRectangle expectedBoundingRectangle) +// { +// var actualBoundingRectangle = BoundingRectangle.CreateFrom(minimum, maximum); +// Assert.Equal(expectedBoundingRectangle, actualBoundingRectangle); +// } + +// public IEnumerable<TestCaseData> CreateFromPointsTestCases +// { +// get +// { +// yield return +// new TestCaseData(null, new BoundingRectangle()).SetName( +// "The bounding rectangle created from null points is the empty bounding rectangle."); +// yield return +// new TestCaseData(new Point2[0], new BoundingRectangle()).SetName( +// "The bounding rectangle created from the empty set of points is the empty bounding rectangle."); +// yield return +// new TestCaseData( +// new[] +// { +// new Point2(5, 5), new Point2(10, 10), new Point2(15, 15), new Point2(-5, -5), +// new Point2(-15, -15) +// }, new BoundingRectangle(new Point2(0, 0), new Size2(15, 15))).SetName( +// "The bounding rectangle created from a non-empty set of points is the expected bounding rectangle."); +// } +// } + +// [Fact] +// [TestCaseSource(nameof(CreateFromPointsTestCases))] +// public void CreateFromPoints(Point2[] points, BoundingRectangle expectedBoundingRectangle) +// { +// var actualBoundingRectangle = BoundingRectangle.CreateFrom(points); +// Assert.Equal(expectedBoundingRectangle, actualBoundingRectangle); +// } + +// public IEnumerable<TestCaseData> CreateFromTransformedTestCases +// { +// get +// { +// yield return +// new TestCaseData(new BoundingRectangle(), Matrix2.Identity, new BoundingRectangle()).SetName( +// "The bounding rectangle created from the empty bounding rectangle transformed by the identity matrix is the empty bounding rectangle.") +// ; +// yield return +// new TestCaseData(new BoundingRectangle(new Point2(0, 0), new Size2(20, 20)), Matrix2.CreateScale(2), new BoundingRectangle(new Point2(0, 0), new Size2(40, 40))).SetName( +// "The bounding rectangle created from a non-empty bounding rectangle transformed by a non-identity matrix is the expected bounding rectangle.") +// ; +// } +// } + +// [Fact] +// [TestCaseSource(nameof(CreateFromTransformedTestCases))] +// public void CreateFromTransformed(BoundingRectangle boundingRectangle, Matrix2 transformMatrix, +// BoundingRectangle expectedBoundingRectangle) +// { +// var actualBoundingRectangle = BoundingRectangle.Transform(boundingRectangle, ref transformMatrix); +// Assert.Equal(expectedBoundingRectangle, actualBoundingRectangle); +// } + +// public IEnumerable<TestCaseData> UnionTestCases +// { +// get +// { +// yield return +// new TestCaseData(new BoundingRectangle(), new BoundingRectangle(), new BoundingRectangle()).SetName( +// "The union of two empty bounding rectangles is the empty bounding rectangle."); +// yield return +// new TestCaseData(new BoundingRectangle(new Point2(0, 0), new Size2(15, 15)), +// new BoundingRectangle(new Point2(20, 20), new Size2(40, 40)), new BoundingRectangle(new Point2(20, 20), new Size2(40, 40))) +// .SetName( +// "The union of two non-empty bounding rectangles is the expected bounding rectangle."); +// } +// } + +// [Fact] +// [TestCaseSource(nameof(UnionTestCases))] +// public void Union(BoundingRectangle boundingRectangle1, BoundingRectangle boundingRectangle2, BoundingRectangle expectedBoundingRectangle) +// { +// Assert.Equal(expectedBoundingRectangle, boundingRectangle1.Union(boundingRectangle2)); +// Assert.Equal(expectedBoundingRectangle, BoundingRectangle.Union(boundingRectangle1, boundingRectangle2)); +// } + +// public IEnumerable<TestCaseData> IntersectionTestCases +// { +// get +// { +// yield return +// new TestCaseData(new BoundingRectangle(), new BoundingRectangle(), new BoundingRectangle()).SetName( +// "The intersection of two empty bounding rectangles is the empty bounding box."); +// yield return +// new TestCaseData(new BoundingRectangle(new Point2(-10, -10), new Size2(15, 15)), +// new BoundingRectangle(new Point2(20, 20), new Size2(40, 40)), +// new BoundingRectangle(new Point2(-7.5f, -7.5f), new Size2(12.5f, 12.5f))).SetName( +// "The intersection of two overlapping non-empty bounding rectangles is the expected bounding rectangle."); +// yield return +// new TestCaseData(new BoundingRectangle(new Point2(-30, -30), new Size2(15, 15)), +// new BoundingRectangle(new Point2(20, 20), new Size2(10, 10)), +// BoundingRectangle.Empty).SetName( +// "The intersection of two non-overlapping non-empty bounding rectangles is the empty bounding rectangle."); +// } +// } + +// [Fact] +// [TestCaseSource(nameof(IntersectionTestCases))] +// public void Intersection(BoundingRectangle boundingRectangle1, BoundingRectangle boundingRectangle2, +// BoundingRectangle? expectedBoundingRectangle) +// { +// Assert.Equal(expectedBoundingRectangle, boundingRectangle1.Intersection(boundingRectangle2)); +// Assert.Equal(expectedBoundingRectangle, BoundingRectangle.Intersection(boundingRectangle1, boundingRectangle2)); +// } + +// public IEnumerable<TestCaseData> IntersectsTestCases +// { +// get +// { +// yield return +// new TestCaseData(new BoundingRectangle(), new BoundingRectangle(), true).SetName( +// "Two empty bounding rectangles intersect."); +// yield return +// new TestCaseData(new BoundingRectangle(new Point2(-10, -10), new Size2(15, 15)), +// new BoundingRectangle(new Point2(20, 20), new Size2(40, 40)), true).SetName( +// "Two overlapping non-empty bounding rectangles intersect."); +// yield return +// new TestCaseData(new BoundingRectangle(new Point2(-40, -50), new Size2(15, 15)), +// new BoundingRectangle(new Point2(20, 20), new Size2(15, 15)), false).SetName( +// "Two non-overlapping non-empty bounding rectangles do not intersect."); +// } +// } + +// [Fact] +// [TestCaseSource(nameof(IntersectsTestCases))] +// public void Intersects(BoundingRectangle boundingRectangle1, BoundingRectangle boundingRectangle2, bool expectedToIntersect) +// { +// Assert.Equal(expectedToIntersect, boundingRectangle1.Intersects(boundingRectangle2)); +// Assert.Equal(expectedToIntersect, BoundingRectangle.Intersects(boundingRectangle1, boundingRectangle2)); +// } + +// public IEnumerable<TestCaseData> ContainsPointTestCases +// { +// get +// { +// yield return +// new TestCaseData(new BoundingRectangle(), new Point2(), true).SetName( +// "The empty bounding rectangle contains the zero point."); +// yield return +// new TestCaseData(new BoundingRectangle(new Point2(0, 0), new Size2(15, 15)), new Point2(-15, -15), true) +// .SetName( +// "A non-empty bounding rectangle contains a point inside it."); +// yield return +// new TestCaseData(new BoundingRectangle(new Point2(0, 0), new Size2(15, 15)), new Point2(-16, 15), false) +// .SetName( +// "A non-empty bounding rectangle does not contain a point outside it."); +// } +// } + +// [Fact] +// [TestCaseSource(nameof(ContainsPointTestCases))] +// public void ContainsPoint(BoundingRectangle boundingRectangle, Point2 point, bool expectedToContainPoint) +// { +// Assert.Equal(expectedToContainPoint, boundingRectangle.Contains(point)); +// Assert.Equal(expectedToContainPoint, BoundingRectangle.Contains(boundingRectangle, point)); +// } + +// public IEnumerable<TestCaseData> ClosestPointTestCases +// { +// get +// { +// yield return +// new TestCaseData(new BoundingRectangle(), new Point2(), new Point2()).SetName( +// "The closest point on the empty bounding rectangle to the zero point is the zero point."); +// yield return +// new TestCaseData(new BoundingRectangle(new Point2(0, 0), new Point2(50, 50)), new Point2(25, 25), +// new Point2(25, 25)).SetName( +// "The closest point on a non-empty bounding rectangle to a point which is inside the bounding rectangle is that point.") +// ; +// yield return +// new TestCaseData(new BoundingRectangle(new Point2(0, 0), new Point2(50, 50)), new Point2(400, 0), +// new Point2(50, 0)).SetName( +// "The closest point on a non-empty bounding rectangle to a point which is outside the bounding rectangle is the expected point.") +// ; +// } +// } + +// [Fact] +// [TestCaseSource(nameof(ClosestPointTestCases))] +// public void ClosestPoint(BoundingRectangle boundingRectangle, Point2 point, Point2 expectedClosestPoint) +// { +// var actualClosestPoint = boundingRectangle.ClosestPointTo(point); +// Assert.Equal(expectedClosestPoint, actualClosestPoint); +// } + +// public IEnumerable<TestCaseData> EqualityTestCases +// { +// get +// { +// yield return +// new TestCaseData(new BoundingRectangle(), new BoundingRectangle(), true).SetName( +// "Empty bounding rectangles are equal.") +// ; +// yield return +// new TestCaseData( +// new BoundingRectangle(new Point2(0, 0), new Size2(float.MaxValue, float.MinValue)), +// new BoundingRectangle(new Point2(0, 0), +// new Point2(float.MinValue, float.MaxValue)), false).SetName( +// "Two different non-empty bounding rectangles are not equal."); +// yield return +// new TestCaseData( +// new BoundingRectangle(new Point2(0, 0), new Size2(float.MinValue, float.MaxValue)), +// new BoundingRectangle(new Point2(0, 0), +// new Size2(float.MinValue, float.MaxValue)), true).SetName( +// "Two identical non-empty bounding rectangles are equal."); +// } +// } + +// [Fact] +// [TestCaseSource(nameof(EqualityTestCases))] +// public void Equality(BoundingRectangle boundingRectangle1, BoundingRectangle boundingRectangle2, bool expectedToBeEqual) +// { +// Assert.IsTrue(Equals(boundingRectangle1, boundingRectangle2) == expectedToBeEqual); +// Assert.IsTrue(boundingRectangle1 == boundingRectangle2 == expectedToBeEqual); +// Assert.IsFalse(boundingRectangle1 == boundingRectangle2 != expectedToBeEqual); +// Assert.IsTrue(boundingRectangle1.Equals(boundingRectangle2) == expectedToBeEqual); + +// if (expectedToBeEqual) +// Assert.Equal(boundingRectangle1.GetHashCode(), boundingRectangle2.GetHashCode()); +// } + +// public IEnumerable<TestCaseData> InequalityTestCases +// { +// get +// { +// yield return +// new TestCaseData(new BoundingRectangle(), null, false).SetName( +// "A bounding rectangle is not equal to a null object."); +// yield return +// new TestCaseData(new BoundingRectangle(), new object(), false).SetName( +// "A bounding rectangle is not equal to an instantiated object."); +// } +// } + +// [Fact] +// [TestCaseSource(nameof(InequalityTestCases))] +// public void Inequality(BoundingRectangle boundingRectangle, object obj, bool expectedToBeEqual) +// { +// Assert.IsTrue(boundingRectangle.Equals(obj) == expectedToBeEqual); +// } + +// public IEnumerable<TestCaseData> HashCodeTestCases +// { +// get +// { +// yield return +// new TestCaseData(new BoundingRectangle(), new BoundingRectangle(), true).SetName( +// "Two empty bounding rectangles have the same hash code."); +// yield return +// new TestCaseData(new BoundingRectangle(new Point2(0, 0), new Size2(50, 50)), +// new BoundingRectangle(new Point2(0, 0), new Size2(50, 50)), true).SetName( +// "Two indentical non-empty bounding rectangles have the same hash code."); +// yield return +// new TestCaseData(new BoundingRectangle(new Point2(0, 0), new Size2(50, 50)), +// new BoundingRectangle(new Point2(50, 50), new Size2(50, 50)), false).SetName( +// "Two different non-empty bounding rectangles do not have the same hash code."); +// } +// } + +// [Fact] +// [TestCaseSource(nameof(HashCodeTestCases))] +// public void HashCode(BoundingRectangle boundingRectangle1, BoundingRectangle boundingRectangle2, bool expectedThatHashCodesAreEqual) +// { +// var hashCode1 = boundingRectangle1.GetHashCode(); +// var hashCode2 = boundingRectangle2.GetHashCode(); +// if (expectedThatHashCodesAreEqual) +// Assert.Equal(hashCode1, hashCode2); +// else +// Assert.AreNotEqual(hashCode1, hashCode2); +// } + +// public IEnumerable<TestCaseData> ToRectangleTestCases +// { +// get +// { +// yield return +// new TestCaseData(new BoundingRectangle(), new Rectangle()).SetName( +// "The empty bounding rectangle point converted to a rectangle is the empty rectangle."); +// yield return +// new TestCaseData(new BoundingRectangle(new Point2(25, 25), new Size2(25, 25)), +// new Rectangle(0, 0, 50, 50)).SetName( +// "A non-empty bounding rectangle converted to a rectangle is the expected rectangle."); +// } +// } + +// [Fact] +// [TestCaseSource(nameof(ToRectangleTestCases))] +// public void ToRectangle(BoundingRectangle boundingRectangle, Rectangle expectedRectangle) +// { +// var actualRectangle = (Rectangle)boundingRectangle; +// Assert.Equal(expectedRectangle, actualRectangle); +// } + +// public IEnumerable<TestCaseData> FromRectangleTestCases +// { +// get +// { +// yield return +// new TestCaseData(new Rectangle(), new BoundingRectangle()).SetName( +// "The empty rectangle converted to a bounding rectangle is the empty bounding rectangle."); +// yield return +// new TestCaseData(new Rectangle(0, 0, 50, 50), +// new BoundingRectangle(new Point2(25, 25), new Size2(25, 25))).SetName( +// "A non-empty rectangle converted to a bounding rectangle is the expected bounding rectangle."); +// } +// } + +// [Fact] +// [TestCaseSource(nameof(FromRectangleTestCases))] +// public void FromRectangle(Rectangle rectangle, BoundingRectangle expectedBoundingRectangle) +// { +// var actualBoundingRectangle = (BoundingRectangle)rectangle; +// Assert.Equal(expectedBoundingRectangle, actualBoundingRectangle); +// } + +// public IEnumerable<TestCaseData> StringCases +// { +// get +// { +// yield return +// new TestCaseData(new BoundingRectangle(), +// string.Format(CultureInfo.CurrentCulture, "Centre: {0}, Radii: {1}", new Point2(), +// new Vector2())).SetName( +// "The empty bounding rectangle has the expected string representation using the current culture."); +// yield return new TestCaseData(new BoundingRectangle(new Point2(5.1f, -5.123f), new Size2(5.4f, -5.4123f)), +// string.Format(CultureInfo.CurrentCulture, "Centre: {0}, Radii: {1}", new Point2(5.1f, -5.123f), +// new Vector2(5.4f, -5.4123f))).SetName( +// "A non-empty bounding rectangle has the expected string representation using the current culture."); +// } +// } + +// [Fact] +// [TestCaseSource(nameof(StringCases))] +// public void String(BoundingRectangle boundingRectangle, string expectedString) +// { +// var actualString = boundingRectangle.ToString(); +// Assert.Equal(expectedString, actualString); +// } +// } +//} diff --git a/Plugins/MonoGame.Extended/tests/MonoGame.Extended.Tests/Primitives/CircleFTests.cs b/Plugins/MonoGame.Extended/tests/MonoGame.Extended.Tests/Primitives/CircleFTests.cs new file mode 100644 index 0000000..f88155f --- /dev/null +++ b/Plugins/MonoGame.Extended/tests/MonoGame.Extended.Tests/Primitives/CircleFTests.cs @@ -0,0 +1,420 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using Microsoft.Xna.Framework; +using Xunit; + +namespace MonoGame.Extended.Tests.Primitives +{ + +public class CircleFTests +{ + + [Fact] + public void CircCircIntersectionDiagonalCircleTest() + { + var circle = new CircleF(new Point2(16.0f, 16.0f), 16.0f); + var point = new Point2(0, 0); + + Assert.False(circle.Contains(point)); + } +// public IEnumerable<TestCaseData> ConstructorTestCases +// { +// get +// { +// yield return +// new TestCaseData(new Point2(), 0.0f).SetName( +// "The empty circle has the expected position and radius."); +// yield return +// new TestCaseData(new Point2(5, 5), 15f).SetName( +// "A non-empty circle has the expected position and radius."); +// } +// } + +// [Fact] +// [TestCaseSource(nameof(ConstructorTestCases))] +// public void Constructor(Point2 centre, float radius) +// { +// var circle = new CircleF(centre, radius); +// Assert.Equal(centre, circle.Center); +// Assert.Equal(radius, circle.Radius); +// } + +// public IEnumerable<TestCaseData> CreateFromMinimumMaximumTestCases +// { +// get +// { +// yield return +// new TestCaseData(new Point2(), new Point2(), new CircleF()).SetName( +// "The bounding circle created from the zero minimum point and zero maximum point is the empty bounding circle.") +// ; +// yield return +// new TestCaseData(new Point2(5, 5), new Point2(15, 15), +// new CircleF(new Point2(10, 10), 5f)).SetName( +// "The bounding circle created from the non-zero minimum point and the non-zero maximum point is the expected bounding circle.") +// ; +// } +// } + +// [Fact] +// [TestCaseSource(nameof(CreateFromMinimumMaximumTestCases))] +// public void CreateFromMinimumMaximum(Point2 minimum, Point2 maximum, CircleF expectedBoundingCircle) +// { +// var actualBoundingCircle = CircleF.CreateFrom(minimum, maximum); +// Assert.Equal(expectedBoundingCircle, actualBoundingCircle); +// } + +// public IEnumerable<TestCaseData> CreateFromPointsTestCases +// { +// get +// { +// yield return +// new TestCaseData(null, new CircleF()).SetName( +// "The bounding circle created from null points is the empty bounding circle."); +// yield return +// new TestCaseData(new Point2[0], new CircleF()).SetName( +// "The bounding circle created from the empty set of points is the empty bounding circle."); +// yield return +// new TestCaseData( +// new[] +// { +// new Point2(5, 5), new Point2(10, 10), new Point2(15, 15), new Point2(-5, -5), +// new Point2(-15, -15) +// }, new CircleF(new Point2(0, 0), 15)).SetName( +// "The bounding circle created from a non-empty set of points is the expected bounding circle."); +// } +// } + +// [Fact] +// [TestCaseSource(nameof(CreateFromPointsTestCases))] +// public void CreateFromPoints(Point2[] points, CircleF expectedCircle) +// { +// var actualCircle = CircleF.CreateFrom(points); +// Assert.Equal(expectedCircle, actualCircle); +// } + +// public IEnumerable<TestCaseData> IntersectsCircleTestCases +// { +// get +// { +// yield return +// new TestCaseData(new CircleF(), new CircleF(), true).SetName( +// "Two empty circles intersect."); +// yield return +// new TestCaseData(new CircleF(new Point2(-10, -10), 15), +// new CircleF(new Point2(20, 20), 40), true).SetName( +// "Two overlapping non-empty circles intersect."); +// yield return +// new TestCaseData(new CircleF(new Point2(-40, -50), 15), +// new CircleF(new Point2(20, 20), 15), false).SetName( +// "Two non-overlapping non-empty circles do not intersect."); +// } +// } + +// [Fact] +// [TestCaseSource(nameof(IntersectsCircleTestCases))] +// public void Intersects(CircleF circle, CircleF circle2, bool expectedToIntersect) +// { +// Assert.Equal(expectedToIntersect, circle.Intersects(circle2)); +// Assert.Equal(expectedToIntersect, CircleF.Intersects(circle, circle2)); +// } + +// public IEnumerable<TestCaseData> IntersectsRectangleTestCases +// { +// get +// { +// yield return +// new TestCaseData(new CircleF(), new RectangleF(), true).SetName( +// "The empty circle and the empty rectangle intersect."); +// yield return +// new TestCaseData(new CircleF(new Point2(0, 0), 15), +// new RectangleF(new Point2(0, 0), new Size2(40, 40)), true).SetName( +// "The non-empty circle and a non-empty overlapping rectangle intersect."); +// yield return +// new TestCaseData(new CircleF(new Point2(-40, -50), 15), +// new RectangleF(new Point2(20, 20), new Size2(15, 15)), false).SetName( +// "The non-empty circle and a non-empty non-overlapping rectangle do not intersect."); +// } +// } + +// [Fact] +// [TestCaseSource(nameof(IntersectsRectangleTestCases))] +// public void Intersects(CircleF circle, RectangleF rectangle, bool expectedToIntersect) +// { +// Assert.Equal(expectedToIntersect, circle.Intersects((BoundingRectangle)rectangle)); +// Assert.Equal(expectedToIntersect, CircleF.Intersects(circle, (BoundingRectangle)rectangle)); +// } + +// public IEnumerable<TestCaseData> ContainsPointTestCases +// { +// get +// { +// yield return +// new TestCaseData(new CircleF(), new Point2(), true).SetName( +// "The empty circle contains the zero point."); +// yield return +// new TestCaseData(new CircleF(new Point2(0, 0), 15), new Point2(-15, -15), true) +// .SetName( +// "A non-empty circle contains a point inside it."); +// yield return +// new TestCaseData(new CircleF(new Point2(0, 0), 15), new Point2(-16, 15), false) +// .SetName( +// "A non-empty circle does not contain a point outside it."); +// } +// } + +// [Fact] +// [TestCaseSource(nameof(ContainsPointTestCases))] +// public void ContainsPoint(CircleF circle, Point2 point, bool expectedToContainPoint) +// { +// Assert.Equal(expectedToContainPoint, circle.Contains(point)); +// Assert.Equal(expectedToContainPoint, CircleF.Contains(circle, point)); +// } + +// public IEnumerable<TestCaseData> ClosestPointTestCases +// { +// get +// { +// yield return +// new TestCaseData(new CircleF(), new Point2(), new Point2()).SetName( +// "The closest point on the empty circle to the zero point is the zero point."); +// yield return +// new TestCaseData(new CircleF(new Point2(0, 0), 50), new Point2(25, 25), +// new Point2(25, 25)).SetName( +// "The closest point on a non-empty circle to a point which is inside the circle is that point.") +// ; +// yield return +// new TestCaseData(new CircleF(new Point2(0, 0), 50), new Point2(400, 0), +// new Point2(50, 0)).SetName( +// "The closest point on a non-empty circle to a point which is outside the circle is the expected point.") +// ; +// } +// } + +// [Fact] +// [TestCaseSource(nameof(ClosestPointTestCases))] +// public void ClosestPoint(CircleF circle, Point2 point, Point2 expectedClosestPoint) +// { +// var actualClosestPoint = circle.ClosestPointTo(point); +// Assert.Equal(expectedClosestPoint, actualClosestPoint); +// } + +// public IEnumerable<TestCaseData> BoundaryPointTestCases +// { +// get +// { +// yield return +// new TestCaseData(new CircleF(), 0.0f, new Point2()).SetName( +// "The boundary point on the empty circle at an angle is the zero point."); +// yield return +// new TestCaseData(new CircleF(new Point2(0, 0), 50), MathHelper.PiOver2, +// new Point2(0, 50)).SetName( +// "The boundary point on a non-empty circle at an angle is the expected point."); +// } +// } + +// [Fact] +// [TestCaseSource(nameof(BoundaryPointTestCases))] +// public void BoundaryPointAt(CircleF circle, float angle, Point2 expectedPoint) +// { +// var actualPoint = circle.BoundaryPointAt(angle); +// AssertExtensions.AreApproximatelyEqual(expectedPoint, actualPoint); +// } + +// public IEnumerable<TestCaseData> EqualityTestCases +// { +// get +// { +// yield return +// new TestCaseData(new CircleF(), new CircleF(), true).SetName( +// "Empty circles are equal.") +// ; +// yield return +// new TestCaseData( +// new CircleF(new Point2(0, 0), float.MaxValue), +// new CircleF(new Point2(0, 0), float.MinValue), false).SetName( +// "Two different non-empty circles are not equal."); +// yield return +// new TestCaseData( +// new CircleF(new Point2(0, 0), float.MinValue), +// new CircleF(new Point2(0, 0), float.MinValue), true).SetName( +// "Two identical non-empty circles are equal."); +// } +// } + +// [Fact] +// [TestCaseSource(nameof(EqualityTestCases))] +// public void Equality(CircleF circle1, CircleF circle2, bool expectedToBeEqual) +// { +// Assert.IsTrue(Equals(circle1, circle2) == expectedToBeEqual); +// Assert.IsTrue(circle1 == circle2 == expectedToBeEqual); +// Assert.IsFalse(circle1 == circle2 != expectedToBeEqual); +// Assert.IsTrue(circle1.Equals(circle2) == expectedToBeEqual); + +// if (expectedToBeEqual) +// Assert.Equal(circle1.GetHashCode(), circle2.GetHashCode()); +// } + +// public IEnumerable<TestCaseData> InequalityTestCases +// { +// get +// { +// yield return +// new TestCaseData(new CircleF(), null, false).SetName( +// "A circle is not equal to a null object."); +// yield return +// new TestCaseData(new CircleF(), new object(), false).SetName( +// "A circle is not equal to an instantiated object."); +// } +// } + +// [Fact] +// [TestCaseSource(nameof(InequalityTestCases))] +// public void Inequality(CircleF circle, object obj, bool expectedToBeEqual) +// { +// Assert.IsTrue(circle.Equals(obj) == expectedToBeEqual); +// } + +// public IEnumerable<TestCaseData> HashCodeTestCases +// { +// get +// { +// yield return +// new TestCaseData(new CircleF(), new CircleF(), true).SetName( +// "Two empty circles have the same hash code."); +// yield return +// new TestCaseData(new CircleF(new Point2(0, 0), 50), +// new CircleF(new Point2(0, 0), 50), true).SetName( +// "Two indentical non-empty circles have the same hash code."); +// yield return +// new TestCaseData(new CircleF(new Point2(0, 0), 50), +// new CircleF(new Point2(50, 50), 50), false).SetName( +// "Two different non-empty circles do not have the same hash code."); +// } +// } + +// [Fact] +// [TestCaseSource(nameof(HashCodeTestCases))] +// public void HashCode(CircleF circle1, CircleF circle2, bool expectedThatHashCodesAreEqual) +// { +// var hashCode1 = circle1.GetHashCode(); +// var hashCode2 = circle2.GetHashCode(); +// if (expectedThatHashCodesAreEqual) +// Assert.Equal(hashCode1, hashCode2); +// else +// Assert.AreNotEqual(hashCode1, hashCode2); +// } + +// public IEnumerable<TestCaseData> ToRectangleTestCases +// { +// get +// { +// yield return +// new TestCaseData(new CircleF(), new Rectangle()).SetName( +// "The empty circle converted to a rectangle is the empty integer rectangle."); +// yield return +// new TestCaseData(new CircleF(new Point2(25, 25), 25), +// new Rectangle(0, 0, 50, 50)).SetName( +// "A non-empty circle converted to a rectangle is the expected integer rectangle."); +// } +// } + +// [Fact] +// [TestCaseSource(nameof(ToRectangleTestCases))] +// public void ToRectangle(CircleF circle, Rectangle expectedRectangle) +// { +// var actualRectangle = (Rectangle)circle; +// Assert.Equal(expectedRectangle, actualRectangle); +// } + +// public IEnumerable<TestCaseData> ToRectangleFTestCases +// { +// get +// { +// yield return +// new TestCaseData(new CircleF(), new RectangleF()).SetName( +// "The empty circle converted to a rectangle is the empty float rectangle."); +// yield return +// new TestCaseData(new CircleF(new Point2(25, 25), 25), +// new RectangleF(0, 0, 50, 50)).SetName( +// "A non-empty circle converted to a rectangle is the expected float rectangle."); +// } +// } + +// [Fact] +// [TestCaseSource(nameof(ToRectangleFTestCases))] +// public void ToRectangleF(CircleF circle, RectangleF expectedRectangle) +// { +// var actualRectangle = (RectangleF)circle; +// Assert.Equal(expectedRectangle, actualRectangle); +// } + +// public IEnumerable<TestCaseData> FromRectangleTestCases +// { +// get +// { +// yield return +// new TestCaseData(new Rectangle(), new CircleF()).SetName( +// "The empty rectangle converted to a circle is the empty circle."); +// yield return +// new TestCaseData(new Rectangle(0, 0, 50, 50), +// new CircleF(new Point2(25, 25), 25)).SetName( +// "A non-empty rectangle converted to a circle is the expected circle."); +// } +// } + +// [Fact] +// [TestCaseSource(nameof(FromRectangleTestCases))] +// public void FromRectangle(Rectangle rectangle, CircleF expectedCircle) +// { +// var actualCircle = (CircleF)rectangle; +// Assert.Equal(expectedCircle, actualCircle); +// } + +// public IEnumerable<TestCaseData> FromRectangleFTestCases +// { +// get +// { +// yield return +// new TestCaseData(new RectangleF(), new CircleF()).SetName( +// "The empty rectangle converted to a circle is the empty circle."); +// yield return +// new TestCaseData(new RectangleF(0, 0, 50, 50), +// new CircleF(new Point2(25, 25), 25)).SetName( +// "A non-empty rectangle converted to a circle is the expected circle."); +// } +// } + +// [Fact] +// [TestCaseSource(nameof(FromRectangleFTestCases))] +// public void FromRectangleF(RectangleF rectangle, CircleF expectedCircle) +// { +// var actualCircle = (CircleF)rectangle; +// Assert.Equal(expectedCircle, actualCircle); +// } + +// public IEnumerable<TestCaseData> StringCases +// { +// get +// { +// yield return +// new TestCaseData(new CircleF(), +// string.Format(CultureInfo.CurrentCulture, "Centre: {0}, Radius: {1}", new Point2(), +// 0)).SetName( +// "The empty circle has the expected string representation using the current culture."); +// yield return new TestCaseData(new CircleF(new Point2(5.1f, -5.123f), 5.4f), +// string.Format(CultureInfo.CurrentCulture, "Centre: {0}, Radius: {1}", new Point2(5.1f, -5.123f), +// 5.4f)).SetName( +// "A non-empty circle has the expected string representation using the current culture."); +// } +// } + +// [Fact] +// [TestCaseSource(nameof(StringCases))] +// public void String(CircleF circle, string expectedString) +// { +// var actualString = circle.ToString(); +// Assert.Equal(expectedString, actualString); +// } + } +} diff --git a/Plugins/MonoGame.Extended/tests/MonoGame.Extended.Tests/Primitives/EllipseFTest.cs b/Plugins/MonoGame.Extended/tests/MonoGame.Extended.Tests/Primitives/EllipseFTest.cs new file mode 100644 index 0000000..4a4ecdf --- /dev/null +++ b/Plugins/MonoGame.Extended/tests/MonoGame.Extended.Tests/Primitives/EllipseFTest.cs @@ -0,0 +1,38 @@ +using Microsoft.Xna.Framework; +using Xunit; + +namespace MonoGame.Extended.Tests.Primitives +{ + + public class EllipseFTest + { + [Theory] + [InlineData(-1, -1, false)] + [InlineData(110, 300, true)] + [InlineData(200, 300, true)] + [InlineData(290, 300, true)] + [InlineData(400, 400, false)] + public void ContainsPoint_Circle(int x, int y, bool expected) + { + var ellipse = new EllipseF(new Vector2(200.0f, 300.0f), 100.0f, 100.0f); + + Assert.Equal(expected, ellipse.Contains(x, y)); + } + + [Theory] + [InlineData(299, 400, false)] + [InlineData(501, 400, false)] + [InlineData(400, 199, false)] + [InlineData(400, 601, false)] + [InlineData(301, 400, true)] + [InlineData(499, 400, true)] + [InlineData(400, 201, true)] + [InlineData(400, 599, true)] + public void ContainsPoint_NonCircle(int x, int y, bool expected) + { + var ellipse = new EllipseF(new Vector2(400.0f, 400.0f), 100.0f, 200.0f); + + Assert.Equal(expected, ellipse.Contains(x, y)); + } + } +} diff --git a/Plugins/MonoGame.Extended/tests/MonoGame.Extended.Tests/Primitives/OrientedRectangleTests.cs b/Plugins/MonoGame.Extended/tests/MonoGame.Extended.Tests/Primitives/OrientedRectangleTests.cs new file mode 100644 index 0000000..e3b40c8 --- /dev/null +++ b/Plugins/MonoGame.Extended/tests/MonoGame.Extended.Tests/Primitives/OrientedRectangleTests.cs @@ -0,0 +1,234 @@ +using System.Collections.Generic; +using Microsoft.Xna.Framework; +using Xunit; +using Vector2 = Microsoft.Xna.Framework.Vector2; + +namespace MonoGame.Extended.Tests.Primitives; + +public class OrientedRectangleTests +{ + [Fact] + public void Initializes_oriented_rectangle() + { + var rectangle = new OrientedRectangle(new Point2(1, 2), new Size2(3, 4), new Matrix2(5, 6, 7, 8, 9, 10)); + + Assert.Equal(new Point2(1, 2), rectangle.Center); + Assert.Equal(new Vector2(3, 4), rectangle.Radii); + Assert.Equal(new Matrix2(5, 6, 7, 8, 9, 10), rectangle.Orientation); + CollectionAssert.Equal( + new List<Vector2> + { + new(-3, -2), + new(-33, -38), + new(23, 26), + new(53, 62) + }, + rectangle.Points); + } + + public static readonly IEnumerable<object[]> _equalsComparisons = new[] + { + new object[] + { + "empty compared with empty is true", + new OrientedRectangle(Point2.Zero, Size2.Empty, Matrix2.Identity), + new OrientedRectangle(Point2.Zero, Size2.Empty, Matrix2.Identity) + }, + new object[] + { + "initialized compared with initialized true", + new OrientedRectangle(new Point2(1, 2), new Size2(3, 4), new Matrix2(5, 6, 7, 8, 9, 10)), + new OrientedRectangle(new Point2(1, 2), new Size2(3, 4), new Matrix2(5, 6, 7, 8, 9, 10)) + } + }; + + [Theory] + [MemberData(nameof(_equalsComparisons))] +#pragma warning disable xUnit1026 + public void Equals_comparison(string name, OrientedRectangle first, OrientedRectangle second) +#pragma warning restore xUnit1026 + { + Assert.True(first == second); + Assert.False(first != second); + } + + public class Transform + { + [Fact] + public void Center_point_is_not_translated() + { + var rectangle = new OrientedRectangle(new Point2(1, 2), new Size2(), Matrix2.Identity); + var transform = Matrix2.Identity; + + var result = OrientedRectangle.Transform(rectangle, ref transform); + + Assert.Equal(new Point2(1, 2), result.Center); + } + + [Fact] + public void Center_point_is_translated() + { + var rectangle = new OrientedRectangle(new Point2(0, 0), new Size2(), new Matrix2()); + var transform = Matrix2.CreateTranslation(1, 2); + + var result = OrientedRectangle.Transform(rectangle, ref transform); + + Assert.Equal(new Point2(1, 2), result.Center); + } + + [Fact] + public void Radii_is_not_changed_by_identity_transform() + { + var rectangle = new OrientedRectangle(new Point2(), new Size2(10, 20), new Matrix2()); + var transform = Matrix2.Identity; + + var result = OrientedRectangle.Transform(rectangle, ref transform); + + Assert.Equal(new Vector2(10, 20), result.Radii); + } + + [Fact] + public void Radii_is_not_changed_by_translation() + { + var rectangle = new OrientedRectangle(new Point2(1, 2), new Size2(10, 20), new Matrix2()); + var transform = Matrix2.CreateTranslation(1, 2); + + var result = OrientedRectangle.Transform(rectangle, ref transform); + + Assert.Equal(new Vector2(10, 20), result.Radii); + } + + [Fact] + public void Orientation_is_rotated_45_degrees_left() + { + var rectangle = new OrientedRectangle(new Point2(), new Size2(), Matrix2.Identity); + var transform = Matrix2.CreateRotationZ(MathHelper.PiOver4); + + var result = OrientedRectangle.Transform(rectangle, ref transform); + + Assert.Equal(new Point2(), result.Center); + Assert.Equal(new Vector2(), result.Radii); + Assert.Equal(Matrix2.CreateRotationZ(MathHelper.PiOver4), result.Orientation); + } + + [Fact] + public void Orientation_is_rotated_to_45_degrees_from_180() + { + var rectangle = new OrientedRectangle(new Point2(), new Size2(), Matrix2.CreateRotationZ(MathHelper.Pi)); + var transform = Matrix2.CreateRotationZ(-3 * MathHelper.PiOver4); + var expectedOrientation = Matrix2.CreateRotationZ(MathHelper.PiOver4); + + var result = OrientedRectangle.Transform(rectangle, ref transform); + + Assert.Equal(new Point2(), result.Center); + Assert.Equal(new Vector2(), result.Radii); + Assert.Equal(expectedOrientation.M11, result.Orientation.M11, 6); + Assert.Equal(expectedOrientation.M12, result.Orientation.M12, 6); + Assert.Equal(expectedOrientation.M21, result.Orientation.M21, 6); + Assert.Equal(expectedOrientation.M22, result.Orientation.M22, 6); + Assert.Equal(expectedOrientation.M31, result.Orientation.M31, 6); + Assert.Equal(expectedOrientation.M32, result.Orientation.M32, 6); + } + + [Fact] + public void Points_are_same_as_center() + { + var rectangle = new OrientedRectangle(new Point2(1, 2), new Size2(), Matrix2.Identity); + var transform = Matrix2.Identity; + + var result = OrientedRectangle.Transform(rectangle, ref transform); + + CollectionAssert.Equal( + new List<Vector2> + { + new(1, 2), + new(1, 2), + new(1, 2), + new(1, 2) + }, + result.Points); + } + + [Fact] + public void Points_are_translated() + { + var rectangle = new OrientedRectangle(new Point2(0, 0), new Size2(2, 4), Matrix2.Identity); + var transform = Matrix2.CreateTranslation(10, 20); + + var result = OrientedRectangle.Transform(rectangle, ref transform); + + CollectionAssert.Equal( + new List<Vector2> + { + new(8, 16), + new(8, 24), + new(12, 24), + new(12, 16) + }, + result.Points); + } + + [Fact] + public void Applies_rotation_and_translation() + { + /* Rectangle with center point p, aligned in coordinate system with origin 0. + * + * : + * : + * +-+ + * | | + * |p| + * | | + * ...............0-+............ + * : + * : + * : + * + * Rotate around center point p, 90 degrees around origin 0. + * + * : + * : + * +---+ + * | p | + * ...........+---0.............. + * : + * : + * : + * + * Then translate rectangle by x=10 and y=20. + * : + * : +---+ + * : | p | + * y=21 - - - - - - - -> +---+ + * . + * : + * ...............0.............. + * : + * : + * : + */ + var rectangle = new OrientedRectangle(new Point2(1, 2), new Size2(2, 4), Matrix2.Identity); + var transform = + Matrix2.CreateRotationZ(MathHelper.PiOver2) + * + Matrix2.CreateTranslation(10, 20); + + var result = OrientedRectangle.Transform(rectangle, ref transform); + + Assert.Equal(8, result.Center.X, 6); + Assert.Equal(21, result.Center.Y, 6); + Assert.Equal(2, result.Radii.X, 6); + Assert.Equal(4, result.Radii.Y, 6); + Assert.Equal(Matrix2.CreateRotationZ(MathHelper.PiOver2), result.Orientation); + CollectionAssert.Equal( + new List<Vector2> + { + new(4, 23), + new(4, 19), + new(12, 19), + new(12, 23) + }, + result.Points); + } + } +} diff --git a/Plugins/MonoGame.Extended/tests/MonoGame.Extended.Tests/Primitives/Point2Tests.cs b/Plugins/MonoGame.Extended/tests/MonoGame.Extended.Tests/Primitives/Point2Tests.cs new file mode 100644 index 0000000..ce5f89b --- /dev/null +++ b/Plugins/MonoGame.Extended/tests/MonoGame.Extended.Tests/Primitives/Point2Tests.cs @@ -0,0 +1,356 @@ +//using System.Collections.Generic; +//using System.Globalization; +//using Microsoft.Xna.Framework; +//using Xunit; + +//namespace MonoGame.Extended.Tests.Primitives +//{ +// +// public class Point2Tests +// { +// public IEnumerable<TestCaseData> ConstructorTestCases +// { +// get +// { +// yield return +// new TestCaseData(0, 0).SetName( +// "The zero point has the expected coordinates."); +// yield return +// new TestCaseData(float.MinValue, float.MaxValue).SetName +// ( +// "A non-zero point has the expected coordinates."); +// } +// } + +// [Fact] +// [TestCaseSource(nameof(ConstructorTestCases))] +// public void Constructor(float x, float y) +// { +// var point = new Point2(x, y); +// Assert.Equal(x, point.X); +// Assert.Equal(y, point.Y); +// } + +// public IEnumerable<TestCaseData> CoordinatesTestCases +// { +// get +// { +// yield return +// new TestCaseData(new Point2(), 0, 0).SetName( +// "The zero point has the expected coordinates."); +// yield return +// new TestCaseData(new Point2(float.MinValue, float.MaxValue), float.MinValue, float.MaxValue).SetName +// ( +// "A non-zero point has the expected coordinates."); +// } +// } + +// [Fact] +// [TestCaseSource(nameof(CoordinatesTestCases))] +// public void Coordinates(Point2 point, float expectedX, float expecetedY) +// { +// Assert.Equal(expectedX, point.X); +// Assert.Equal(expecetedY, point.Y); + +// point.X = 10; +// Assert.Equal(10, point.X); + +// point.Y = -10.123f; +// Assert.Equal(-10.123f, point.Y); +// } + +// public IEnumerable<TestCaseData> VectorAdditionTestCases +// { +// get +// { +// yield return +// new TestCaseData(new Point2(), new Vector2(), new Point2()).SetName( +// "The addition of the zero point and the zero vector is the zero point."); +// yield return +// new TestCaseData(new Point2(5, 5), new Vector2(15, 15), new Point2(20, 20)).SetName( +// "The addition of a non-zero point and a non-zero vector is the expected point."); +// } +// } + +// [Fact] +// [TestCaseSource(nameof(VectorAdditionTestCases))] +// public void VectorAddition(Point2 point, Vector2 vector, Point2 expectedPoint) +// { +// Assert.Equal(expectedPoint, point + vector); +// Assert.Equal(expectedPoint, Point2.Add(point, vector)); +// } + +// public IEnumerable<TestCaseData> VectorSubtractionTestCases +// { +// get +// { +// yield return +// new TestCaseData(new Point2(), new Vector2(), new Point2()).SetName( +// "The vector subtraction of two zero points is the zero vector."); +// yield return +// new TestCaseData(new Point2(5, 5), new Vector2(15, 15), new Point2(-10, -10)).SetName( +// "The vector subtraction of two non-zero points is the expected vector."); +// } +// } + +// [Fact] +// [TestCaseSource(nameof(VectorSubtractionTestCases))] +// public void VectorSubtraction(Point2 point, Vector2 vector, Point2 expectedPoint) +// { +// Assert.Equal(expectedPoint, point - vector); +// Assert.Equal(expectedPoint, Point2.Subtract(point, vector)); +// } + + +// public IEnumerable<TestCaseData> DisplacementTestCases +// { +// get +// { +// yield return +// new TestCaseData(new Point2(), new Point2(), new Vector2()).SetName( +// "The displacement between two zero points is the zero vector."); +// yield return +// new TestCaseData(new Point2(5, 5), new Point2(15, 15), new Vector2(10, 10)).SetName( +// "The displacement between two non-zero points is the expected vector."); +// } +// } + +// [Fact] +// [TestCaseSource(nameof(DisplacementTestCases))] +// public void Displacement(Point2 point1, Point2 point2, Vector2 expectedVector) +// { +// Assert.Equal(expectedVector, point2 - point1); +// Assert.Equal(expectedVector, Point2.Displacement(point2, point1)); +// } + +// public IEnumerable<TestCaseData> SizeAdditionTestCases +// { +// get +// { +// yield return +// new TestCaseData(new Point2(), new Size2(), new Point2()).SetName( +// "The size addition of the zero point with the empty size is the zero point."); +// yield return +// new TestCaseData(new Point2(5, 5), new Size2(15, 15), new Point2(20, 20)).SetName( +// "The size addition of a non-zero point with a non-empty size is the expected point."); +// } +// } + +// [Fact] +// [TestCaseSource(nameof(SizeAdditionTestCases))] +// public void SizeAdditon(Point2 point, Size2 size, Point2 expectedPoint) +// { +// Assert.Equal(expectedPoint, point + size); +// Assert.Equal(expectedPoint, Point2.Add(point, size)); +// } + +// public IEnumerable<TestCaseData> SizeSubtractionTestCases +// { +// get +// { +// yield return +// new TestCaseData(new Point2(), new Size2(), new Point2()).SetName( +// "The size substraction of the zero point with the empty size is the zero point."); +// yield return +// new TestCaseData(new Point2(5, 5), new Size2(15, 15), new Point2(-10, -10)).SetName( +// "The size subscration of a non-zero point with a non-empty size is the expected point."); +// } +// } + +// [Fact] +// [TestCaseSource(nameof(SizeSubtractionTestCases))] +// public void SizeSubtraction(Point2 point, Size2 size, Point2 expectedPoint) +// { +// Assert.Equal(expectedPoint, point - size); +// Assert.Equal(expectedPoint, Point2.Subtract(point, size)); +// } + +// public IEnumerable<TestCaseData> MinimumTestCases +// { +// get +// { +// yield return +// new TestCaseData(new Point2(), new Point2(), new Point2()).SetName( +// "The minimum coordinates of two zero points is the coordinates of the zero point."); +// yield return +// new TestCaseData(new Point2(float.MaxValue, float.MinValue), new Point2(int.MaxValue, int.MinValue), +// new Point2(int.MaxValue, float.MinValue)).SetName( +// "The minimum coordaintes of two non-zero points is the expected coordinates."); +// } +// } + +// [Fact] +// [TestCaseSource(nameof(MinimumTestCases))] +// public void Minimum(Point2 point1, Point2 point2, Point2 expectedPoint) +// { +// var actualPoint = Point2.Minimum(point1, point2); +// Assert.Equal(expectedPoint, actualPoint); +// } + +// public IEnumerable<TestCaseData> MaximumTestCases +// { +// get +// { +// yield return +// new TestCaseData(new Point2(), new Point2(), new Point2()).SetName( +// "The maximum coordinates of two zero points is the coordinates of the zero point."); +// yield return +// new TestCaseData(new Point2(float.MaxValue, float.MinValue), new Point2(int.MaxValue, int.MinValue), +// new Point2(float.MaxValue, int.MinValue)).SetName( +// "The maximum coordaintes of two non-zero points is the expected coordinates."); +// } +// } + +// [Fact] +// [TestCaseSource(nameof(MaximumTestCases))] +// public void Maximum(Point2 point1, Point2 point2, Point2 expectedPoint) +// { +// var actualPoint = Point2.Maximum(point1, point2); +// Assert.Equal(expectedPoint, actualPoint); +// } + +// public IEnumerable<TestCaseData> EqualityTestCases +// { +// get +// { +// yield return +// new TestCaseData(new Point2(), new Point2(), true).SetName("Two zero points are equal."); +// yield return +// new TestCaseData(new Point2(float.MinValue, float.MaxValue), +// new Point2(float.MaxValue, float.MinValue), false).SetName( +// "Two different non-zero points are not equal."); +// yield return +// new TestCaseData( +// new Point2(float.MinValue, float.MaxValue), new Point2(float.MinValue, float.MaxValue), true) +// .SetName( +// "Two identical non-zero points are equal."); +// } +// } + +// [Fact] +// [TestCaseSource(nameof(EqualityTestCases))] +// public void Equality(Point2 point1, Point2 point2, bool expectedToBeEqual) +// { +// Assert.IsTrue(Equals(point1, point2) == expectedToBeEqual); +// Assert.IsTrue(point1 == point2 == expectedToBeEqual); +// Assert.IsFalse(point1 == point2 != expectedToBeEqual); +// Assert.IsTrue(point1.Equals(point2) == expectedToBeEqual); + +// if (expectedToBeEqual) +// Assert.Equal(point1.GetHashCode(), point2.GetHashCode()); +// } + +// public IEnumerable<TestCaseData> InequalityTestCases +// { +// get +// { +// yield return +// new TestCaseData(new Point2(), null, false).SetName("A point is not equal to a null object."); +// yield return +// new TestCaseData(new Point2(), new object(), false).SetName( +// "A point is not equal to an instantiated object."); +// } +// } + +// [Fact] +// [TestCaseSource(nameof(InequalityTestCases))] +// public void Inequality(Point2 point, object obj, bool expectedToBeEqual) +// { +// Assert.IsTrue(point.Equals(obj) == expectedToBeEqual); +// } + +// public IEnumerable<TestCaseData> HashCodeTestCases +// { +// get +// { +// yield return +// new TestCaseData(new Point2(), new Point2(), true).SetName( +// "Two zero points have the same hash code."); +// yield return +// new TestCaseData(new Point2(50, 50), new Point2(50, 50), true).SetName( +// "Two indentical non-zero points have the same hash code."); +// yield return +// new TestCaseData(new Point2(0, 0), new Point2(50, 50), false).SetName( +// "Two different non-zero points do not have the same hash code."); +// } +// } + +// [Fact] +// [TestCaseSource(nameof(HashCodeTestCases))] +// public void HashCode(Point2 point1, Point2 point2, bool expectedThatHashCodesAreEqual) +// { +// var hashCode1 = point1.GetHashCode(); +// var hashCode2 = point2.GetHashCode(); +// if (expectedThatHashCodesAreEqual) +// Assert.Equal(hashCode1, hashCode2); +// else +// Assert.AreNotEqual(hashCode1, hashCode2); +// } + +// public IEnumerable<TestCaseData> ToVectorTestCases +// { +// get +// { +// yield return +// new TestCaseData(new Point2(), new Vector2()).SetName( +// "The zero point converted to a vector is the zero vector."); +// yield return +// new TestCaseData(new Point2(float.MinValue, float.MaxValue), +// new Vector2(float.MinValue, float.MaxValue)).SetName( +// "A non-zero point converted to a vector is the expected vector."); +// } +// } + +// [Fact] +// [TestCaseSource(nameof(ToVectorTestCases))] +// public void ToVector(Point2 point, Vector2 expectedVector) +// { +// var actualVector = (Vector2)point; +// Assert.Equal(expectedVector, actualVector); +// } + +// public IEnumerable<TestCaseData> FromVectorTestCases +// { +// get +// { +// yield return +// new TestCaseData(new Vector2(), new Point2()).SetName( +// "The zero vector converted to a point is the zero point."); +// yield return +// new TestCaseData(new Vector2(float.MinValue, float.MaxValue), +// new Point2(float.MinValue, float.MaxValue)).SetName( +// "A non-zero vector converted to a point is the expected point."); +// } +// } + +// [Fact] +// [TestCaseSource(nameof(FromVectorTestCases))] +// public void FromVector(Vector2 vector, Point2 expectedPoint) +// { +// var actualPoint = (Point2)vector; +// Assert.Equal(expectedPoint, actualPoint); +// } + +// public IEnumerable<TestCaseData> StringCases +// { +// get +// { +// yield return +// new TestCaseData(new Point2(), +// string.Format(CultureInfo.CurrentCulture, "({0}, {1})", 0, 0)).SetName( +// "The zero point has the expected string representation using the current culture."); +// yield return new TestCaseData(new Point2(5.1f, -5.123f), +// string.Format(CultureInfo.CurrentCulture, "({0}, {1})", 5.1f, -5.123f)).SetName( +// "A non-zero point has the expected string representation using the current culture."); +// } +// } + +// [Fact] +// [TestCaseSource(nameof(StringCases))] +// public void String(Point2 point, string expectedString) +// { +// var actualString = point.ToString(); +// Assert.Equal(expectedString, actualString); +// } +// } +//} diff --git a/Plugins/MonoGame.Extended/tests/MonoGame.Extended.Tests/Primitives/Ray2DTests.cs b/Plugins/MonoGame.Extended/tests/MonoGame.Extended.Tests/Primitives/Ray2DTests.cs new file mode 100644 index 0000000..04e8b00 --- /dev/null +++ b/Plugins/MonoGame.Extended/tests/MonoGame.Extended.Tests/Primitives/Ray2DTests.cs @@ -0,0 +1,217 @@ +//using System.Collections.Generic; +//using System.Globalization; +//using Microsoft.Xna.Framework; +//using Xunit; + +//namespace MonoGame.Extended.Tests.Primitives +//{ +// +// public class Ray2Tests +// { +// public IEnumerable<TestCaseData> ConstructorTestCases +// { +// get +// { +// yield return +// new TestCaseData(new Point2(), new Vector2()).SetName( +// "The degenerate ray has the expected position and direction."); +// yield return +// new TestCaseData(new Point2(5, 5), new Vector2(15, 15)).SetName( +// "A non-degenerate ray has the expected position and direction."); +// } +// } + +// [Fact] +// [TestCaseSource(nameof(ConstructorTestCases))] +// public void Constructor(Point2 position, Vector2 direction) +// { +// var ray = new Ray2(position, direction); +// Assert.Equal(position, ray.Position); +// Assert.Equal(direction, ray.Direction); +// } + +// public IEnumerable<TestCaseData> PositionDirectionTestCases +// { +// get +// { +// yield return +// new TestCaseData(new Ray2(), new Point2(), new Vector2()).SetName( +// "The degenerate ray has the expected position and direction."); +// yield return +// new TestCaseData(new Ray2(new Point2(5, 5), new Vector2(15, 15)), new Point2(5, 5), +// new Vector2(15, 15)).SetName +// ( +// "A non-degenerate ray has the expected position and direction."); +// } +// } + +// [Fact] +// [TestCaseSource(nameof(PositionDirectionTestCases))] +// public void PositionDirection(Ray2 ray, Point2 expectedPosition, Vector2 expecetedDirection) +// { +// Assert.Equal(expectedPosition, ray.Position); +// Assert.Equal(expecetedDirection, ray.Direction); + +// ray.Position.X = 10; +// ray.Position.Y = 10; +// Assert.Equal(new Point2(10, 10), ray.Position); + +// ray.Direction.X = -10.123f; +// ray.Direction.Y = 10.123f; +// Assert.Equal(new Vector2(-10.123f, 10.123f), ray.Direction); +// } + +// public IEnumerable<TestCaseData> IntersectsBoundingRectangleTestCases +// { +// get +// { +// yield return +// new TestCaseData(new Ray2(), new BoundingRectangle(), true, Point2.Zero, Point2.Zero).SetName( +// "The degenerate ray intersects the empty bounding box."); +// yield return +// new TestCaseData(new Ray2(new Point2(-75, -75), new Vector2(75, -75)), +// new BoundingRectangle(new Point2(), new Size2(50, 50)), false, Point2.NaN, Point2.NaN).SetName( +// "A non-degenerate ray that does not cross a non-empty bounding box does not intersect the bounding box."); +// yield return +// new TestCaseData(new Ray2(new Point2(0, 0), new Vector2(25, 0)), new BoundingRectangle(new Point2(), new Size2(50, 50)), +// true, new Point2(0, 0), new Point2(50, 0)).SetName( +// "A non-degenerate ray starting from inside a non-empty bounding box intersects the bounding box."); +// yield return +// new TestCaseData(new Ray2(new Point2(-100, 0), new Vector2(100, 0)), +// new BoundingRectangle(new Point2(), new Size2(50, 50)), +// true, new Point2(-50, 0), new Point2(50, 0)).SetName( +// "A non-degenerate ray crossing a non-empty bounding box intersects the bounding box."); +// } +// } + +// [Fact] +// [TestCaseSource(nameof(IntersectsBoundingRectangleTestCases))] +// public void IntersectsBoundingRectangle(Ray2 ray, BoundingRectangle boundingRectangle, bool expectedResult, +// Point2 firstExpectedIntersectionPoint, Point2 secondExpectedIntersectionPoint) +// { +// float rayNearDistance, rayFarDistance; +// var actualResult = ray.Intersects(boundingRectangle, out rayNearDistance, out rayFarDistance); +// Assert.Equal(expectedResult, actualResult); + +// if (actualResult) +// { +// var firstActualIntersectionPoint = ray.Position + ray.Direction * rayNearDistance; +// Assert.Equal(firstExpectedIntersectionPoint, firstActualIntersectionPoint); +// var secondActualIntersectionPoint = ray.Position + ray.Direction * rayFarDistance; +// Assert.Equal(secondExpectedIntersectionPoint, secondActualIntersectionPoint); +// } +// else +// { +// Assert.IsTrue(float.IsNaN(rayNearDistance)); +// Assert.IsTrue(float.IsNaN(rayFarDistance)); +// } +// } + +// public IEnumerable<TestCaseData> EqualityTestCases +// { +// get +// { +// yield return +// new TestCaseData(new Ray2(), new Ray2(), true).SetName("Two degenerate rays are equal."); +// yield return +// new TestCaseData(new Ray2(new Point2(float.MinValue, float.MaxValue), +// new Vector2(float.MaxValue, float.MinValue)), new Ray2(new Point2(float.MaxValue, float.MinValue), +// new Vector2(float.MaxValue, float.MinValue)), false).SetName( +// "Two different non-degenerate rays are not equal."); +// yield return +// new TestCaseData( +// new Ray2(new Point2(float.MinValue, float.MaxValue), +// new Vector2(float.MinValue, float.MaxValue)), new Ray2(new Point2(float.MinValue, float.MaxValue), +// new Vector2(float.MinValue, float.MaxValue)), true) +// .SetName( +// "Two identical non-degenerate rays are equal."); +// } +// } + +// [Fact] +// [TestCaseSource(nameof(EqualityTestCases))] +// public void Equality(Ray2 ray1, Ray2 ray2, bool expectedToBeEqual) +// { +// Assert.IsTrue(Equals(ray1, ray2) == expectedToBeEqual); +// Assert.IsTrue(ray1 == ray2 == expectedToBeEqual); +// Assert.IsFalse(ray1 == ray2 != expectedToBeEqual); +// Assert.IsTrue(ray1.Equals(ray2) == expectedToBeEqual); + +// if (expectedToBeEqual) +// Assert.Equal(ray1.GetHashCode(), ray2.GetHashCode()); +// } + +// public IEnumerable<TestCaseData> InequalityTestCases +// { +// get +// { +// yield return +// new TestCaseData(new Ray2(), null, false).SetName("A ray is not equal to a null object."); +// yield return +// new TestCaseData(new Ray2(), new object(), false).SetName( +// "A ray is not equal to an instantiated object."); +// } +// } + +// [Fact] +// [TestCaseSource(nameof(InequalityTestCases))] +// public void Inequality(Ray2 ray, object obj, bool expectedToBeEqual) +// { +// Assert.IsTrue(ray.Equals(obj) == expectedToBeEqual); +// } + +// public IEnumerable<TestCaseData> HashCodeTestCases +// { +// get +// { +// yield return +// new TestCaseData(new Ray2(), new Ray2(), true).SetName( +// "Two degenerate rays have the same hash code."); +// yield return +// new TestCaseData(new Ray2(new Point2(50, 50), new Vector2(50, 50)), +// new Ray2(new Point2(50, 50), new Vector2(50, 50)), true).SetName( +// "Two indentical non-zero points have the same hash code."); +// yield return +// new TestCaseData(new Ray2(new Point2(0, 0), new Vector2(50, 50)), +// new Ray2(new Point2(50, 50), new Vector2(50, 50)), false).SetName( +// "Two different non-zero points do not have the same hash code."); +// } +// } + +// [Fact] +// [TestCaseSource(nameof(HashCodeTestCases))] +// public void HashCode(Ray2 ray1, Ray2 ray2, bool expectedThatHashCodesAreEqual) +// { +// var hashCode1 = ray1.GetHashCode(); +// var hashCode2 = ray2.GetHashCode(); +// if (expectedThatHashCodesAreEqual) +// Assert.Equal(hashCode1, hashCode2); +// else +// Assert.AreNotEqual(hashCode1, hashCode2); +// } + +// public IEnumerable<TestCaseData> StringCases +// { +// get +// { +// yield return +// new TestCaseData(new Ray2(), +// string.Format(CultureInfo.CurrentCulture, "Position: {0}, Direction: {1}", new Point2(), +// new Vector2())).SetName( +// "The degenerate ray has the expected string representation using the current culture."); +// yield return new TestCaseData(new Ray2(new Point2(5.1f, -5.123f), new Vector2(0, 1)), +// string.Format(CultureInfo.CurrentCulture, "Position: {0}, Direction: {1}", new Point2(5.1f, -5.123f), +// new Vector2(0, 1))).SetName( +// "A non-degenerate ray has the expected string representation using the current culture."); +// } +// } + +// [Fact] +// [TestCaseSource(nameof(StringCases))] +// public void String(Ray2 ray, string expectedString) +// { +// var actualString = ray.ToString(); +// Assert.Equal(expectedString, actualString); +// } +// } +//} diff --git a/Plugins/MonoGame.Extended/tests/MonoGame.Extended.Tests/Primitives/RectangleFTests.cs b/Plugins/MonoGame.Extended/tests/MonoGame.Extended.Tests/Primitives/RectangleFTests.cs new file mode 100644 index 0000000..9121d6a --- /dev/null +++ b/Plugins/MonoGame.Extended/tests/MonoGame.Extended.Tests/Primitives/RectangleFTests.cs @@ -0,0 +1,135 @@ +using Microsoft.Xna.Framework; +using Xunit; + +namespace MonoGame.Extended.Tests.Primitives +{ + public class RectangleFTests + { + [Fact] + public void Rectangle_Intersects_Test() + { + var rect1 = new Rectangle(0, 0, 32, 32); + var rect2 = new Rectangle(32, 32, 32, 32); + + Assert.False(rect1.Intersects(rect2)); + } + + [Fact] + public void PassVector2AsConstructorParameter_Test() + { + var rect1 = new RectangleF(new Vector2(0, 0), new Size2(12.34f, 56.78f)); + var rect2 = new RectangleF(new Vector2(0, 0), new Vector2(12.34f, 56.78f)); + + Assert.Equal(rect1, rect2); + } + + [Fact] + public void PassPointAsConstructorParameter_Test() + { + var rect1 = new RectangleF(new Vector2(0, 0), new Size2(12, 56)); + var rect2 = new RectangleF(new Vector2(0, 0), new Size2(12, 56)); + + Assert.Equal(rect1, rect2); + } + + public class Transform + { + [Fact] + public void Center_point_is_not_translated() + { + var rectangle = new RectangleF(new Point2(0, 0), new Size2(20, 30)); + var transform = Matrix2.Identity; + + var result = RectangleF.Transform(rectangle, ref transform); + + Assert.Equal(new Point2(10, 15), result.Center); + } + + [Fact] + public void Center_point_is_translated() + { + var rectangleF = new RectangleF(new Point2(0, 0), new Size2(20, 30)); + var transform = Matrix2.CreateTranslation(1, 2); + + var result = RectangleF.Transform(rectangleF, ref transform); + + Assert.Equal(new Point2(11, 17), result.Center); + } + + [Fact] + public void Size_is_not_changed_by_identity_transform() + { + var rectangle = new RectangleF(new Point2(0, 0), new Size2(20, 30)); + var transform = Matrix2.Identity; + + var result = RectangleF.Transform(rectangle, ref transform); + + Assert.Equal(new Size2(20, 30), result.Size); + } + + [Fact] + public void Size_is_not_changed_by_translation() + { + var rectangle = new RectangleF(new Point2(0, 0), new Size2(20, 30)); + var transform = Matrix2.CreateTranslation(1, 2); + + var result = RectangleF.Transform(rectangle, ref transform); + + Assert.Equal(new Size2(20, 30), result.Size); + } + + [Fact] + public void Applies_rotation_and_translation() + { + /* Rectangle with center point aligned in coordinate system with origin 0. + * + * : + * : + * +-+ + * | | + * |p| + * | | + * ...............0-+............ + * : + * : + * : + * + * Rotate center point p, 90 degrees around origin 0. + * + * : + * : + * +---+ + * | p | + * ...........+---0.............. + * : + * : + * : + * + * Then translate rectangle by x=10 and y=20. + * : + * : +---+ + * : | p | + * y=21 - - - - - - - -> +---+ + * . + * : + * ...............0.............. + * : + * : + * : + */ + var rectangle = new RectangleF(new Point2(0, 0), new Size2(2, 4)); + var transform = + Matrix2.CreateRotationZ(MathHelper.PiOver2) + * + Matrix2.CreateTranslation(10, 20); + + var result = RectangleF.Transform(rectangle, ref transform); + + Assert.Equal(-2 + 10, result.Center.X, 6); + Assert.Equal(1 + 20, result.Center.Y, 6); + Assert.Equal(4, result.Size.Width, 6); + Assert.Equal(2, result.Size.Height, 6); + } + } + } +} diff --git a/Plugins/MonoGame.Extended/tests/MonoGame.Extended.Tests/Primitives/Segment2DTests.cs b/Plugins/MonoGame.Extended/tests/MonoGame.Extended.Tests/Primitives/Segment2DTests.cs new file mode 100644 index 0000000..b994527 --- /dev/null +++ b/Plugins/MonoGame.Extended/tests/MonoGame.Extended.Tests/Primitives/Segment2DTests.cs @@ -0,0 +1,251 @@ +//using System.Collections.Generic; +//using System.Globalization; +//using Xunit; + +//namespace MonoGame.Extended.Tests.Primitives +//{ +// public class Segment2DTests +// { +// public IEnumerable<TestCaseData> ConstructorTestCases +// { +// get +// { +// yield return +// new TestCaseData(new Point2(), new Point2()).SetName( +// "The empty segment has expected starting and ending points."); +// yield return +// new TestCaseData( +// new Point2(float.MaxValue, float.MinValue), +// new Point2(int.MaxValue, int.MinValue)).SetName( +// "A non-empty segment has the expected starting and ending points."); +// } +// } + +// [Fact] +// [TestCaseSource(nameof(ConstructorTestCases))] +// public void Constructor(Point2 startingPoint, Point2 endingPoint) +// { +// var segment = new Segment2(startingPoint, endingPoint); +// Assert.Equal(startingPoint, segment.Start); +// Assert.Equal(endingPoint, segment.End); +// } + +// public IEnumerable<TestCaseData> ClosestPointTestCases +// { +// get +// { +// yield return +// new TestCaseData(new Segment2(), new Point2(), new Point2()).SetName( +// "The closest point on the empty segment to the zero point is the zero point."); +// yield return +// new TestCaseData(new Segment2(new Point2(0, 0), new Point2(200, 0)), new Point2(-100, 200), +// new Point2(0, 0)).SetName( +// "The closest point on a non-empty segment to a point which is projected beyond the start of the segment is the segment's starting point.") +// ; +// yield return +// new TestCaseData(new Segment2(new Point2(0, 0), new Point2(200, 0)), new Point2(400, 200), +// new Point2(200, 0)).SetName( +// "The closest point on a non-empty segment to a point which is projected beyond the end of the segment is the segment's ending point.") +// ; +// yield return +// new TestCaseData(new Segment2(new Point2(0, 0), new Point2(200, 0)), new Point2(100, 200), +// new Point2(100, 0)).SetName( +// "The closest point on a non-empty segment to a point which is projected inside the segment is the projected point.") +// ; +// } +// } + +// [Fact] +// [TestCaseSource(nameof(ClosestPointTestCases))] +// public void ClosestPoint(Segment2 segment, Point2 point, Point2 expectedClosestPoint) +// { +// var actualClosestPoint = segment.ClosestPointTo(point); +// Assert.Equal(expectedClosestPoint, actualClosestPoint); +// } + +// public IEnumerable<TestCaseData> SquaredDistanceToPointTestCases +// { +// get +// { +// yield return +// new TestCaseData(new Segment2(), new Point2(), 0).SetName( +// "The squared distance of the zero point to the empty segment is 0."); +// yield return +// new TestCaseData(new Segment2(new Point2(0, 0), new Point2(20, 0)), new Point2(-10, 20), 500) +// .SetName( +// "The squared distance of a point projected beyond the start of a non-empty segment is the squared distance from the segment's starting point to the point.") +// ; +// yield return +// new TestCaseData(new Segment2(new Point2(0, 0), new Point2(20, 0)), new Point2(40, 20), 400) +// .SetName( +// "The squared distance of a point projected beyond the end of a non-empty segment is the squared distance from the segment's ending point to the point.") +// ; +// yield return +// new TestCaseData(new Segment2(new Point2(0, 0), new Point2(20, 0)), new Point2(10, 25), 625).SetName +// ( +// "The squared distance of a point projected inside a non-empty segment is the squared distance from the projected point to the point.") +// ; +// } +// } + +// [Fact] +// [TestCaseSource(nameof(SquaredDistanceToPointTestCases))] +// public void SquaredDistanceToPoint(Segment2 segment, Point2 point, +// float expectedDistance) +// { +// var actualDistance = segment.SquaredDistanceTo(point); +// Assert.Equal(expectedDistance, actualDistance); +// } + +// public IEnumerable<TestCaseData> IntersectsBoundingRectangleTestCases +// { +// get +// { +// yield return +// new TestCaseData(new Segment2(), new BoundingRectangle(), true, Point2.Zero).SetName( +// "The empty segment intersects the empty bounding box."); +// yield return +// new TestCaseData(new Segment2(new Point2(-75, -75), new Point2(75, -75)), +// new BoundingRectangle(new Point2(), new Size2(50, 50)), false, Point2.NaN).SetName( +// "A non-empty segment outside a non-empty bounding box does not intersect the bounding box."); +// yield return +// new TestCaseData(new Segment2(new Point2(0, 0), new Point2(25, 0)), new BoundingRectangle(new Point2(), new Size2(50, 50)), +// true, new Point2(0, 0)).SetName( +// "A non-empty segment inside a non-empty bounding box intersects the bounding box."); +// yield return +// new TestCaseData(new Segment2(new Point2(-100, 0), new Point2(100, 0)), +// new BoundingRectangle(new Point2(), new Size2(50, 50)), +// true, new Point2(-50, 0)).SetName( +// "A non-empty segment crossing a non-empty bounding box intersects the bounding box."); +// } +// } + +// [Fact] +// [TestCaseSource(nameof(IntersectsBoundingRectangleTestCases))] +// public void IntersectsBoundingRectangle(Segment2 segment, BoundingRectangle boundingRectangle, bool expectedResult, +// Point2 expectedIntersectionPoint) +// { +// Point2 actualIntersectionPoint; +// var actualResult = segment.Intersects(boundingRectangle, out actualIntersectionPoint); +// Assert.Equal(expectedResult, actualResult); + +// if (actualResult) +// { +// Assert.Equal(expectedIntersectionPoint, actualIntersectionPoint); +// } +// else +// { +// Assert.IsTrue(float.IsNaN(actualIntersectionPoint.X)); +// Assert.IsTrue(float.IsNaN(actualIntersectionPoint.Y)); +// } +// } + +// public IEnumerable<TestCaseData> EqualityTestCases +// { +// get +// { +// yield return +// new TestCaseData(new Segment2(), new Segment2(), true).SetName("Empty segments are equal.") +// ; +// yield return +// new TestCaseData( +// new Segment2(new Point2(0, 0), new Point2(float.MaxValue, float.MinValue)), +// new Segment2(new Point2(0, 0), +// new Point2(float.MinValue, float.MaxValue)), false).SetName( +// "Two different non-empty segments are not equal."); +// yield return +// new TestCaseData( +// new Segment2(new Point2(0, 0), new Point2(float.MinValue, float.MaxValue)), +// new Segment2(new Point2(0, 0), +// new Point2(float.MinValue, float.MaxValue)), true).SetName( +// "Two identical non-empty segments are equal."); +// } +// } + +// [Fact] +// [TestCaseSource(nameof(EqualityTestCases))] +// public void Equality(Segment2 segment1, Segment2 segment2, bool expectedToBeEqual) +// { +// Assert.IsTrue(Equals(segment1, segment2) == expectedToBeEqual); +// Assert.IsTrue(segment1 == segment2 == expectedToBeEqual); +// Assert.IsFalse(segment1 == segment2 != expectedToBeEqual); +// Assert.IsTrue(segment1.Equals(segment2) == expectedToBeEqual); + +// if (expectedToBeEqual) +// Assert.Equal(segment1.GetHashCode(), segment2.GetHashCode()); +// } + +// public IEnumerable<TestCaseData> InequalityTestCases +// { +// get +// { +// yield return +// new TestCaseData(new Segment2(), null, false).SetName("A segment is not equal to a null object."); +// yield return +// new TestCaseData(new Segment2(), new object(), false).SetName( +// "A segment is not equal to an instantiated object."); +// } +// } + +// [Fact] +// [TestCaseSource(nameof(InequalityTestCases))] +// public void Inequality(Segment2 segment, object obj, bool expectedToBeEqual) +// { +// Assert.IsTrue(segment.Equals(obj) == expectedToBeEqual); +// } + +// public IEnumerable<TestCaseData> HashCodeTestCases +// { +// get +// { +// yield return +// new TestCaseData(new Segment2(), new Segment2(), true).SetName( +// "Two empty segments have the same hash code."); +// yield return +// new TestCaseData(new Segment2(new Point2(0, 0), new Point2(50, 50)), +// new Segment2(new Point2(0, 0), new Point2(50, 50)), true).SetName( +// "Two indentical non-empty segments have the same hash code."); +// yield return +// new TestCaseData(new Segment2(new Point2(0, 0), new Point2(50, 50)), +// new Segment2(new Point2(50, 50), new Point2(50, 50)), false).SetName( +// "Two different non-empty segments do not have the same hash code."); +// } +// } + +// [Fact] +// [TestCaseSource(nameof(HashCodeTestCases))] +// public void HashCode(Segment2 segment1, Segment2 segment2, bool expectedThatHashCodesAreEqual) +// { +// var hashCode1 = segment1.GetHashCode(); +// var hashCode2 = segment2.GetHashCode(); +// if (expectedThatHashCodesAreEqual) +// Assert.Equal(hashCode1, hashCode2); +// else +// Assert.AreNotEqual(hashCode1, hashCode2); +// } + +// public IEnumerable<TestCaseData> StringCases +// { +// get +// { +// yield return +// new TestCaseData(new Segment2(), +// string.Format(CultureInfo.CurrentCulture, "{0} -> {1}", new Point2(), +// new Point2())).SetName( +// "The empty segment has the expected string representation using the current culture."); +// yield return new TestCaseData(new Segment2(new Point2(5.1f, -5.123f), new Point2(5.4f, -5.4123f)), +// string.Format(CultureInfo.CurrentCulture, "{0} -> {1}", new Point2(5.1f, -5.123f), +// new Point2(5.4f, -5.4123f))).SetName( +// "A non-empty segment has the expected string representation using the current culture."); +// } +// } + +// [Fact] +// [TestCaseSource(nameof(StringCases))] +// public void String(Segment2 segment, string expectedString) +// { +// var actualString = segment.ToString(); +// Assert.Equal(expectedString, actualString); +// } +// } +//} diff --git a/Plugins/MonoGame.Extended/tests/MonoGame.Extended.Tests/Primitives/ShapeTests.cs b/Plugins/MonoGame.Extended/tests/MonoGame.Extended.Tests/Primitives/ShapeTests.cs new file mode 100644 index 0000000..37b2fc4 --- /dev/null +++ b/Plugins/MonoGame.Extended/tests/MonoGame.Extended.Tests/Primitives/ShapeTests.cs @@ -0,0 +1,180 @@ +using Microsoft.Xna.Framework; +using Xunit; + +namespace MonoGame.Extended.Tests.Primitives; + +public class ShapeTests +{ + public class CircleFTests + { + [Fact] + public void CircCircIntersectionSameCircleTest() + { + IShapeF shape1 = new CircleF(Point2.Zero, 2.0f); + IShapeF shape2 = new CircleF(Point2.Zero, 2.0f); + + Assert.True(shape1.Intersects(shape2)); + } + + [Fact] + public void CircCircIntersectionOverlappingTest() + { + IShapeF shape1 = new CircleF(new Point2(1, 2), 2.0f); + IShapeF shape2 = new CircleF(Point2.Zero, 2.0f); + + Assert.True(shape1.Intersects(shape2)); + } + + [Fact] + public void CircleCircleNotIntersectingTest() + { + IShapeF shape1 = new CircleF(new Point2(5, 5), 2.0f); + IShapeF shape2 = new CircleF(Point2.Zero, 2.0f); + + Assert.False(shape1.Intersects(shape2)); + } + } + + public class RectangleFTests + { + [Fact] + public void RectRectSameRectTest() + { + IShapeF shape1 = new RectangleF(Point2.Zero, new Size2(5, 5)); + IShapeF shape2 = new RectangleF(Point2.Zero, new Size2(5, 5)); + + Assert.True(shape1.Intersects(shape2)); + } + + [Fact] + public void RectRectIntersectingTest() + { + IShapeF shape1 = new RectangleF(Point2.Zero, new Size2(5, 5)); + IShapeF shape2 = new RectangleF(new Point2(3, 3), new Size2(5, 5)); + + Assert.True(shape1.Intersects(shape2)); + } + + [Fact] + public void RectRectNotIntersectingTest() + { + IShapeF shape1 = new RectangleF(Point2.Zero, new Size2(5, 5)); + IShapeF shape2 = new RectangleF(new Point2(10, 10), new Size2(5, 5)); + + Assert.False(shape1.Intersects(shape2)); + } + + [Fact] + public void RectCircContainedTest() + { + IShapeF shape1 = new RectangleF(Point2.Zero, new Size2(5, 5)); + IShapeF shape2 = new CircleF(Point2.Zero, 4); + + Assert.True(shape1.Intersects(shape2)); + Assert.True(shape2.Intersects(shape1)); + } + + [Fact] + public void RectCircOnEdgeTest() + { + IShapeF shape1 = new RectangleF(Point2.Zero, new Size2(5, 5)); + IShapeF shape2 = new CircleF(new Point2(5, 0), 4); + + Assert.True(shape1.Intersects(shape2)); + Assert.True(shape2.Intersects(shape1)); + } + } + + public class OrientedRectangleTests + { + [Fact] + public void Axis_aligned_rectangle_intersects_circle() + { + /* + * : + * : + * +*+ + * ...........* *......... + * +*+ + * : + * : + */ + IShapeF rectangle = new OrientedRectangle(Point2.Zero, new Size2(1, 1), Matrix2.Identity); + var circle = new CircleF(Point2.Zero, 1); + + Assert.True(rectangle.Intersects(circle)); + } + + [Fact] + public void Axis_aligned_rectangle_does_not_intersect_circle_in_top_left() + { + /* + * * : + * * *: + * *+-+ + * ...........| |......... + * +-+ + * : + * : + */ + IShapeF rectangle = new OrientedRectangle(Point2.Zero, new Size2(1, 1), Matrix2.Identity); + var circle = new CircleF(new Point2(-2, 1), .99f); + + Assert.False(rectangle.Intersects(circle)); + } + + [Fact] + public void Rectangle_rotated_45_degrees_does_not_intersect_circle_in_bottom_right() + { + /* + * : + * : + * +-. + * .........../ / ........ + * +./* * + * * * + * :* * + */ + IShapeF rectangle = new OrientedRectangle(new Point2(-1, 1), new Size2(1.42f, 1.42f), Matrix2.CreateRotationZ(-MathHelper.PiOver4)); + var circle = new CircleF(new Point2(1, -1), 1.4f); + + Assert.False(rectangle.Intersects(circle)); + } + + [Fact] + public void Axis_aligned_rectangle_does_not_intersect_rectangle() + { + /* + * : + * : + * +-+ + * ..........| |**....... + * +-+ * + * :** + * : + */ + IShapeF rectangle = new OrientedRectangle(new Point2(-1, 0), new Size2(1, 1), Matrix2.Identity); + var rect = new RectangleF(new Point2(0.001f, 0), new Size2(2, 2)); + + Assert.False(rectangle.Intersects(rect)); + } + + [Fact] + public void Axis_aligned_rectangle_intersects_rectangle() + { + /* + * : + * : + * +-+ + * ..........| |**....... + * +-+ * + * :** + * : + */ + IShapeF rectangle = new OrientedRectangle(new Point2(-1, 0), new Size2(1, 1), Matrix2.Identity); + var rect = new RectangleF(new Point2(0, 0), new Size2(2, 2)); + + Assert.True(rectangle.Intersects(rect)); + } + } +} diff --git a/Plugins/MonoGame.Extended/tests/MonoGame.Extended.Tests/Primitives/Size2Tests.cs b/Plugins/MonoGame.Extended/tests/MonoGame.Extended.Tests/Primitives/Size2Tests.cs new file mode 100644 index 0000000..faaa426 --- /dev/null +++ b/Plugins/MonoGame.Extended/tests/MonoGame.Extended.Tests/Primitives/Size2Tests.cs @@ -0,0 +1,304 @@ +//using System.Collections.Generic; +//using System.Globalization; +//using Microsoft.Xna.Framework; +//using Xunit; + +//namespace MonoGame.Extended.Tests.Primitives +//{ +// +// public class Size2Tests +// { +// public IEnumerable<TestCaseData> ConstructorTestCases +// { +// get +// { +// yield return +// new TestCaseData(0, 0).SetName( +// "The empty size has the expected dimensions."); +// yield return +// new TestCaseData(float.MinValue, float.MaxValue).SetName +// ( +// "A non-empty size has the expected dimensions."); +// } +// } + +// [Fact] +// [TestCaseSource(nameof(ConstructorTestCases))] +// public void Constructor(float width, float height) +// { +// var size = new Size2(width, height); +// Assert.Equal(width, size.Width); +// Assert.Equal(height, size.Height); +// } + +// public IEnumerable<TestCaseData> DimensionsTestCases +// { +// get +// { +// yield return +// new TestCaseData(new Size2(), 0, 0).SetName( +// "The empty size has the expected dimensions."); +// yield return +// new TestCaseData(new Size2(float.MinValue, float.MaxValue), float.MinValue, float.MaxValue).SetName +// ( +// "A non-empty size has the expected dimensions."); +// } +// } + +// [Fact] +// [TestCaseSource(nameof(DimensionsTestCases))] +// public void Dimensions(Size2 size, float expectedWidth, float expecetedHeight) +// { +// Assert.Equal(expectedWidth, size.Width); +// Assert.Equal(expecetedHeight, size.Height); + +// size.Width = 10; +// Assert.Equal(10, size.Width); + +// size.Height = -10.123f; +// Assert.Equal(-10.123f, size.Height); +// } + +// public IEnumerable<TestCaseData> AdditionTestCases +// { +// get +// { +// yield return +// new TestCaseData(new Size2(), new Size2(), new Size2()).SetName( +// "The addition of two empty sizes is the empty size."); +// yield return +// new TestCaseData(new Size2(5, 5), new Size2(15, 15), new Size2(20, 20)).SetName( +// "The addition of two non-empty sizes is the expected size."); +// } +// } + +// [Fact] +// [TestCaseSource(nameof(AdditionTestCases))] +// public void Addition(Size2 size1, Size2 size2, Size2 expectedSize) +// { +// Assert.Equal(expectedSize, size1 + size2); +// Assert.Equal(expectedSize, Size2.Add(size1, size2)); +// } + +// public IEnumerable<TestCaseData> SubtractionTestCases +// { +// get +// { +// yield return +// new TestCaseData(new Size2(), new Size2(), new Size2()).SetName( +// "The subtraction of two empty sizes is the empty size."); +// yield return +// new TestCaseData(new Size2(5, 5), new Size2(15, 15), new Size2(-10, -10)).SetName( +// "The subtraction of two non-empty sizes is the expected size."); +// } +// } + +// [Fact] +// [TestCaseSource(nameof(SubtractionTestCases))] +// public void Subtraction(Size2 size1, Size2 size2, Size2 expectedSize) +// { +// Assert.Equal(expectedSize, size1 - size2); +// Assert.Equal(expectedSize, Size2.Subtract(size1, size2)); +// } + +// public IEnumerable<TestCaseData> EqualityTestCases +// { +// get +// { +// yield return +// new TestCaseData(new Size2(), new Size2(), true).SetName("Two empty sizes are equal."); +// yield return +// new TestCaseData(new Size2(float.MinValue, float.MaxValue), +// new Size2(float.MaxValue, float.MinValue), false).SetName( +// "Two different non-empty sizes are not equal."); +// yield return +// new TestCaseData( +// new Size2(float.MinValue, float.MaxValue), new Size2(float.MinValue, float.MaxValue), true) +// .SetName( +// "Two identical non-empty sizes are equal."); +// } +// } + +// [Fact] +// [TestCaseSource(nameof(EqualityTestCases))] +// public void Equality(Size2 size1, Size2 size2, bool expectedToBeEqual) +// { +// Assert.IsTrue(Equals(size1, size2) == expectedToBeEqual); +// Assert.IsTrue(size1 == size2 == expectedToBeEqual); +// Assert.IsFalse(size1 == size2 != expectedToBeEqual); +// Assert.IsTrue(size1.Equals(size2) == expectedToBeEqual); + +// if (expectedToBeEqual) +// Assert.Equal(size1.GetHashCode(), size2.GetHashCode()); +// } + +// public IEnumerable<TestCaseData> InequalityTestCases +// { +// get +// { +// yield return +// new TestCaseData(new Size2(), null, false).SetName("A size is not equal to a null object."); +// yield return +// new TestCaseData(new Size2(), new object(), false).SetName( +// "A size is not equal to an instantiated object."); +// } +// } + +// [Fact] +// [TestCaseSource(nameof(InequalityTestCases))] +// public void Inequality(Size2 size, object obj, bool expectedToBeEqual) +// { +// Assert.IsTrue(size.Equals(obj) == expectedToBeEqual); +// } + +// public IEnumerable<TestCaseData> HashCodeTestCases +// { +// get +// { +// yield return +// new TestCaseData(new Size2(), new Size2(), true).SetName( +// "Two empty sizes have the same hash code."); +// yield return +// new TestCaseData(new Size2(50, 50), new Size2(50, 50), true).SetName( +// "Two indentical non-empty sizes have the same hash code."); +// yield return +// new TestCaseData(new Size2(0, 0), new Size2(50, 50), false).SetName( +// "Two different non-empty sizes do not have the same hash code."); +// } +// } + +// [Fact] +// [TestCaseSource(nameof(HashCodeTestCases))] +// public void HashCode(Size2 size1, Size2 size2, bool expectedThatHashCodesAreEqual) +// { +// var hashCode1 = size1.GetHashCode(); +// var hashCode2 = size2.GetHashCode(); +// if (expectedThatHashCodesAreEqual) +// Assert.Equal(hashCode1, hashCode2); +// else +// Assert.AreNotEqual(hashCode1, hashCode2); +// } + +// public IEnumerable<TestCaseData> ToPointTestCases +// { +// get +// { +// yield return +// new TestCaseData(new Size2(), new Point2()).SetName("The empty size converted to a point is the zero point."); +// yield return +// new TestCaseData(new Size2(float.MinValue, float.MaxValue), new Point2(float.MinValue, float.MaxValue)).SetName( +// "A non-empty size converted to a point is the expected point."); +// } +// } + +// [Fact] +// [TestCaseSource(nameof(ToPointTestCases))] +// public void ToPoint(Size2 size, Point2 expectedPoint) +// { +// var actualPoint = (Point2)size; +// Assert.Equal(expectedPoint, actualPoint); +// } + +// public IEnumerable<TestCaseData> FromPointTestCases +// { +// get +// { +// yield return +// new TestCaseData(new Point2(), new Size2()).SetName("The zero point converted to a size is the empty size."); +// yield return +// new TestCaseData(new Point2(float.MinValue, float.MaxValue), new Size2(float.MinValue, float.MaxValue)).SetName( +// "A non-zero point converted to a size is the expected size."); +// } +// } + +// [Fact] +// [TestCaseSource(nameof(FromPointTestCases))] +// public void FromPoint(Point2 point, Size2 expectedSize) +// { +// var actualSize = (Size2)point; +// Assert.Equal(expectedSize, actualSize); +// } + +// public IEnumerable<TestCaseData> ToVectorTestCases +// { +// get +// { +// yield return +// new TestCaseData(new Size2(), new Vector2()).SetName("The empty size converted to a vector is the zero vector."); +// yield return +// new TestCaseData(new Size2(float.MinValue, float.MaxValue), new Vector2(float.MinValue, float.MaxValue)).SetName( +// "A non-empty size converted to a vector is the expected vector."); +// } +// } + +// [Fact] +// [TestCaseSource(nameof(ToVectorTestCases))] +// public void ToVector(Size2 size, Vector2 expectedVector) +// { +// var actualVector = (Vector2)size; +// Assert.Equal(expectedVector, actualVector); +// } + +// public IEnumerable<TestCaseData> FromVectorTestCases +// { +// get +// { +// yield return +// new TestCaseData(new Vector2(), new Size2()).SetName("The zero vector converted to a size is the empty size."); +// yield return +// new TestCaseData(new Vector2(float.MinValue, float.MaxValue), new Size2(float.MinValue, float.MaxValue)).SetName( +// "A non-zero vector converted to a size is the expected size."); +// } +// } + +// [Fact] +// [TestCaseSource(nameof(FromVectorTestCases))] +// public void FromVector(Vector2 vector, Size2 expectedSize) +// { +// var actualSize = (Size2)vector; +// Assert.Equal(expectedSize, actualSize); +// } + +// //public IEnumerable<TestCaseData> FromSizeTestCases +// //{ +// // get +// // { +// // yield return +// // new TestCaseData(new Size2(), new Size2()).SetName("The empty size converted to a size is the empty size."); +// // yield return +// // new TestCaseData(new Size2(int.MinValue, int.MaxValue), new Size2(int.MinValue, int.MaxValue)).SetName( +// // "A non-zero size converted to a size is the expected size."); +// // } +// //} + +// //[Fact] +// //[TestCaseSource(nameof(FromSizeTestCases))] +// //public void FromSize(Size2 size, Size2 expectedSize) +// //{ +// // var actualSize = (Size2)size; +// // Assert.Equal(expectedSize, actualSize); +// //} + +// public IEnumerable<TestCaseData> StringCases +// { +// get +// { +// yield return +// new TestCaseData(new Size2(), +// string.Format(CultureInfo.CurrentCulture, "Width: {0}, Height: {1}", 0, 0)).SetName( +// "The empty size has the expected string representation using the current culture."); +// yield return new TestCaseData(new Size2(5.1f, -5.123f), +// string.Format(CultureInfo.CurrentCulture, "Width: {0}, Height: {1}", 5.1f, -5.123f)).SetName( +// "A non-empty size has the expected string representation using the current culture."); +// } +// } + +// [Fact] +// [TestCaseSource(nameof(StringCases))] +// public void String(Size2 size, string expectedString) +// { +// var actualString = size.ToString(); +// Assert.Equal(expectedString, actualString); +// } +// } +//} |