|
| 1 | +ο»Ώusing System; |
| 2 | +using Bearded.Utilities.Geometry; |
| 3 | +using Bearded.Utilities.Tests.Generators; |
| 4 | +using FluentAssertions; |
| 5 | +using FsCheck.Xunit; |
| 6 | +using OpenTK.Mathematics; |
| 7 | + |
| 8 | +namespace Bearded.Utilities.Tests.Geometry; |
| 9 | + |
| 10 | +public sealed class RectangleTest |
| 11 | +{ |
| 12 | + private const float epsilon = 0.01f; |
| 13 | + |
| 14 | + [Property(Arbitrary = new[] { typeof(FloatGenerators.ForArithmetic) })] |
| 15 | + public void RectangleMadeFromXyWidthHeightHasCorrectDimensions(float x, float y, float w, float h) |
| 16 | + { |
| 17 | + if (w < 0) w *= -1; |
| 18 | + if (h < 0) h *= -1; |
| 19 | + |
| 20 | + var rectangle = new Rectangle(x, y, w, h); |
| 21 | + |
| 22 | + // from input |
| 23 | + rectangle.Left.Should().BeApproximately(x, epsilon); |
| 24 | + rectangle.Top.Should().BeApproximately(y, epsilon); |
| 25 | + rectangle.Width.Should().BeApproximately(w, epsilon); |
| 26 | + rectangle.Height.Should().BeApproximately(h, epsilon); |
| 27 | + |
| 28 | + // derived |
| 29 | + rectangle.Right.Should().BeApproximately(x + w, epsilon); |
| 30 | + rectangle.Bottom.Should().BeApproximately(y + h, epsilon); |
| 31 | + } |
| 32 | + |
| 33 | + [Property(Arbitrary = new[] { typeof(FloatGenerators.ForArithmetic) })] |
| 34 | + public void RectangleMadeFromSidesHasCorrectDimensions(float x1, float x2, float y1, float y2) |
| 35 | + { |
| 36 | + var top = Math.Min(y1, y2); |
| 37 | + var right = Math.Max(x1, x2); |
| 38 | + var bottom = Math.Max(y1, y2); |
| 39 | + var left = Math.Min(x1, x2); |
| 40 | + |
| 41 | + var rectangle = Rectangle.WithSides(top, right, bottom, left); |
| 42 | + |
| 43 | + // from input |
| 44 | + rectangle.Top.Should().BeApproximately(top, epsilon); |
| 45 | + rectangle.Right.Should().BeApproximately(right, epsilon); |
| 46 | + rectangle.Bottom.Should().BeApproximately(bottom, epsilon); |
| 47 | + rectangle.Left.Should().BeApproximately(left, epsilon); |
| 48 | + |
| 49 | + // derived |
| 50 | + rectangle.Width.Should().BeApproximately(right - left, epsilon); |
| 51 | + rectangle.Height.Should().BeApproximately(bottom - top, epsilon); |
| 52 | + } |
| 53 | + |
| 54 | + [Property(Arbitrary = new[] { typeof(FloatGenerators.ForArithmetic) })] |
| 55 | + public void RectangleMadeFromCornersHasCorrectDimensions(float x1, float x2, float y1, float y2) |
| 56 | + { |
| 57 | + var top = Math.Min(y1, y2); |
| 58 | + var right = Math.Max(x1, x2); |
| 59 | + var bottom = Math.Max(y1, y2); |
| 60 | + var left = Math.Min(x1, x2); |
| 61 | + |
| 62 | + var rectangle = Rectangle.WithCorners(new Vector2(left, top), new Vector2(right, bottom)); |
| 63 | + |
| 64 | + // from input |
| 65 | + rectangle.Top.Should().BeApproximately(top, epsilon); |
| 66 | + rectangle.Right.Should().BeApproximately(right, epsilon); |
| 67 | + rectangle.Bottom.Should().BeApproximately(bottom, epsilon); |
| 68 | + rectangle.Left.Should().BeApproximately(left, epsilon); |
| 69 | + |
| 70 | + // derived |
| 71 | + rectangle.Width.Should().BeApproximately(right - left, epsilon); |
| 72 | + rectangle.Height.Should().BeApproximately(bottom - top, epsilon); |
| 73 | + } |
| 74 | +} |
0 commit comments