[Numpy-svn] r5720 - trunk/numpy/testing

numpy-svn at scipy.org numpy-svn at scipy.org
Thu Aug 28 21:29:32 EDT 2008


Author: charris
Date: 2008-08-28 20:29:30 -0500 (Thu, 28 Aug 2008)
New Revision: 5720

Modified:
   trunk/numpy/testing/utils.py
Log:
Make testing functions work when python is called with the -OO flag.


Modified: trunk/numpy/testing/utils.py
===================================================================
--- trunk/numpy/testing/utils.py	2008-08-29 01:23:43 UTC (rev 5719)
+++ trunk/numpy/testing/utils.py	2008-08-29 01:29:30 UTC (rev 5720)
@@ -162,10 +162,12 @@
 
     """
     if isinstance(desired, dict):
-        assert isinstance(actual, dict), repr(type(actual))
+        if not isinstance(actual, dict) :
+            raise AssertionError(repr(type(actual)))
         assert_equal(len(actual),len(desired),err_msg,verbose)
         for k,i in desired.items():
-            assert k in actual, repr(k)
+            if k not in actual :
+                raise AssertionError(repr(k))
             assert_equal(actual[k], desired[k], 'key=%r\n%s' % (k,err_msg), verbose)
         return
     if isinstance(desired, (list,tuple)) and isinstance(actual, (list,tuple)):
@@ -177,7 +179,8 @@
     if isinstance(actual, ndarray) or isinstance(desired, ndarray):
         return assert_array_equal(actual, desired, err_msg, verbose)
     msg = build_err_msg([actual, desired], err_msg, verbose=verbose)
-    assert desired == actual, msg
+    if desired != actual :
+        raise AssertionError(msg)
 
 def print_assert_equal(test_string,actual,desired):
     import pprint
@@ -191,7 +194,7 @@
         pprint.pprint(actual,msg)
         msg.write('DESIRED: \n')
         pprint.pprint(desired,msg)
-        raise AssertionError, msg.getvalue()
+        raise AssertionError(msg.getvalue())
 
 def assert_almost_equal(actual,desired,decimal=7,err_msg='',verbose=True):
     """ Raise an assertion if two items are not equal.
@@ -204,7 +207,8 @@
     if isinstance(actual, ndarray) or isinstance(desired, ndarray):
         return assert_array_almost_equal(actual, desired, decimal, err_msg)
     msg = build_err_msg([actual, desired], err_msg, verbose=verbose)
-    assert round(abs(desired - actual),decimal) == 0, msg
+    if round(abs(desired - actual),decimal) != 0 :
+        raise AssertionError(msg)
 
 
 def assert_approx_equal(actual,desired,significant=7,err_msg='',verbose=True):
@@ -231,7 +235,8 @@
                 header='Items are not equal to %d significant digits:' %
                                  significant,
                 verbose=verbose)
-    assert math.fabs(sc_desired - sc_actual) < pow(10.,-(significant-1)), msg
+    if math.fabs(sc_desired - sc_actual) >= pow(10.,-(significant-1)) :
+        raise AssertionError(msg)
 
 def assert_array_compare(comparison, x, y, err_msg='', verbose=True,
                          header=''):
@@ -252,7 +257,8 @@
                                                                   y.shape),
                                 verbose=verbose, header=header,
                                 names=('x', 'y'))
-            assert cond, msg
+            if not cond :
+                raise AssertionError(msg)
 
         if (isnumber(x) and isnumber(y)) and (any(isnan(x)) or any(isnan(y))):
             # Handling nan: we first check that x and y have the nan at the
@@ -286,7 +292,8 @@
                                 + '\n(mismatch %s%%)' % (match,),
                                 verbose=verbose, header=header,
                                 names=('x', 'y'))
-            assert cond, msg
+            if not cond :
+                raise AssertionError(msg)
     except ValueError:
         msg = build_err_msg([x, y], err_msg, verbose=verbose, header=header,
                             names=('x', 'y'))
@@ -315,8 +322,10 @@
     # delay import of difflib to reduce startup time
     import difflib
 
-    assert isinstance(actual, str),`type(actual)`
-    assert isinstance(desired, str),`type(desired)`
+    if not isinstance(actual, str) :
+        raise AssertionError(`type(actual)`)
+    if not isinstance(desired, str):
+        raise AssertionError(`type(desired)`)
     if re.match(r'\A'+desired+r'\Z', actual, re.M): return
     diff = list(difflib.Differ().compare(actual.splitlines(1), desired.splitlines(1)))
     diff_list = []
@@ -330,7 +339,8 @@
             if d2.startswith('? '):
                 l.append(d2)
                 d2 = diff.pop(0)
-            assert d2.startswith('+ '),`d2`
+            if not d2.startswith('+ ') :
+                raise AssertionError(`d2`)
             l.append(d2)
             d3 = diff.pop(0)
             if d3.startswith('? '):
@@ -341,10 +351,12 @@
                 continue
             diff_list.extend(l)
             continue
-        assert False, `d1`
-    if not diff_list: return
+        raise AssertionError(`d1`)
+    if not diff_list:
+        return
     msg = 'Differences in strings:\n%s' % (''.join(diff_list)).rstrip()
-    assert actual==desired, msg
+    if actual != desired :
+        raise AssertionError(msg)
 
 
 def rundocs(filename=None):
@@ -430,7 +442,7 @@
                    'exec')
     i = 0
     elapsed = jiffies()
-    while i<times:
+    while i < times:
         i += 1
         exec code in globs,locs
     elapsed = jiffies() - elapsed




More information about the Numpy-svn mailing list