diff options
| author | Baptiste Lepilleur <gaiacrtn@free.fr> | 2007-02-24 20:13:04 +0000 |
|---|---|---|
| committer | Baptiste Lepilleur <gaiacrtn@free.fr> | 2007-02-24 20:13:04 +0000 |
| commit | 0d30a2aec28085cfb9fe359c321c289609b884ca (patch) | |
| tree | d5848b1cb981a9fa5ba351b5eb01e4a0e06e1cf8 /src/cppunit/TestAssert.cpp | |
| parent | 3ca9c5d071cb8162c89fd514a6116ee6b450d763 (diff) | |
| download | cppunit-0d30a2aec28085cfb9fe359c321c289609b884ca.tar.gz | |
Src/cppunit/TestAssert.
src/cppunit/TestAssert.cpp (assertDoubleEquals): Moved finite & NaN
tests to include/cppunit/portability/FloatingPoint.h. Changed
implementation assertDoubleEquals to explicitly test for NaN
in case of non-finite values to force equality failure in the
presence of NaN. Previous implementation failed on Microsoft
Visual Studio 6 (on this platform: NaN == NaN).
* examples/cppunittest/TestAssertTest.cpp: Add more unit tests to
test the portable floating-point primitive. Added missing
include <limits>.
* include/cppunit/portability/Makefile.am:
* include/cppunit/portability/FloatingPoint.h: Added file. Extracted
isfinite() from TestAssert.cpp.
* include/cppunit/config-evc4:
* include/cppunit/config-msvc6: Added support for _finite().
Diffstat (limited to 'src/cppunit/TestAssert.cpp')
| -rw-r--r-- | src/cppunit/TestAssert.cpp | 33 |
1 files changed, 16 insertions, 17 deletions
diff --git a/src/cppunit/TestAssert.cpp b/src/cppunit/TestAssert.cpp index 44be55f..6e4e794 100644 --- a/src/cppunit/TestAssert.cpp +++ b/src/cppunit/TestAssert.cpp @@ -1,19 +1,5 @@ #include <cppunit/TestAssert.h> - -#include <math.h> - -#if !defined(CPPUNIT_HAVE_ISFINITE) - - static inline bool isfinite( double x ) - { -#if defined(CPPUNIT_HAVE_FINITE) - return finite( x ); -#else - return ( x * 0.0 ) == 0.0; -#endif - } - -#endif +#include <cppunit/portability/FloatingPoint.h> CPPUNIT_NS_BEGIN @@ -30,10 +16,23 @@ assertDoubleEquals( double expected, msg.addDetail( AdditionalMessage(message) ); bool equal; - if ( isfinite(expected) && isfinite(actual) ) + if ( floatingPointIsFinite(expected) && floatingPointIsFinite(actual) ) equal = fabs( expected - actual ) <= delta; else - equal = expected == actual; + { + // If expected or actual is not finite, it may be +inf, -inf or NaN (Not a Number). + // Value of +inf or -inf leads to a true equality regardless of delta if both + // expected and actual have the same value (infinity sign). + // NaN Value should always lead to a failed equality. + if ( floatingPointIsUnordered(expected) || floatingPointIsUnordered(actual) ) + { + equal = false; // expected or actual is a NaN + } + else // ordered values, +inf or -inf comparison + { + equal = expected == actual; + } + } Asserter::failNotEqualIf( !equal, assertion_traits<double>::toString(expected), |
