Skip to content

Commit

Permalink
equals handles null
Browse files Browse the repository at this point in the history
  • Loading branch information
NishantChandrashekhar committed Jun 8, 2023
1 parent d526a36 commit 96ada57
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/GShark.Test.XUnit/Geometry/Point3Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
14 changes: 14 additions & 0 deletions src/GShark/Geometry/Point3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,12 @@ public static Point3 Add(Vector3 vector, Point3 point)
/// <returns>true if the coordinates of the two points are exactly equal; otherwise false.</returns>
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);
Expand Down Expand Up @@ -384,6 +390,14 @@ public double this[int i]
/// <returns>true if obj is a Point3 and has the same coordinates as this; otherwise false.</returns>
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;
}

Expand Down

0 comments on commit 96ada57

Please sign in to comment.