From 91b617c46298cec9479fb14141b645c5fdf6204c Mon Sep 17 00:00:00 2001 From: Will Handley Date: Mon, 14 Jan 2019 14:31:09 +0000 Subject: [PATCH] Added a vector approximate matcher --- include/internal/catch_matchers_vector.h | 42 +++++++ .../Baselines/compact.sw.approved.txt | 11 +- .../Baselines/console.std.approved.txt | 28 ++++- .../Baselines/console.sw.approved.txt | 94 ++++++++++++++- .../SelfTest/Baselines/junit.sw.approved.txt | 16 ++- .../SelfTest/Baselines/xml.sw.approved.txt | 109 +++++++++++++++++- .../SelfTest/UsageTests/Matchers.tests.cpp | 58 ++++++++++ 7 files changed, 350 insertions(+), 8 deletions(-) diff --git a/include/internal/catch_matchers_vector.h b/include/internal/catch_matchers_vector.h index 347c5d50..2dc8a715 100644 --- a/include/internal/catch_matchers_vector.h +++ b/include/internal/catch_matchers_vector.h @@ -9,6 +9,7 @@ #define TWOBLUECUBES_CATCH_MATCHERS_VECTOR_H_INCLUDED #include "catch_matchers.h" +#include "catch_approx.h" #include @@ -90,6 +91,42 @@ namespace Matchers { std::vector const& m_comparator; }; + template + struct ApproxMatcher : MatcherBase> { + + ApproxMatcher(std::vector const& comparator) : m_comparator( comparator ) {} + + bool match(std::vector const &v) const override { + if (m_comparator.size() != v.size()) + return false; + for (std::size_t i = 0; i < v.size(); ++i) + if (m_comparator[i] != approx(v[i])) + return false; + return true; + } + std::string describe() const override { + return "is approx: " + ::Catch::Detail::stringify( m_comparator ); + } + template ::value>::type> + ApproxMatcher& epsilon( T const& newEpsilon ) { + approx.epsilon(newEpsilon); + return *this; + } + template ::value>::type> + ApproxMatcher& margin( T const& newMargin ) { + approx.margin(newMargin); + return *this; + } + template ::value>::type> + ApproxMatcher& scale( T const& newScale ) { + approx.scale(newScale); + return *this; + } + + std::vector const& m_comparator; + mutable Catch::Detail::Approx approx = Catch::Detail::Approx::custom(); + }; + template struct UnorderedEqualsMatcher : MatcherBase> { UnorderedEqualsMatcher(std::vector const& target) : m_target(target) {} @@ -129,6 +166,11 @@ namespace Matchers { return Vector::EqualsMatcher( comparator ); } + template + Vector::ApproxMatcher Approx( std::vector const& comparator ) { + return Vector::ApproxMatcher( comparator ); + } + template Vector::UnorderedEqualsMatcher UnorderedEquals(std::vector const& target) { return Vector::UnorderedEqualsMatcher(target); diff --git a/projects/SelfTest/Baselines/compact.sw.approved.txt b/projects/SelfTest/Baselines/compact.sw.approved.txt index 99eca94d..6fedbb92 100644 --- a/projects/SelfTest/Baselines/compact.sw.approved.txt +++ b/projects/SelfTest/Baselines/compact.sw.approved.txt @@ -1189,6 +1189,15 @@ Approx.tests.cpp:: passed: approx( d ) == 1.22 for: Approx( 1.23 ) Approx.tests.cpp:: passed: approx( d ) == 1.24 for: Approx( 1.23 ) == 1.24 Approx.tests.cpp:: passed: approx( d ) != 1.25 for: Approx( 1.23 ) != 1.25 VariadicMacros.tests.cpp:: passed: with 1 message: 'no assertions' +Matchers.tests.cpp:: passed: empty, Approx(empty) for: { } is approx: { } +Matchers.tests.cpp:: passed: v1, Approx(v1) for: { 1.0, 2.0, 3.0 } is approx: { 1.0, 2.0, 3.0 } +Matchers.tests.cpp:: passed: v1, !Approx(temp) for: { 1.0, 2.0, 3.0 } not is approx: { 1.0, 2.0, 3.0, 4.0 } +Matchers.tests.cpp:: passed: v1, !Approx(v2) for: { 1.0, 2.0, 3.0 } not is approx: { 1.5, 2.5, 3.5 } +Matchers.tests.cpp:: passed: v1, Approx(v2).margin(0.5) for: { 1.0, 2.0, 3.0 } is approx: { 1.5, 2.5, 3.5 } +Matchers.tests.cpp:: passed: v1, Approx(v2).epsilon(0.5) for: { 1.0, 2.0, 3.0 } is approx: { 1.5, 2.5, 3.5 } +Matchers.tests.cpp:: passed: v1, Approx(v2).epsilon(0.1).scale(500) for: { 1.0, 2.0, 3.0 } is approx: { 1.5, 2.5, 3.5 } +Matchers.tests.cpp:: failed: empty, Approx(t1) for: { } is approx: { 1.0, 2.0 } +Matchers.tests.cpp:: failed: v1, Approx(v2) for: { 2.0, 4.0, 6.0 } is approx: { 1.0, 3.0, 5.0 } Matchers.tests.cpp:: passed: v, VectorContains(1) for: { 1, 2, 3 } Contains: 1 Matchers.tests.cpp:: passed: v, VectorContains(2) for: { 1, 2, 3 } Contains: 2 Matchers.tests.cpp:: passed: v, Contains(v2) for: { 1, 2, 3 } Contains: { 1, 2 } @@ -1505,5 +1514,5 @@ Misc.tests.cpp:: passed: v.size() == 5 for: 5 == 5 Misc.tests.cpp:: passed: v.capacity() >= 5 for: 5 >= 5 Misc.tests.cpp:: passed: Misc.tests.cpp:: passed: -Failed 78 test cases, failed 139 assertions. +Failed 79 test cases, failed 141 assertions. diff --git a/projects/SelfTest/Baselines/console.std.approved.txt b/projects/SelfTest/Baselines/console.std.approved.txt index 3b6f2068..e5a1d5da 100644 --- a/projects/SelfTest/Baselines/console.std.approved.txt +++ b/projects/SelfTest/Baselines/console.std.approved.txt @@ -861,6 +861,30 @@ Exception.tests.cpp:: FAILED: due to unexpected exception with message: 3.14 +------------------------------------------------------------------------------- +Vector Approx matcher -- failing + Empty and non empty vectors are not approx equal +------------------------------------------------------------------------------- +Matchers.tests.cpp: +............................................................................... + +Matchers.tests.cpp:: FAILED: + CHECK_THAT( empty, Approx(t1) ) +with expansion: + { } is approx: { 1.0, 2.0 } + +------------------------------------------------------------------------------- +Vector Approx matcher -- failing + Just different vectors +------------------------------------------------------------------------------- +Matchers.tests.cpp: +............................................................................... + +Matchers.tests.cpp:: FAILED: + CHECK_THAT( v1, Approx(v2) ) +with expansion: + { 2.0, 4.0, 6.0 } is approx: { 1.0, 3.0, 5.0 } + ------------------------------------------------------------------------------- Vector matchers that fail Contains (element) @@ -1275,6 +1299,6 @@ due to unexpected exception with message: Why would you throw a std::string? =============================================================================== -test cases: 258 | 192 passed | 62 failed | 4 failed as expected -assertions: 1419 | 1276 passed | 122 failed | 21 failed as expected +test cases: 260 | 193 passed | 63 failed | 4 failed as expected +assertions: 1428 | 1283 passed | 124 failed | 21 failed as expected diff --git a/projects/SelfTest/Baselines/console.sw.approved.txt b/projects/SelfTest/Baselines/console.sw.approved.txt index c63de78a..9def37d9 100644 --- a/projects/SelfTest/Baselines/console.sw.approved.txt +++ b/projects/SelfTest/Baselines/console.sw.approved.txt @@ -8720,6 +8720,96 @@ VariadicMacros.tests.cpp:: PASSED: with message: no assertions +------------------------------------------------------------------------------- +Vector Approx matcher + Empty vector is roughly equal to an empty vector +------------------------------------------------------------------------------- +Matchers.tests.cpp: +............................................................................... + +Matchers.tests.cpp:: PASSED: + REQUIRE_THAT( empty, Approx(empty) ) +with expansion: + { } is approx: { } + +------------------------------------------------------------------------------- +Vector Approx matcher + Vectors with elements + A vector is approx equal to itself +------------------------------------------------------------------------------- +Matchers.tests.cpp: +............................................................................... + +Matchers.tests.cpp:: PASSED: + REQUIRE_THAT( v1, Approx(v1) ) +with expansion: + { 1.0, 2.0, 3.0 } is approx: { 1.0, 2.0, 3.0 } + +------------------------------------------------------------------------------- +Vector Approx matcher + Vectors with elements + Different length +------------------------------------------------------------------------------- +Matchers.tests.cpp: +............................................................................... + +Matchers.tests.cpp:: PASSED: + REQUIRE_THAT( v1, !Approx(temp) ) +with expansion: + { 1.0, 2.0, 3.0 } not is approx: { 1.0, 2.0, 3.0, 4.0 } + +------------------------------------------------------------------------------- +Vector Approx matcher + Vectors with elements + Same length, different elements +------------------------------------------------------------------------------- +Matchers.tests.cpp: +............................................................................... + +Matchers.tests.cpp:: PASSED: + REQUIRE_THAT( v1, !Approx(v2) ) +with expansion: + { 1.0, 2.0, 3.0 } not is approx: { 1.5, 2.5, 3.5 } + +Matchers.tests.cpp:: PASSED: + REQUIRE_THAT( v1, Approx(v2).margin(0.5) ) +with expansion: + { 1.0, 2.0, 3.0 } is approx: { 1.5, 2.5, 3.5 } + +Matchers.tests.cpp:: PASSED: + REQUIRE_THAT( v1, Approx(v2).epsilon(0.5) ) +with expansion: + { 1.0, 2.0, 3.0 } is approx: { 1.5, 2.5, 3.5 } + +Matchers.tests.cpp:: PASSED: + REQUIRE_THAT( v1, Approx(v2).epsilon(0.1).scale(500) ) +with expansion: + { 1.0, 2.0, 3.0 } is approx: { 1.5, 2.5, 3.5 } + +------------------------------------------------------------------------------- +Vector Approx matcher -- failing + Empty and non empty vectors are not approx equal +------------------------------------------------------------------------------- +Matchers.tests.cpp: +............................................................................... + +Matchers.tests.cpp:: FAILED: + CHECK_THAT( empty, Approx(t1) ) +with expansion: + { } is approx: { 1.0, 2.0 } + +------------------------------------------------------------------------------- +Vector Approx matcher -- failing + Just different vectors +------------------------------------------------------------------------------- +Matchers.tests.cpp: +............................................................................... + +Matchers.tests.cpp:: FAILED: + CHECK_THAT( v1, Approx(v2) ) +with expansion: + { 2.0, 4.0, 6.0 } is approx: { 1.0, 3.0, 5.0 } + ------------------------------------------------------------------------------- Vector matchers Contains (element) @@ -11128,6 +11218,6 @@ Misc.tests.cpp: Misc.tests.cpp:: PASSED: =============================================================================== -test cases: 258 | 176 passed | 78 failed | 4 failed as expected -assertions: 1436 | 1276 passed | 139 failed | 21 failed as expected +test cases: 260 | 177 passed | 79 failed | 4 failed as expected +assertions: 1445 | 1283 passed | 141 failed | 21 failed as expected diff --git a/projects/SelfTest/Baselines/junit.sw.approved.txt b/projects/SelfTest/Baselines/junit.sw.approved.txt index 43c78d7e..af4ebe07 100644 --- a/projects/SelfTest/Baselines/junit.sw.approved.txt +++ b/projects/SelfTest/Baselines/junit.sw.approved.txt @@ -1,7 +1,7 @@ - + @@ -741,6 +741,20 @@ Exception.tests.cpp: + + + + + + +Matchers.tests.cpp: + + + + +Matchers.tests.cpp: + + diff --git a/projects/SelfTest/Baselines/xml.sw.approved.txt b/projects/SelfTest/Baselines/xml.sw.approved.txt index b526a5f7..d053b408 100644 --- a/projects/SelfTest/Baselines/xml.sw.approved.txt +++ b/projects/SelfTest/Baselines/xml.sw.approved.txt @@ -10713,6 +10713,111 @@ Message from section two + +
+ + + empty, Approx(empty) + + + { } is approx: { } + + + +
+
+
+ + + v1, Approx(v1) + + + { 1.0, 2.0, 3.0 } is approx: { 1.0, 2.0, 3.0 } + + + +
+ +
+
+
+ + + v1, !Approx(temp) + + + { 1.0, 2.0, 3.0 } not is approx: { 1.0, 2.0, 3.0, 4.0 } + + + +
+ +
+
+
+ + + v1, !Approx(v2) + + + { 1.0, 2.0, 3.0 } not is approx: { 1.5, 2.5, 3.5 } + + + + + v1, Approx(v2).margin(0.5) + + + { 1.0, 2.0, 3.0 } is approx: { 1.5, 2.5, 3.5 } + + + + + v1, Approx(v2).epsilon(0.5) + + + { 1.0, 2.0, 3.0 } is approx: { 1.5, 2.5, 3.5 } + + + + + v1, Approx(v2).epsilon(0.1).scale(500) + + + { 1.0, 2.0, 3.0 } is approx: { 1.5, 2.5, 3.5 } + + + +
+ +
+ +
+ +
+ + + empty, Approx(t1) + + + { } is approx: { 1.0, 2.0 } + + + +
+
+ + + v1, Approx(v2) + + + { 2.0, 4.0, 6.0 } is approx: { 1.0, 3.0, 5.0 } + + + +
+ +
@@ -13399,7 +13504,7 @@ loose text artifact
- + - + diff --git a/projects/SelfTest/UsageTests/Matchers.tests.cpp b/projects/SelfTest/UsageTests/Matchers.tests.cpp index f07481d4..d741e57c 100644 --- a/projects/SelfTest/UsageTests/Matchers.tests.cpp +++ b/projects/SelfTest/UsageTests/Matchers.tests.cpp @@ -213,6 +213,16 @@ namespace { namespace MatchersTests { v2.push_back(1); v2.push_back(2); + std::vector v3; + v3.push_back(1); + v3.push_back(2); + v3.push_back(3); + + std::vector v4; + v4.push_back(1 + 1e-8); + v4.push_back(2 + 1e-8); + v4.push_back(3 + 1e-8); + std::vector empty; SECTION("Contains (element)") { @@ -265,6 +275,16 @@ namespace { namespace MatchersTests { v2.push_back(1); v2.push_back(2); + std::vector v3; + v3.push_back(1); + v3.push_back(2); + v3.push_back(3); + + std::vector v4; + v4.push_back(1.1); + v4.push_back(2.1); + v4.push_back(3.1); + std::vector empty; SECTION("Contains (element)") { @@ -436,6 +456,44 @@ namespace { namespace MatchersTests { REQUIRE_THAT("foo", Predicate([] (const char* const&) { return true; })); } + TEST_CASE("Vector Approx matcher", "[matchers][approx][vector]") { + using Catch::Matchers::Approx; + SECTION("Empty vector is roughly equal to an empty vector") { + std::vector empty; + REQUIRE_THAT(empty, Approx(empty)); + } + SECTION("Vectors with elements") { + std::vector v1({1., 2., 3.}); + SECTION("A vector is approx equal to itself") { + REQUIRE_THAT(v1, Approx(v1)); + } + std::vector v2({1.5, 2.5, 3.5}); + SECTION("Different length") { + auto temp(v1); + temp.push_back(4); + REQUIRE_THAT(v1, !Approx(temp)); + } + SECTION("Same length, different elements") { + REQUIRE_THAT(v1, !Approx(v2)); + REQUIRE_THAT(v1, Approx(v2).margin(0.5)); + REQUIRE_THAT(v1, Approx(v2).epsilon(0.5)); + REQUIRE_THAT(v1, Approx(v2).epsilon(0.1).scale(500)); + } + } + } + + TEST_CASE("Vector Approx matcher -- failing", "[matchers][approx][vector][.failing]") { + using Catch::Matchers::Approx; + SECTION("Empty and non empty vectors are not approx equal") { + std::vector empty, t1({1, 2}); + CHECK_THAT(empty, Approx(t1)); + } + SECTION("Just different vectors") { + std::vector v1({2., 4., 6.}), v2({1., 3., 5.}); + CHECK_THAT(v1, Approx(v2)); + } + } + } } // namespace MatchersTests #endif // CATCH_CONFIG_DISABLE_MATCHERS