From 3b78235957d34858f8f4dd8c68935af0fc8cd42b Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Wed, 1 Jun 2022 19:43:52 +0100 Subject: [PATCH] Add tests for 3D shapes --- X10D.Tests/src/Drawing/CuboidTests.cs | 76 +++++++++ X10D.Tests/src/Drawing/PolyhedronTests.cs | 188 ++++++++++++++++++++++ X10D.Tests/src/Drawing/SphereTests.cs | 129 +++++++++++++++ 3 files changed, 393 insertions(+) create mode 100644 X10D.Tests/src/Drawing/CuboidTests.cs create mode 100644 X10D.Tests/src/Drawing/PolyhedronTests.cs create mode 100644 X10D.Tests/src/Drawing/SphereTests.cs diff --git a/X10D.Tests/src/Drawing/CuboidTests.cs b/X10D.Tests/src/Drawing/CuboidTests.cs new file mode 100644 index 0000000..d879a5b --- /dev/null +++ b/X10D.Tests/src/Drawing/CuboidTests.cs @@ -0,0 +1,76 @@ +using System.Numerics; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using X10D.Drawing; + +namespace X10D.Tests.Drawing; + +[TestClass] +public class CuboidTests +{ + [TestMethod] + public void Corners_ShouldBeCorrect_GivenCubeOfSize1() + { + Cuboid cube = Cuboid.Cube; + + Assert.AreEqual(new Vector3(0.5f, 0.5f, -0.5f), cube.FrontTopRight); + Assert.AreEqual(new Vector3(-0.5f, 0.5f, -0.5f), cube.FrontTopLeft); + Assert.AreEqual(new Vector3(0.5f, -0.5f, -0.5f), cube.FrontBottomRight); + Assert.AreEqual(new Vector3(-0.5f, -0.5f, -0.5f), cube.FrontBottomLeft); + Assert.AreEqual(new Vector3(0.5f, 0.5f, 0.5f), cube.BackTopRight); + Assert.AreEqual(new Vector3(-0.5f, 0.5f, 0.5f), cube.BackTopLeft); + Assert.AreEqual(new Vector3(0.5f, -0.5f, 0.5f), cube.BackBottomRight); + Assert.AreEqual(new Vector3(-0.5f, -0.5f, 0.5f), cube.BackBottomLeft); + } + + [TestMethod] + public void Equals_ShouldBeTrue_GivenTwoCubesOfSize1() + { + var cube1 = Cuboid.Cube; + var cube2 = Cuboid.Cube; + Assert.AreEqual(cube1, cube2); + Assert.IsTrue(cube1 == cube2); + Assert.IsFalse(cube1 != cube2); + } + + [TestMethod] + public void Equals_ShouldBeFalse_GivenDifferentCubes() + { + Assert.AreNotEqual(Cuboid.Cube, Cuboid.Empty); + Assert.IsFalse(Cuboid.Cube == Cuboid.Empty); + Assert.IsTrue(Cuboid.Cube != Cuboid.Empty); + } + + [TestMethod] + public void GetHashCode_ShouldBeCorrect_GivenEmptyCircle() + { + // this test is pretty pointless, it exists only for code coverage purposes + int hashCode = Cuboid.Empty.GetHashCode(); + Assert.AreEqual(hashCode, Cuboid.Empty.GetHashCode()); + } + + [TestMethod] + public void GetHashCode_ShouldBeCorrect_GivenCubeOfSize1() + { + // this test is pretty pointless, it exists only for code coverage purposes + int hashCode = Cuboid.Cube.GetHashCode(); + Assert.AreEqual(hashCode, Cuboid.Cube.GetHashCode()); + } + + [TestMethod] + public void Size_ShouldBeOne_GivenCubeOfSize1() + { + Assert.AreEqual(Vector3.One, Cuboid.Cube.Size); + } + + [TestMethod] + public void Size_ShouldBeOne_GivenRotatedCube() + { + Assert.AreEqual(Vector3.One, new Cuboid(0, 0, 0, 1, 1, 1, 90, 0, 0).Size); + } + + [TestMethod] + public void Volume_ShouldBe1_GivenCubeOfSize1() + { + Assert.AreEqual(1.0f, Cuboid.Cube.Volume, 1e-6f); + } +} diff --git a/X10D.Tests/src/Drawing/PolyhedronTests.cs b/X10D.Tests/src/Drawing/PolyhedronTests.cs new file mode 100644 index 0000000..9d72ec0 --- /dev/null +++ b/X10D.Tests/src/Drawing/PolyhedronTests.cs @@ -0,0 +1,188 @@ +using System.Numerics; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using X10D.Drawing; + +namespace X10D.Tests.Drawing; + +[TestClass] +public class PolyhedronTests +{ + [TestMethod] + public void AddVertices_ShouldAddVertices() + { + var polyhedron = Polyhedron.Empty; + polyhedron.AddVertices(new[] {new Vector3(1, 2, 3), new Vector3(4, 5, 6)}); + Assert.AreEqual(2, polyhedron.VertexCount); + + // assert that the empty polyhedron was not modified + Assert.AreEqual(0, Polyhedron.Empty.VertexCount); + } + + [TestMethod] + public void ClearVertices_ShouldClearVertices() + { + var polyhedron = Polyhedron.Empty; + polyhedron.AddVertices(new[] {new Vector3(1, 2, 3), new Vector3(4, 5, 6)}); + Assert.AreEqual(2, polyhedron.VertexCount); + + // assert that the empty polyhedron was not modified + Assert.AreEqual(0, Polyhedron.Empty.VertexCount); + + polyhedron.ClearVertices(); + Assert.AreEqual(0, polyhedron.VertexCount); + } + + [TestMethod] + public void Constructor_ShouldPopulateVertices_GivenPolyhedron() + { + var polyhedron = new Polyhedron(new[] {new Vector3(1, 2, 3), new Vector3(4, 5, 6)}); + Assert.AreEqual(2, polyhedron.VertexCount); + } + + [TestMethod] + public void CopyConstructor_ShouldCopyVertices_GivenPolyhedron() + { + var first = Polyhedron.Empty; + first.AddVertices(new[] {new Vector3(1, 2, 3), new Vector3(4, 5, 6)}); + + var second = new Polyhedron(first); + Assert.AreEqual(2, first.VertexCount); + Assert.AreEqual(2, second.VertexCount); + + // we cannot use CollectionAssert here for reasons I am not entirely sure of. + // it seems to dislike casting from IReadOnlyList to ICollection. but okay. + Assert.IsTrue(first.Vertices.SequenceEqual(second.Vertices)); + + // assert that the empty polyhedron was not modified + Assert.AreEqual(0, Polyhedron.Empty.VertexCount); + } + + [TestMethod] + public void Equals_ShouldBeTrue_GivenTwoEmptyPolyhedrons() + { + var first = Polyhedron.Empty; + var second = Polyhedron.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() + { + Polyhedron first = CreateHexagon(); + Polyhedron 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_GivenHexagonAndEmptyPolyhedron() + { + Polyhedron first = CreateHexagon(); + Polyhedron second = Polyhedron.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 op_Implicit_ShouldReturnEquivalentPolyhedron_GivenPolyhedron() + { + Polygon polygon = PolygonTests.CreateHexagon(); + Polyhedron converted = polygon; + + Assert.AreEqual(polygon, converted); + Assert.AreEqual(polygon.VertexCount, converted.VertexCount); + + Assert.IsTrue(converted.Vertices.SequenceEqual(polygon.Vertices.Select(p => + { + var point = p.ToVector2(); + return new Vector3(point.X, point.Y, 0); + }))); + } + + [TestMethod] + public void op_Implicit_ShouldReturnEquivalentPolyhedron_GivenPolyhedronF() + { + PolygonF polygon = PolygonFTests.CreateHexagon(); + Polyhedron converted = polygon; + + Assert.AreEqual(polygon, converted); + Assert.AreEqual(polygon.VertexCount, converted.VertexCount); + + Assert.IsTrue(converted.Vertices.SequenceEqual(polygon.Vertices.Select(v => + { + var point = v.ToVector2(); + return new Vector3(point.X, point.Y, 0); + }))); + } + + [TestMethod] + public void PointCount_ShouldBe1_GivenPolyhedronWith1Point() + { + var polyhedron = new Polyhedron(); + polyhedron.AddVertex(Vector3.One); + + Assert.AreEqual(1, polyhedron.VertexCount); + + // assert that the empty polyhedron was not modified + Assert.AreEqual(0, Polyhedron.Empty.VertexCount); + } + + [TestMethod] + public void PointCount_ShouldBe0_GivenEmptyPolyhedron() + { + Assert.AreEqual(0, Polyhedron.Empty.VertexCount); + } + + [TestMethod] + public void GetHashCode_ShouldBeCorrect_GivenEmptyCircle() + { + // this test is pretty pointless, it exists only for code coverage purposes + int hashCode = Polyhedron.Empty.GetHashCode(); + Assert.AreEqual(hashCode, Polyhedron.Empty.GetHashCode()); + } + + internal static Polyhedron CreateHexagon() + { + var hexagon = new Polyhedron(); + hexagon.AddVertex(new Vector3(0, 0, 0)); + hexagon.AddVertex(new Vector3(1, 0, 0)); + hexagon.AddVertex(new Vector3(1, 1, 0)); + hexagon.AddVertex(new Vector3(0, 1, 0)); + hexagon.AddVertex(new Vector3(-1, 1, 0)); + hexagon.AddVertex(new Vector3(-1, 0, 0)); + return hexagon; + } + + internal static Polyhedron CreateConcavePolyhedron() + { + var hexagon = new Polyhedron(); + hexagon.AddVertex(new Vector3(0, 0, 0)); + hexagon.AddVertex(new Vector3(2, 0, 0)); + hexagon.AddVertex(new Vector3(2, 1, 0)); + hexagon.AddVertex(new Vector3(2, 1, 0)); + hexagon.AddVertex(new Vector3(0, 1, 0)); + return hexagon; + } +} diff --git a/X10D.Tests/src/Drawing/SphereTests.cs b/X10D.Tests/src/Drawing/SphereTests.cs new file mode 100644 index 0000000..32b321f --- /dev/null +++ b/X10D.Tests/src/Drawing/SphereTests.cs @@ -0,0 +1,129 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using X10D.Drawing; + +namespace X10D.Tests.Drawing; + +[TestClass] +public class SphereTests +{ + [TestMethod] + public void Circumference_ShouldBe2PiRadius_GivenUnitCircle() + { + var unitSphere = Sphere.Unit; + Assert.AreEqual(2.0f * MathF.PI * unitSphere.Radius, unitSphere.Circumference, 1e-6f); + } + + [TestMethod] + public void CompareTo_ShouldBeNegativeOne_GivenUnitCircleAndEmpty() + { + Assert.AreEqual(-1, Sphere.Empty.CompareTo(Sphere.Unit)); + } + + [TestMethod] + public void CompareTo_ShouldBeOne_GivenUnitCircleAndEmpty() + { + Assert.AreEqual(1, Sphere.Unit.CompareTo(Sphere.Empty)); + } + + [TestMethod] + public void CompareTo_ShouldBeNegativeOne_GivenEmptyCircleAndUnitCircleAsObject() + { + Assert.AreEqual(-1, Sphere.Empty.CompareTo((object)Sphere.Unit)); + } + + [TestMethod] + public void CompareTo_ShouldBeOne_GivenNull() + { + Assert.AreEqual(1, Sphere.Unit.CompareTo(null)); + } + + [TestMethod] + public void CompareTo_ShouldBeZero_GivenUnitCircle() + { + var unitCircle = Sphere.Unit; + Assert.AreEqual(0, unitCircle.CompareTo(unitCircle)); + } + + [TestMethod] + public void CompareTo_ShouldThrowArgumentException_GivenInvalidType() + { + Assert.ThrowsException(() => Sphere.Unit.CompareTo(new object())); + } + + [TestMethod] + public void Diameter_ShouldBe2_GivenUnitSphere() + { + Assert.AreEqual(2.0f, Sphere.Unit.Diameter, 1e-6f); + } + + [TestMethod] + public void Equals_ShouldBeTrue_GivenTwoUnitCircles() + { + var unitCircle1 = Sphere.Unit; + var unitCircle2 = Sphere.Unit; + Assert.AreEqual(unitCircle1, unitCircle2); + Assert.IsTrue(unitCircle1 == unitCircle2); + Assert.IsFalse(unitCircle1 != unitCircle2); + } + + [TestMethod] + public void Equals_ShouldBeFalse_GivenDifferentCircles() + { + Assert.AreNotEqual(Sphere.Unit, Sphere.Empty); + Assert.IsFalse(Sphere.Unit == Sphere.Empty); + Assert.IsTrue(Sphere.Unit != Sphere.Empty); + } + + [TestMethod] + public void GetHashCode_ShouldBeCorrect_GivenEmptyCircle() + { + // this test is pretty pointless, it exists only for code coverage purposes + int hashCode = Sphere.Empty.GetHashCode(); + Assert.AreEqual(hashCode, Sphere.Empty.GetHashCode()); + } + + [TestMethod] + public void GetHashCode_ShouldBeCorrect_GivenUnitCircle() + { + // this test is pretty pointless, it exists only for code coverage purposes + int hashCode = Sphere.Unit.GetHashCode(); + Assert.AreEqual(hashCode, Sphere.Unit.GetHashCode()); + } + + [TestMethod] + public void op_GreaterThan_True_GivenUnitAndEmptyCircle() + { + Assert.IsTrue(Sphere.Unit > Sphere.Empty); + Assert.IsTrue(Sphere.Unit >= Sphere.Empty); + Assert.IsFalse(Sphere.Unit < Sphere.Empty); + Assert.IsFalse(Sphere.Unit <= Sphere.Empty); + } + + [TestMethod] + public void op_LessThan_True_GivenEmptyAndUnitCircle() + { + Assert.IsTrue(Sphere.Empty < Sphere.Unit); + Assert.IsTrue(Sphere.Empty <= Sphere.Unit); + Assert.IsFalse(Sphere.Empty > Sphere.Unit); + Assert.IsFalse(Sphere.Empty >= Sphere.Unit); + } + + [TestMethod] + public void Radius_ShouldBe0_GivenEmptySphere() + { + Assert.AreEqual(0, Sphere.Empty.Radius); + } + + [TestMethod] + public void Radius_ShouldBe1_GivenUnitSphere() + { + Assert.AreEqual(1, Sphere.Unit.Radius); + } + + [TestMethod] + public void Volume_ShouldBe4Over3TimesPi_GivenUnitCircle() + { + var unitSphere = Sphere.Unit; + Assert.AreEqual(4.0f / 3.0f * MathF.PI, unitSphere.Volume); + } +}