summaryrefslogtreecommitdiff
path: root/include/cppunit
diff options
context:
space:
mode:
authorBaptiste Lepilleur <gaiacrtn@free.fr>2004-11-06 08:05:45 +0000
committerBaptiste Lepilleur <gaiacrtn@free.fr>2004-11-06 08:05:45 +0000
commit9bad41893a55777868a84125fae10aa491cb7abc (patch)
treedf52ef5f4345f5deff5ccdb31f4bd15b2d0b2781 /include/cppunit
parente298c653900aa83d02c50b6edd541668ba5002f0 (diff)
downloadcppunit-9bad41893a55777868a84125fae10aa491cb7abc.tar.gz
include/cppunit/TestAssert.h: integrated Neil Ferguson patch for high
precision conversion to string for double number. Modified the patch to works even if DBL_DIG C99 macro is not defined.
Diffstat (limited to 'include/cppunit')
-rw-r--r--include/cppunit/TestAssert.h32
1 files changed, 32 insertions, 0 deletions
diff --git a/include/cppunit/TestAssert.h b/include/cppunit/TestAssert.h
index 3490b40..8348a1b 100644
--- a/include/cppunit/TestAssert.h
+++ b/include/cppunit/TestAssert.h
@@ -6,6 +6,8 @@
#include <cppunit/Asserter.h>
#include <cppunit/portability/Stream.h>
+#include <float.h> // For struct assertion_traits<double>
+
CPPUNIT_NS_BEGIN
@@ -50,6 +52,36 @@ struct assertion_traits
};
+/*! \brief Traits used by CPPUNIT_ASSERT_DOUBLES_EQUAL().
+ *
+ * This specialisation from @c struct @c assertion_traits<> ensures that
+ * doubles are converted in full, instead of being rounded to the default
+ * 6 digits of precision. Use the system defined ISO C99 macro DBL_DIG
+ * within float.h is available to define the maximum precision, otherwise
+ * use the hard-coded maximum precision of 15.
+ */
+template <>
+struct assertion_traits<double>
+{
+ static bool equal( double x, double y )
+ {
+ return x == y;
+ }
+
+ static std::string toString( double x )
+ {
+#ifdef DBL_DIG
+ const int precision = DBL_DIG;
+#else
+ const int precision = 15;
+#endif // #ifdef DBL_DIG
+ char buffer[128];
+ sprintf(buffer, "%.*g", DBL_DIG, x);
+ return buffer;
+ }
+};
+
+
/*! \brief (Implementation) Asserts that two objects of the same type are equals.
* Use CPPUNIT_ASSERT_EQUAL instead of this function.
* \sa assertion_traits, Asserter::failNotEqual().