File: test_zmath.py

package info (click to toggle)
pypy3 7.3.19%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 212,236 kB
  • sloc: python: 2,098,316; ansic: 540,565; sh: 21,462; asm: 14,419; cpp: 4,451; makefile: 4,209; objc: 761; xml: 530; exp: 499; javascript: 314; pascal: 244; lisp: 45; csh: 12; awk: 4
file content (74 lines) | stat: -rw-r--r-- 2,418 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
""" Test that the math module still behaves even when
    compiled to C with SSE2 enabled.
"""
import py, math
from rpython.translator.c.test.test_genc import compile
from rpython.jit.backend.x86.support import ensure_sse2_floats
from rpython.rlib import rfloat
from rpython.rlib.unroll import unrolling_iterable
from rpython.rlib.debug import debug_print
from rpython.rtyper.lltypesystem.module.test.math_cases import (MathTests,
                                                                get_tester)

def get_test_case((fnname, args, expected)):
    try:
        fn = getattr(math, fnname)
    except AttributeError:
        fn = getattr(rfloat, fnname)
    expect_valueerror = (expected == ValueError)
    expect_overflowerror = (expected == OverflowError)
    check = get_tester(expected)
    unroll_args = unrolling_iterable(args)
    #
    def testfn():
        debug_print('calling', fnname, 'with arguments:')
        for arg in unroll_args:
            debug_print('\t', arg)
        try:
            got = fn(*args)
        except ValueError:
            if expect_valueerror:
                return True
            else:
                debug_print('unexpected ValueError!')
                return False
        except OverflowError:
            if expect_overflowerror:
                return True
            else:
                debug_print('unexpected OverflowError!')
                return False
        else:
            if check(got):
                return True
            else:
                debug_print('unexpected result:', got)
                return False
    #
    testfn.__name__ = 'test_' + fnname
    return testfn


testfnlist = [get_test_case(testcase)
              for testcase in MathTests.TESTCASES]

def fn():
    ensure_sse2_floats()
    for i in range(len(testfnlist)):
        testfn = testfnlist[i]
        if not testfn():
            return i
    return -42  # ok

def test_math():
    # note: we use lldebug because in the normal optimizing mode, some
    # calls may be completely inlined and constant-folded by the
    # compiler (with -flto), e.g. atanh(0.3).  They give then slightly
    # different result than if they were executed at runtime.
    # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=79973
    f = compile(fn, [], lldebug=True)
    res = f()
    if res >= 0:
        py.test.fail(repr(MathTests.TESTCASES[res]))
    else:
        assert res == -42