#if ENABLE_UNITTESTS
using System;
using UnityEngine;
using NUnit.Framework;
namespace Framework.Support {
/// Unit tests for the utility methods
[TestFixture]
internal class IntersectorTest {
///
/// Verifies that the ground height determination clamps the X coordinate to
/// the left side of the ground
///
[Test]
public void PointIsNeverInZeroSizeEllipse() {
Assert.IsFalse(
Intersector.IsPointInEllipse(Vector2.zero, Vector2.zero, Vector2.zero)
);
}
/// Verifies that points in tall ellipses are detected correctly
///
/// Checks both X and Y offsets to catch when the extents are the wrong way around
///
[Test]
public void PointInTallEllipseIsDetected() {
Vector2 ellipseExtents = new Vector2(5.0f, 50.0f);
Assert.IsTrue(
Intersector.IsPointInEllipse(new Vector2(0.0f, 49.0f), Vector2.zero, ellipseExtents)
);
Assert.IsTrue(
Intersector.IsPointInEllipse(new Vector2(0.0f, -49.0f), Vector2.zero, ellipseExtents)
);
Assert.IsFalse(
Intersector.IsPointInEllipse(new Vector2(49.0f, 0.0f), Vector2.zero, ellipseExtents)
);
Assert.IsFalse(
Intersector.IsPointInEllipse(new Vector2(-49.0f, 0.0f), Vector2.zero, ellipseExtents)
);
}
/// Verifies that points in flat ellipses are detected correctly
///
/// Checks both X and Y offsets to catch when the extents are the wrong way around
///
[Test]
public void PointInFlatEllipseIsDetected() {
Vector2 ellipseExtents = new Vector2(50.0f, 5.0f);
Assert.IsTrue(
Intersector.IsPointInEllipse(new Vector2(49.0f, 0.0f), Vector2.zero, ellipseExtents)
);
Assert.IsTrue(
Intersector.IsPointInEllipse(new Vector2(-49.0f, 0.0f), Vector2.zero, ellipseExtents)
);
Assert.IsFalse(
Intersector.IsPointInEllipse(new Vector2(0.0f, 49.0f), Vector2.zero, ellipseExtents)
);
Assert.IsFalse(
Intersector.IsPointInEllipse(new Vector2(0.0f, -49.0f), Vector2.zero, ellipseExtents)
);
}
///
/// Verifies that the union between an ellipse and a line segment can be found
///
[Test]
public void LineSegmentEllipseUnionCanBeFound() {
Vector2 ellipseExtents = new Vector2(5.0f, 50.0f);
LineSegment2? union = Intersector.GetLineSegmentEllipseUnion(
new LineSegment2(new Vector2(-10.0f, 0.0f), new Vector2(+10.0f, 0.0f)),
Vector2.zero, ellipseExtents
);
Assert.IsTrue(union.HasValue);
Assert.That(union.Value.From.x, Is.EqualTo(-5.0f).Within(32).Ulps);
Assert.That(union.Value.To.x, Is.EqualTo(5.0f).Within(32).Ulps);
}
///
/// Verifies that the union between a rectangle and a horizontal line segment can be found
///
[Test]
public void HorizontalLineSegmentRectangleUnionCanBeFound() {
Vector2 rectangleMin = new Vector2(-5.0f, -10.0f);
Vector2 rectangleMax = new Vector2(+5.0f, +10.0f);
LineSegment2? union = Intersector.GetLineSegmentRectangleUnion(
new LineSegment2(new Vector2(-10.0f, 0.0f), new Vector2(+10.0f, 0.0f)),
rectangleMin, rectangleMax
);
Assert.IsTrue(union.HasValue);
Assert.That(union.Value.From.x, Is.EqualTo(-5.0f).Within(32).Ulps);
Assert.That(union.Value.To.x, Is.EqualTo(5.0f).Within(32).Ulps);
}
///
/// Verifies that the union between a rectangle and a vertical line segment can be found
///
[Test]
public void VerticalLineSegmentRectangleUnionCanBeFound() {
Vector2 rectangleMin = new Vector2(-5.0f, -10.0f);
Vector2 rectangleMax = new Vector2(+5.0f, +10.0f);
LineSegment2? union = Intersector.GetLineSegmentRectangleUnion(
new LineSegment2(new Vector2(0.0f, -20.0f), new Vector2(0.0f, +20.0f)),
rectangleMin, rectangleMax
);
Assert.IsTrue(union.HasValue);
Assert.That(union.Value.From.y, Is.EqualTo(-10.0f).Within(32).Ulps);
Assert.That(union.Value.To.y, Is.EqualTo(10.0f).Within(32).Ulps);
}
///
/// Verifies that the union between a rectangle and a diagonal line segment can be found
///
[Test]
public void DiagonalLineSegmentRectangleUnionCanBeFound() {
Vector2 rectangleMin = new Vector2(-5.0f, -5.0f);
Vector2 rectangleMax = new Vector2(+5.0f, +5.0f);
LineSegment2? union = Intersector.GetLineSegmentRectangleUnion(
new LineSegment2(new Vector2(-10.0f, -10.0f), new Vector2(+10.0f, +10.0f)),
rectangleMin, rectangleMax
);
Assert.IsTrue(union.HasValue);
Assert.That(union.Value.From.x, Is.EqualTo(-5.0f).Within(32).Ulps);
Assert.That(union.Value.From.y, Is.EqualTo(-5.0f).Within(32).Ulps);
Assert.That(union.Value.To.x, Is.EqualTo(5.0f).Within(32).Ulps);
Assert.That(union.Value.To.y, Is.EqualTo(5.0f).Within(32).Ulps);
}
}
} // namespace Framework.Support
#endif // ENABLE_UNITTESTS