[pypy-svn] r72301 - in pypy/trunk/pypy: module/math/test rpython/lltypesystem/module/test translator/c/test

arigo at codespeak.net arigo at codespeak.net
Wed Mar 17 12:19:51 CET 2010


Author: arigo
Date: Wed Mar 17 12:19:47 2010
New Revision: 72301

Added:
   pypy/trunk/pypy/translator/c/test/test_math.py   (contents, props changed)
Modified:
   pypy/trunk/pypy/module/math/test/test_direct.py
   pypy/trunk/pypy/module/math/test/test_math.py
   pypy/trunk/pypy/rpython/lltypesystem/module/test/test_ll_math.py
   pypy/trunk/pypy/translator/c/test/test_extfunc.py
Log:
Improve tests.  Fix an "oups": OverflowError is callable too.
Add C tests too.


Modified: pypy/trunk/pypy/module/math/test/test_direct.py
==============================================================================
--- pypy/trunk/pypy/module/math/test/test_direct.py	(original)
+++ pypy/trunk/pypy/module/math/test/test_direct.py	Wed Mar 17 12:19:47 2010
@@ -4,10 +4,10 @@
 import py, sys, math
 from pypy.rlib.rarithmetic import isinf, isnan, INFINITY, NAN
 
-consistent = True
+consistent_host = True
 if '__pypy__' not in sys.builtin_module_names:
     if sys.version_info < (2, 6):
-        consistent = False
+        consistent_host = False
 
 def positiveinf(x):
     return isinf(x) and x > 0.0
@@ -100,8 +100,8 @@
         ('pow', (INFINITY, 0.001), positiveinf),
         ('pow', (INFINITY, -0.001), 0.0),
         ('pow', (-INFINITY, 0.0), 1.0),
-        ('pow', (-INFINITY, 0.001), ValueError),
-        ('pow', (-INFINITY, -0.001), ValueError),
+        ('pow', (-INFINITY, 0.001), positiveinf),
+        ('pow', (-INFINITY, -0.001), 0.0),
         ('pow', (-INFINITY, 3.0), negativeinf),
         ('pow', (-INFINITY, 6.0), positiveinf),
         ('pow', (-INFINITY, -13.0), -0.0),
@@ -111,13 +111,13 @@
         ('pow', (0.999, INFINITY), 0.0),
         ('pow', (-0.999,INFINITY), 0.0),
         #('pow', (-1.0, INFINITY), 1.0, but strange, could also be -1.0),
-        ('pow', (-1.001,INFINITY), OverflowError),
+        ('pow', (-1.001,INFINITY), positiveinf),
         ('pow', (1.001, -INFINITY), 0.0),
         ('pow', (1.0,   -INFINITY), 1.0),
         #('pow', (0.999, -INFINITY), positiveinf, but get OverflowError),
         #('pow', (INFINITY, INFINITY), positiveinf, but get OverflowError),
         ('pow', (INFINITY, -INFINITY), 0.0),
-        ('pow', (-INFINITY, INFINITY), OverflowError),
+        ('pow', (-INFINITY, INFINITY), positiveinf),
         ]
 
     IRREGERRCASES = [
@@ -167,6 +167,8 @@
         ('modf', (NAN,), lambda x: (isnan(x[0]) and isnan(x[1]))),
         ]
 
+    # The list of test cases.  Note that various tests import this,
+    # notably in rpython/lltypesystem/module and in translator/c/test.
     TESTCASES = (REGCASES + IRREGCASES + OVFCASES + INFCASES + IRREGERRCASES
                  + NANCASES1 + NANCASES2 + NANCASES3 + NANCASES4 + NANCASES5
                  + NANCASES6)
@@ -175,6 +177,25 @@
 class TestDirect(MathTests):
     pass
 
+def get_tester(expected):
+    if type(expected) is type(Exception):
+        def tester(value):
+            return False
+    elif callable(expected):
+        def tester(value):
+            ok = expected(value)
+            assert isinstance(ok, bool)
+            return ok
+    else:
+        assert finite(expected), "badly written test"
+        def tester(got):
+            gotsign = expectedsign = 1
+            if got < 0.0: gotsign = -gotsign
+            if expected < 0.0: expectedsign = -expectedsign
+            return finite(got) and (got == expected and
+                                    gotsign == expectedsign)
+    return tester
+
 def do_test(fn, fnname, args, expected):
     repr = "%s(%s)" % (fnname, ', '.join(map(str, args)))
     try:
