Skip to content

Commit d07e429

Browse files
Fix tests
1 parent 5077d42 commit d07e429

File tree

2 files changed

+31
-19
lines changed

2 files changed

+31
-19
lines changed

tests/ImageSharp.Drawing.Tests/Shapes/RegularPolygonTests.cs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,43 +51,49 @@ public void RadiusMustBeGreaterThan0(float radius, bool throws)
5151
[Fact]
5252
public void GeneratesCorrectPath()
5353
{
54-
const float Radius = 10;
54+
const float radius = 10;
5555
int pointsCount = new Random().Next(3, 20);
5656

57-
RegularPolygon poly = new(Vector2.Zero, pointsCount, Radius, 0);
57+
RegularPolygon poly = new(Vector2.Zero, pointsCount, radius, 0);
5858

59-
IReadOnlyList<PointF> points = poly.Flatten().ToArray()[0].Points.ToArray();
59+
PointF[] points = poly.Flatten().ToArray()[0].Points.ToArray();
6060

6161
// calculates baselineDistance
6262
float baseline = Vector2.Distance(points[0], points[1]);
6363

6464
// all points are exact the same distance away from the center
65-
for (int i = 0; i < points.Count; i++)
65+
for (int i = 0; i < points.Length; i++)
6666
{
6767
int j = i - 1;
6868
if (i == 0)
6969
{
70-
j = points.Count - 1;
70+
j = points.Length - 1;
7171
}
7272

7373
float actual = Vector2.Distance(points[i], points[j]);
7474
Assert.Equal(baseline, actual, 3F);
75-
Assert.Equal(Radius, Vector2.Distance(Vector2.Zero, points[i]), 3F);
75+
Assert.Equal(radius, Vector2.Distance(Vector2.Zero, points[i]), 3F);
7676
}
7777
}
7878

7979
[Fact]
8080
public void AngleChangesOnePointToStartAtThatPosition()
8181
{
82-
const float Radius = 10;
82+
const float radius = 10;
83+
const double tolerance = 1e-3D;
8384
double anAngleDegrees = new Random().NextDouble() * 360D;
85+
double expectedAngleDegrees = anAngleDegrees + 90D;
86+
if (expectedAngleDegrees >= 360D)
87+
{
88+
expectedAngleDegrees -= 360D;
89+
}
8490

85-
RegularPolygon poly = new(Vector2.Zero, 3, Radius, (float)anAngleDegrees);
91+
RegularPolygon poly = new(Vector2.Zero, 3, radius, (float)anAngleDegrees);
8692
IReadOnlyList<PointF> points = poly.Flatten().ToArray()[0].Points.ToArray();
8793

8894
IEnumerable<double> allAngles = points.Select(b => GeometryUtilities.RadianToDegree((float)Math.Atan2(b.Y, b.X)))
8995
.Select(x => x < 0 ? x + 360D : x); // normalise it from +/- 180 to 0 to 360
9096

91-
Assert.Contains(allAngles, a => Math.Abs(a - anAngleDegrees) < 0.000001);
97+
Assert.Contains(allAngles, a => Math.Abs(a - expectedAngleDegrees) < tolerance);
9298
}
9399
}

tests/ImageSharp.Drawing.Tests/Shapes/StarTests.cs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@ public void RadiusMustBeGreaterThan0(float radius, bool throws)
5353
[Fact]
5454
public void GeneratesCorrectPath()
5555
{
56-
const float Radius = 5;
57-
const float Radius2 = 30;
56+
const float radius = 5;
57+
const float radius2 = 30;
5858
int pointsCount = new Random().Next(3, 20);
5959

60-
Star poly = new(Vector2.Zero, pointsCount, Radius, Radius2, 0);
60+
Star poly = new(Vector2.Zero, pointsCount, radius, radius2, 0);
6161

6262
PointF[] points = poly.Flatten().ToArray()[0].Points.ToArray();
6363

@@ -78,29 +78,35 @@ public void GeneratesCorrectPath()
7878
Assert.Equal(baseline, actual, 3F);
7979
if (i % 2 == 1)
8080
{
81-
Assert.Equal(Radius, Vector2.Distance(Vector2.Zero, points[i]), 3F);
81+
Assert.Equal(radius, Vector2.Distance(Vector2.Zero, points[i]), 3F);
8282
}
8383
else
8484
{
85-
Assert.Equal(Radius2, Vector2.Distance(Vector2.Zero, points[i]), 3F);
85+
Assert.Equal(radius2, Vector2.Distance(Vector2.Zero, points[i]), 3F);
8686
}
8787
}
8888
}
8989

9090
[Fact]
9191
public void AngleChangesOnePointToStartAtThatPosition()
9292
{
93-
const float Radius = 10;
94-
const float Radius2 = 20;
93+
const float radius = 10;
94+
const float radius2 = 20;
95+
const double tolerance = 1e-3D;
9596
double anAngleDegrees = new Random().NextDouble() * 360D;
97+
double expectedAngleDegrees = anAngleDegrees + 90D;
98+
if (expectedAngleDegrees >= 360D)
99+
{
100+
expectedAngleDegrees -= 360D;
101+
}
96102

97-
Star poly = new(Vector2.Zero, 3, Radius, Radius2, (float)anAngleDegrees);
98-
ISimplePath[] points = poly.Flatten().ToArray();
103+
Star poly = new(Vector2.Zero, 3, radius, radius2, (float)anAngleDegrees);
104+
ISimplePath[] points = [.. poly.Flatten()];
99105

100106
IEnumerable<double> allAngles = points[0].Points.ToArray()
101107
.Select(b => GeometryUtilities.RadianToDegree((float)Math.Atan2(b.Y, b.X)))
102108
.Select(x => x < 0 ? x + 360D : x); // normalise it from +/- 180 to 0 to 360
103109

104-
Assert.Contains(allAngles, a => Math.Abs(a - anAngleDegrees) < 0.000001);
110+
Assert.Contains(allAngles, a => Math.Abs(a - expectedAngleDegrees) < tolerance);
105111
}
106112
}

0 commit comments

Comments
 (0)