mirror of
				https://github.com/catchorg/Catch2.git
				synced 2025-10-31 12:17:11 +01:00 
			
		
		
		
	Add float/int literal for Approx
This commit is contained in:
		 Henry Schreiner
					Henry Schreiner
				
			
				
					committed by
					
						 Henry Fredrick Schreiner
						Henry Fredrick Schreiner
					
				
			
			
				
	
			
			
			 Henry Fredrick Schreiner
						Henry Fredrick Schreiner
					
				
			
						parent
						
							df0b0e64e1
						
					
				
				
					commit
					77230e69ad
				
			| @@ -25,6 +25,14 @@ namespace Detail { | |||||||
|  |  | ||||||
|         static Approx custom(); |         static Approx custom(); | ||||||
|  |  | ||||||
|  |         Approx operator-() const { | ||||||
|  |             Approx approx( -static_cast<double>(m_value) ); | ||||||
|  |             approx.epsilon( m_epsilon ); | ||||||
|  |             approx.margin( m_margin ); | ||||||
|  |             approx.scale( m_scale ); | ||||||
|  |             return approx; | ||||||
|  |         } | ||||||
|  |  | ||||||
|         template <typename T, typename = typename std::enable_if<std::is_constructible<double, T>::value>::type> |         template <typename T, typename = typename std::enable_if<std::is_constructible<double, T>::value>::type> | ||||||
|         Approx operator()( T const& value ) { |         Approx operator()( T const& value ) { | ||||||
|             Approx approx( static_cast<double>(value) ); |             Approx approx( static_cast<double>(value) ); | ||||||
| @@ -121,7 +129,17 @@ namespace Detail { | |||||||
|         double m_scale; |         double m_scale; | ||||||
|         double m_value; |         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<> | template<> | ||||||
| struct StringMaker<Catch::Detail::Approx> { | struct StringMaker<Catch::Detail::Approx> { | ||||||
|   | |||||||
| @@ -60,6 +60,11 @@ Class.tests.cpp:<line number>: failed: s == "world" for: "hello" == "world" | |||||||
| Class.tests.cpp:<line number>: passed: s == "hello" for: "hello" == "hello" | Class.tests.cpp:<line number>: passed: s == "hello" for: "hello" == "hello" | ||||||
| Class.tests.cpp:<line number>: failed: m_a == 2 for: 1 == 2 | Class.tests.cpp:<line number>: failed: m_a == 2 for: 1 == 2 | ||||||
| Class.tests.cpp:<line number>: passed: m_a == 1 for: 1 == 1 | Class.tests.cpp:<line number>: passed: m_a == 1 for: 1 == 1 | ||||||
|  | Approx.tests.cpp:<line number>: passed: d == 1.23_a for: 1.23 == Approx( 1.23 ) | ||||||
|  | Approx.tests.cpp:<line number>: passed: d != 1.22_a for: 1.23 != Approx( 1.22 ) | ||||||
|  | Approx.tests.cpp:<line number>: passed: -d == -1.23_a for: -1.23 == Approx( -1.23 ) | ||||||
|  | Approx.tests.cpp:<line number>: passed: d == 1.2_a .epsilon(.1) for: 1.23 == Approx( 1.2 ) | ||||||
|  | Approx.tests.cpp:<line number>: passed: d != 1.2_a .epsilon(.001) for: 1.23 != Approx( 1.2 ) | ||||||
| Misc.tests.cpp:<line number>: passed: with 1 message: 'that's not flying - that's failing in style' | Misc.tests.cpp:<line number>: passed: with 1 message: 'that's not flying - that's failing in style' | ||||||
| Misc.tests.cpp:<line number>: failed: explicitly with 1 message: 'to infinity and beyond' | Misc.tests.cpp:<line number>: failed: explicitly with 1 message: 'to infinity and beyond' | ||||||
| Tricky.tests.cpp:<line number>: failed: &o1 == &o2 for: 0x<hex digits> == 0x<hex digits> | Tricky.tests.cpp:<line number>: failed: &o1 == &o2 for: 0x<hex digits> == 0x<hex digits> | ||||||
| @@ -614,6 +619,8 @@ A string sent to stderr via clog | |||||||
| Approx.tests.cpp:<line number>: passed: d == Approx( 1.23 ) for: 1.23 == Approx( 1.23 ) | Approx.tests.cpp:<line number>: passed: d == Approx( 1.23 ) for: 1.23 == Approx( 1.23 ) | ||||||
| Approx.tests.cpp:<line number>: passed: d != Approx( 1.22 ) for: 1.23 != Approx( 1.22 ) | Approx.tests.cpp:<line number>: passed: d != Approx( 1.22 ) for: 1.23 != Approx( 1.22 ) | ||||||
| Approx.tests.cpp:<line number>: passed: d != Approx( 1.24 ) for: 1.23 != Approx( 1.24 ) | Approx.tests.cpp:<line number>: passed: d != Approx( 1.24 ) for: 1.23 != Approx( 1.24 ) | ||||||
|  | Approx.tests.cpp:<line number>: passed: d == 1.23_a for: 1.23 == Approx( 1.23 ) | ||||||
|  | Approx.tests.cpp:<line number>: passed: d != 1.22_a for: 1.23 != Approx( 1.22 ) | ||||||
| Approx.tests.cpp:<line number>: passed: Approx( d ) == 1.23 for: Approx( 1.23 ) == 1.23 | Approx.tests.cpp:<line number>: passed: Approx( d ) == 1.23 for: Approx( 1.23 ) == 1.23 | ||||||
| Approx.tests.cpp:<line number>: passed: Approx( d ) != 1.22 for: Approx( 1.23 ) != 1.22 | Approx.tests.cpp:<line number>: passed: Approx( d ) != 1.22 for: Approx( 1.23 ) != 1.22 | ||||||
| Approx.tests.cpp:<line number>: passed: Approx( d ) != 1.24 for: Approx( 1.23 ) != 1.24 | Approx.tests.cpp:<line number>: passed: Approx( d ) != 1.24 for: Approx( 1.23 ) != 1.24 | ||||||
|   | |||||||
| @@ -1084,6 +1084,6 @@ due to unexpected exception with message: | |||||||
|   Why would you throw a std::string? |   Why would you throw a std::string? | ||||||
|  |  | ||||||
| =============================================================================== | =============================================================================== | ||||||
| test cases:  207 | 154 passed |  49 failed |  4 failed as expected | test cases:  208 | 155 passed |  49 failed |  4 failed as expected | ||||||
| assertions: 1064 | 936 passed | 107 failed | 21 failed as expected | assertions: 1071 | 943 passed | 107 failed | 21 failed as expected | ||||||
|  |  | ||||||
|   | |||||||
| @@ -535,6 +535,42 @@ PASSED: | |||||||
| with expansion: | with expansion: | ||||||
|   1 == 1 |   1 == 1 | ||||||
|  |  | ||||||
|  | ------------------------------------------------------------------------------- | ||||||
|  | A comparison that uses literals instead of the normal constructor | ||||||
|  | ------------------------------------------------------------------------------- | ||||||
|  | Approx.tests.cpp:<line number> | ||||||
|  | ............................................................................... | ||||||
|  |  | ||||||
|  | Approx.tests.cpp:<line number>: | ||||||
|  | PASSED: | ||||||
|  |   REQUIRE( d == 1.23_a ) | ||||||
|  | with expansion: | ||||||
|  |   1.23 == Approx( 1.23 ) | ||||||
|  |  | ||||||
|  | Approx.tests.cpp:<line number>: | ||||||
|  | PASSED: | ||||||
|  |   REQUIRE( d != 1.22_a ) | ||||||
|  | with expansion: | ||||||
|  |   1.23 != Approx( 1.22 ) | ||||||
|  |  | ||||||
|  | Approx.tests.cpp:<line number>: | ||||||
|  | PASSED: | ||||||
|  |   REQUIRE( -d == -1.23_a ) | ||||||
|  | with expansion: | ||||||
|  |   -1.23 == Approx( -1.23 ) | ||||||
|  |  | ||||||
|  | Approx.tests.cpp:<line number>: | ||||||
|  | PASSED: | ||||||
|  |   REQUIRE( d == 1.2_a .epsilon(.1) ) | ||||||
|  | with expansion: | ||||||
|  |   1.23 == Approx( 1.2 ) | ||||||
|  |  | ||||||
|  | Approx.tests.cpp:<line number>: | ||||||
|  | PASSED: | ||||||
|  |   REQUIRE( d != 1.2_a .epsilon(.001) ) | ||||||
|  | with expansion: | ||||||
|  |   1.23 != Approx( 1.2 ) | ||||||
|  |  | ||||||
| ------------------------------------------------------------------------------- | ------------------------------------------------------------------------------- | ||||||
| A couple of nested sections followed by a failure | A couple of nested sections followed by a failure | ||||||
|   Outer |   Outer | ||||||
| @@ -4872,6 +4908,18 @@ PASSED: | |||||||
| with expansion: | with expansion: | ||||||
|   1.23 != Approx( 1.24 ) |   1.23 != Approx( 1.24 ) | ||||||
|  |  | ||||||
|  | Approx.tests.cpp:<line number>: | ||||||
|  | PASSED: | ||||||
|  |   REQUIRE( d == 1.23_a ) | ||||||
|  | with expansion: | ||||||
|  |   1.23 == Approx( 1.23 ) | ||||||
|  |  | ||||||
|  | Approx.tests.cpp:<line number>: | ||||||
|  | PASSED: | ||||||
|  |   REQUIRE( d != 1.22_a ) | ||||||
|  | with expansion: | ||||||
|  |   1.23 != Approx( 1.22 ) | ||||||
|  |  | ||||||
| Approx.tests.cpp:<line number>: | Approx.tests.cpp:<line number>: | ||||||
| PASSED: | PASSED: | ||||||
|   REQUIRE( Approx( d ) == 1.23 ) |   REQUIRE( Approx( d ) == 1.23 ) | ||||||
| @@ -8978,6 +9026,6 @@ Misc.tests.cpp:<line number>: | |||||||
| PASSED: | PASSED: | ||||||
|  |  | ||||||
| =============================================================================== | =============================================================================== | ||||||
| test cases:  207 | 141 passed |  62 failed |  4 failed as expected | test cases:  208 | 142 passed |  62 failed |  4 failed as expected | ||||||
| assertions: 1078 | 936 passed | 121 failed | 21 failed as expected | assertions: 1085 | 943 passed | 121 failed | 21 failed as expected | ||||||
|  |  | ||||||
|   | |||||||
| @@ -1,7 +1,7 @@ | |||||||
| <?xml version="1.0" encoding="UTF-8"?> | <?xml version="1.0" encoding="UTF-8"?> | ||||||
| <testsuitesloose text artifact | <testsuitesloose text artifact | ||||||
| > | > | ||||||
|   <testsuite name="<exe-name>" errors="17" failures="105" tests="1079" hostname="tbd" time="{duration}" timestamp="{iso8601-timestamp}"> |   <testsuite name="<exe-name>" errors="17" failures="105" tests="1086" 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="# 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="#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}"/> |     <testcase classname="<exe-name>.global" name="#1027" time="{duration}"/> | ||||||
| @@ -81,6 +81,7 @@ Class.tests.cpp:<line number> | |||||||
|       </failure> |       </failure> | ||||||
|     </testcase> |     </testcase> | ||||||
|     <testcase classname="<exe-name>.Fixture" name="A TEST_CASE_METHOD based test run that succeeds" time="{duration}"/> |     <testcase classname="<exe-name>.Fixture" name="A TEST_CASE_METHOD based test run that succeeds" time="{duration}"/> | ||||||
|  |     <testcase classname="<exe-name>.global" name="A comparison that uses literals instead of the normal constructor" time="{duration}"/> | ||||||
|     <testcase classname="<exe-name>.global" name="A couple of nested sections followed by a failure" time="{duration}"> |     <testcase classname="<exe-name>.global" name="A couple of nested sections followed by a failure" time="{duration}"> | ||||||
|       <failure type="FAIL"> |       <failure type="FAIL"> | ||||||
| to infinity and beyond | to infinity and beyond | ||||||
|   | |||||||
| @@ -554,6 +554,49 @@ | |||||||
|       </Expression> |       </Expression> | ||||||
|       <OverallResult success="true"/> |       <OverallResult success="true"/> | ||||||
|     </TestCase> |     </TestCase> | ||||||
|  |     <TestCase name="A comparison that uses literals instead of the normal constructor" tags="[Approx]" filename="projects/<exe-name>/UsageTests/Approx.tests.cpp" > | ||||||
|  |       <Expression success="true" type="REQUIRE" filename="projects/<exe-name>/UsageTests/Approx.tests.cpp" > | ||||||
|  |         <Original> | ||||||
|  |           d == 1.23_a | ||||||
|  |         </Original> | ||||||
|  |         <Expanded> | ||||||
|  |           1.23 == Approx( 1.23 ) | ||||||
|  |         </Expanded> | ||||||
|  |       </Expression> | ||||||
|  |       <Expression success="true" type="REQUIRE" filename="projects/<exe-name>/UsageTests/Approx.tests.cpp" > | ||||||
|  |         <Original> | ||||||
|  |           d != 1.22_a | ||||||
|  |         </Original> | ||||||
|  |         <Expanded> | ||||||
|  |           1.23 != Approx( 1.22 ) | ||||||
|  |         </Expanded> | ||||||
|  |       </Expression> | ||||||
|  |       <Expression success="true" type="REQUIRE" filename="projects/<exe-name>/UsageTests/Approx.tests.cpp" > | ||||||
|  |         <Original> | ||||||
|  |           -d == -1.23_a | ||||||
|  |         </Original> | ||||||
|  |         <Expanded> | ||||||
|  |           -1.23 == Approx( -1.23 ) | ||||||
|  |         </Expanded> | ||||||
|  |       </Expression> | ||||||
|  |       <Expression success="true" type="REQUIRE" filename="projects/<exe-name>/UsageTests/Approx.tests.cpp" > | ||||||
|  |         <Original> | ||||||
|  |           d == 1.2_a .epsilon(.1) | ||||||
|  |         </Original> | ||||||
|  |         <Expanded> | ||||||
|  |           1.23 == Approx( 1.2 ) | ||||||
|  |         </Expanded> | ||||||
|  |       </Expression> | ||||||
|  |       <Expression success="true" type="REQUIRE" filename="projects/<exe-name>/UsageTests/Approx.tests.cpp" > | ||||||
|  |         <Original> | ||||||
|  |           d != 1.2_a .epsilon(.001) | ||||||
|  |         </Original> | ||||||
|  |         <Expanded> | ||||||
|  |           1.23 != Approx( 1.2 ) | ||||||
|  |         </Expanded> | ||||||
|  |       </Expression> | ||||||
|  |       <OverallResult success="true"/> | ||||||
|  |     </TestCase> | ||||||
|     <TestCase name="A couple of nested sections followed by a failure" tags="[.][failing]" filename="projects/<exe-name>/UsageTests/Misc.tests.cpp" > |     <TestCase name="A couple of nested sections followed by a failure" tags="[.][failing]" filename="projects/<exe-name>/UsageTests/Misc.tests.cpp" > | ||||||
|       <Section name="Outer" filename="projects/<exe-name>/UsageTests/Misc.tests.cpp" > |       <Section name="Outer" filename="projects/<exe-name>/UsageTests/Misc.tests.cpp" > | ||||||
|         <Section name="Inner" filename="projects/<exe-name>/UsageTests/Misc.tests.cpp" > |         <Section name="Inner" filename="projects/<exe-name>/UsageTests/Misc.tests.cpp" > | ||||||
| @@ -5591,6 +5634,22 @@ A string sent to stderr via clog | |||||||
|           1.23 != Approx( 1.24 ) |           1.23 != Approx( 1.24 ) | ||||||
|         </Expanded> |         </Expanded> | ||||||
|       </Expression> |       </Expression> | ||||||
|  |       <Expression success="true" type="REQUIRE" filename="projects/<exe-name>/UsageTests/Approx.tests.cpp" > | ||||||
|  |         <Original> | ||||||
|  |           d == 1.23_a | ||||||
|  |         </Original> | ||||||
|  |         <Expanded> | ||||||
|  |           1.23 == Approx( 1.23 ) | ||||||
|  |         </Expanded> | ||||||
|  |       </Expression> | ||||||
|  |       <Expression success="true" type="REQUIRE" filename="projects/<exe-name>/UsageTests/Approx.tests.cpp" > | ||||||
|  |         <Original> | ||||||
|  |           d != 1.22_a | ||||||
|  |         </Original> | ||||||
|  |         <Expanded> | ||||||
|  |           1.23 != Approx( 1.22 ) | ||||||
|  |         </Expanded> | ||||||
|  |       </Expression> | ||||||
|       <Expression success="true" type="REQUIRE" filename="projects/<exe-name>/UsageTests/Approx.tests.cpp" > |       <Expression success="true" type="REQUIRE" filename="projects/<exe-name>/UsageTests/Approx.tests.cpp" > | ||||||
|         <Original> |         <Original> | ||||||
|           Approx( d ) == 1.23 |           Approx( d ) == 1.23 | ||||||
| @@ -9923,7 +9982,7 @@ loose text artifact | |||||||
|       </Section> |       </Section> | ||||||
|       <OverallResult success="true"/> |       <OverallResult success="true"/> | ||||||
|     </TestCase> |     </TestCase> | ||||||
|     <OverallResults successes="936" failures="122" expectedFailures="21"/> |     <OverallResults successes="943" failures="122" expectedFailures="21"/> | ||||||
|   </Group> |   </Group> | ||||||
|   <OverallResults successes="936" failures="121" expectedFailures="21"/> |   <OverallResults successes="943" failures="121" expectedFailures="21"/> | ||||||
| </Catch> | </Catch> | ||||||
|   | |||||||
| @@ -33,8 +33,20 @@ namespace { namespace ApproxTests { | |||||||
|  |  | ||||||
| #endif | #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]" ) { | TEST_CASE( "Some simple comparisons between doubles", "[Approx]" ) { | ||||||
|     double d = 1.23; |     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.22 ) ); | ||||||
|     REQUIRE( d != Approx( 1.24 ) ); |     REQUIRE( d != Approx( 1.24 ) ); | ||||||
|  |  | ||||||
|  |     REQUIRE( d == 1.23_a ); | ||||||
|  |     REQUIRE( d != 1.22_a ); | ||||||
|  |  | ||||||
|     REQUIRE( Approx( d ) == 1.23 ); |     REQUIRE( Approx( d ) == 1.23 ); | ||||||
|     REQUIRE( Approx( d ) != 1.22 ); |     REQUIRE( Approx( d ) != 1.22 ); | ||||||
|     REQUIRE( Approx( d ) != 1.24 ); |     REQUIRE( Approx( d ) != 1.24 ); | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user