From 0c122c135d5aa726ae206c30410700655c33821d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Ho=C5=99e=C5=88ovsk=C3=BD?= Date: Wed, 6 Dec 2017 15:37:13 +0100 Subject: [PATCH] Add constructor arg checking to WithinAbsMatcher Also tests :-) --- include/internal/catch_matchers_floating.cpp | 8 +- .../Baselines/console.std.approved.txt | 2 +- .../Baselines/console.sw.approved.txt | 48 +++++++++++- .../SelfTest/Baselines/junit.sw.approved.txt | 4 +- .../SelfTest/Baselines/xml.sw.approved.txt | 74 ++++++++++++++++++- .../SelfTest/UsageTests/Matchers.tests.cpp | 14 ++++ 6 files changed, 143 insertions(+), 7 deletions(-) diff --git a/include/internal/catch_matchers_floating.cpp b/include/internal/catch_matchers_floating.cpp index 3d2e342a..c6d2b1dd 100644 --- a/include/internal/catch_matchers_floating.cpp +++ b/include/internal/catch_matchers_floating.cpp @@ -79,7 +79,11 @@ namespace Catch { namespace Matchers { namespace Floating { WithinAbsMatcher::WithinAbsMatcher(double target, double margin) - :m_target{ target }, m_margin{ margin } {} + :m_target{ target }, m_margin{ margin } { + if (m_margin < 0) { + throw std::domain_error("Allowed margin difference has to be >= 0"); + } + } // Performs equivalent check of std::fabs(lhs - rhs) <= margin // But without the subtraction to allow for INFINITY in comparison @@ -95,7 +99,7 @@ namespace Floating { WithinUlpsMatcher::WithinUlpsMatcher(double target, int ulps, FloatingPointKind baseType) :m_target{ target }, m_ulps{ ulps }, m_type{ baseType } { if (m_ulps < 0) { - throw std::domain_error("Expected ulp difference has to be >0"); + throw std::domain_error("Allowed ulp difference has to be >= 0"); } } diff --git a/projects/SelfTest/Baselines/console.std.approved.txt b/projects/SelfTest/Baselines/console.std.approved.txt index c4ed431b..eaee3ee1 100644 --- a/projects/SelfTest/Baselines/console.std.approved.txt +++ b/projects/SelfTest/Baselines/console.std.approved.txt @@ -1054,5 +1054,5 @@ with expansion: =============================================================================== test cases: 191 | 139 passed | 48 failed | 4 failed as expected -assertions: 955 | 831 passed | 103 failed | 21 failed as expected +assertions: 963 | 839 passed | 103 failed | 21 failed as expected diff --git a/projects/SelfTest/Baselines/console.sw.approved.txt b/projects/SelfTest/Baselines/console.sw.approved.txt index 82d5e121..24c5a26f 100644 --- a/projects/SelfTest/Baselines/console.sw.approved.txt +++ b/projects/SelfTest/Baselines/console.sw.approved.txt @@ -1872,6 +1872,29 @@ PASSED: with expansion: nanf not ( is within 100.0 of nan or is within 123 ULPs of nanf ) +------------------------------------------------------------------------------- +Floating point matchers: double + Constructor validation +------------------------------------------------------------------------------- +Matchers.tests.cpp: +............................................................................... + +Matchers.tests.cpp:: +PASSED: + REQUIRE_NOTHROW( WithinAbs(1., 0.) ) + +Matchers.tests.cpp:: +PASSED: + REQUIRE_THROWS_AS( WithinAbs(1., -1.), std::domain_error ) + +Matchers.tests.cpp:: +PASSED: + REQUIRE_NOTHROW( WithinULP(1., 0) ) + +Matchers.tests.cpp:: +PASSED: + REQUIRE_THROWS_AS( WithinULP(1., -1), std::domain_error ) + ------------------------------------------------------------------------------- Floating point matchers: float Margin @@ -1989,6 +2012,29 @@ PASSED: with expansion: nanf not ( is within 100.0 of nan or is within 123 ULPs of nanf ) +------------------------------------------------------------------------------- +Floating point matchers: float + Constructor validation +------------------------------------------------------------------------------- +Matchers.tests.cpp: +............................................................................... + +Matchers.tests.cpp:: +PASSED: + REQUIRE_NOTHROW( WithinAbs(1.f, 0.f) ) + +Matchers.tests.cpp:: +PASSED: + REQUIRE_THROWS_AS( WithinAbs(1.f, -1.f), std::domain_error ) + +Matchers.tests.cpp:: +PASSED: + REQUIRE_NOTHROW( WithinULP(1.f, 0) ) + +Matchers.tests.cpp:: +PASSED: + REQUIRE_THROWS_AS( WithinULP(1.f, -1), std::domain_error ) + ------------------------------------------------------------------------------- Greater-than inequalities with different epsilons ------------------------------------------------------------------------------- @@ -8011,5 +8057,5 @@ PASSED: =============================================================================== test cases: 191 | 137 passed | 50 failed | 4 failed as expected -assertions: 954 | 827 passed | 106 failed | 21 failed as expected +assertions: 962 | 835 passed | 106 failed | 21 failed as expected diff --git a/projects/SelfTest/Baselines/junit.sw.approved.txt b/projects/SelfTest/Baselines/junit.sw.approved.txt index dd5805a5..4436121f 100644 --- a/projects/SelfTest/Baselines/junit.sw.approved.txt +++ b/projects/SelfTest/Baselines/junit.sw.approved.txt @@ -1,7 +1,7 @@ - + @@ -276,9 +276,11 @@ Message.tests.cpp: + + diff --git a/projects/SelfTest/Baselines/xml.sw.approved.txt b/projects/SelfTest/Baselines/xml.sw.approved.txt index 0821516a..73fc131e 100644 --- a/projects/SelfTest/Baselines/xml.sw.approved.txt +++ b/projects/SelfTest/Baselines/xml.sw.approved.txt @@ -2142,6 +2142,41 @@ +
+ + + WithinAbs(1., 0.) + + + WithinAbs(1., 0.) + + + + + WithinAbs(1., -1.), std::domain_error + + + WithinAbs(1., -1.), std::domain_error + + + + + WithinULP(1., 0) + + + WithinULP(1., 0) + + + + + WithinULP(1., -1), std::domain_error + + + WithinULP(1., -1), std::domain_error + + + +
@@ -2282,6 +2317,41 @@ +
+ + + WithinAbs(1.f, 0.f) + + + WithinAbs(1.f, 0.f) + + + + + WithinAbs(1.f, -1.f), std::domain_error + + + WithinAbs(1.f, -1.f), std::domain_error + + + + + WithinULP(1.f, 0) + + + WithinULP(1.f, 0) + + + + + WithinULP(1.f, -1), std::domain_error + + + WithinULP(1.f, -1), std::domain_error + + + +
@@ -8898,7 +8968,7 @@ loose text artifact - + - + diff --git a/projects/SelfTest/UsageTests/Matchers.tests.cpp b/projects/SelfTest/UsageTests/Matchers.tests.cpp index 34f167ab..8ffa995c 100644 --- a/projects/SelfTest/UsageTests/Matchers.tests.cpp +++ b/projects/SelfTest/UsageTests/Matchers.tests.cpp @@ -298,6 +298,13 @@ namespace { namespace MatchersTests { REQUIRE_THAT(NAN, !(WithinAbs(NAN, 100) || WithinULP(NAN, 123))); } + SECTION("Constructor validation") { + REQUIRE_NOTHROW(WithinAbs(1.f, 0.f)); + REQUIRE_THROWS_AS(WithinAbs(1.f, -1.f), std::domain_error); + + REQUIRE_NOTHROW(WithinULP(1.f, 0)); + REQUIRE_THROWS_AS(WithinULP(1.f, -1), std::domain_error); + } } TEST_CASE("Floating point matchers: double", "[matchers][floating-point]") { @@ -328,6 +335,13 @@ namespace { namespace MatchersTests { REQUIRE_THAT(NAN, !(WithinAbs(NAN, 100) || WithinULP(NAN, 123))); } + SECTION("Constructor validation") { + REQUIRE_NOTHROW(WithinAbs(1., 0.)); + REQUIRE_THROWS_AS(WithinAbs(1., -1.), std::domain_error); + + REQUIRE_NOTHROW(WithinULP(1., 0)); + REQUIRE_THROWS_AS(WithinULP(1., -1), std::domain_error); + } } } } // namespace MatchersTests