catch3
Progmasoft Catch3: an independently maintained C++17 test framework with a parallel runner, property checks, snapshots and structured results.
Clonegit clone https://git.progmasoft.com/catch3
Add constructor arg checking to WithinAbsMatcher
- Commit
0c122c135d5aa726ae206c30410700655c33821d- Author
- Martin Hořeňovský ·
- Parents
d19b7292b375
Changes
include/internal/catch_matchers_floating.cpp | 8 ++- .../SelfTest/Baselines/console.std.approved.txt | 2 +- .../SelfTest/Baselines/console.sw.approved.txt | 48 +++++++++++++- projects/SelfTest/Baselines/junit.sw.approved.txt | 4 +- projects/SelfTest/Baselines/xml.sw.approved.txt | 74 +++++++++++++++++++++- projects/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:<line number> +............................................................................... + +Matchers.tests.cpp:<line number>: +PASSED: + REQUIRE_NOTHROW( WithinAbs(1., 0.) ) + +Matchers.tests.cpp:<line number>: +PASSED: + REQUIRE_THROWS_AS( WithinAbs(1., -1.), std::domain_error ) + +Matchers.tests.cpp:<line number>: +PASSED: + REQUIRE_NOTHROW( WithinULP(1., 0) ) + +Matchers.tests.cpp:<line number>: +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:<line number> +............................................................................... + +Matchers.tests.cpp:<line number>: +PASSED: + REQUIRE_NOTHROW( WithinAbs(1.f, 0.f) ) + +Matchers.tests.cpp:<line number>: +PASSED: + REQUIRE_THROWS_AS( WithinAbs(1.f, -1.f), std::domain_error ) + +Matchers.tests.cpp:<line number>: +PASSED: + REQUIRE_NOTHROW( WithinULP(1.f, 0) ) + +Matchers.tests.cpp:<line number>: +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 @@ <?xml version="1.0" encoding="UTF-8"?> <testsuitesloose text artifact > - <testsuite name="<exe-name>" errors="15" failures="92" tests="955" hostname="tbd" time="{duration}" timestamp="{iso8601-timestamp}"> + <testsuite name="<exe-name>" errors="15" failures="92" tests="963" hostname="tbd" time="{duration}" timestamp="{iso8601-timestamp}"> <testcase classname="<exe-name>.global" name="# A test name that starts with a #" time="{duration}"/> <testcase classname="<exe-name>.global" name="#1005: Comparing pointer to int and long (NULL can be either on various systems)" time="{duration}"/> <testcase classname="<exe-name>.global" name="#1027" time="{duration}"/> @@ -276,9 +276,11 @@ Message.tests.cpp:<line number> <testcase classname="<exe-name>.global" name="Floating point matchers: double/Margin" time="{duration}"/> <testcase classname="<exe-name>.global" name="Floating point matchers: double/ULPs" time="{duration}"/> <testcase classname="<exe-name>.global" name="Floating point matchers: double/Composed" time="{duration}"/> + <testcase classname="<exe-name>.global" name="Floating point matchers: double/Constructor validation" time="{duration}"/> <testcase classname="<exe-name>.global" name="Floating point matchers: float/Margin" time="{duration}"/> <testcase classname="<exe-name>.global" name="Floating point matchers: float/ULPs" time="{duration}"/> <testcase classname="<exe-name>.global" name="Floating point matchers: float/Composed" time="{duration}"/> + <testcase classname="<exe-name>.global" name="Floating point matchers: float/Constructor validation" time="{duration}"/> <testcase classname="<exe-name>.global" name="Greater-than inequalities with different epsilons" time="{duration}"/> <testcase classname="<exe-name>.global" name="INFO and WARN do not abort tests" time="{duration}"/> <testcase classname="<exe-name>.global" name="INFO gets logged on failure" time="{duration}"> 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 @@ </Expression> <OverallResults successes="3" failures="0" expectedFailures="0"/> </Section> + <Section name="Constructor validation" filename="projects/<exe-name>/UsageTests/Matchers.tests.cpp" > + <Expression success="true" type="REQUIRE_NOTHROW" filename="projects/<exe-name>/UsageTests/Matchers.tests.cpp" > + <Original> + WithinAbs(1., 0.) + </Original> + <Expanded> + WithinAbs(1., 0.) + </Expanded> + </Expression> + <Expression success="true" type="REQUIRE_THROWS_AS" filename="projects/<exe-name>/UsageTests/Matchers.tests.cpp" > + <Original> + WithinAbs(1., -1.), std::domain_error + </Original> + <Expanded> + WithinAbs(1., -1.), std::domain_error + </Expanded> + </Expression> + <Expression success="true" type="REQUIRE_NOTHROW" filename="projects/<exe-name>/UsageTests/Matchers.tests.cpp" > + <Original> + WithinULP(1., 0) + </Original> + <Expanded> + WithinULP(1., 0) + </Expanded> + </Expression> + <Expression success="true" type="REQUIRE_THROWS_AS" filename="projects/<exe-name>/UsageTests/Matchers.tests.cpp" > + <Original> + WithinULP(1., -1), std::domain_error + </Original> + <Expanded> + WithinULP(1., -1), std::domain_error + </Expanded> + </Expression> + <OverallResults successes="4" failures="0" expectedFailures="0"/> + </Section> <OverallResult success="true"/> </TestCase> <TestCase name="Floating point matchers: float" tags="[floating-point][matchers]" filename="projects/<exe-name>/UsageTests/Matchers.tests.cpp" > @@ -2282,6 +2317,41 @@ </Expression> <OverallResults successes="3" failures="0" expectedFailures="0"/> </Section> + <Section name="Constructor validation" filename="projects/<exe-name>/UsageTests/Matchers.tests.cpp" > + <Expression success="true" type="REQUIRE_NOTHROW" filename="projects/<exe-name>/UsageTests/Matchers.tests.cpp" > + <Original> + WithinAbs(1.f, 0.f) + </Original> + <Expanded> + WithinAbs(1.f, 0.f) + </Expanded> + </Expression> + <Expression success="true" type="REQUIRE_THROWS_AS" filename="projects/<exe-name>/UsageTests/Matchers.tests.cpp" > + <Original> + WithinAbs(1.f, -1.f), std::domain_error + </Original> + <Expanded> + WithinAbs(1.f, -1.f), std::domain_error + </Expanded> + </Expression> + <Expression success="true" type="REQUIRE_NOTHROW" filename="projects/<exe-name>/UsageTests/Matchers.tests.cpp" > + <Original> + WithinULP(1.f, 0) + </Original> + <Expanded> + WithinULP(1.f, 0) + </Expanded> + </Expression> + <Expression success="true" type="REQUIRE_THROWS_AS" filename="projects/<exe-name>/UsageTests/Matchers.tests.cpp" > + <Original> + WithinULP(1.f, -1), std::domain_error + </Original> + <Expanded> + WithinULP(1.f, -1), std::domain_error + </Expanded> + </Expression> + <OverallResults successes="4" failures="0" expectedFailures="0"/> + </Section> <OverallResult success="true"/> </TestCase> <TestCase name="Greater-than inequalities with different epsilons" tags="[Approx]" filename="projects/<exe-name>/UsageTests/Approx.tests.cpp" > @@ -8898,7 +8968,7 @@ loose text artifact </Section> <OverallResult success="true"/> </TestCase> - <OverallResults successes="827" failures="107" expectedFailures="21"/> + <OverallResults successes="835" failures="107" expectedFailures="21"/> </Group> - <OverallResults successes="827" failures="106" expectedFailures="21"/> + <OverallResults successes="835" failures="106" expectedFailures="21"/> </Catch> 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