[pypy-svn] rev 569 - in pypy/trunk/src/pypy/appspace: . test

mwh at codespeak.net mwh at codespeak.net
Tue May 27 16:06:19 CEST 2003


Author: mwh
Date: Tue May 27 16:06:19 2003
New Revision: 569

Modified:
   pypy/trunk/src/pypy/appspace/cmathmodule.py
   pypy/trunk/src/pypy/appspace/complexobject.py
   pypy/trunk/src/pypy/appspace/test/test_cmathmodule.py
   pypy/trunk/src/pypy/appspace/test/test_complexobject.py
Log:
Bang on appspace tests.  Two still fail, but this is not the time
and place to work out why (complexobject.complex.__mod__, sheesh).


Modified: pypy/trunk/src/pypy/appspace/cmathmodule.py
==============================================================================
--- pypy/trunk/src/pypy/appspace/cmathmodule.py	(original)
+++ pypy/trunk/src/pypy/appspace/cmathmodule.py	Tue May 27 16:06:19 2003
@@ -131,7 +131,7 @@
     Return the base-10 logarithm of x."""
     
     l = math.hypot(x.real, x.imag)
-    imag = math.atan2(x.imag, x.real)/log(10.)
+    imag = math.atan2(x.imag, x.real)/math.log(10.)
     real = math.log10(l)
     return complex(real, imag)
 

Modified: pypy/trunk/src/pypy/appspace/complexobject.py
==============================================================================
--- pypy/trunk/src/pypy/appspace/complexobject.py	(original)
+++ pypy/trunk/src/pypy/appspace/complexobject.py	Tue May 27 16:06:19 2003
@@ -146,21 +146,6 @@
         return complex(real, imag)
 
 
-    def __d_i_v__(self, other):
-        # the canonical alternative, which is said to have problems 
-        # with floating point precision...
-        if other.__class__ != complex:
-            return complex(self.real/other, self.imag/other)
-
-        a, b = self.real, self.imag
-        c, d = other.real, other.imag
-        zr = a*c + b*d
-        zi = b*c - a*d
-        n = c*c + d*d
-
-        return complex(zr/n, zi/n)
-
-
     def __floordiv__(self, other):
         return self / other
         
@@ -251,7 +236,9 @@
             return self, complex(other)
         elif other.__class__ == complex:
             return self, other
-
+        elif typ is types.ComplexType: # cough
+            return self, complex(other.real, other.imag)
+            
         raise TypeError, "number coercion failed"
 
 
@@ -260,6 +247,7 @@
 
 
     def __ne__(self, other):
+        self, other = self.__coerce__(other)
         if self.real != other.real:
             return 1
         if self.imag != other.imag:

Modified: pypy/trunk/src/pypy/appspace/test/test_cmathmodule.py
==============================================================================
--- pypy/trunk/src/pypy/appspace/test/test_cmathmodule.py	(original)
+++ pypy/trunk/src/pypy/appspace/test/test_cmathmodule.py	Tue May 27 16:06:19 2003
@@ -16,25 +16,29 @@
 import unittest
 import testsupport
 
-try:
-    from pypy.appspace import cmathmodule
-    from pypy.appspace.complexobject import complex as pycomplex
-except ImportError:
-    import cmathmodule
-    from complexobject import complex as pycomplex
+#try:
+from pypy.appspace import cmathmodule
+from pypy.appspace.complexobject import complex as pycomplex
+#except ImportError:
+#    import cmathmodule
+#    from pypy.complexobject import complex as pycomplex
 
 from test_complexobject import equal, enumerate
 
 
 class TestCMathModule(unittest.TestCase):
 
+    def assertAEqual(self, a, b):
+        if not equal(a, b):
+            raise self.failureException, '%s ~== %s'%(a, b)
+
     def test_funcs(self):
         "Compare many functions with CPython."
         
         for (z0c, z1c, z0p, z1p) in enumerate():
             mc = z0c*z1c
             mp = z0p*z1p
-            self.assert_(equal(mc, mp))
+            self.assertAEqual(mc, mp)
 
             for op in "sqrt acos acosh asin asinh atan atanh cos cosh exp".split():
                 if op == "atan" and equal(z0c, complex(0,-1)) or equal(z0c, complex(0,1)):
@@ -43,13 +47,13 @@
                     continue
                 op0 = cmath.__dict__[op](z0c)
                 op1 = cmathmodule.__dict__[op](z0p)
-                self.assert_(equal(op0, op1))
+                self.assertAEqual(op0, op1)
 
             # check divisions
             if equal(z0c, complex(0,0)) or equal(z1c, complex(0,0)):
                 continue
-            self.assert_(equal(mc/z0c, mp/z0p))
-            self.assert_(equal(mc/z1c, mp/z1p))
+            self.assertAEqual(mc/z0c, mp/z0p)
+            self.assertAEqual(mc/z1c, mp/z1p)
 
 
     def test_log_log10(self):
@@ -57,10 +61,10 @@
         
         for (z0c, z1c, z0p, z1p) in enumerate():
             for op in "log log10".split():
-                op0 = cmath.__dict__[op](z0c)
-                op1 = cmathmodule.__dict__[op](z0p)
-                self.assert_(equal(op0, op1))
-
+                if z0p != 0:
+                    op0 = cmath.__dict__[op](z0c)
+                    op1 = cmathmodule.__dict__[op](z0p)
+                    self.assertAEqual(op0, op1)
 
 
 if __name__ == "__main__":

Modified: pypy/trunk/src/pypy/appspace/test/test_complexobject.py
==============================================================================
--- pypy/trunk/src/pypy/appspace/test/test_complexobject.py	(original)
+++ pypy/trunk/src/pypy/appspace/test/test_complexobject.py	Tue May 27 16:06:19 2003
@@ -16,14 +16,10 @@
 import sys
 import types
 import unittest
+import testsupport
 
-
-try:
-    import setpath
-    from appspace.complexobject import complex as pycomplex
-except ImportError:
-    from complexobject import complex as pycomplex
-
+from pypy.appspace.complexobject import complex as pycomplex
+    
 
 try:
     unicode
@@ -74,6 +70,10 @@
 
 class TestComplex(unittest.TestCase):
 
+    def assertAEqual(self, a, b):
+        if not equal(a, b):
+            raise self.failureException, '%s ~== %s'%(a, b)
+
     def test_wrongInit1(self):
         "Compare wrong init. with CPython."
         
@@ -149,41 +149,37 @@
         for (z0c, z1c, z0p, z1p) in enumerate():
             mc = z0c*z1c
             mp = z0p*z1p
-            self.assert_(equal(mc, mp))
+            self.assertAEqual(mc, mp)
 
             sc = z0c+z1c
             sp = z0p+z1p
-            self.assert_(equal(sc, sp))
+            self.assertAEqual(sc, sp)
 
             dc = z0c-z1c
             dp = z0p-z1p
-            self.assert_(equal(dc, dp))
+            self.assertAEqual(dc, dp)
 
             if not equal(z1c, complex(0,0)): 
-#                try:
-                    qc = z0c/z1c
-                    qp = z0p/z1p
-                    self.assert_(equal(qc, qp))
-#                except AssertionError:
-#                    print "c: (%s/%s) = (%s)" % (z0c, z1c, qc)
-#                    print "py:(%s/%s) = (%s)" % (z0p, z1p, qp)
+                qc = z0c/z1c
+                qp = z0p/z1p
+                self.assertAEqual(qc, qp)
 
                 
     def test_special(self):
         "Compare special methods with CPython."
         
-        ass = self.assert_
         for (x, y) in [(0,0), (0,1), (1,3.)]:
             zc = complex(x, y)
             zp = pycomplex(x, y)
 
-            ass(equal(zc, zp), "%s != %s" % (zc, zp))
-            ass(equal(-zc, -zp), "%s != %s" % (-zc, -zp))
-            ass(equal(+zc, +zp), "%s != %s" % (+zc, +zp))
-            ass(equal(abs(zc), abs(zp)), "%s != %s" % (abs(zc), abs(zp)))
-            ass(equal(zc.conjugate(), zp.conjugate()), "%s != %s" % (zc.conjugate(), zp.conjugate()))
-            ass(str(zc) == str(zp), "str(%s) != str(%s)" % (str(zc), str(zp)))
-            ass(hash(zc) == hash(zp), "%s == hash(%s) != hash(%s) == %s" % (hash(zc), zc, zp, hash(zp)))
+            self.assertAEqual(zc, zp)
+            self.assertAEqual(-zc, -zp)
+            self.assertAEqual(+zc, +zp)
+            self.assertAEqual(abs(zc), abs(zp))
+            self.assertAEqual(zc, zp)
+            self.assertEqual(zc.conjugate(), zp.conjugate())
+            self.assertEqual(str(zc), str(zp))
+            self.assertEqual(hash(zc), hash(zp))
 
 
     def test_divmod(self):
@@ -192,19 +188,15 @@
         for (z0c, z1c, z0p, z1p) in enumerate():
             mc = z0c*z1c
             mp = z0p*z1p
-            self.assert_(equal(mc, mp))
+            self.assertAEqual(mc, mp)
 
             if not equal(z1c, complex(0,0)): 
-#                try:
-                    ddc, mmc = divmod(z0c, z1c)
-                    self.assert_(ddc*z1c + mmc == z0c)
-                    ddp, mmp = divmod(z0p, z1p)
-                    # self.assert_(ddp*z1p + mmp == z0p)
-                    self.assert_(equal(ddc, ddp))
-                    self.assert_(equal(mmc, mmp))
-#                except AssertionError:
-#                    print "c: divmod(%s,%s) = (%s,%s)" % (z0c, z1c, ddc,mmc)
-#                    print "py:divmod(%s,%s) = (%s,%s)" % (z0p, z1p, ddp,mmp)
+                ddc, mmc = divmod(z0c, z1c)
+                self.assertAEqual(ddc*z1c + mmc, z0c)
+                ddp, mmp = divmod(z0p, z1p)
+                self.assertAEqual(ddp*z1p + mmp, z0p)
+                self.assertAEqual(ddc, ddp)
+                self.assertAEqual(mmc, mmp)
 
 
     def test_mod(self):
@@ -213,16 +205,25 @@
         for (z0c, z1c, z0p, z1p) in enumerate():
             mc = z0c*z1c
             mp = z0p*z1p
-            self.assert_(equal(mc, mp))
+            self.assertAEqual(mc, mp)
 
             if not equal(z1c, complex(0,0)): 
-#                try:
-                    rc = z0c%z1c
-                    rp = z0p%z1p
-                    self.assert_(equal(rc, rp))
-#                except AssertionError:
-#                    print "c: %s%%%s = %s" % (z0c, z1c, rc)
-#                    print "py:%s%%%s = %s" % (z0p, z1p, rp)
+                rc = z0c%z1c
+                rp = z0p%z1p
+                self.assertAEqual(rc, rp)
+                    
+    def test_div(self):
+        "Compare mod with CPython."
+        
+        for (z0c, z1c, z0p, z1p) in enumerate():
+            mc = z0c*z1c
+            mp = z0p*z1p
+            self.assertAEqual(mc, mp)
+
+            if not equal(z1c, complex(0,0)): 
+                rc = z0c/z1c
+                rp = z0p/z1p
+                self.assertAEqual(rc, rp)
                     
 
     def test_pow(self):
@@ -232,40 +233,10 @@
             if not equal(z0c, 0j) and (z1c.imag != 0.0):
                 pc = z0c**z1c
                 pp = z0p**z1p
-                assert equal(pc, pp)
+                self.assertAEqual(pc, pp)
                 pc = z0c**z0c.real
                 pp = z0p**z0p.real
-                self.assert_(equal(pc, pp))
-
-
-
-# used previously for investigating numerical instabilities
-
-def dm(self, other):
-    # a divmod like used in complex.
-    
-    div = self/other
-    print div
-    div = complex(math.floor(div.real), 0.0)
-    print div
-    mod = self - div*other
-    print mod
-    return div, mod
-
-
-def testNumericalInstability():
-    x, y = -3+1j, -1-3j
-    print x, y, divmod(x, y)
-    print x/y
-    print math.floor((x/y).real)+0j
-    print
-
-    x, y = complex(-3,1), complex(-1,-3)
-    print x, y
-    dm(x, y)
-
-
-
+                self.assertAEqual(pc, pp)
 
 if __name__ == "__main__":
     unittest.main()


More information about the Pypy-commit mailing list