From 96ada57355ddcfd93ca4d424ce73d81606254a69 Mon Sep 17 00:00:00 2001 From: Nishant Chandrashekhar <53821078+NishantChandrashekhar@users.noreply.github.com> Date: Thu, 8 Jun 2023 10:35:03 +0530 Subject: [PATCH] equals handles null --- src/GShark.Test.XUnit/Geometry/Point3Tests.cs | 32 +++++++++++++++++++ src/GShark/Geometry/Point3.cs | 14 ++++++++ 2 files changed, 46 insertions(+) diff --git a/src/GShark.Test.XUnit/Geometry/Point3Tests.cs b/src/GShark.Test.XUnit/Geometry/Point3Tests.cs index f863d37e..0190c21f 100644 --- a/src/GShark.Test.XUnit/Geometry/Point3Tests.cs +++ b/src/GShark.Test.XUnit/Geometry/Point3Tests.cs @@ -153,6 +153,38 @@ public void It_Returns_True_If_Two_Points_Are_Equal() (p1 == p2).Should().BeTrue(); } + [Fact] + public void It_Returns_True_If_Two_Points_Are_Equal_Null_Case1() + { + // Arrange + Point3 p1 = new Point3(5.982099, 5.950299, 0); + Point3 p2 = null; + + // Assert + (p1 == p2).Should().BeFalse(); + } + + [Fact] + public void It_Returns_True_If_Two_Points_Are_Equal_Null_Case2() + { + // Arrange + Point3 p1 = null; + Point3 p2 = new Point3(5.982099, 5.950299, 0); + + // Assert + (p1 == p2).Should().BeFalse(); + } + [Fact] + public void It_Returns_True_If_Two_Points_Are_Equal_Null_Case3() + { + // Arrange + Point3 p1 = null; + Point3 p2 = null; + + // Assert + (p1 == p2).Should().BeFalse(); + } + [Fact] public void It_Returns_Whether_A_Point_Is_Inside_Outside_Or_Coincident_With_A_Polygon() { diff --git a/src/GShark/Geometry/Point3.cs b/src/GShark/Geometry/Point3.cs index 0d482468..b7159a78 100644 --- a/src/GShark/Geometry/Point3.cs +++ b/src/GShark/Geometry/Point3.cs @@ -198,6 +198,12 @@ public static Point3 Add(Vector3 vector, Point3 point) /// true if the coordinates of the two points are exactly equal; otherwise false. public static bool operator ==(Point3 a, Point3 b) { + if (a == null && b == null) + return true; + if (a == null || b == null) + return false; + if (ReferenceEquals(a, b)) + return true; return (Math.Abs(a.X - b.X) < GSharkMath.MaxTolerance && Math.Abs(a.Y - b.Y) < GSharkMath.MaxTolerance && Math.Abs(a.Z - b.Z) < GSharkMath.MaxTolerance); @@ -384,6 +390,14 @@ public double this[int i] /// true if obj is a Point3 and has the same coordinates as this; otherwise false. public override bool Equals(object obj) { + if (obj == null) + return false; + + if (ReferenceEquals(this, obj)) + return true; + + if (this.GetType() != obj.GetType()) + return false; return obj is Point3 point3 && this == point3; }