From 0d30a2aec28085cfb9fe359c321c289609b884ca Mon Sep 17 00:00:00 2001 From: Baptiste Lepilleur Date: Sat, 24 Feb 2007 20:13:04 +0000 Subject: 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 . * 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(). --- include/cppunit/TestAssert.h | 9 ++++++ include/cppunit/config/config-evc4.h | 5 +++ include/cppunit/config/config-msvc6.h | 5 +++ include/cppunit/portability/FloatingPoint.h | 50 +++++++++++++++++++++++++++++ include/cppunit/portability/Makefile.am | 11 ++++--- 5 files changed, 75 insertions(+), 5 deletions(-) create mode 100644 include/cppunit/portability/FloatingPoint.h (limited to 'include/cppunit') diff --git a/include/cppunit/TestAssert.h b/include/cppunit/TestAssert.h index 07783a2..f74797b 100644 --- a/include/cppunit/TestAssert.h +++ b/include/cppunit/TestAssert.h @@ -109,6 +109,7 @@ void assertEquals( const T& expected, /*! \brief (Implementation) Asserts that two double are equals given a tolerance. * Use CPPUNIT_ASSERT_DOUBLES_EQUAL instead of this function. * \sa Asserter::failNotEqual(). + * \sa CPPUNIT_ASSERT_DOUBLES_EQUAL for detailed semantic of the assertion. */ void CPPUNIT_API assertDoubleEquals( double expected, double actual, @@ -218,6 +219,13 @@ void CPPUNIT_API assertDoubleEquals( double expected, /*! \brief Macro for primitive double value comparisons. * \ingroup Assertions + * + * The assertion pass if both expected and actual are finite and + * \c fabs( \c expected - \c actual ) <= \c delta. + * If either \c expected or actual are infinite (+/- inf), the + * assertion pass if \c expected == \c actual. + * If either \c expected or \c actual is a NaN (not a number), then + * the assertion fails. */ #define CPPUNIT_ASSERT_DOUBLES_EQUAL(expected,actual,delta) \ ( CPPUNIT_NS::assertDoubleEquals( (expected), \ @@ -230,6 +238,7 @@ void CPPUNIT_API assertDoubleEquals( double expected, /*! \brief Macro for primitive double value comparisons, setting a * user-supplied message in case of failure. * \ingroup Assertions + * \sa CPPUNIT_ASSERT_DOUBLES_EQUAL for detailed semantic of the assertion. */ #define CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(message,expected,actual,delta) \ ( CPPUNIT_NS::assertDoubleEquals( (expected), \ diff --git a/include/cppunit/config/config-evc4.h b/include/cppunit/config/config-evc4.h index cb141b0..a791698 100644 --- a/include/cppunit/config/config-evc4.h +++ b/include/cppunit/config/config-evc4.h @@ -60,6 +60,11 @@ # define CPPUNIT_COMPILER_LOCATION_FORMAT "%p(%l):" #endif +/* define to 1 if the compiler has _finite() */ +#ifndef CPPUNIT_HAVE__FINITE +#define CPPUNIT_HAVE__FINITE 1 +#endif + // Uncomment to turn on STL wrapping => use this to test compilation. // This will make CppUnit subclass std::vector & co to provide default // parameter. diff --git a/include/cppunit/config/config-msvc6.h b/include/cppunit/config/config-msvc6.h index a1b0af2..d688171 100644 --- a/include/cppunit/config/config-msvc6.h +++ b/include/cppunit/config/config-msvc6.h @@ -64,6 +64,11 @@ // Define to 1 if the compiler support C++ style cast. #define CPPUNIT_HAVE_CPP_CAST 1 +/* define to 1 if the compiler has _finite() */ +#ifndef CPPUNIT_HAVE__FINITE +#define CPPUNIT_HAVE__FINITE 1 +#endif + // Uncomment to turn on STL wrapping => use this to test compilation. // This will make CppUnit subclass std::vector & co to provide default diff --git a/include/cppunit/portability/FloatingPoint.h b/include/cppunit/portability/FloatingPoint.h new file mode 100644 index 0000000..fa3095e --- /dev/null +++ b/include/cppunit/portability/FloatingPoint.h @@ -0,0 +1,50 @@ +#ifndef CPPUNIT_PORTABILITY_FLOATINGPOINT_H_INCLUDED +#define CPPUNIT_PORTABILITY_FLOATINGPOINT_H_INCLUDED + +#include + +CPPUNIT_NS_BEGIN + +/// \brief Tests if a floating-point is a NaN. +// According to IEEE-754 floating point standard, NaN comparison should always +// be 'false'. +// At least Microsoft Visual Studio 6 is known not to implement this test correctly. +// It emits the following code to test equality: +// fcomp qword ptr [nan] +// fnstsw ax // copie fp (floating-point) status register to ax +// test ah,40h // test bit 14 of ax (0x4000) => C3 of fp status register +// According to the following documentation on the x86 floating point status register, +// the C2 bit should be tested to test for NaN value. +// http://webster.cs.ucr.edu/AoA/Windows/HTML/RealArithmetic.html#1000117 +// In Microsoft Visual Studio 2003 & 2005, the test is implemented with: +// test ah,44h // Visual Studio 2005 test both C2 & C3... +// +// To work around this, a NaN is assumed to be detected if no strict ordering is found. +inline bool floatingPointIsUnordered( double x ) +{ + // x != x will detect a NaN on conformant platform + // (2.0 < x && x < 1.0) will detect a NaN on non conformant platform: + // => no ordering can be found for x. + return (x != x) || (2.0 < x && x < 1.0); +} + + +/// \brief Tests if a floating-point is finite. +/// @return \c true if x is neither a NaN, nor +inf, nor -inf, \c false otherwise. +inline bool floatingPointIsFinite( double x ) +{ +#if defined(CPPUNIT_HAVE_ISFINITE) + return (bool)isfinite( x ); +#elif defined(CPPUNIT_HAVE_FINITE) + return (bool)finite( x ); +#elif defined(CPPUNIT_HAVE__FINITE) + return _finite(x); +#else + double testInf = x * 0.0; // Produce 0.0 if x is finite, a NaN otherwise. + return testInf == 0.0 && !floatingPointIsUnordered(testInf); +#endif +} + +CPPUNIT_NS_END + +#endif // CPPUNIT_PORTABILITY_FLOATINGPOINT_H_INCLUDED diff --git a/include/cppunit/portability/Makefile.am b/include/cppunit/portability/Makefile.am index 3c578e3..2caeb32 100644 --- a/include/cppunit/portability/Makefile.am +++ b/include/cppunit/portability/Makefile.am @@ -1,9 +1,10 @@ libcppunitincludedir = $(includedir)/cppunit/portability libcppunitinclude_HEADERS = \ - CppUnitDeque.h \ - CppUnitMap.h \ - CppUnitSet.h \ - CppUnitStack.h \ + CppUnitDeque.h \ + CppUnitMap.h \ + CppUnitSet.h \ + CppUnitStack.h \ CppUnitVector.h \ - Stream.h + FloatingPoint.h \ + Stream.h -- cgit v1.2.1