File: test_backendoptimized.py

package info (click to toggle)
pypy 7.0.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 107,216 kB
  • sloc: python: 1,201,787; ansic: 62,419; asm: 5,169; cpp: 3,017; sh: 2,534; makefile: 545; xml: 243; lisp: 45; awk: 4
file content (237 lines) | stat: -rw-r--r-- 6,736 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
from rpython.rlib.rarithmetic import r_uint, r_longlong, r_ulonglong
from rpython.translator.c.test.test_typed import TestTypedTestCase as _TestTypedTestCase
from rpython.translator.c.test.test_genc import compile


class TestTypedOptimizedTestCase(_TestTypedTestCase):
    getcompiled = staticmethod(compile)

    def test_remove_same_as(self):
        def f(n):
            if bool(bool(bool(n))):
                return 123
            else:
                return 456
        fn = self.getcompiled(f, [bool])
        assert fn(True) == 123
        assert fn(False) == 456

    def test__del__(self):
        class B(object):
            pass
        b = B()
        b.nextid = 0
        b.num_deleted = 0
        class A(object):
            def __init__(self):
                self.id = b.nextid
                b.nextid += 1

            def __del__(self):
                b.num_deleted += 1

        def f(x):
            a = A()
            for i in range(x):
                a = A()
            return b.num_deleted

        fn = self.getcompiled(f, [int], gcpolicy='ref')
        res = f(5)
        assert res == 5
        res = fn(5)
        # translated function loses its last reference earlier
        assert res == 6

    def test_del_inheritance(self):
        class State:
            pass
        s = State()
        s.a_dels = 0
        s.b_dels = 0
        class A(object):
            def __del__(self):
                s.a_dels += 1
        class B(A):
            def __del__(self):
                s.b_dels += 1
        class C(A):
            pass
        def f(x):
            A()
            B()
            C()
            A()
            B()
            C()
            if x:
                return s.a_dels * 10 + s.b_dels
            else:
                return -1
        fn = self.getcompiled(f, [int], gcpolicy='ref')
        res = f(1)
        assert res == 42
        res = fn(1)
        assert res == 42

class TestTypedOptimizedSwitchTestCase:
    def getcompiled(self, func, argtypes):
        return compile(func, argtypes, merge_if_blocks=True)

    def test_int_switch(self):
        def f(x):
            if x == 3:
                return 9
            elif x == 9:
                return 27
            elif x == 27:
                return 3
            return 0
        fn = self.getcompiled(f, [int])
        for x in (0,1,2,3,9,27,48, -9):
            assert fn(x) == f(x)

    def test_int_switch_nonsparse(self):
        def f(x):
            if x == 1:
                return 9
            elif x == 2:
                return 27
            elif x == 3:
                return 3
            return 0
        fn = self.getcompiled(f, [int])
        for x in (0,1,2,3,9,27,48, -9):
            assert fn(x) == f(x)

    def test_int_switch_nonsparse_neg(self):
        def f(x):
            if x == -1:
                return 9
            elif x == 2:
                return 27
            elif x == 3:
                return 3
            return 0
        fn = self.getcompiled(f, [int])
        for x in (0,1,2,3,9,27,48, -9):
            assert fn(x) == f(x)

    def test_uint_switch(self):
        def f(x):
            if x == r_uint(3):
                return 9
            elif x == r_uint(9):
                return 27
            elif x == r_uint(27):
                return 3
            return 0
        fn = self.getcompiled(f, [r_uint])
        for x in (0,1,2,3,9,27,48):
            assert fn(r_uint(x)) == f(r_uint(x))

    def test_longlong_switch(self):
        def f(x):
            if x == r_longlong(3):
                return 9
            elif x == r_longlong(9):
                return 27
            elif x == r_longlong(27):
                return 3
            return 0
        fn = self.getcompiled(f, [r_longlong])
        for x in (0,1,2,3,9,27,48, -9):
            assert fn(r_longlong(x)) == f(r_longlong(x))

    def test_ulonglong_switch(self):
        def f(x):
            if x == r_ulonglong(3):
                return 9
            elif x == r_ulonglong(9):
                return 27
            elif x == r_ulonglong(27):
                return 3
            return 0
        fn = self.getcompiled(f, [r_ulonglong])
        for x in (0,1,2,3,9,27,48, r_ulonglong(-9)):
            assert fn(r_ulonglong(x)) == f(r_ulonglong(x))

    def test_chr_switch(self):
        def f(y):
            x = chr(y)
            if x == 'a':
                return 'b'
            elif x == 'b':
                return 'c'
            elif x == 'c':
                return 'd'
            return '@'
        fn = self.getcompiled(f, [int])
        for x in 'ABCabc@':
            y = ord(x)
            assert fn(y) == f(y)

    def test_char_may_be_signed(self):
        def f(n):
            case = chr(n)
            if case == '\xFF': return 1
            if case == '\xFE': return 2
            if case == '\xFD': return 3
            if case == '\xFC': return 4
            if case == '\xFB': return 5
            if case == '\xFA': return 6
            return 7
        fn = self.getcompiled(f, [int])
        for input, expected in [(255, 1), (253, 3), (251, 5), (161, 7)]:
            res = fn(input)
            assert res == expected

    def test_unichr_switch(self):
        def f(y):
            x = unichr(y)
            if x == u'a':
                return 'b'
            elif x == u'b':
                return 'c'
            elif x == u'c':
                return 'd'
            return '@'
        fn = self.getcompiled(f, [int])
        for x in u'ABCabc@':
            y = ord(x)
            assert fn(y) == f(y)


class TestTypedOptimizedRaisingOps:
    def getcompiled(self, func, argtypes):
        return compile(func, argtypes)

    def test_int_floordiv_zer(self):
        def f(x):
            try:
                y = 123 / x
            except:
                y = 456
            return y
        fn = self.getcompiled(f, [int])
        for x in (0,1,2,3,9,27,48, -9):
            assert fn(x) == f(x)

    def test_ovf_op_in_loop(self):
        # This checks whether the raising operations are implemented using
        # unsigned arithmetic. The problem with using signed arithmetic is that
        # signed overflow is undefined in C and the optimizer is allowed to
        # remove the overflow check.
        from sys import maxint
        from rpython.rlib.rarithmetic import ovfcheck
        def f(x, y):
            ret = 0
            for i in range(y):
                try:
                    ret = ovfcheck(x + i)
                except OverflowError:
                    break
            return ret
        fc = self.getcompiled(f, [int, int])
        assert fc(10, 10) == 19
        assert fc(maxint, 10) == maxint