Skip to content

Commit

Permalink
Implementing == and != operators
Browse files Browse the repository at this point in the history
  • Loading branch information
roigcarlo committed Apr 26, 2024
1 parent e2ec331 commit 7a23e40
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,17 @@ PointWithId::PointWithId(const PointWithId& rOther)

bool PointWithId::operator<(const PointWithId& rOther) const
{
if (Point::operator==(rOther)) return false;
return mDistance < rOther.mDistance;
return (!Point::operator==(rOther)) && (mDistance < rOther.mDistance);
}

bool PointWithId::operator==(const PointWithId& rOther) const
{
return ( Point::operator==(rOther)) && std::abs(mDistance - rOther.mDistance) < std::numeric_limits<double>::epsilon();
}

bool PointWithId::operator!=(const PointWithId& rOther) const
{
return (!Point::operator==(rOther)) || std::abs(mDistance - rOther.mDistance) > std::numeric_limits<double>::epsilon();
}

void PointWithId::save(Serializer &rSerializer) const
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,17 @@ KRATOS_TEST_CASE_IN_SUITE(PointWithIdEqualComparison, KratosMappingApplicationSe
PointWithId point_2(id, coords, dist);
PointWithId point_3(id, coords2, dist);

// only the position aka the coordinates are used for the equal comparison!
PointWithId point_4(id+1, coords, dist);
PointWithId point_5(id, coords, dist+1.0);
PointWithId point_6(id+1, coords, dist+1.0);

// Coordinates and Distance (id excluded) are used for the equal comparison!
KRATOS_EXPECT_EQ(point_1, point_2);
KRATOS_EXPECT_NE(point_1, point_3);

KRATOS_EXPECT_EQ(point_1, point_4);
KRATOS_EXPECT_EQ(point_1, point_5);
KRATOS_EXPECT_EQ(point_1, point_6);
KRATOS_EXPECT_NE(point_1, point_5);
KRATOS_EXPECT_NE(point_1, point_6);
}

KRATOS_TEST_CASE_IN_SUITE(PointWithIdLessComparison, KratosMappingApplicationSerialTestSuite)
Expand Down

0 comments on commit 7a23e40

Please sign in to comment.