@@ -185,22 +206,13 @@
         assert expected == OverflowError, "%s: got an OverflowError" % (
             repr,)
     else:
-        if callable(expected):
-            ok = expected(got)
-        else:
-            assert finite(expected), "badly written test"
-            gotsign = expectedsign = 1
-            if got < 0.0: gotsign = -gotsign
-            if expected < 0.0: expectedsign = -expectedsign
-            ok = finite(got) and (got == expected and
-                                  gotsign == expectedsign)
-        if not ok:
+        if not get_tester(expected)(got):
             raise AssertionError("%r: got %s" % (repr, got))
 
 def make_test_case((fnname, args, expected), dict):
     #
     def test_func(self):
-        if not consistent:
+        if not consistent_host:
             py.test.skip("inconsistent behavior before 2.6")
         fn = getattr(math, fnname)
         do_test(fn, fnname, args, expected)

Modified: pypy/trunk/pypy/module/math/test/test_math.py
==============================================================================
--- pypy/trunk/pypy/module/math/test/test_math.py	(original)
+++ pypy/trunk/pypy/module/math/test/test_math.py	Wed Mar 17 12:19:47 2010
@@ -7,9 +7,11 @@
     def setup_class(cls):
         cls.space = gettestobjspace(usemodules=['math'])
         cls.w_cases = cls.space.wrap(test_direct.MathTests.TESTCASES)
-        cls.w_consistent = cls.space.wrap(test_direct.consistent)
+        cls.w_consistent_host = cls.space.wrap(test_direct.consistent_host)
 
     def test_all_cases(self):
+        if not self.consistent_host:
+            skip("please test this on top of PyPy or CPython >= 2.6")
         import math
         for fnname, args, expected in self.cases:
             fn = getattr(math, fnname)
@@ -19,14 +21,11 @@
             except ValueError:
                 assert expected == ValueError
             except OverflowError:
-                if not self.consistent:
-                    if expected == ValueError:
-                        continue      # e.g. for 'log'
-                    if callable(expected):
-                        continue      # e.g. for 'ceil'
                 assert expected == OverflowError
             else:
-                if callable(expected):
+                if type(expected) is type(Exception):
+                    ok = False
+                elif callable(expected):
                     ok = expected(got)
                 else:
                     gotsign = expectedsign = 1
@@ -34,4 +33,5 @@
                     if expected < 0.0: expectedsign = -expectedsign
                     ok = got == expected and gotsign == expectedsign
                 if not ok:
-                    raise AssertionError("%r: got %s" % (repr, got))
+                    raise AssertionError("%s(%s): got %s" % (
+                        fnname, ', '.join(map(str, args)), got))

