From d89e74faff158a921336b759cfbb4ceae8450225 Mon Sep 17 00:00:00 2001 From: Phil Nash Date: Wed, 9 Jul 2014 18:16:40 +0100 Subject: [PATCH] =?UTF-8?q?Format=20floats=20like=20doubles=20when=20print?= =?UTF-8?q?ing=20-=20but=20add=20=E2=80=98f=E2=80=99=20suffix=20(a=20l?= =?UTF-8?q?=C3=A1=20#291)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/internal/catch_tostring.h | 1 + include/internal/catch_tostring.hpp | 12 +++++- .../Baselines/console.std.approved.txt | 16 ++++---- .../Baselines/console.sw.approved.txt | 40 +++++++++---------- .../Baselines/console.swa4.approved.txt | 20 +++++----- .../SelfTest/Baselines/junit.sw.approved.txt | 16 ++++---- .../SelfTest/Baselines/xml.sw.approved.txt | 40 +++++++++---------- 7 files changed, 77 insertions(+), 68 deletions(-) diff --git a/include/internal/catch_tostring.h b/include/internal/catch_tostring.h index 7268d156..4b468834 100644 --- a/include/internal/catch_tostring.h +++ b/include/internal/catch_tostring.h @@ -158,6 +158,7 @@ std::string toString( int value ); std::string toString( unsigned long value ); std::string toString( unsigned int value ); std::string toString( const double value ); +std::string toString( const float value ); std::string toString( bool value ); std::string toString( char value ); std::string toString( signed char value ); diff --git a/include/internal/catch_tostring.hpp b/include/internal/catch_tostring.hpp index b957dd50..825ceb55 100644 --- a/include/internal/catch_tostring.hpp +++ b/include/internal/catch_tostring.hpp @@ -103,9 +103,10 @@ std::string toString( unsigned int value ) { return toString( static_cast( value ) ); } -std::string toString( const double value ) { +template +std::string fpToString( T value, int precision ) { std::ostringstream oss; - oss << std::setprecision( 10 ) + oss << std::setprecision( precision ) << std::fixed << value; std::string d = oss.str(); @@ -118,6 +119,13 @@ std::string toString( const double value ) { return d; } +std::string toString( const double value ) { + return fpToString( value, 10 ); +} +std::string toString( const float value ) { + return fpToString( value, 5 ) + "f"; +} + std::string toString( bool value ) { return value ? "true" : "false"; } diff --git a/projects/SelfTest/Baselines/console.std.approved.txt b/projects/SelfTest/Baselines/console.std.approved.txt index f47ee3da..58a4c7eb 100644 --- a/projects/SelfTest/Baselines/console.std.approved.txt +++ b/projects/SelfTest/Baselines/console.std.approved.txt @@ -49,22 +49,22 @@ with expansion: ConditionTests.cpp:: FAILED: CHECK( data.float_nine_point_one == Approx( 9.11f ) ) with expansion: - 9.1 == Approx( 9.1099996567 ) + 9.1f == Approx( 9.1099996567 ) ConditionTests.cpp:: FAILED: CHECK( data.float_nine_point_one == Approx( 9.0f ) ) with expansion: - 9.1 == Approx( 9.0 ) + 9.1f == Approx( 9.0 ) ConditionTests.cpp:: FAILED: CHECK( data.float_nine_point_one == Approx( 1 ) ) with expansion: - 9.1 == Approx( 1.0 ) + 9.1f == Approx( 1.0 ) ConditionTests.cpp:: FAILED: CHECK( data.float_nine_point_one == Approx( 0 ) ) with expansion: - 9.1 == Approx( 0.0 ) + 9.1f == Approx( 0.0 ) ConditionTests.cpp:: FAILED: CHECK( data.double_pi == Approx( 3.1415 ) ) @@ -110,7 +110,7 @@ with expansion: ConditionTests.cpp:: FAILED: CHECK( data.float_nine_point_one != Approx( 9.1f ) ) with expansion: - 9.1 != Approx( 9.1000003815 ) + 9.1f != Approx( 9.1000003815 ) ConditionTests.cpp:: FAILED: CHECK( data.double_pi != Approx( 3.1415926535 ) ) @@ -176,17 +176,17 @@ with expansion: ConditionTests.cpp:: FAILED: CHECK( data.float_nine_point_one < 9 ) with expansion: - 9.1 < 9 + 9.1f < 9 ConditionTests.cpp:: FAILED: CHECK( data.float_nine_point_one > 10 ) with expansion: - 9.1 > 10 + 9.1f > 10 ConditionTests.cpp:: FAILED: CHECK( data.float_nine_point_one > 9.2 ) with expansion: - 9.1 > 9.2 + 9.1f > 9.2 ConditionTests.cpp:: FAILED: CHECK( data.str_hello > "hello" ) diff --git a/projects/SelfTest/Baselines/console.sw.approved.txt b/projects/SelfTest/Baselines/console.sw.approved.txt index 1b38d3d6..ae011b05 100644 --- a/projects/SelfTest/Baselines/console.sw.approved.txt +++ b/projects/SelfTest/Baselines/console.sw.approved.txt @@ -73,13 +73,13 @@ ApproxTests.cpp:: PASSED: REQUIRE( 1.23f == Approx( 1.23f ) ) with expansion: - 1.23 == Approx( 1.2300000191 ) + 1.23f == Approx( 1.2300000191 ) ApproxTests.cpp:: PASSED: REQUIRE( 0.0f == Approx( 0.0f ) ) with expansion: - 0 == Approx( 0.0 ) + 0.0f == Approx( 0.0 ) ------------------------------------------------------------------------------- Approximate comparisons with ints @@ -109,7 +109,7 @@ ApproxTests.cpp:: PASSED: REQUIRE( 1.0f == Approx( 1 ) ) with expansion: - 1 == Approx( 1.0 ) + 1.0f == Approx( 1.0 ) ApproxTests.cpp:: PASSED: @@ -127,7 +127,7 @@ ApproxTests.cpp:: PASSED: REQUIRE( 1.234f == Approx( dMedium ) ) with expansion: - 1.234 == Approx( 1.234 ) + 1.234f == Approx( 1.234 ) ApproxTests.cpp:: PASSED: @@ -269,7 +269,7 @@ ConditionTests.cpp:: PASSED: REQUIRE( data.float_nine_point_one == Approx( 9.1f ) ) with expansion: - 9.1 == Approx( 9.1000003815 ) + 9.1f == Approx( 9.1000003815 ) ConditionTests.cpp:: PASSED: @@ -325,22 +325,22 @@ with expansion: ConditionTests.cpp:: FAILED: CHECK( data.float_nine_point_one == Approx( 9.11f ) ) with expansion: - 9.1 == Approx( 9.1099996567 ) + 9.1f == Approx( 9.1099996567 ) ConditionTests.cpp:: FAILED: CHECK( data.float_nine_point_one == Approx( 9.0f ) ) with expansion: - 9.1 == Approx( 9.0 ) + 9.1f == Approx( 9.0 ) ConditionTests.cpp:: FAILED: CHECK( data.float_nine_point_one == Approx( 1 ) ) with expansion: - 9.1 == Approx( 1.0 ) + 9.1f == Approx( 1.0 ) ConditionTests.cpp:: FAILED: CHECK( data.float_nine_point_one == Approx( 0 ) ) with expansion: - 9.1 == Approx( 0.0 ) + 9.1f == Approx( 0.0 ) ConditionTests.cpp:: FAILED: CHECK( data.double_pi == Approx( 3.1415 ) ) @@ -394,25 +394,25 @@ ConditionTests.cpp:: PASSED: REQUIRE( data.float_nine_point_one != Approx( 9.11f ) ) with expansion: - 9.1 != Approx( 9.1099996567 ) + 9.1f != Approx( 9.1099996567 ) ConditionTests.cpp:: PASSED: REQUIRE( data.float_nine_point_one != Approx( 9.0f ) ) with expansion: - 9.1 != Approx( 9.0 ) + 9.1f != Approx( 9.0 ) ConditionTests.cpp:: PASSED: REQUIRE( data.float_nine_point_one != Approx( 1 ) ) with expansion: - 9.1 != Approx( 1.0 ) + 9.1f != Approx( 1.0 ) ConditionTests.cpp:: PASSED: REQUIRE( data.float_nine_point_one != Approx( 0 ) ) with expansion: - 9.1 != Approx( 0.0 ) + 9.1f != Approx( 0.0 ) ConditionTests.cpp:: PASSED: @@ -458,7 +458,7 @@ with expansion: ConditionTests.cpp:: FAILED: CHECK( data.float_nine_point_one != Approx( 9.1f ) ) with expansion: - 9.1 != Approx( 9.1000003815 ) + 9.1f != Approx( 9.1000003815 ) ConditionTests.cpp:: FAILED: CHECK( data.double_pi != Approx( 3.1415926535 ) ) @@ -533,19 +533,19 @@ ConditionTests.cpp:: PASSED: REQUIRE( data.float_nine_point_one > 9 ) with expansion: - 9.1 > 9 + 9.1f > 9 ConditionTests.cpp:: PASSED: REQUIRE( data.float_nine_point_one < 10 ) with expansion: - 9.1 < 10 + 9.1f < 10 ConditionTests.cpp:: PASSED: REQUIRE( data.float_nine_point_one < 9.2 ) with expansion: - 9.1 < 9.2 + 9.1f < 9.2 ConditionTests.cpp:: PASSED: @@ -632,17 +632,17 @@ with expansion: ConditionTests.cpp:: FAILED: CHECK( data.float_nine_point_one < 9 ) with expansion: - 9.1 < 9 + 9.1f < 9 ConditionTests.cpp:: FAILED: CHECK( data.float_nine_point_one > 10 ) with expansion: - 9.1 > 10 + 9.1f > 10 ConditionTests.cpp:: FAILED: CHECK( data.float_nine_point_one > 9.2 ) with expansion: - 9.1 > 9.2 + 9.1f > 9.2 ConditionTests.cpp:: FAILED: CHECK( data.str_hello > "hello" ) diff --git a/projects/SelfTest/Baselines/console.swa4.approved.txt b/projects/SelfTest/Baselines/console.swa4.approved.txt index 418af3e5..3432b802 100644 --- a/projects/SelfTest/Baselines/console.swa4.approved.txt +++ b/projects/SelfTest/Baselines/console.swa4.approved.txt @@ -73,13 +73,13 @@ ApproxTests.cpp:: PASSED: REQUIRE( 1.23f == Approx( 1.23f ) ) with expansion: - 1.23 == Approx( 1.2300000191 ) + 1.23f == Approx( 1.2300000191 ) ApproxTests.cpp:: PASSED: REQUIRE( 0.0f == Approx( 0.0f ) ) with expansion: - 0 == Approx( 0.0 ) + 0.0f == Approx( 0.0 ) ------------------------------------------------------------------------------- Approximate comparisons with ints @@ -109,7 +109,7 @@ ApproxTests.cpp:: PASSED: REQUIRE( 1.0f == Approx( 1 ) ) with expansion: - 1 == Approx( 1.0 ) + 1.0f == Approx( 1.0 ) ApproxTests.cpp:: PASSED: @@ -127,7 +127,7 @@ ApproxTests.cpp:: PASSED: REQUIRE( 1.234f == Approx( dMedium ) ) with expansion: - 1.234 == Approx( 1.234 ) + 1.234f == Approx( 1.234 ) ApproxTests.cpp:: PASSED: @@ -269,7 +269,7 @@ ConditionTests.cpp:: PASSED: REQUIRE( data.float_nine_point_one == Approx( 9.1f ) ) with expansion: - 9.1 == Approx( 9.1000003815 ) + 9.1f == Approx( 9.1000003815 ) ConditionTests.cpp:: PASSED: @@ -339,25 +339,25 @@ ConditionTests.cpp:: PASSED: REQUIRE( data.float_nine_point_one != Approx( 9.11f ) ) with expansion: - 9.1 != Approx( 9.1099996567 ) + 9.1f != Approx( 9.1099996567 ) ConditionTests.cpp:: PASSED: REQUIRE( data.float_nine_point_one != Approx( 9.0f ) ) with expansion: - 9.1 != Approx( 9.0 ) + 9.1f != Approx( 9.0 ) ConditionTests.cpp:: PASSED: REQUIRE( data.float_nine_point_one != Approx( 1 ) ) with expansion: - 9.1 != Approx( 1.0 ) + 9.1f != Approx( 1.0 ) ConditionTests.cpp:: PASSED: REQUIRE( data.float_nine_point_one != Approx( 0 ) ) with expansion: - 9.1 != Approx( 0.0 ) + 9.1f != Approx( 0.0 ) ConditionTests.cpp:: PASSED: @@ -403,7 +403,7 @@ with expansion: ConditionTests.cpp:: FAILED: CHECK( data.float_nine_point_one != Approx( 9.1f ) ) with expansion: - 9.1 != Approx( 9.1000003815 ) + 9.1f != Approx( 9.1000003815 ) =============================================================================== test cases: 11 passed | 3 failed | 1 failed as expected | total: 15 diff --git a/projects/SelfTest/Baselines/junit.sw.approved.txt b/projects/SelfTest/Baselines/junit.sw.approved.txt index 5ccdb502..b6271d0f 100644 --- a/projects/SelfTest/Baselines/junit.sw.approved.txt +++ b/projects/SelfTest/Baselines/junit.sw.approved.txt @@ -30,16 +30,16 @@ ConditionTests.cpp: ConditionTests.cpp: - + ConditionTests.cpp: - + ConditionTests.cpp: - + ConditionTests.cpp: - + ConditionTests.cpp: @@ -66,7 +66,7 @@ ConditionTests.cpp: ConditionTests.cpp: - + ConditionTests.cpp: @@ -105,13 +105,13 @@ ConditionTests.cpp: ConditionTests.cpp: - + ConditionTests.cpp: - + ConditionTests.cpp: - + ConditionTests.cpp: diff --git a/projects/SelfTest/Baselines/xml.sw.approved.txt b/projects/SelfTest/Baselines/xml.sw.approved.txt index 8c84d912..7de2eabb 100644 --- a/projects/SelfTest/Baselines/xml.sw.approved.txt +++ b/projects/SelfTest/Baselines/xml.sw.approved.txt @@ -76,7 +76,7 @@ 1.23f == Approx( 1.23f ) - 1.23 == Approx( 1.2300000191 ) + 1.23f == Approx( 1.2300000191 ) @@ -84,7 +84,7 @@ 0.0f == Approx( 0.0f ) - 0 == Approx( 0.0 ) + 0.0f == Approx( 0.0 ) @@ -114,7 +114,7 @@ 1.0f == Approx( 1 ) - 1 == Approx( 1.0 ) + 1.0f == Approx( 1.0 ) @@ -138,7 +138,7 @@ 1.234f == Approx( dMedium ) - 1.234 == Approx( 1.234 ) + 1.234f == Approx( 1.234 ) @@ -295,7 +295,7 @@ data.float_nine_point_one == Approx( 9.1f ) - 9.1 == Approx( 9.1000003815 ) + 9.1f == Approx( 9.1000003815 ) @@ -370,7 +370,7 @@ data.float_nine_point_one == Approx( 9.11f ) - 9.1 == Approx( 9.1099996567 ) + 9.1f == Approx( 9.1099996567 ) @@ -378,7 +378,7 @@ data.float_nine_point_one == Approx( 9.0f ) - 9.1 == Approx( 9.0 ) + 9.1f == Approx( 9.0 ) @@ -386,7 +386,7 @@ data.float_nine_point_one == Approx( 1 ) - 9.1 == Approx( 1.0 ) + 9.1f == Approx( 1.0 ) @@ -394,7 +394,7 @@ data.float_nine_point_one == Approx( 0 ) - 9.1 == Approx( 0.0 ) + 9.1f == Approx( 0.0 ) @@ -469,7 +469,7 @@ data.float_nine_point_one != Approx( 9.11f ) - 9.1 != Approx( 9.1099996567 ) + 9.1f != Approx( 9.1099996567 ) @@ -477,7 +477,7 @@ data.float_nine_point_one != Approx( 9.0f ) - 9.1 != Approx( 9.0 ) + 9.1f != Approx( 9.0 ) @@ -485,7 +485,7 @@ data.float_nine_point_one != Approx( 1 ) - 9.1 != Approx( 1.0 ) + 9.1f != Approx( 1.0 ) @@ -493,7 +493,7 @@ data.float_nine_point_one != Approx( 0 ) - 9.1 != Approx( 0.0 ) + 9.1f != Approx( 0.0 ) @@ -552,7 +552,7 @@ data.float_nine_point_one != Approx( 9.1f ) - 9.1 != Approx( 9.1000003815 ) + 9.1f != Approx( 9.1000003815 ) @@ -651,7 +651,7 @@ data.float_nine_point_one > 9 - 9.1 > 9 + 9.1f > 9 @@ -659,7 +659,7 @@ data.float_nine_point_one < 10 - 9.1 < 10 + 9.1f < 10 @@ -667,7 +667,7 @@ data.float_nine_point_one < 9.2 - 9.1 < 9.2 + 9.1f < 9.2 @@ -790,7 +790,7 @@ data.float_nine_point_one < 9 - 9.1 < 9 + 9.1f < 9 @@ -798,7 +798,7 @@ data.float_nine_point_one > 10 - 9.1 > 10 + 9.1f > 10 @@ -806,7 +806,7 @@ data.float_nine_point_one > 9.2 - 9.1 > 9.2 + 9.1f > 9.2