From 7d59afbdd99fe5f01c2c2e87f11fc88c883739cf Mon Sep 17 00:00:00 2001 From: Mike Smith Date: Fri, 12 Feb 2016 18:16:00 -0800 Subject: [PATCH] + Added some tests for Cube + Adjusted some of the Sphere tests + Overrides for .Equals and .GetHashCode on Cube and Sphere --- OSCADSharp/OSCADSharp.UnitTests/CubeTests.cs | 57 +++++++++++++++++++ .../OSCADSharp.UnitTests.csproj | 5 +- .../OSCADSharp.UnitTests/SphereTests.cs | 8 +-- .../OSCADSharp/Scripting/BlockFormatter.cs | 3 + OSCADSharp/OSCADSharp/Solids/Cube.cs | 34 +++++++++++ OSCADSharp/OSCADSharp/Solids/Sphere.cs | 17 ++++-- 6 files changed, 113 insertions(+), 11 deletions(-) create mode 100644 OSCADSharp/OSCADSharp.UnitTests/CubeTests.cs diff --git a/OSCADSharp/OSCADSharp.UnitTests/CubeTests.cs b/OSCADSharp/OSCADSharp.UnitTests/CubeTests.cs new file mode 100644 index 0000000..1a7e636 --- /dev/null +++ b/OSCADSharp/OSCADSharp.UnitTests/CubeTests.cs @@ -0,0 +1,57 @@ +using System; +using System.Text; +using System.Collections.Generic; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using OSCADSharp.Solids; + +namespace OSCADSharp.UnitTests +{ + [TestClass] + public class CubeTests + { + [TestMethod] + public void Cube_LengthWidthHeightAffectSizeVector() + { + const double length = 10; + const double width = 7; + const double height = 9; + + var cube = new Cube(length, width, height); + + Assert.AreEqual(length, cube.Size.X); + Assert.AreEqual(width, cube.Size.Y); + Assert.AreEqual(height, cube.Size.Z); + } + + [TestMethod] + public void Cube_SizeAppearsInOutput() + { + var cube = new Cube(new Vector3(1.5, 5.5, 8.7)); + + string script = cube.ToString(); + + Assert.IsTrue(script.Contains(String.Format("size = [{0}, {1}, {2}]", 1.5, 5.5, 8.7))); + } + + [TestMethod] + public void Cube_CloneYieldsEqualObject() + { + var cube = new Cube(new Vector3(1.5, 5.5, 8.7)); + + var clone = cube.Clone(); + + Assert.IsTrue(clone.Equals(cube)); + } + + [TestMethod] + public void Cube_ParameterlessCubeHasMethodCallInIt() + { + var cube = new Cube(); + + string script = cube.ToString(); + + Assert.IsTrue(script.StartsWith("cube(")); + Assert.IsTrue(script.EndsWith(");")); + } + } +} diff --git a/OSCADSharp/OSCADSharp.UnitTests/OSCADSharp.UnitTests.csproj b/OSCADSharp/OSCADSharp.UnitTests/OSCADSharp.UnitTests.csproj index 66b6edd..3dfa9e2 100644 --- a/OSCADSharp/OSCADSharp.UnitTests/OSCADSharp.UnitTests.csproj +++ b/OSCADSharp/OSCADSharp.UnitTests/OSCADSharp.UnitTests.csproj @@ -45,11 +45,14 @@ - + + False + + diff --git a/OSCADSharp/OSCADSharp.UnitTests/SphereTests.cs b/OSCADSharp/OSCADSharp.UnitTests/SphereTests.cs index c7d296a..9d1c4f2 100644 --- a/OSCADSharp/OSCADSharp.UnitTests/SphereTests.cs +++ b/OSCADSharp/OSCADSharp.UnitTests/SphereTests.cs @@ -22,7 +22,7 @@ namespace OSCADSharp.UnitTests } [TestMethod] - public void Sphere_ResolutionAffectsFNvalue_scriptWithFNof30RR() + public void Sphere_ResolutionAffectsFNvalue() { this.sphere.Resolution = 30; @@ -32,7 +32,7 @@ namespace OSCADSharp.UnitTests } [TestMethod] - public void Sphere_FAandFSAreAffectedByAngleANdFragmentSize_scriptWithMatchingAngleAndFragmentSize() + public void Sphere_FAandFSAreAffectedByAngleANdFragmentSize() { this.sphere.MinimumAngle = 2; this.sphere.MinimumFragmentSize = 4; @@ -44,7 +44,7 @@ namespace OSCADSharp.UnitTests } [TestMethod] - public void Sphere_ParameterlessSphereHasMethodCallAndEndsWithSemicolon_scriptWithSphereMethodCall() + public void Sphere_ParameterlessSphereHasMethodCallAndEndsWithSemicolon() { var basicSphere = new Sphere(); @@ -55,7 +55,7 @@ namespace OSCADSharp.UnitTests } [TestMethod] - public void Sphere_CloneCoversEveryAttribute_copyWithAllAttributes() + public void Sphere_CloneYieldsEqualObject() { var sphere = new Sphere() { diff --git a/OSCADSharp/OSCADSharp/Scripting/BlockFormatter.cs b/OSCADSharp/OSCADSharp/Scripting/BlockFormatter.cs index 1f62e37..7ef2c90 100644 --- a/OSCADSharp/OSCADSharp/Scripting/BlockFormatter.cs +++ b/OSCADSharp/OSCADSharp/Scripting/BlockFormatter.cs @@ -23,6 +23,9 @@ namespace OSCADSharp.Scripting this.innerCode = innerCode; } + //TODO: Assess perf and find a better performing way to apply indentation + // this approach will likely be a big performance drain with deeply nested structures. + // -MLS 2/12/2016 public override string ToString() { StringBuilder sb = new StringBuilder(); diff --git a/OSCADSharp/OSCADSharp/Solids/Cube.cs b/OSCADSharp/OSCADSharp/Solids/Cube.cs index f55ea02..1902770 100644 --- a/OSCADSharp/OSCADSharp/Solids/Cube.cs +++ b/OSCADSharp/OSCADSharp/Solids/Cube.cs @@ -43,6 +43,22 @@ namespace OSCADSharp.Solids this.Size = size ?? new Vector3(1, 1, 1); this.Center = center; } + + /// + /// Creates a new Cube object with Length/Width/Height + /// + /// Size on the X axis + /// Size on the Y axis + /// Size on the Z axis + /// Indicates whether the cube should be centered on the origin + public Cube(double length, double width, double height, bool center = false) + { + this.Size.X = length; + this.Size.Y = width; + this.Size.Z = height; + + this.Center = center; + } #endregion #region Overrides @@ -60,6 +76,24 @@ namespace OSCADSharp.Solids Center = this.Center }; } + + public override bool Equals(object other) + { + if (other.GetType() == typeof(Cube)) + { + Cube otherSphere = other as Cube; + return this.GetHashCode() == other.GetHashCode(); + } + else + { + return false; + } + } + + public override int GetHashCode() + { + return this.ToString().GetHashCode(); + } #endregion } } diff --git a/OSCADSharp/OSCADSharp/Solids/Sphere.cs b/OSCADSharp/OSCADSharp/Solids/Sphere.cs index 42dee6a..4d8a00f 100644 --- a/OSCADSharp/OSCADSharp/Solids/Sphere.cs +++ b/OSCADSharp/OSCADSharp/Solids/Sphere.cs @@ -83,20 +83,25 @@ namespace OSCADSharp.Solids public override bool Equals(object other) { + // This logic is because we want to prevent the weird scenario + // where perhaps this sphere is being compared to a string that's an + // exact match to this sphere's string output. Since the hashcode is + // borrowed from the string output if(other.GetType() == typeof(Sphere)) { Sphere otherSphere = other as Sphere; - return this.Diameter == otherSphere.Diameter && - this.Radius == otherSphere.Radius && - this.MinimumAngle == otherSphere.MinimumAngle && - this.MinimumFragmentSize == otherSphere.MinimumFragmentSize && - this.Resolution == otherSphere.Resolution; + return this.GetHashCode() == other.GetHashCode(); } else { - return base.Equals(other); + return false; } } + + public override int GetHashCode() + { + return this.ToString().GetHashCode(); + } #endregion } }