From 8b780874121b77d33f98c8134503d5091b4c2e89 Mon Sep 17 00:00:00 2001 From: jsc Date: Mon, 19 Mar 2018 20:36:07 +0100 Subject: [PATCH] Fix bug in WithinAbs::match() and add tests for it --- include/internal/catch_matchers_floating.cpp | 2 +- .../Baselines/compact.sw.approved.txt | 8 +++ .../Baselines/console.std.approved.txt | 2 +- .../Baselines/console.sw.approved.txt | 50 ++++++++++++- .../SelfTest/Baselines/junit.sw.approved.txt | 2 +- .../SelfTest/Baselines/xml.sw.approved.txt | 72 +++++++++++++++++-- .../SelfTest/UsageTests/Matchers.tests.cpp | 10 +++ 7 files changed, 138 insertions(+), 8 deletions(-) diff --git a/include/internal/catch_matchers_floating.cpp b/include/internal/catch_matchers_floating.cpp index c6d2b1dd..b758bcf9 100644 --- a/include/internal/catch_matchers_floating.cpp +++ b/include/internal/catch_matchers_floating.cpp @@ -88,7 +88,7 @@ namespace Floating { // Performs equivalent check of std::fabs(lhs - rhs) <= margin // But without the subtraction to allow for INFINITY in comparison bool WithinAbsMatcher::match(double const& matchee) const { - return (matchee + m_margin >= m_target) && (m_target + m_margin >= m_margin); + return (matchee + m_margin >= m_target) && (m_target + m_margin >= matchee); } std::string WithinAbsMatcher::describe() const { diff --git a/projects/SelfTest/Baselines/compact.sw.approved.txt b/projects/SelfTest/Baselines/compact.sw.approved.txt index 4a1aeb78..b02a9e0e 100644 --- a/projects/SelfTest/Baselines/compact.sw.approved.txt +++ b/projects/SelfTest/Baselines/compact.sw.approved.txt @@ -235,6 +235,10 @@ Matchers.tests.cpp:: passed: 0., WithinAbs(1., 1) for: 0.0 is withi Matchers.tests.cpp:: passed: 0., !WithinAbs(1., 0.99) for: 0.0 not is within 0.99 of 1.0 Matchers.tests.cpp:: passed: 0., !WithinAbs(1., 0.99) for: 0.0 not is within 0.99 of 1.0 Matchers.tests.cpp:: passed: NAN, !WithinAbs(NAN, 0) for: nanf not is within 0.0 of nan +Matchers.tests.cpp:: passed: 11., !WithinAbs(10., 0.5) for: 11.0 not is within 0.5 of 10.0 +Matchers.tests.cpp:: passed: 10., !WithinAbs(11., 0.5) for: 10.0 not is within 0.5 of 11.0 +Matchers.tests.cpp:: passed: -10., WithinAbs(-10., 0.5) for: -10.0 is within 0.5 of -10.0 +Matchers.tests.cpp:: passed: -10., WithinAbs(-9.6, 0.5) for: -10.0 is within 0.5 of -9.6 Matchers.tests.cpp:: passed: 1., WithinULP(1., 0) for: 1.0 is within 0 ULPs of 1.0 Matchers.tests.cpp:: passed: nextafter(1., 2.), WithinULP(1., 1) for: 1.0 is within 1 ULPs of 1.0 Matchers.tests.cpp:: passed: nextafter(1., 0.), WithinULP(1., 1) for: 1.0 is within 1 ULPs of 1.0 @@ -255,6 +259,10 @@ Matchers.tests.cpp:: passed: 0.f, !WithinAbs(1.f, 0.99f) for: 0.0f Matchers.tests.cpp:: passed: 0.f, !WithinAbs(1.f, 0.99f) for: 0.0f not is within 0.9900000095 of 1.0 Matchers.tests.cpp:: passed: 0.f, WithinAbs(-0.f, 0) for: 0.0f is within 0.0 of -0.0 Matchers.tests.cpp:: passed: NAN, !WithinAbs(NAN, 0) for: nanf not is within 0.0 of nan +Matchers.tests.cpp:: passed: 11.f, !WithinAbs(10.f, 0.5f) for: 11.0f not is within 0.5 of 10.0 +Matchers.tests.cpp:: passed: 10.f, !WithinAbs(11.f, 0.5f) for: 10.0f not is within 0.5 of 11.0 +Matchers.tests.cpp:: passed: -10.f, WithinAbs(-10.f, 0.5f) for: -10.0f is within 0.5 of -10.0 +Matchers.tests.cpp:: passed: -10.f, WithinAbs(-9.6f, 0.5f) for: -10.0f is within 0.5 of -9.6000003815 Matchers.tests.cpp:: passed: 1.f, WithinULP(1.f, 0) for: 1.0f is within 0 ULPs of 1.0f Matchers.tests.cpp:: passed: nextafter(1.f, 2.f), WithinULP(1.f, 1) for: 1.0f is within 1 ULPs of 1.0f Matchers.tests.cpp:: passed: nextafter(1.f, 0.f), WithinULP(1.f, 1) for: 1.0f is within 1 ULPs of 1.0f diff --git a/projects/SelfTest/Baselines/console.std.approved.txt b/projects/SelfTest/Baselines/console.std.approved.txt index 5091ff1d..6df0a222 100644 --- a/projects/SelfTest/Baselines/console.std.approved.txt +++ b/projects/SelfTest/Baselines/console.std.approved.txt @@ -1085,5 +1085,5 @@ due to unexpected exception with message: =============================================================================== test cases: 202 | 149 passed | 49 failed | 4 failed as expected -assertions: 1007 | 879 passed | 107 failed | 21 failed as expected +assertions: 1015 | 887 passed | 107 failed | 21 failed as expected diff --git a/projects/SelfTest/Baselines/console.sw.approved.txt b/projects/SelfTest/Baselines/console.sw.approved.txt index 70bff4a8..3a611b72 100644 --- a/projects/SelfTest/Baselines/console.sw.approved.txt +++ b/projects/SelfTest/Baselines/console.sw.approved.txt @@ -1858,6 +1858,30 @@ PASSED: with expansion: nanf not is within 0.0 of nan +Matchers.tests.cpp:: +PASSED: + REQUIRE_THAT( 11., !WithinAbs(10., 0.5) ) +with expansion: + 11.0 not is within 0.5 of 10.0 + +Matchers.tests.cpp:: +PASSED: + REQUIRE_THAT( 10., !WithinAbs(11., 0.5) ) +with expansion: + 10.0 not is within 0.5 of 11.0 + +Matchers.tests.cpp:: +PASSED: + REQUIRE_THAT( -10., WithinAbs(-10., 0.5) ) +with expansion: + -10.0 is within 0.5 of -10.0 + +Matchers.tests.cpp:: +PASSED: + REQUIRE_THAT( -10., WithinAbs(-9.6, 0.5) ) +with expansion: + -10.0 is within 0.5 of -9.6 + ------------------------------------------------------------------------------- Floating point matchers: double ULPs @@ -1998,6 +2022,30 @@ PASSED: with expansion: nanf not is within 0.0 of nan +Matchers.tests.cpp:: +PASSED: + REQUIRE_THAT( 11.f, !WithinAbs(10.f, 0.5f) ) +with expansion: + 11.0f not is within 0.5 of 10.0 + +Matchers.tests.cpp:: +PASSED: + REQUIRE_THAT( 10.f, !WithinAbs(11.f, 0.5f) ) +with expansion: + 10.0f not is within 0.5 of 11.0 + +Matchers.tests.cpp:: +PASSED: + REQUIRE_THAT( -10.f, WithinAbs(-10.f, 0.5f) ) +with expansion: + -10.0f is within 0.5 of -10.0 + +Matchers.tests.cpp:: +PASSED: + REQUIRE_THAT( -10.f, WithinAbs(-9.6f, 0.5f) ) +with expansion: + -10.0f is within 0.5 of -9.6000003815 + ------------------------------------------------------------------------------- Floating point matchers: float ULPs @@ -8551,5 +8599,5 @@ PASSED: =============================================================================== test cases: 202 | 136 passed | 62 failed | 4 failed as expected -assertions: 1021 | 879 passed | 121 failed | 21 failed as expected +assertions: 1029 | 887 passed | 121 failed | 21 failed as expected diff --git a/projects/SelfTest/Baselines/junit.sw.approved.txt b/projects/SelfTest/Baselines/junit.sw.approved.txt index 895e41f9..3cdf5a4f 100644 --- a/projects/SelfTest/Baselines/junit.sw.approved.txt +++ b/projects/SelfTest/Baselines/junit.sw.approved.txt @@ -1,7 +1,7 @@ - + diff --git a/projects/SelfTest/Baselines/xml.sw.approved.txt b/projects/SelfTest/Baselines/xml.sw.approved.txt index 227dee93..69d35c83 100644 --- a/projects/SelfTest/Baselines/xml.sw.approved.txt +++ b/projects/SelfTest/Baselines/xml.sw.approved.txt @@ -2108,7 +2108,39 @@ nanf not is within 0.0 of nan - + + + 11., !WithinAbs(10., 0.5) + + + 11.0 not is within 0.5 of 10.0 + + + + + 10., !WithinAbs(11., 0.5) + + + 10.0 not is within 0.5 of 11.0 + + + + + -10., WithinAbs(-10., 0.5) + + + -10.0 is within 0.5 of -10.0 + + + + + -10., WithinAbs(-9.6, 0.5) + + + -10.0 is within 0.5 of -9.6 + + +
@@ -2283,7 +2315,39 @@ nanf not is within 0.0 of nan - + + + 11.f, !WithinAbs(10.f, 0.5f) + + + 11.0f not is within 0.5 of 10.0 + + + + + 10.f, !WithinAbs(11.f, 0.5f) + + + 10.0f not is within 0.5 of 11.0 + + + + + -10.f, WithinAbs(-10.f, 0.5f) + + + -10.0f is within 0.5 of -10.0 + + + + + -10.f, WithinAbs(-9.6f, 0.5f) + + + -10.0f is within 0.5 of -9.6000003815 + + +
@@ -9405,7 +9469,7 @@ loose text artifact
- + - + diff --git a/projects/SelfTest/UsageTests/Matchers.tests.cpp b/projects/SelfTest/UsageTests/Matchers.tests.cpp index 05056b43..7032c2cc 100644 --- a/projects/SelfTest/UsageTests/Matchers.tests.cpp +++ b/projects/SelfTest/UsageTests/Matchers.tests.cpp @@ -321,6 +321,11 @@ namespace { namespace MatchersTests { REQUIRE_THAT(0.f, WithinAbs(-0.f, 0)); REQUIRE_THAT(NAN, !WithinAbs(NAN, 0)); + + REQUIRE_THAT(11.f, !WithinAbs(10.f, 0.5f)); + REQUIRE_THAT(10.f, !WithinAbs(11.f, 0.5f)); + REQUIRE_THAT(-10.f, WithinAbs(-10.f, 0.5f)); + REQUIRE_THAT(-10.f, WithinAbs(-9.6f, 0.5f)); } SECTION("ULPs") { REQUIRE_THAT(1.f, WithinULP(1.f, 0)); @@ -358,6 +363,11 @@ namespace { namespace MatchersTests { REQUIRE_THAT(0., !WithinAbs(1., 0.99)); REQUIRE_THAT(NAN, !WithinAbs(NAN, 0)); + + REQUIRE_THAT(11., !WithinAbs(10., 0.5)); + REQUIRE_THAT(10., !WithinAbs(11., 0.5)); + REQUIRE_THAT(-10., WithinAbs(-10., 0.5)); + REQUIRE_THAT(-10., WithinAbs(-9.6, 0.5)); } SECTION("ULPs") { REQUIRE_THAT(1., WithinULP(1., 0));