1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-09 23:45:42 +00:00

Add missing unit tests, bumps coverage to 99%

This commit is contained in:
Oliver Booth 2022-06-01 15:36:45 +01:00
parent 5e835e10f1
commit b666b272a1
No known key found for this signature in database
GPG Key ID: 32A00B35503AF634
8 changed files with 470 additions and 47 deletions

View File

@ -10,14 +10,14 @@ public class CircleFTests
public void Area_ShouldBePiRadiusRadius_GivenUnitCircle()
{
var unitCircle = CircleF.Unit;
Assert.AreEqual(MathF.PI * unitCircle.Radius * unitCircle.Radius, unitCircle.Area);
Assert.AreEqual(MathF.PI, unitCircle.Area);
}
[TestMethod]
public void Circumference_ShouldBe2PiRadius_GivenUnitCircle()
{
var unitCircle = CircleF.Unit;
Assert.AreEqual(2.0f * MathF.PI * unitCircle.Radius, unitCircle.Circumference, 1e-6f);
Assert.AreEqual(2 * MathF.PI, unitCircle.Circumference, 1e-6f);
}
[TestMethod]
@ -32,6 +32,18 @@ public class CircleFTests
Assert.AreEqual(1, CircleF.Unit.CompareTo(CircleF.Empty));
}
[TestMethod]
public void CompareTo_ShouldBeNegativeOne_GivenEmptyCircleAndUnitCircleAsObject()
{
Assert.AreEqual(-1, CircleF.Empty.CompareTo((object)CircleF.Unit));
}
[TestMethod]
public void CompareTo_ShouldBeOne_GivenNull()
{
Assert.AreEqual(1, CircleF.Unit.CompareTo(null));
}
[TestMethod]
public void CompareTo_ShouldBeZero_GivenUnitCircle()
{
@ -39,6 +51,12 @@ public class CircleFTests
Assert.AreEqual(0, unitCircle.CompareTo(unitCircle));
}
[TestMethod]
public void CompareTo_ShouldThrowArgumentException_GivenInvalidType()
{
Assert.ThrowsException<ArgumentException>(() => CircleF.Unit.CompareTo(new object()));
}
[TestMethod]
public void Diameter_ShouldBe2_GivenUnitCircle()
{
@ -79,6 +97,46 @@ public class CircleFTests
Assert.AreEqual(hashCode, CircleF.Unit.GetHashCode());
}
[TestMethod]
public void op_Explicit_ShouldReturnEquivalentCircle_GivenCircle()
{
CircleF unitCircle = CircleF.Unit;
Circle converted = (Circle)unitCircle;
Assert.AreEqual(unitCircle, converted);
Assert.AreEqual(unitCircle.Radius, converted.Radius);
Assert.AreEqual(unitCircle.Center, converted.Center);
}
[TestMethod]
public void op_GreaterThan_True_GivenUnitAndEmptyCircle()
{
Assert.IsTrue(CircleF.Unit > CircleF.Empty);
Assert.IsTrue(CircleF.Unit >= CircleF.Empty);
Assert.IsFalse(CircleF.Unit < CircleF.Empty);
Assert.IsFalse(CircleF.Unit <= CircleF.Empty);
}
[TestMethod]
public void op_Implicit_ShouldReturnEquivalentCircle_GivenCircle()
{
Circle unitCircle = Circle.Unit;
CircleF converted = unitCircle;
Assert.AreEqual(unitCircle, converted);
Assert.AreEqual(unitCircle.Radius, converted.Radius);
Assert.AreEqual(unitCircle.Center, converted.Center);
}
[TestMethod]
public void op_LessThan_True_GivenEmptyAndUnitCircle()
{
Assert.IsTrue(CircleF.Empty < CircleF.Unit);
Assert.IsTrue(CircleF.Empty <= CircleF.Unit);
Assert.IsFalse(CircleF.Empty > CircleF.Unit);
Assert.IsFalse(CircleF.Empty >= CircleF.Unit);
}
[TestMethod]
public void Radius_ShouldBe0_GivenEmptyCircle()
{

View File

@ -32,6 +32,18 @@ public class CircleTests
Assert.AreEqual(1, Circle.Unit.CompareTo(Circle.Empty));
}
[TestMethod]
public void CompareTo_ShouldBeNegativeOne_GivenEmptyCircleAndUnitCircleAsObject()
{
Assert.AreEqual(-1, Circle.Empty.CompareTo((object)Circle.Unit));
}
[TestMethod]
public void CompareTo_ShouldBeOne_GivenNull()
{
Assert.AreEqual(1, Circle.Unit.CompareTo(null));
}
[TestMethod]
public void CompareTo_ShouldBeZero_GivenUnitCircle()
{
@ -39,6 +51,12 @@ public class CircleTests
Assert.AreEqual(0, unitCircle.CompareTo(unitCircle));
}
[TestMethod]
public void CompareTo_ShouldThrowArgumentException_GivenInvalidType()
{
Assert.ThrowsException<ArgumentException>(() => Circle.Unit.CompareTo(new object()));
}
[TestMethod]
public void Diameter_ShouldBe2_GivenUnitCircle()
{
@ -79,6 +97,24 @@ public class CircleTests
Assert.AreEqual(hashCode, Circle.Unit.GetHashCode());
}
[TestMethod]
public void op_GreaterThan_True_GivenUnitAndEmptyCircle()
{
Assert.IsTrue(Circle.Unit > Circle.Empty);
Assert.IsTrue(Circle.Unit >= Circle.Empty);
Assert.IsFalse(Circle.Unit < Circle.Empty);
Assert.IsFalse(Circle.Unit <= Circle.Empty);
}
[TestMethod]
public void op_LessThan_True_GivenEmptyAndUnitCircle()
{
Assert.IsTrue(Circle.Empty < Circle.Unit);
Assert.IsTrue(Circle.Empty <= Circle.Unit);
Assert.IsFalse(Circle.Empty > Circle.Unit);
Assert.IsFalse(Circle.Empty >= Circle.Unit);
}
[TestMethod]
public void Radius_ShouldBe0_GivenEmptyCircle()
{

View File

@ -12,6 +12,18 @@ public class LineFTests
Assert.AreEqual(-1, LineF.Empty.CompareTo(LineF.One));
}
[TestMethod]
public void CompareTo_ShouldBeNegativeOne_GivenEmptyLineAndOneLineAsObject()
{
Assert.AreEqual(-1, LineF.Empty.CompareTo((object)LineF.One));
}
[TestMethod]
public void CompareTo_ShouldBeOne_GivenNull()
{
Assert.AreEqual(1, LineF.One.CompareTo(null));
}
[TestMethod]
public void CompareTo_ShouldBeOne_GivenOneAndEmpty()
{
@ -25,6 +37,12 @@ public class LineFTests
Assert.AreEqual(0, unitLineF.CompareTo(unitLineF));
}
[TestMethod]
public void CompareTo_ShouldThrowArgumentException_GivenInvalidType()
{
Assert.ThrowsException<ArgumentException>(() => LineF.Empty.CompareTo(new object()));
}
[TestMethod]
public void Length_ShouldBe0_GivenEmptyLine()
{
@ -76,4 +94,46 @@ public class LineFTests
int hashCode = LineF.One.GetHashCode();
Assert.AreEqual(hashCode, LineF.One.GetHashCode());
}
[TestMethod]
public void op_Explicit_ShouldReturnEquivalentLine_GivenLine()
{
LineF oneLine = LineF.One;
Line converted = (Line)oneLine;
Assert.AreEqual(oneLine, converted);
Assert.AreEqual(oneLine.Length, converted.Length);
Assert.AreEqual(oneLine.Start, converted.Start);
Assert.AreEqual(oneLine.End, converted.End);
}
[TestMethod]
public void op_GreaterThan_True_GivenUnitAndEmptyCircle()
{
Assert.IsTrue(LineF.One > LineF.Empty);
Assert.IsTrue(LineF.One >= LineF.Empty);
Assert.IsFalse(LineF.One < LineF.Empty);
Assert.IsFalse(LineF.One <= LineF.Empty);
}
[TestMethod]
public void op_Implicit_ShouldReturnEquivalentLine_GivenLine()
{
Line oneLine = Line.One;
LineF converted = oneLine;
Assert.AreEqual(oneLine, converted);
Assert.AreEqual(oneLine.Length, converted.Length);
Assert.AreEqual(oneLine.Start, converted.Start);
Assert.AreEqual(oneLine.End, converted.End);
}
[TestMethod]
public void op_LessThan_True_GivenEmptyAndUnitCircle()
{
Assert.IsTrue(LineF.Empty < LineF.One);
Assert.IsTrue(LineF.Empty <= LineF.One);
Assert.IsFalse(LineF.Empty > LineF.One);
Assert.IsFalse(LineF.Empty >= LineF.One);
}
}

