From 77230e69ad7ffcb58fb178bc0c4e3c2caabcbb8e Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Mon, 21 May 2018 15:42:40 +0200 Subject: [PATCH] Add float/int literal for Approx --- include/internal/catch_approx.h | 20 +++++- .../Baselines/compact.sw.approved.txt | 7 +++ .../Baselines/console.std.approved.txt | 4 +- .../Baselines/console.sw.approved.txt | 52 ++++++++++++++- .../SelfTest/Baselines/junit.sw.approved.txt | 3 +- .../SelfTest/Baselines/xml.sw.approved.txt | 63 ++++++++++++++++++- projects/SelfTest/UsageTests/Approx.tests.cpp | 15 +++++ 7 files changed, 156 insertions(+), 8 deletions(-) diff --git a/include/internal/catch_approx.h b/include/internal/catch_approx.h index 27068872..eb8dd123 100644 --- a/include/internal/catch_approx.h +++ b/include/internal/catch_approx.h @@ -25,6 +25,14 @@ namespace Detail { static Approx custom(); + Approx operator-() const { + Approx approx( -static_cast(m_value) ); + approx.epsilon( m_epsilon ); + approx.margin( m_margin ); + approx.scale( m_scale ); + return approx; + } + template ::value>::type> Approx operator()( T const& value ) { Approx approx( static_cast(value) ); @@ -121,7 +129,17 @@ namespace Detail { double m_scale; double m_value; }; -} +} // end namespace Detail + +namespace literals { + inline Detail::Approx operator "" _a(long double val) { + return Detail::Approx(val); + } + + inline Detail::Approx operator "" _a(unsigned long long val) { + return Detail::Approx(val); + } +} // end namespace literals template<> struct StringMaker { diff --git a/projects/SelfTest/Baselines/compact.sw.approved.txt b/projects/SelfTest/Baselines/compact.sw.approved.txt index 7e844449..8192303b 100644 --- a/projects/SelfTest/Baselines/compact.sw.approved.txt +++ b/projects/SelfTest/Baselines/compact.sw.approved.txt @@ -60,6 +60,11 @@ Class.tests.cpp:: failed: s == "world" for: "hello" == "world" Class.tests.cpp:: passed: s == "hello" for: "hello" == "hello" Class.tests.cpp:: failed: m_a == 2 for: 1 == 2 Class.tests.cpp:: passed: m_a == 1 for: 1 == 1 +Approx.tests.cpp:: passed: d == 1.23_a for: 1.23 == Approx( 1.23 ) +Approx.tests.cpp:: passed: d != 1.22_a for: 1.23 != Approx( 1.22 ) +Approx.tests.cpp:: passed: -d == -1.23_a for: -1.23 == Approx( -1.23 ) +Approx.tests.cpp:: passed: d == 1.2_a .epsilon(.1) for: 1.23 == Approx( 1.2 ) +Approx.tests.cpp:: passed: d != 1.2_a .epsilon(.001) for: 1.23 != Approx( 1.2 ) Misc.tests.cpp:: passed: with 1 message: 'that's not flying - that's failing in style' Misc.tests.cpp:: failed: explicitly with 1 message: 'to infinity and beyond' Tricky.tests.cpp:: failed: &o1 == &o2 for: 0x == 0x @@ -614,6 +619,8 @@ A string sent to stderr via clog Approx.tests.cpp:: passed: d == Approx( 1.23 ) for: 1.23 == Approx( 1.23 ) Approx.tests.cpp:: passed: d != Approx( 1.22 ) for: 1.23 != Approx( 1.22 ) Approx.tests.cpp:: passed: d != Approx( 1.24 ) for: 1.23 != Approx( 1.24 ) +Approx.tests.cpp:: passed: d == 1.23_a for: 1.23 == Approx( 1.23 ) +Approx.tests.cpp:: passed: d != 1.22_a for: 1.23 != Approx( 1.22 ) Approx.tests.cpp:: passed: Approx( d ) == 1.23 for: Approx( 1.23 ) == 1.23 Approx.tests.cpp:: passed: Approx( d ) != 1.22 for: Approx( 1.23 ) != 1.22 Approx.tests.cpp:: passed: Approx( d ) != 1.24 for: Approx( 1.23 ) != 1.24 diff --git a/projects/SelfTest/Baselines/console.std.approved.txt b/projects/SelfTest/Baselines/console.std.approved.txt index ff31a88a..635c1d88 100644 --- a/projects/SelfTest/Baselines/console.std.approved.txt +++ b/projects/SelfTest/Baselines/console.std.approved.txt @@ -1084,6 +1084,6 @@ due to unexpected exception with message: Why would you throw a std::string? =============================================================================== -test cases: 207 | 154 passed | 49 failed | 4 failed as expected -assertions: 1064 | 936 passed | 107 failed | 21 failed as expected +test cases: 208 | 155 passed | 49 failed | 4 failed as expected +assertions: 1071 | 943 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 a32ef10e..42a0f5c2 100644 --- a/projects/SelfTest/Baselines/console.sw.approved.txt +++ b/projects/SelfTest/Baselines/console.sw.approved.txt @@ -535,6 +535,42 @@ PASSED: with expansion: 1 == 1 +------------------------------------------------------------------------------- +A comparison that uses literals instead of the normal constructor +------------------------------------------------------------------------------- +Approx.tests.cpp: +............................................................................... + +Approx.tests.cpp:: +PASSED: + REQUIRE( d == 1.23_a ) +with expansion: + 1.23 == Approx( 1.23 ) + +Approx.tests.cpp:: +PASSED: + REQUIRE( d != 1.22_a ) +with expansion: + 1.23 != Approx( 1.22 ) + +Approx.tests.cpp:: +PASSED: + REQUIRE( -d == -1.23_a ) +with expansion: + -1.23 == Approx( -1.23 ) + +Approx.tests.cpp:: +PASSED: + REQUIRE( d == 1.2_a .epsilon(.1) ) +with expansion: + 1.23 == Approx( 1.2 ) + +Approx.tests.cpp:: +PASSED: + REQUIRE( d != 1.2_a .epsilon(.001) ) +with expansion: + 1.23 != Approx( 1.2 ) + ------------------------------------------------------------------------------- A couple of nested sections followed by a failure Outer @@ -4872,6 +4908,18 @@ PASSED: with expansion: 1.23 != Approx( 1.24 ) +Approx.tests.cpp:: +PASSED: + REQUIRE( d == 1.23_a ) +with expansion: + 1.23 == Approx( 1.23 ) + +Approx.tests.cpp:: +PASSED: + REQUIRE( d != 1.22_a ) +with expansion: + 1.23 != Approx( 1.22 ) + Approx.tests.cpp:: PASSED: REQUIRE( Approx( d ) == 1.23 ) @@ -8978,6 +9026,6 @@ Misc.tests.cpp:: PASSED: =============================================================================== -test cases: 207 | 141 passed | 62 failed | 4 failed as expected -assertions: 1078 | 936 passed | 121 failed | 21 failed as expected +test cases: 208 | 142 passed | 62 failed | 4 failed as expected +assertions: 1085 | 943 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 ffc85e6b..3ca9c02e 100644 --- a/projects/SelfTest/Baselines/junit.sw.approved.txt +++ b/projects/SelfTest/Baselines/junit.sw.approved.txt @@ -1,7 +1,7 @@ - + @@ -81,6 +81,7 @@ Class.tests.cpp: + to infinity and beyond diff --git a/projects/SelfTest/Baselines/xml.sw.approved.txt b/projects/SelfTest/Baselines/xml.sw.approved.txt index af7f468e..f9c86bc0 100644 --- a/projects/SelfTest/Baselines/xml.sw.approved.txt +++ b/projects/SelfTest/Baselines/xml.sw.approved.txt @@ -554,6 +554,49 @@ + + + + d == 1.23_a + + + 1.23 == Approx( 1.23 ) + + + + + d != 1.22_a + + + 1.23 != Approx( 1.22 ) + + + + + -d == -1.23_a + + + -1.23 == Approx( -1.23 ) + + + + + d == 1.2_a .epsilon(.1) + + + 1.23 == Approx( 1.2 ) + + + + + d != 1.2_a .epsilon(.001) + + + 1.23 != Approx( 1.2 ) + + + +
@@ -5591,6 +5634,22 @@ A string sent to stderr via clog 1.23 != Approx( 1.24 ) + + + d == 1.23_a + + + 1.23 == Approx( 1.23 ) + + + + + d != 1.22_a + + + 1.23 != Approx( 1.22 ) + + Approx( d ) == 1.23 @@ -9923,7 +9982,7 @@ loose text artifact
- + - + diff --git a/projects/SelfTest/UsageTests/Approx.tests.cpp b/projects/SelfTest/UsageTests/Approx.tests.cpp index 5930075d..ebe7983b 100644 --- a/projects/SelfTest/UsageTests/Approx.tests.cpp +++ b/projects/SelfTest/UsageTests/Approx.tests.cpp @@ -33,8 +33,20 @@ namespace { namespace ApproxTests { #endif +using namespace Catch::literals; /////////////////////////////////////////////////////////////////////////////// +TEST_CASE( "A comparison that uses literals instead of the normal constructor", "[Approx]" ) { + double d = 1.23; + + REQUIRE( d == 1.23_a ); + REQUIRE( d != 1.22_a ); + REQUIRE( -d == -1.23_a ); + + REQUIRE( d == 1.2_a .epsilon(.1)); + REQUIRE( d != 1.2_a .epsilon(.001)); +} + TEST_CASE( "Some simple comparisons between doubles", "[Approx]" ) { double d = 1.23; @@ -42,6 +54,9 @@ TEST_CASE( "Some simple comparisons between doubles", "[Approx]" ) { REQUIRE( d != Approx( 1.22 ) ); REQUIRE( d != Approx( 1.24 ) ); + REQUIRE( d == 1.23_a ); + REQUIRE( d != 1.22_a ); + REQUIRE( Approx( d ) == 1.23 ); REQUIRE( Approx( d ) != 1.22 ); REQUIRE( Approx( d ) != 1.24 );