Re: [Cppunit-devel] CPPUNIT_ASSERT_DOUBLES_EQUAL() with non-finite numbers
Brought to you by:
blep
From: CppUnit d. m. l. <cpp...@li...> - 2006-11-16 16:55:02
|
Quoting CppUnit development mailing list =20 <cpp...@li...>: > CppUnit development mailing list wrote: >> Contrary to the claim of Baptiste in the latter patch, isnan() >> is standard: it is part of C99. Hopefully we can assume it is >> portable by now. >> My patch (below) uses isfinite() instead, which is also from C99. > > C99 is non-standard in the context of C++, which is a superset of C90. > So, all C++ compilers might not support this. True, they might not; do you know of a current compiler that has =20 neither isfinite() nor the older, BSD finite()? I can revise the patch to include testing for isfinite() and finite() at configuration time. Then inside TestAssert.cpp, add code like this: #if !defined(HAS_ISFINITE) # if defined(HAS_FINITE) # define isfinite(x) finite(x) # else # define isfinite(x) 1 # endif #endif Recall that the rest of the patch is: Index: src/cppunit/TestAssert.cpp =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D RCS file: /cvsroot/cppunit/cppunit/src/cppunit/TestAssert.cpp,v retrieving revision 1.12 diff -u -b -B -r1.12 TestAssert.cpp --- src/cppunit/TestAssert.cpp 5 Nov 2004 22:47:20 -0000 1.12 +++ src/cppunit/TestAssert.cpp 11 Nov 2006 03:57:56 -0000 @@ -21,7 +21,13 @@ assertion_traits<double>::toString(delta) ); msg.addDetail( AdditionalMessage(message) ); - Asserter::failNotEqualIf( fabs( expected - actual ) > delta, + bool equal; + if ( isfinite(expected) && isfinite(actual) ) + equal =3D fabs( expected - actual ) <=3D delta; + else + equal =3D expected =3D=3D actual; + + Asserter::failNotEqualIf( !equal, assertion_traits<double>::toString(expected), assertion_traits<double>::toString(actual), sourceLine, This revision keeps systems with neither isfinite() nor finite() able to compile CppUnit, but they retain the old buggy behaviour with respect to NaN and Inf. Would that be more acceptable? Hmm. Now that I think of it, just flipping the expression from testing "is not equal" to "is equal" fixes the problem with NaNs anyway: all boolean expressions involving a NaN are false. Maybe we don't need the isfinite() test after all ... or maybe we do; I can't remember whether "Inf - Inf" evaluates to 0. -Steve |