View File

@ -12,6 +12,18 @@ public class LineTests
Assert.AreEqual(-1, Line.Empty.CompareTo(Line.One));
}
[TestMethod]
public void CompareTo_ShouldBeNegativeOne_GivenEmptyLineAndOneLineAsObject()
{
Assert.AreEqual(-1, Line.Empty.CompareTo((object)Line.One));
}
[TestMethod]
public void CompareTo_ShouldBeOne_GivenNull()
{
Assert.AreEqual(1, Line.One.CompareTo(null));
}
[TestMethod]
public void CompareTo_ShouldBeOne_GivenOneAndEmpty()
{
@ -26,21 +38,9 @@ public class LineTests
}
[TestMethod]
public void Length_ShouldBe0_GivenEmptyLine()
public void CompareTo_ShouldThrowArgumentException_GivenInvalidType()
{
Assert.AreEqual(0.0f, Line.Empty.Length);
}
[TestMethod]
public void Length_ShouldBe1_GivenUnitXLine()
{
Assert.AreEqual(1.0f, Line.UnitX.Length, 1e-6f);
}
[TestMethod]
public void Length_ShouldBe1_GivenUnitYLine()
{
Assert.AreEqual(1.0f, Line.UnitY.Length, 1e-6f);
Assert.ThrowsException<ArgumentException>(() => Line.Empty.CompareTo(new object()));
}
[TestMethod]
@ -76,4 +76,40 @@ public class LineTests
int hashCode = Line.One.GetHashCode();
Assert.AreEqual(hashCode, Line.One.GetHashCode());
}
[TestMethod]
public void Length_ShouldBe0_GivenEmptyLine()
{
Assert.AreEqual(0.0f, Line.Empty.Length);
}
[TestMethod]
public void Length_ShouldBe1_GivenUnitXLine()
{
Assert.AreEqual(1.0f, Line.UnitX.Length, 1e-6f);
}
[TestMethod]
public void Length_ShouldBe1_GivenUnitYLine()
{
Assert.AreEqual(1.0f, Line.UnitY.Length, 1e-6f);
}
[TestMethod]
public void op_GreaterThan_True_GivenUnitAndEmptyCircle()
{
Assert.IsTrue(Line.One > Line.Empty);
Assert.IsTrue(Line.One >= Line.Empty);
Assert.IsFalse(Line.One < Line.Empty);
Assert.IsFalse(Line.One <= Line.Empty);
}
[TestMethod]
public void op_LessThan_True_GivenEmptyAndUnitCircle()
{
Assert.IsTrue(Line.Empty < Line.One);
Assert.IsTrue(Line.Empty <= Line.One);
Assert.IsFalse(Line.Empty > Line.One);
Assert.IsFalse(Line.Empty >= Line.One);
}
}

