using System; using Chernobyl.Mathematics.Collision; using Chernobyl.Mathematics.Vectors; using NUnit.Framework; namespace Chernobyl.Mathematics.Geometry { [TestFixture, Description("A test fixture for testing the Rectangle type.")] public class RectangleTests { public RectangleTests() { Precision = DefaultPrecision; } [Test, Description("Tests the Rectangle() constructor to ensure it sets " + "the proper values.")] public virtual void TestDefaultConstructor() { Rectangle rectangle = new Rectangle(); Assert.AreEqual(new Vector2(0, 0), rectangle.Origin, "The origin of " + "the Rectangle is not the expected value."); Assert.AreEqual(1, rectangle.Width, "The width of the Rectangle " + "is not the expected value."); Assert.AreEqual(1, rectangle.Height, "The height of the Rectangle" + "is not the expected value."); } [Test, Description("Tests the Rectangle(float, float) constructor to " + "ensure it sets the proper values.")] public virtual void TestWidthHeightConstructor() { Rectangle rectangle = new Rectangle(5, 6); Assert.AreEqual(new Vector2(0, 0), rectangle.Origin, "The origin of " + "the Rectangle is not the expected value."); Assert.AreEqual(5, rectangle.Width, "The width of the Rectangle " + "is not the expected value."); Assert.AreEqual(6, rectangle.Height, "The height of the Rectangle" + "is not the expected value."); } [Test, Description("Tests the Rectangle(float, float, float, float) " + "constructor to ensure it sets the proper values.")] public virtual void TestFloatOriginWidthHeightConstructor() { Rectangle rectangle = new Rectangle(3, 2, 5, 6); Assert.AreEqual(new Vector2(3, 2), rectangle.Origin, "The origin of " + "the Rectangle is not the expected value."); Assert.AreEqual(5, rectangle.Width, "The width of the Rectangle " + "is not the expected value."); Assert.AreEqual(6, rectangle.Height, "The height of the Rectangle" + "is not the expected value."); } [Test, Description("Tests the Rectangle(Vector2, float, float) " + "constructor to ensure it sets the proper values.")] public virtual void TestVector2OriginWidthHeightConstructor() { Vector2 origin = new Vector2(3, 2); Rectangle rectangle = new Rectangle(origin, 5, 6); Assert.AreEqual(origin, rectangle.Origin, "The origin of " + "the Rectangle is not the expected value."); Assert.AreEqual(5, rectangle.Width, "The width of the Rectangle " + "is not the expected value."); Assert.AreEqual(6, rectangle.Height, "The height of the Rectangle" + "is not the expected value."); } [Test, Description("Test the Rectangle.Encloses(IShape) method " + "to ensure it returns true when a slightly smaller rectangle is " + "passed to it.")] public void Encloses() { Rectangle rectangle = CreateRectangle(); Rectangle fullyContainedRectangle = new Rectangle(rectangle.Origin + new Vector2(Precision, Precision), rectangle.Width - Precision, rectangle.Height - Precision); Assert.That(rectangle.Encloses(fullyContainedRectangle), "The rectangle \"" + rectangle + " should fully contain " + "this rectangle: " + fullyContainedRectangle); } [Test, Description("Test the Rectangle.Encloses(IShape) method " + "to ensure it returns true when itself is passed to it.")] public void DoesNotEncloseItself() { Rectangle rectangle = CreateRectangle(); Assert.That(rectangle.Encloses(rectangle) == false, "The " + "rectangle should not fully contain itself."); } [Test, Description("Test the Rectangle.Encloses(IShape) method " + "to ensure it returns false when a slightly larger rectangle is " + "passed to it.")] public void DoesNotnclose() { Rectangle rectangle = CreateRectangle(); Rectangle notFullyContainedRectangle = new Rectangle(rectangle.Origin - new Vector2(Precision, Precision), rectangle.Width + Precision, rectangle.Height + Precision); Assert.That(rectangle.Encloses(notFullyContainedRectangle) == false, "The rectangle \"" + rectangle + " should NOT fully contain " + "this rectangle: " + notFullyContainedRectangle); } [Test, Description("Tests the Origin of the Rectangle created by " + "CreateRectangle() for correctness.")] public virtual void OriginIsCorrect() { Rectangle rectangle = CreateRectangle(); Assert.AreEqual(ExpectedOrigin, rectangle.Origin, "The value of the " + "Origin was not the expected value." ); } [Test, Description("Tests the Center of the Rectangle created by " + "CreateRectangle() for correctness.")] public virtual void CenterIsCorrect() { Rectangle rectangle = CreateRectangle(); Assert.AreEqual(ExpectedCenter, rectangle.Center, "The value of the " + "Center was not the expected value."); } [Test, Description("Tests the DiagonalLength of the Rectangle created by " + "CreateRectangle() for correctness.")] public virtual void DiagonalLengthIsCorrect() { Rectangle rectangle = CreateRectangle(); Assert.AreEqual(ExpectedDiagonalLength, rectangle.DiagonalLength, "The value of the DiagonalLength was not the expected value."); } [Test, Description("Tests the TopLeft of the Rectangle created by " + "CreateRectangle() for correctness.")] public virtual void TopLeftIsCorrect() { Rectangle rectangle = CreateRectangle(); Assert.AreEqual(ExpectedTop, rectangle.Top, "The value of the " + "Top was not the expected value."); } [Test, Description("Tests the BottomLeft of the Rectangle created by " + "CreateRectangle() for correctness.")] public virtual void BottomLeftIsCorrect() { Rectangle rectangle = CreateRectangle(); Assert.AreEqual(ExpectedBottom, rectangle.Bottom, "The value of the " + "Bottom was not the expected value."); } [Test, Description("Tests the BottomRight of the Rectangle created by " + "CreateRectangle() for correctness.")] public virtual void BottomRightIsCorrect() { Rectangle rectangle = CreateRectangle(); Assert.AreEqual(ExpectedBottomRight, rectangle.BottomRight, "The value of the BottomRight was not the expected value."); } [Test, Description("Tests the TopRight of the Rectangle created by " + "CreateRectangle() for correctness.")] public virtual void TopRightIsCorrect() { Rectangle rectangle = CreateRectangle(); Assert.AreEqual(ExpectedTopRight, rectangle.TopRight, "The value of the TopRight was not the expected value."); } [Test, Description("Tests the LeftSide of the Rectangle created by " + "CreateRectangle() for correctness.")] public virtual void LeftSideIsCorrect() { Rectangle rectangle = CreateRectangle(); Assert.AreEqual(ExpectedLeftSide, rectangle.LeftSide, "The value of the LeftSide was not the expected value."); } [Test, Description("Tests the RightSide of the Rectangle created by " + "CreateRectangle() for correctness.")] public virtual void RightSideIsCorrect() { Rectangle rectangle = CreateRectangle(); Assert.AreEqual(ExpectedRightSide, rectangle.RightSide, "The value of the RightSide was not the expected value."); } [Test, Description("Tests the Top of the Rectangle created by " + "CreateRectangle() for correctness.")] public virtual void TopIsCorrect() { Rectangle rectangle = CreateRectangle(); Assert.AreEqual(ExpectedTop, rectangle.Top, "The value of the " + "Top was not the expected value."); } [Test, Description("Tests the Bottom of the Rectangle created by " + "CreateRectangle() for correctness.")] public virtual void BottomIsCorrect() { Rectangle rectangle = CreateRectangle(); Assert.AreEqual(ExpectedBottom, rectangle.Bottom, "The value of the " + "Bottom was not the expected value."); } [Test, Description("Tests the NorthWestQuadrant of the Rectangle " + "created by CreateRectangle() for correctness.")] public virtual void NorthWestQuadrantIsCorrect() { Rectangle rectangle = CreateRectangle(); Assert.AreEqual(ExpectedNorthWestQuadrant, rectangle.NorthWestQuadrant, "The value of the NorthWestQuadrant was not the expected value."); } [Test, Description("Tests the NorthEastQuadrant of the Rectangle created " + "by CreateRectangle() for correctness.")] public virtual void NorthEastQuadrantIsCorrect() { Rectangle rectangle = CreateRectangle(); Assert.AreEqual(ExpectedNorthEastQuadrant, rectangle.NorthEastQuadrant, "The value of the NorthEastQuadrant was not the expected value."); } [Test, Description("Tests the SouthWestQuadrant of the Rectangle created " + "by CreateRectangle() for correctness.")] public virtual void SouthWestQuadrantIsCorrect() { Rectangle rectangle = CreateRectangle(); Assert.AreEqual(ExpectedSouthWestQuadrant, rectangle.SouthWestQuadrant, "The value of the SouthWestQuadrant was not the expected value."); } [Test, Description("Tests the SouthEastQuadrant of the Rectangle created " + "by CreateRectangle() for correctness.")] public virtual void SouthEastQuadrantIsCorrect() { Rectangle rectangle = CreateRectangle(); Assert.AreEqual(ExpectedSouthEastQuadrant, rectangle.SouthEastQuadrant, "The value of the SouthEastQuadrant was not the expected value."); } /// /// Creates the that is to be tested. This /// method should always create a new instance and never reuse the same /// instance. By default, this method creates a /// whose origin is at (0, 0) with a width and height of 5. /// /// The that is to be tested. protected virtual Rectangle CreateRectangle() { return new Rectangle(0, 0, 5, 5); } /// /// The default value of the property. The value /// of this field is .0001f. /// public const float DefaultPrecision = .0001f; /// /// The amount of precision to use when testing the . /// By default, the value of this property is specified by /// . /// protected float Precision { get; set; } /// /// The expected value of the property . /// protected virtual Vector2 ExpectedOrigin { get { return new Vector2(0, 0); } } /// /// The expected value of the property . /// protected virtual Vector2 ExpectedCenter { get { return new Vector2(2.5f, 2.5f); } } /// /// The expected value of the property . /// protected virtual float ExpectedDiagonalLength { get { return 7.0710678118655f; } } /// /// The expected value of the property . /// protected virtual Vector2 ExpectedTopLeft { get { return new Vector2(0, 5); } } /// /// The expected value of the property . /// protected virtual Vector2 ExpectedBottomLeft { get { return new Vector2(0, 0); } } /// /// The expected value of the property . /// protected virtual Vector2 ExpectedBottomRight { get { return new Vector2(5, 0); } } /// /// The expected value of the property . /// protected virtual Vector2 ExpectedTopRight { get { return new Vector2(5, 5); } } /// /// The expected value of the property . /// protected virtual float ExpectedLeftSide { get { return 0; } } /// /// The expected value of the property . /// protected virtual float ExpectedRightSide { get { return 5; } } /// /// The expected value of the property . /// protected virtual float ExpectedTop { get { return 5; } } /// /// The expected value of the property . /// protected virtual float ExpectedBottom { get { return 0; } } /// /// The expected value of the property . /// protected virtual Rectangle ExpectedNorthWestQuadrant { get { return new Rectangle(0, 2.5f, 2.5f, 2.5f); } } /// /// The expected value of the property . /// protected virtual Rectangle ExpectedNorthEastQuadrant { get { return new Rectangle(2.5f, 2.5f, 2.5f, 2.5f); } } /// /// The expected value of the property . /// protected virtual Rectangle ExpectedSouthWestQuadrant { get { return new Rectangle(0, 0, 2.5f, 2.5f); } } /// /// The expected value of the property . /// protected virtual Rectangle ExpectedSouthEastQuadrant { get { return new Rectangle(2.5f, 0, 2.5f, 2.5f); } } [TestFixture, Description("Tests the IShape portion " + "of the Rectangle.")] public class RectangleShapeTests : ShapeTests { protected override IShape CreateShape() { return new Rectangle(0, 0, 5, 5); } [Test] public override void Rotate() { Assert.Throws(() => base.Rotate()); } [Test] public override void Pitch() { Assert.Throws(() => base.Pitch()); } [Test] public override void Yaw() { Assert.Throws(() => base.Yaw()); } [Test] public override void Roll() { Assert.Throws(() => base.Roll()); } protected override float ExpectedWidth { get { return 5; } } protected override float ExpectedHeight { get { return 5; } } protected override float ExpectedDepth { get { return 0; } } protected override bool ExpectedIsConvex { get { return true; } } protected override float ExpectedArea { get { return 25; } } protected override float ExpectedVolume { get { return 0; } } protected override float ExpectedPerimeter { get { return 20; } } protected override uint ExpectedDimensions { get { return 2; } } } [TestFixture, Description("Tests the ICollidable portion " + "of the Rectangle.")] public class PointExtendedCollidableTests : ExtendedCollidableTests { protected override IExtendedCollidable CreateExtendedCollidable() { return new Rectangle(0, 0, 5, 5); } protected override Point2D CreateCollidingCollidable() { return new Point2D(3, 3); } protected override Point2D CreateEdgeCollidingCollidable() { return new Point2D(4.9f, 4.9f); } protected override Point2D CreateNonCollidingCollidable() { return new Point2D(5, 5); } } [TestFixture, Description("Tests the ICollidable portion " + "of the Rectangle.")] public class CircleExtendedCollidableTests : ExtendedCollidableTests { protected override IExtendedCollidable CreateExtendedCollidable() { return new Rectangle(0, 0, 5, 5); } protected override Circle CreateCollidingCollidable() { return new Circle(3, 3, 1); } protected override Circle CreateEdgeCollidingCollidable() { return new Circle(6, 6, 1.1f); } protected override Circle CreateNonCollidingCollidable() { return new Circle(6, 5, .49f); } } [TestFixture, Description("Tests the IExtendedCollidable " + "portion of the Rectangle.")] public class RectangleExtendedCollidableTests : ExtendedCollidableTests { protected override IExtendedCollidable CreateExtendedCollidable() { return new Rectangle(0, 0, 5, 5); } protected override Rectangle CreateCollidingCollidable() { return new Rectangle(1, 1, 1, 1); } protected override Rectangle CreateEdgeCollidingCollidable() { return new Rectangle(4.9f, 4.9f, 3, 3); } protected override Rectangle CreateNonCollidingCollidable() { return new Rectangle(-5, -5, 4, 4); } } [TestFixture, Description("Tests the IEquatable and Object " + "portion of the Rectangle.")] public class RectangleEquatableTests : EquatableTests { protected override IEquatable CreateEquatable() { return new Rectangle(74.87f, 294.92f, 827.2f, 27.2f); } protected override Rectangle CreateEqualObject() { return new Rectangle(74.87f, 294.92f, 827.2f, 27.2f); } protected override Rectangle CreateNotEqualObject() { return new Rectangle(); } protected override string ExpectedToStringResult { get { return "{X=74.87, Y=294.92, Width=827.2, Height=27.2}"; } } } [TestFixture, Description("Tests the equality operators of the Rectangle.")] public class EqualityOperatorTests : ReferenceEqualityOperatorTests { protected override Rectangle CreateX() { return new Rectangle(74.87f, 294.92f, 827.2f, 27.2f); } protected override Rectangle CreateEqual() { return new Rectangle(74.87f, 294.92f, 827.2f, 27.2f); } protected override Rectangle CreateNotEqual() { return new Rectangle(); } } } }