[pypy-svn] pypy post-release-1.5: Improve the tests. Needs an obscure workaround, but it's only

arigo commits-noreply at bitbucket.org
Mon Apr 25 11:14:04 CEST 2011


Author: Armin Rigo <arigo at tunes.org>
Branch: post-release-1.5
Changeset: r43553:240957c0ff30
Date: 2011-04-23 11:43 +0200
http://bitbucket.org/pypy/pypy/changeset/240957c0ff30/

Log:	Improve the tests. Needs an obscure workaround, but it's only a
	problem with the test, not in real code.

diff --git a/pypy/rpython/lltypesystem/module/test/test_ll_math.py b/pypy/rpython/lltypesystem/module/test/test_ll_math.py
--- a/pypy/rpython/lltypesystem/module/test/test_ll_math.py
+++ b/pypy/rpython/lltypesystem/module/test/test_ll_math.py
@@ -22,11 +22,39 @@
         assert ll_math.ll_math_isnan(nan)
         assert not ll_math.ll_math_isnan(inf)
 
+    def test_compiled_isnan(self):
+        def f(x, y):
+            n1 = normalize(x * x)
+            n2 = normalize(y * y * y)
+            return ll_math.ll_math_isnan(n1 / n2)
+        f = compile(f, [float, float], backendopt=False)
+        assert f(1e200, 1e200)     # nan
+        assert not f(1e200, 1.0)   # +inf
+        assert not f(1e200, -1.0)  # -inf
+        assert not f(42.5, 2.3)    # +finite
+        assert not f(42.5, -2.3)   # -finite
+
     def test_compiled_isinf(self):
-        def f(x):
-            return ll_math.ll_math_isinf(1. / x)
-        f = compile(f, [float], backendopt=False)
-        assert f(5.5e-309)
+        def f(x, y):
+            n1 = normalize(x * x)
+            n2 = normalize(y * y * y)
+            return ll_math.ll_math_isinf(n1 / n2)
+        f = compile(f, [float, float], backendopt=False)
+        assert f(1e200, 1.0)       # +inf
+        assert f(1e200, -1.0)      # -inf
+        assert not f(1e200, 1e200) # nan
+        assert not f(42.5, 2.3)    # +finite
+        assert not f(42.5, -2.3)   # -finite
+
+
+from pypy.rpython.lltypesystem import lltype
+_A = lltype.GcArray(lltype.Float)
+def normalize(x):
+    # workaround: force the C compiler to cast to a double
+    a = lltype.malloc(_A, 1)
+    a[0] = x
+    import time; time.time()
+    return a[0]
 
 
 def make_test_case((fnname, args, expected), dict):

diff --git a/pypy/rpython/test/test_rfloat.py b/pypy/rpython/test/test_rfloat.py
--- a/pypy/rpython/test/test_rfloat.py
+++ b/pypy/rpython/test/test_rfloat.py
@@ -157,9 +157,9 @@
         self.interpret(fn, [1.0, 2.0, 3.0])
 
     def test_copysign(self):
-        import math
+        from pypy.rlib import rfloat
         def fn(x, y):
-            return math.copysign(x, y)
+            return rfloat.copysign(x, y)
         assert self.interpret(fn, [42, -1]) == -42
         assert self.interpret(fn, [42, -0.0]) == -42
         assert self.interpret(fn, [42, 0.0]) == 42
@@ -172,21 +172,30 @@
         assert self.interpret(fn, [0]) == 42.3
 
     def test_isnan(self):
-        import math
-        def fn(x):
-            inf = x * x
-            nan = inf / inf
-            return math.isnan(nan)
-        assert self.interpret(fn, [1e200])
+        from pypy.rlib import rfloat
+        def fn(x, y):
+            n1 = x * x
+            n2 = y * y * y
+            return rfloat.isnan(n1 / n2)
+        assert self.interpret(fn, [1e200, 1e200])   # nan
+        assert not self.interpret(fn, [1e200, 1.0])   # +inf
+        assert not self.interpret(fn, [1e200, -1.0])  # -inf
+        assert not self.interpret(fn, [42.5, 2.3])    # +finite
+        assert not self.interpret(fn, [42.5, -2.3])   # -finite
 
     def test_isinf(self):
-        import math
-        def fn(x):
-            inf = x * x
-            return math.isinf(inf)
-        assert self.interpret(fn, [1e200])
+        from pypy.rlib import rfloat
+        def fn(x, y):
+            n1 = x * x
+            n2 = y * y * y
+            return rfloat.isinf(n1 / n2)
+        assert self.interpret(fn, [1e200, 1.0])       # +inf
+        assert self.interpret(fn, [1e200, -1.0])      # -inf
+        assert not self.interpret(fn, [1e200, 1e200]) # nan
+        assert not self.interpret(fn, [42.5, 2.3])    # +finite
+        assert not self.interpret(fn, [42.5, -2.3])   # -finite
 
-        
+
 class TestLLtype(BaseTestRfloat, LLRtypeMixin):
 
     def test_hash(self):

diff --git a/pypy/rpython/lltypesystem/module/ll_math.py b/pypy/rpython/lltypesystem/module/ll_math.py
--- a/pypy/rpython/lltypesystem/module/ll_math.py
+++ b/pypy/rpython/lltypesystem/module/ll_math.py
@@ -91,8 +91,8 @@
 # Custom implementations
 
 def ll_math_isnan(y):
-    # By not calling into the extenal function the JIT can inline this.  Floats
-    # are awesome.
+    # By not calling into the external function the JIT can inline this.
+    # Floats are awesome.
     return y != y
 
 def ll_math_isinf(y):


More information about the Pypy-commit mailing list