View File

@ -8,6 +8,107 @@ namespace X10D.Tests.Drawing;
[TestClass]
public class PolygonFTests
{
[TestMethod]
public void AddPoints_ShouldAddPoints()
{
var polygon = PolygonF.Empty;
polygon.AddPoints(new[] {new PointF(1, 2), new PointF(3, 4)});
Assert.AreEqual(2, polygon.PointCount);
// assert that the empty polygon was not modified
Assert.AreEqual(0, PolygonF.Empty.PointCount);
}
[TestMethod]
public void ClearPoints_ShouldClearPoints()
{
var polygon = PolygonF.Empty;
polygon.AddPoints(new[] {new Vector2(1, 2), new Vector2(3, 4)});
Assert.AreEqual(2, polygon.PointCount);
// assert that the empty polygon was not modified
Assert.AreEqual(0, PolygonF.Empty.PointCount);
polygon.ClearPoints();
Assert.AreEqual(0, polygon.PointCount);
}
[TestMethod]
public void Constructor_ShouldPopulatePoints_GivenPolygon()
{
var pointPolygon = new PolygonF(new[] {new PointF(1, 2), new PointF(3, 4)});
var vectorPolygon = new PolygonF(new[] {new Vector2(1, 2), new Vector2(3, 4)});
Assert.AreEqual(2, pointPolygon.PointCount);
Assert.AreEqual(2, vectorPolygon.PointCount);
}
[TestMethod]
public void CopyConstructor_ShouldCopyPoints_GivenPolygon()
{
var first = PolygonF.Empty;
first.AddPoints(new[] {new PointF(1, 2), new PointF(3, 4)});
var second = new PolygonF(first);
Assert.AreEqual(2, first.PointCount);
Assert.AreEqual(2, second.PointCount);
// we cannot use CollectionAssert here for reasons I am not entirely sure of.
// it seems to dislike casting from IReadOnlyList<Point> to ICollection. but okay.
Assert.IsTrue(first.Points.SequenceEqual(second.Points));
// assert that the empty polygon was not modified
Assert.AreEqual(0, PolygonF.Empty.PointCount);
}
[TestMethod]
public void Equals_ShouldBeTrue_GivenTwoEmptyPolygons()
{
var first = PolygonF.Empty;
var second = PolygonF.Empty;
Assert.AreEqual(first, second);
Assert.AreEqual(second, first);
Assert.IsTrue(first.Equals(second));
Assert.IsTrue(second.Equals(first));
Assert.IsTrue(first == second);
Assert.IsTrue(second == first);
Assert.IsFalse(first != second);
Assert.IsFalse(second != first);
}
[TestMethod]
public void Equals_ShouldBeTrue_GivenTwoHexagons()
{
PolygonF first = CreateHexagon();
PolygonF second = CreateHexagon();
Assert.AreEqual(first, second);
Assert.AreEqual(second, first);
Assert.IsTrue(first.Equals(second));
Assert.IsTrue(second.Equals(first));
Assert.IsTrue(first == second);
Assert.IsTrue(second == first);
Assert.IsFalse(first != second);
Assert.IsFalse(second != first);
}
[TestMethod]
public void Equals_ShouldBeFalse_GivenHexagonAndEmptyPolygon()
{
PolygonF first = CreateHexagon();
PolygonF second = PolygonF.Empty;
Assert.AreNotEqual(first, second);
Assert.AreNotEqual(second, first);
Assert.IsFalse(first.Equals(second));
Assert.IsFalse(second.Equals(first));
Assert.IsFalse(first == second);
Assert.IsFalse(second == first);
Assert.IsTrue(first != second);
Assert.IsTrue(second != first);
}
[TestMethod]
public void IsConvex_ShouldBeFalse_GivenEmptyPolygon()
{
@ -20,6 +121,38 @@ public class PolygonFTests
Assert.IsTrue(CreateHexagon().IsConvex);
}
[TestMethod]
public void IsConvex_ShouldBeFalse_GivenConcavePolygon()
{
Assert.IsFalse(CreateConcavePolygon().IsConvex);
}
[TestMethod]
public void op_Explicit_ShouldReturnEquivalentCircle_GivenCircle()
{
PolygonF polygon = CreateHexagon();
Polygon converted = (Polygon)polygon;
Assert.AreEqual(polygon, converted);
Assert.AreEqual(polygon.IsConvex, converted.IsConvex);
Assert.AreEqual(polygon.PointCount, converted.PointCount);
Assert.IsTrue(polygon.Points.SequenceEqual(converted.Points.Select(p => (PointF)p)));
}
[TestMethod]
public void op_Implicit_ShouldReturnEquivalentCircle_GivenCircle()
{
Polygon polygon = PolygonTests.CreateHexagon();
PolygonF converted = polygon;
Assert.AreEqual(polygon, converted);
Assert.AreEqual(polygon.IsConvex, converted.IsConvex);
Assert.AreEqual(polygon.PointCount, converted.PointCount);
Assert.IsTrue(converted.Points.SequenceEqual(polygon.Points.Select(p => (PointF)p)));
}
[TestMethod]
public void PointCount_ShouldBe1_GivenPolygonFWith1Point()
{
@ -38,16 +171,6 @@ public class PolygonFTests
Assert.AreEqual(0, PolygonF.Empty.PointCount);
}
[TestMethod]
public void Equals_ShouldBeTrue_GivenTwoUnitCircles()
{
var emptyPolygonF1 = PolygonF.Empty;
var emptyPolygonF2 = PolygonF.Empty;
Assert.AreEqual(emptyPolygonF1, emptyPolygonF2);
Assert.IsTrue(emptyPolygonF1 == emptyPolygonF2);
Assert.IsFalse(emptyPolygonF1 != emptyPolygonF2);
}
[TestMethod]
public void GetHashCode_ShouldBeCorrect_GivenEmptyCircle()
{
@ -56,7 +179,7 @@ public class PolygonFTests
Assert.AreEqual(hashCode, PolygonF.Empty.GetHashCode());
}
private static PolygonF CreateHexagon()
internal static PolygonF CreateHexagon()
{
var hexagon = new PolygonF();
hexagon.AddPoint(new Vector2(0, 0));
@ -67,4 +190,15 @@ public class PolygonFTests
hexagon.AddPoint(new Vector2(-1, 0));
return hexagon;
}
internal static PolygonF CreateConcavePolygon()
{
var hexagon = new PolygonF();
hexagon.AddPoint(new Vector2(0, 0));
hexagon.AddPoint(new Vector2(2, 0));
hexagon.AddPoint(new Vector2(1, 1));
hexagon.AddPoint(new Vector2(2, 1));
hexagon.AddPoint(new Vector2(0, 1));
return hexagon;
}
}

