From 39f036d57d24ea4374286916139e2675428da9d5 Mon Sep 17 00:00:00 2001 From: Scott Williams Date: Mon, 16 Jan 2017 22:25:57 +0000 Subject: [PATCH] get Rectangle to 100% test coverage --- src/Shaper2D/Rectangle.cs | 51 +++--- tests/Shaper2D.Tests/RectangleTests.cs | 218 +++++++++++++++++++++++++ tests/Shaper2D.Tests/TestData.cs | 17 -- 3 files changed, 248 insertions(+), 38 deletions(-) delete mode 100644 tests/Shaper2D.Tests/TestData.cs diff --git a/src/Shaper2D/Rectangle.cs b/src/Shaper2D/Rectangle.cs index 299b93d..35a01dc 100644 --- a/src/Shaper2D/Rectangle.cs +++ b/src/Shaper2D/Rectangle.cs @@ -215,17 +215,14 @@ namespace Shaper2D { var buffer = new Point[2]; var c = this.FindIntersections(start, end, buffer, 2, 0); - if (c == 2) + switch (c) { - return buffer; - } - else if (c == 1) - { - return new[] { buffer[0] }; - } - else - { - return Enumerable.Empty(); + case 2: + return buffer; + case 1: + return new[] { buffer[0] }; + default: + return Enumerable.Empty(); } } @@ -248,18 +245,26 @@ namespace Shaper2D Vector2 startPoint = Vector2.Clamp(start, this.topLeft, this.bottomRight); Vector2 endPoint = Vector2.Clamp(end, this.topLeft, this.bottomRight); - if (startPoint == Vector2.Clamp(startPoint, start, end)) + // start doesn't change when its inside the shape thus not crossing + if (startPoint != start.ToVector2()) { - // if start closest is within line then its a valid point - discovered++; - buffer[offset++] = startPoint; + if (startPoint == Vector2.Clamp(startPoint, start, end)) + { + // if start closest is within line then its a valid point + discovered++; + buffer[offset++] = startPoint; + } } - if (endPoint == Vector2.Clamp(endPoint, start, end)) + // end didn't change it must not intercept with an edge + if (endPoint != end.ToVector2()) { - // if start closest is within line then its a valid point - discovered++; - buffer[offset++] = endPoint; + if (endPoint == Vector2.Clamp(endPoint, start, end)) + { + // if start closest is within line then its a valid point + discovered++; + buffer[offset] = endPoint; + } } return discovered; @@ -327,8 +332,7 @@ namespace Shaper2D { // closer to rhs clamped.X = this.bottomRight.X; // x is already the same - - distanceAlongEdge = (this.bottomRight.Y - clamped.Y) + this.Size.Width; + distanceAlongEdge = (clamped.Y - this.topLeft.Y) + this.Size.Width; } } } @@ -355,11 +359,16 @@ namespace Shaper2D } else if (this.bottomRight.X == clamped.X) { - distanceAlongEdge = (this.bottomRight.Y - clamped.Y) + this.Size.Width; + distanceAlongEdge = (clamped.Y - this.topLeft.Y) + this.Size.Width; } } } + if (distanceAlongEdge == this.length) + { + distanceAlongEdge = 0; + } + return new PointInfo { SearchPoint = point, diff --git a/tests/Shaper2D.Tests/RectangleTests.cs b/tests/Shaper2D.Tests/RectangleTests.cs index 1842b89..3ef606d 100644 --- a/tests/Shaper2D.Tests/RectangleTests.cs +++ b/tests/Shaper2D.Tests/RectangleTests.cs @@ -24,6 +24,79 @@ namespace Shaper2D.Tests false }, //corner is inside }; + public static TheoryData DistanceTheoryData = + new TheoryData + { + { + new Point(10,10), // loc + new Size(100,100), // size + new Point(10,10), // test + 0f + }, //corner is inside + { + new Point(10,10), // loc + new Size(100,100), // size + new Point(9,10), // test + 1f + }, + { + new Point(10,10), // loc + new Size(100,100), // size + new Point(10,13), // test + 0f + }, + { + new Point(10,10), // loc + new Size(100,100), // size + new Point(14,13), // test + -3f + }, + { + new Point(10,10), // loc + new Size(100,100), // size + new Point(13,14), // test + -3f + }, + { + new Point(10,10), // loc + new Size(100,100), // size + new Point(7,6), // test + 5f + }, + }; + + public static TheoryData PathDistanceTheoryData = + new TheoryData + { + { new Point(0,0), 0f, 0f }, + { new Point(1,0), 0f, 1f }, + { new Point(9,0), 0f, 9f }, + { new Point(10,0), 0f, 10f }, + { new Point(10, 1), 0f, 11f }, + { new Point(10,9), 0f, 19f }, + { new Point(10,10), 0f, 20f }, + { new Point(9,10), 0f, 21f }, + { new Point(1,10), 0f, 29f }, + { new Point(0,10), 0f, 30f }, + { new Point(0,9), 0f, 31f }, + { new Point(0,1), 0f, 39f }, + + { new Point(4,3), 3f, 4f }, + { new Point(3, 4), 3f, 36f }, + + { new Point(-1,0), 1f, 0f }, + { new Point(1,-1), 1f, 1f }, + { new Point(9,-1), 1f, 9f }, + { new Point(11,0), 1f, 10f }, + { new Point(11, 1), 1f, 11f }, + { new Point(11,9), 1f, 19f }, + { new Point(11,10), 1f, 20f }, + { new Point(9,11), 1f, 21f }, + { new Point(1,11), 1f, 29f }, + { new Point(-1,10), 1f, 30f }, + { new Point(-1,9), 1f, 31f }, + { new Point(-1,1), 1f, 39f }, + }; [Theory] [MemberData(nameof(PointInPolygonTheoryData))] @@ -32,5 +105,150 @@ namespace Shaper2D.Tests var shape = new Rectangle(location, size); Assert.Equal(isInside, shape.Contains(point)); } + + [Theory] + [MemberData(nameof(DistanceTheoryData))] + public void Distance(TestPoint location, TestSize size, TestPoint point, float expectecDistance) + { + IShape shape = new Rectangle(location, size); + + Assert.Equal(expectecDistance, shape.Distance(point)); + } + + [Theory] + [MemberData(nameof(PathDistanceTheoryData))] + public void DistanceFromPath_Path(TestPoint point, float expectecDistance, float alongPath) + { + IPath shape = new Rectangle(0, 0, 10, 10); + var info = shape.Distance(point); + Assert.Equal(expectecDistance, info.DistanceFromPath); + Assert.Equal(alongPath, info.DistanceAlongPath); + } + + [Fact] + public void Left() + { + var shape = new Rectangle(10, 11, 12, 13); + Assert.Equal(10, shape.Left); + } + + [Fact] + public void Right() + { + var shape = new Rectangle(10, 11, 12, 13); + Assert.Equal(22, shape.Right); + } + + [Fact] + public void Top() + { + var shape = new Rectangle(10, 11, 12, 13); + Assert.Equal(11, shape.Top); + } + + [Fact] + public void Bottom() + { + var shape = new Rectangle(10, 11, 12, 13); + Assert.Equal(24, shape.Bottom); + } + + [Fact] + public void Size() + { + var shape = new Rectangle(10, 11, 12, 13); + Assert.Equal(12, shape.Size.Width); + Assert.Equal(13, shape.Size.Height); + } + + [Fact] + public void Bounds_Shape() + { + IShape shape = new Rectangle(10, 11, 12, 13); + Assert.Equal(10, shape.Bounds.Left); + Assert.Equal(22, shape.Bounds.Right); + Assert.Equal(11, shape.Bounds.Top); + Assert.Equal(24, shape.Bounds.Bottom); + } + + [Fact] + public void LienearSegements() + { + ILineSegment shape = new Rectangle(10, 11, 12, 13); + var segemnts = shape.AsSimpleLinearPath(); + Assert.Equal(new Point(10, 11), segemnts[0]); + Assert.Equal(new Point(22, 11), segemnts[1]); + Assert.Equal(new Point(22, 24), segemnts[2]); + Assert.Equal(new Point(10, 24), segemnts[3]); + } + + [Fact] + public void Intersections_2() + { + IShape shape = new Rectangle(1, 1, 10, 10); + var intersections = shape.FindIntersections(new Point(0, 5), new Point(20, 5)); + + Assert.Equal(2, intersections.Count()); + Assert.Equal(new Point(1, 5), intersections.First()); + Assert.Equal(new Point(11, 5), intersections.Last()); + } + + [Fact] + public void Intersections_1() + { + IShape shape = new Rectangle(1, 1, 10, 10); + var intersections = shape.FindIntersections(new Point(0, 5), new Point(5, 5)); + + Assert.Equal(1, intersections.Count()); + Assert.Equal(new Point(1, 5), intersections.First()); + } + + [Fact] + public void Intersections_0() + { + IShape shape = new Rectangle(1, 1, 10, 10); + var intersections = shape.FindIntersections(new Point(0, 5), new Point(-5, 5)); + + Assert.Equal(0, intersections.Count()); + } + + [Fact] + public void Bounds_Path() + { + IPath shape = new Rectangle(10, 11, 12, 13); + Assert.Equal(10, shape.Bounds.Left); + Assert.Equal(22, shape.Bounds.Right); + Assert.Equal(11, shape.Bounds.Top); + Assert.Equal(24, shape.Bounds.Bottom); + } + + [Fact] + public void IsClosed_Path() + { + IPath shape = new Rectangle(10, 11, 12, 13); + Assert.Equal(true, shape.IsClosed); + } + + [Fact] + public void Length_Path() + { + IPath shape = new Rectangle(10, 11, 12, 13); + Assert.Equal(50, shape.Length); + } + + [Fact] + public void MaxIntersections_Shape() + { + IShape shape = new Rectangle(10, 11, 12, 13); + Assert.Equal(4, shape.MaxIntersections); + } + + [Fact] + public void ShapePaths() + { + IShape shape = new Rectangle(10, 11, 12, 13); + + Assert.Equal((IPath)shape, shape.Paths.Single()); + } } } diff --git a/tests/Shaper2D.Tests/TestData.cs b/tests/Shaper2D.Tests/TestData.cs deleted file mode 100644 index c53bd45..0000000 --- a/tests/Shaper2D.Tests/TestData.cs +++ /dev/null @@ -1,17 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; - -namespace Shaper2D.Tests -{ - public static class TestData - { - static Random rand = new Random(); - - public static Size RandomSize() - { - return new Size((float)(rand.NextDouble() * rand.Next()), (float)(rand.NextDouble() * rand.Next())); - } - } -}