Modified: pypy/trunk/pypy/rpython/lltypesystem/module/test/test_ll_math.py
==============================================================================
--- pypy/trunk/pypy/rpython/lltypesystem/module/test/test_ll_math.py	(original)
+++ pypy/trunk/pypy/rpython/lltypesystem/module/test/test_ll_math.py	Wed Mar 17 12:19:47 2010
@@ -2,7 +2,7 @@
 """
 
 from pypy.rpython.lltypesystem.module import ll_math
-from pypy.module.math.test.test_direct import MathTests, finite
+from pypy.module.math.test.test_direct import MathTests, get_tester
 
 
 class TestMath(MathTests):
@@ -21,15 +21,7 @@
             assert expected == OverflowError, "%s: got an OverflowError" % (
                 repr,)
         else:
-            if callable(expected):
-                ok = expected(got)
-            else:
-                assert finite(expected), "badly written test"
-                gotsign = ll_math.math_copysign(1.0, got)
-                expectedsign = ll_math.math_copysign(1.0, expected)
-                ok = finite(got) and (got == expected and
-                                      gotsign == expectedsign)
-            if not ok:
+            if not get_tester(expected)(got):
                 raise AssertionError("%r: got %s" % (repr, got))
     #
     dict[fnname] = dict.get(fnname, 0) + 1

Modified: pypy/trunk/pypy/translator/c/test/test_extfunc.py
==============================================================================
--- pypy/trunk/pypy/translator/c/test/test_extfunc.py	(original)
+++ pypy/trunk/pypy/translator/c/test/test_extfunc.py	Wed Mar 17 12:19:47 2010
@@ -235,93 +235,6 @@
     res = f1("echo hello")
     assert res == 0
 
-def test_math_pow():
-    import math
-    def fn(x, y):
-        return math.pow(x, y)
-    f = compile(fn, [float, float])
-    assert f(2.0, 3.0) == math.pow(2.0, 3.0)
-    assert f(3.0, 2.0) == math.pow(3.0, 2.0)
-    assert f(2.3, 0.0) == math.pow(2.3, 0.0)
-    assert f(2.3, -1.0) == math.pow(2.3, -1.0)
-    assert f(2.3, -2.0) == math.pow(2.3, -2.0)
-    assert f(2.3, 0.5) == math.pow(2.3, 0.5)
-    assert f(4.0, 0.5) == math.pow(4.0, 0.5)
-
-def test_math_frexp():
-    from math import frexp
-    def fn(x):
-        return frexp(x)
-    f = compile(fn, [float])
-    assert f(10.123) == frexp(10.123)
-
-def test_math_modf():
-    from math import modf
-    def fn(x):
-        return modf(x)
-    f = compile(fn, [float])
-    assert f(10.123) == modf(10.123)
-
-def test_math_hypot():
-    from math import hypot
-    def fn(x, y):
-        return hypot(x, y)
-    f = compile(fn, [float, float])
-    assert f(9812.231, 1234) == hypot(9812.231, 1234)
-
-simple_math_functions = [
-    'acos', 'asin', 'atan', 'ceil', 'cos', 'cosh', 'exp', 'fabs',
-    'floor', 'log', 'log10', 'sin', 'sinh', 'sqrt', 'tan', 'tanh'
-    ]
-
-def math_function_test(funcname):
-    import random
-    import math
-    mathfn = getattr(math, funcname)
-    print funcname,
-    def fn(x):
-        return mathfn(x)
-    f = compile(fn, [float])
-    for x in [0.12334, 0.3, 0.5, 0.9883]:
-        print x
-        assert (funcname, f(x)) == (funcname, mathfn(x))
-
-def test_simple_math_functions():
-    for funcname in simple_math_functions:
-        yield math_function_test, funcname
-
-def test_math_errors():
-    import math
-    def fn(x):
-        return math.log(x)
-    f = compile(fn, [float])
-    assert f(math.e) == math.log(math.e)
-    # this is a platform specific mess
-    def check(mathf, f, v):
-        try:
-            r = mathf(v)
-        except (OverflowError, ValueError), e:
-            #print mathf, v, e.__class__
-            py.test.raises(e.__class__, f, v)
-        else:
-            if r != r: # nans
-                #print mathf, v, "NAN?", r
-                u = f(v)
-                assert u != u
-            else:
-                #print mathf, v, r
-                u = f(v)
-                assert u == r
-
-    check(math.log, f, -1.0)
-    check(math.log, f, 0.0)
-
-    def fmod1_0(y):
-        return math.fmod(1.0, y)
-    f = compile(fmod1_0, [float])
-    check(fmod1_0, f, 0.0)
-
-
 def test_os_path_exists():
     tmpfile = str(udir.join('test_os_path_exists.TMP'))
     def fn():

Added: pypy/trunk/pypy/translator/c/test/test_math.py
==============================================================================
--- (empty file)
+++ pypy/trunk/pypy/translator/c/test/test_math.py	Wed Mar 17 12:19:47 2010
@@ -0,0 +1,42 @@
+import py, math
+from pypy.module.math.test import test_direct
+from pypy.translator.c.test.test_genc import compile
+
+
+def get_test_case((fnname, args, expected)):
+    fn = getattr(math, fnname)
+    expect_valueerror = (expected == ValueError)
+    expect_overflowerror = (expected == OverflowError)
+    check = test_direct.get_tester(expected)
+    #
+    def testfn():
+        try:
+            got = fn(*args)
+        except ValueError:
+            return expect_valueerror
+        except OverflowError:
+            return expect_overflowerror
+        else:
+            return check(got)
+    #
+    testfn.func_name = 'test_' + fnname
+    return testfn
+
+
+testfnlist = [get_test_case(testcase)
+              for testcase in test_direct.MathTests.TESTCASES]
+
+def fn():
+    for i in range(len(testfnlist)):
+        testfn = testfnlist[i]
+        if not testfn():
+            return i
+    return -42  # ok
+
+def test_math():
+    f = compile(fn, [])
+    res = f()
+    if res >= 0:
+        py.test.fail(repr(test_direct.MathTests.TESTCASES[res]))
+    else:
+        assert res == -42



More information about the Pypy-commit mailing list