View File

@ -7,6 +7,98 @@ namespace X10D.Tests.Drawing;
[TestClass]
public class PolygonTests
{
[TestMethod]
public void AddPoints_ShouldAddPoints()
{
var polygon = Polygon.Empty;
polygon.AddPoints(new[] {new Point(1, 2), new Point(3, 4)});
Assert.AreEqual(2, polygon.PointCount);
// assert that the empty polygon was not modified
Assert.AreEqual(0, Polygon.Empty.PointCount);
}
[TestMethod]
public void ClearPoints_ShouldClearPoints()
{
var polygon = Polygon.Empty;
polygon.AddPoints(new[] {new Point(1, 2), new Point(3, 4)});
Assert.AreEqual(2, polygon.PointCount);
// assert that the empty polygon was not modified
Assert.AreEqual(0, PolygonF.Empty.PointCount);
polygon.ClearPoints();
Assert.AreEqual(0, polygon.PointCount);
}
[TestMethod]
public void CopyConstructor_ShouldCopyPoints_GivenPolygon()
{
var first = Polygon.Empty;
first.AddPoints(new[] {new Point(1, 2), new Point(3, 4)});
var second = new Polygon(first);
Assert.AreEqual(2, first.PointCount);
Assert.AreEqual(2, second.PointCount);
// we cannot use CollectionAssert here for reasons I am not entirely sure of.
// it seems to dislike casting from IReadOnlyList<Point> to ICollection. but okay.
Assert.IsTrue(first.Points.SequenceEqual(second.Points));
// assert that the empty polygon was not modified
Assert.AreEqual(0, Polygon.Empty.PointCount);
}
[TestMethod]
public void Equals_ShouldBeTrue_GivenTwoEmptyPolygons()
{
var first = Polygon.Empty;
var second = Polygon.Empty;
Assert.AreEqual(first, second);
Assert.AreEqual(second, first);
Assert.IsTrue(first.Equals(second));
Assert.IsTrue(second.Equals(first));
Assert.IsTrue(first == second);
Assert.IsTrue(second == first);
Assert.IsFalse(first != second);
Assert.IsFalse(second != first);
}
[TestMethod]
public void Equals_ShouldBeTrue_GivenTwoHexagons()
{
Polygon first = CreateHexagon();
Polygon second = CreateHexagon();
Assert.AreEqual(first, second);
Assert.AreEqual(second, first);
Assert.IsTrue(first.Equals(second));
Assert.IsTrue(second.Equals(first));
Assert.IsTrue(first == second);
Assert.IsTrue(second == first);
Assert.IsFalse(first != second);
Assert.IsFalse(second != first);
}
[TestMethod]
public void Equals_ShouldBeFalse_GivenHexagonAndEmptyPolygon()
{
Polygon first = CreateHexagon();
Polygon second = Polygon.Empty;
Assert.AreNotEqual(first, second);
Assert.AreNotEqual(second, first);
Assert.IsFalse(first.Equals(second));
Assert.IsFalse(second.Equals(first));
Assert.IsFalse(first == second);
Assert.IsFalse(second == first);
Assert.IsTrue(first != second);
Assert.IsTrue(second != first);
}
[TestMethod]
public void IsConvex_ShouldBeFalse_GivenEmptyPolygon()
{
@ -19,10 +111,16 @@ public class PolygonTests
Assert.IsTrue(CreateHexagon().IsConvex);
}
[TestMethod]
public void IsConvex_ShouldBeFalse_GivenConcavePolygon()
{
Assert.IsFalse(CreateConcavePolygon().IsConvex);
}
[TestMethod]
public void PointCount_ShouldBe1_GivenPolygonWith1Point()
{
var polygon = new Polygon();
var polygon = Polygon.Empty;
polygon.AddPoint(new Point(1, 1));
Assert.AreEqual(1, polygon.PointCount);
@ -37,16 +135,6 @@ public class PolygonTests
Assert.AreEqual(0, Polygon.Empty.PointCount);
}
[TestMethod]
public void Equals_ShouldBeTrue_GivenTwoUnitCircles()
{
var emptyPolygon1 = Polygon.Empty;
var emptyPolygon2 = Polygon.Empty;
Assert.AreEqual(emptyPolygon1, emptyPolygon2);
Assert.IsTrue(emptyPolygon1 == emptyPolygon2);
Assert.IsFalse(emptyPolygon1 != emptyPolygon2);
}
[TestMethod]
public void GetHashCode_ShouldBeCorrect_GivenEmptyCircle()
{
@ -55,7 +143,7 @@ public class PolygonTests
Assert.AreEqual(hashCode, Polygon.Empty.GetHashCode());
}
private static Polygon CreateHexagon()
internal static Polygon CreateHexagon()
{
var hexagon = new Polygon();
hexagon.AddPoint(new Point(0, 0));
@ -66,4 +154,15 @@ public class PolygonTests
hexagon.AddPoint(new Point(-1, 0));
return hexagon;
}
internal static Polygon CreateConcavePolygon()
{
var hexagon = new Polygon();
hexagon.AddPoint(new Point(0, 0));
hexagon.AddPoint(new Point(2, 0));
hexagon.AddPoint(new Point(1, 1));
hexagon.AddPoint(new Point(2, 1));
hexagon.AddPoint(new Point(0, 1));
return hexagon;
}
}