]> granicus.if.org Git - python/commitdiff
Incorporate Skip's suggestion to use SciPy's validation test near
authorRaymond Hettinger <python@rcn.com>
Sun, 29 Dec 2002 17:59:24 +0000 (17:59 +0000)
committerRaymond Hettinger <python@rcn.com>
Sun, 29 Dec 2002 17:59:24 +0000 (17:59 +0000)
equality.  Note, there is another flavor that compares to a given
number of significant digits rather than decimal places.  If there
is a demand, that could be added at a later date.

Doc/lib/libunittest.tex
Lib/unittest.py

index b1c091838c9a8b9096afcefb0f914ecde9853a5d..2d710e89b241fc30bd3940b20866a91276fee01c 100644 (file)
@@ -501,6 +501,30 @@ report failures.
   \var{second}.
 \end{methoddesc}
 
+\begin{methoddesc}[TestCase]{assertAlmostEqual}{first, second\optional{,
+                                               places\optional{, msg}}}
+\methodline{failUnlessAlmostEqual}{first, second\optional{,
+                                               places\optional{, msg}}}
+  Test that \var{first} and \var{second} are approximately equal
+  by computing the difference, rounding to the given number of \var{places},
+  and comparing to zero.  Note that comparing a given number of decimal places
+  is not the same as comparing a given number of significant digits.
+  If the values do not compare equal, the test will fail with the explanation
+  given by \var{msg}, or \code{None}.  
+\end{methoddesc}
+
+\begin{methoddesc}[TestCase]{assertNotAlmostEqual}{first, second\optional{,
+                                               places\optional{, msg}}}
+\methodline{failIfAlmostEqual}{first, second\optional{,
+                                               places\optional{, msg}}}
+  Test that \var{first} and \var{second} are not approximately equal
+  by computing the difference, rounding to the given number of \var{places},
+  and comparing to zero.  Note that comparing a given number of decimal places
+  is not the same as comparing a given number of significant digits.
+  If the values do not compare equal, the test will fail with the explanation
+  given by \var{msg}, or \code{None}.  
+\end{methoddesc}
+
 \begin{methoddesc}[TestCase]{assertRaises}{exception, callable, \moreargs}
 \methodline{failUnlessRaises}{exception, callable, \moreargs}
   Test that an exception is raised when \var{callable} is called with
index 9100a78dbbf98d1bb28c1a2e9e8ad476d9bc3693..31d9cb4a59fa5b0e3585784cd424a7ddcb2e39b2 100644 (file)
@@ -300,10 +300,38 @@ class TestCase:
             raise self.failureException, \
                   (msg or '%s == %s' % (`first`, `second`))
 
+    def failUnlessAlmostEqual(self, first, second, places=7, msg=None):
+        """Fail if the two objects are unequal as determined by their
+           difference rounded to the given number of decimal places
+           (default 7) and comparing to zero.
+
+           Note that decimal places (from zero) is usually not the same
+           as significant digits (measured from the most signficant digit).
+        """
+        if round(second-first, places) != 0:
+            raise self.failureException, \
+                  (msg or '%s != %s within %s places' % (`first`, `second`, `places` ))
+
+    def failIfAlmostEqual(self, first, second, places=7, msg=None):
+        """Fail if the two objects are equal as determined by their
+           difference rounded to the given number of decimal places
+           (default 7) and comparing to zero.
+
+           Note that decimal places (from zero) is usually not the same
+           as significant digits (measured from the most signficant digit).
+        """
+        if round(second-first, places) == 0:
+            raise self.failureException, \
+                  (msg or '%s == %s within %s places' % (`first`, `second`, `places`))
+
     assertEqual = assertEquals = failUnlessEqual
 
     assertNotEqual = assertNotEquals = failIfEqual
 
+    assertAlmostEqual = assertAlmostEquals = failUnlessAlmostEqual
+
+    assertNotAlmostEqual = assertNotAlmostEquals = failIfAlmostEqual
+
     assertRaises = failUnlessRaises
 
     assert_ = failUnless