From 53151f606aa66756c8b4c981b32deb04ec573815 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Sun, 8 Mar 2020 20:50:34 +0100 Subject: [PATCH] Math: ensure TypeTraits fuzzy compare is consistent with TestSuite. It wasn't until now (TestSuite was wrong), so prevent similar issues from happening in the future. --- src/Magnum/Math/Test/TypeTraitsTest.cpp | 39 +++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/src/Magnum/Math/Test/TypeTraitsTest.cpp b/src/Magnum/Math/Test/TypeTraitsTest.cpp index 41cb71115..66c9608b0 100644 --- a/src/Magnum/Math/Test/TypeTraitsTest.cpp +++ b/src/Magnum/Math/Test/TypeTraitsTest.cpp @@ -288,6 +288,14 @@ template void TypeTraitsTest::equalsFloatingPoint0() { CORRADE_VERIFY(TypeTraits::equals(T(0)+TypeTraits::epsilon()/T(2), T(0))); CORRADE_VERIFY(!TypeTraits::equals(T(0)+TypeTraits::epsilon()*T(2), T(0))); + + /* Ensure we have the same behavior as TestSuite */ + CORRADE_COMPARE(Corrade::TestSuite::Implementation::FloatComparator{}( + T(0)+TypeTraits::epsilon()/T(2), T(0)), + Corrade::TestSuite::ComparisonStatusFlags{}); + CORRADE_COMPARE(Corrade::TestSuite::Implementation::FloatComparator{}( + T(0)+TypeTraits::epsilon()*T(2), T(0)), + Corrade::TestSuite::ComparisonStatusFlag::Failed); } template void TypeTraitsTest::equalsFloatingPoint1() { @@ -295,6 +303,14 @@ template void TypeTraitsTest::equalsFloatingPoint1() { CORRADE_VERIFY(TypeTraits::equals(T(1)+TypeTraits::epsilon()/T(2), T(1))); CORRADE_VERIFY(!TypeTraits::equals(T(1)+TypeTraits::epsilon()*T(3), T(1))); + + /* Ensure we have the same behavior as TestSuite */ + CORRADE_COMPARE(Corrade::TestSuite::Implementation::FloatComparator{}( + T(1)+TypeTraits::epsilon()/T(2), T(1)), + Corrade::TestSuite::ComparisonStatusFlags{}); + CORRADE_COMPARE(Corrade::TestSuite::Implementation::FloatComparator{}( + T(1)+TypeTraits::epsilon()*T(3), T(1)), + Corrade::TestSuite::ComparisonStatusFlag::Failed); } template void TypeTraitsTest::equalsFloatingPointLarge() { @@ -303,8 +319,13 @@ template void TypeTraitsTest::equalsFloatingPointLarge() { CORRADE_VERIFY(TypeTraits::equals(T(25)+TypeTraits::epsilon()*T(2), T(25))); CORRADE_VERIFY(!TypeTraits::equals(T(25)+TypeTraits::epsilon()*T(75), T(25))); - CORRADE_VERIFY(TypeTraits::equals(T(25)+TypeTraits::epsilon()*T(2), T(25))); - CORRADE_VERIFY(!TypeTraits::equals(T(25)+TypeTraits::epsilon()*T(75), T(25))); + /* Ensure we have the same behavior as TestSuite */ + CORRADE_COMPARE(Corrade::TestSuite::Implementation::FloatComparator{}( + T(25)+TypeTraits::epsilon()*T(2), T(25)), + Corrade::TestSuite::ComparisonStatusFlags{}); + CORRADE_COMPARE(Corrade::TestSuite::Implementation::FloatComparator{}( + T(25)+TypeTraits::epsilon()*T(75), T(25)), + Corrade::TestSuite::ComparisonStatusFlag::Failed); } template void TypeTraitsTest::equalsFloatingPointInfinity() { @@ -314,6 +335,14 @@ template void TypeTraitsTest::equalsFloatingPointInfinity() { Constants::inf())); CORRADE_VERIFY(!TypeTraits::equals(Constants::inf(), -Constants::inf())); + + /* Ensure we have the same behavior as TestSuite */ + CORRADE_COMPARE(Corrade::TestSuite::Implementation::FloatComparator{}( + Constants::inf(), Constants::inf()), + Corrade::TestSuite::ComparisonStatusFlags{}); + CORRADE_COMPARE(Corrade::TestSuite::Implementation::FloatComparator{}( + Constants::inf(), -Constants::inf()), + Corrade::TestSuite::ComparisonStatusFlag::Failed); } template void TypeTraitsTest::equalsFloatingPointNaN() { @@ -321,6 +350,12 @@ template void TypeTraitsTest::equalsFloatingPointNaN() { CORRADE_VERIFY(!TypeTraits::equals(Constants::nan(), Constants::nan())); + + /* OTOH, TestSuite compares two NaNs as equal -- since that makes more + sense in the context of tests */ + CORRADE_COMPARE(Corrade::TestSuite::Implementation::FloatComparator{}( + Constants::nan(), Constants::nan()), + Corrade::TestSuite::ComparisonStatusFlags{}); } /* Argh! Why there is no standard std::abs() for unsigned types? */