Debian Codegraph @

rpython/translator/c/test/test_backendoptimized.py

raw symbols SCIP
1 import gc
2 from rpython.rlib.rarithmetic import r_uint, r_longlong, r_ulonglong
3 from rpython.translator.c.test.test_typed import TestTypedTestCase as _TestTypedTestCase
4 from rpython.translator.c.test.test_genc import compile
5
6
7 class TestTypedOptimizedTestCase(_TestTypedTestCase):
8 getcompiled = staticmethod(compile)
9
10 def test_remove_same_as(self):
11 def f(n):
12 if bool(bool(bool(n))):
13 return 123
14 else:
15 return 456
16 fn = self.getcompiled(f, [bool])
17 assert fn(True) == 123
18 assert fn(False) == 456
19
20 def test__del__(self):
21 import gc
22 class B(object):
23 pass
24 b = B()
25 b.nextid = 0
26 b.num_deleted = 0
27 class A(object):
28 def __init__(self):
29 self.id = b.nextid
30 b.nextid += 1
31
32 def __del__(self):
33 b.num_deleted += 1
34
35 def f(x):
36 a = A()
37 for i in range(x):
38 a = A()
39 gc.collect()
40 gc.collect()
41 gc.collect()
42 return b.num_deleted
43
44 fn = self.getcompiled(f, [int], gcpolicy='ref')
45 res = f(5)
46 assert res == 5
47 res = fn(5)
48 # translated function loses its last reference earlier
49 assert res == 6
50
51 def test_del_inheritance(self):
52 import gc
53 class State:
54 pass
55 s = State()
56 s.a_dels = 0
57 s.b_dels = 0
58 class A(object):
59 def __del__(self):
60 s.a_dels += 1
61 class B(A):
62 def __del__(self):
63 s.b_dels += 1
64 class C(A):
65 pass
66 def f(x):
67 A()
68 B()
69 C()
70 A()
71 B()
72 C()
73 gc.collect()
74 if x:
75 return s.a_dels * 10 + s.b_dels
76 else:
77 return -1
78 fn = self.getcompiled(f, [int], gcpolicy='ref')
79 res = f(1)
80 assert res == 42
81 res = fn(1)
82 assert res == 42
83
84 class TestTypedOptimizedSwitchTestCase:
85 def getcompiled(self, func, argtypes):
86 return compile(func, argtypes, merge_if_blocks=True)
87
88 def test_int_switch(self):
89 def f(x):
90 if x == 3:
91 return 9
92 elif x == 9:
93 return 27
94 elif x == 27:
95 return 3
96 return 0
97 fn = self.getcompiled(f, [int])
98 for x in (0,1,2,3,9,27,48, -9):
99 assert fn(x) == f(x)
100
101 def test_int_switch_nonsparse(self):
102 def f(x):
103 if x == 1:
104 return 9
105 elif x == 2:
106 return 27
107 elif x == 3:
108 return 3
109 return 0
110 fn = self.getcompiled(f, [int])
111 for x in (0,1,2,3,9,27,48, -9):
112 assert fn(x) == f(x)
113
114 def test_int_switch_nonsparse_neg(self):
115 def f(x):
116 if x == -1:
117 return 9
118 elif x == 2:
119 return 27
120 elif x == 3:
121 return 3
122 return 0
123 fn = self.getcompiled(f, [int])
124 for x in (0,1,2,3,9,27,48, -9):
125 assert fn(x) == f(x)
126
127 def test_uint_switch(self):
128 def f(x):
129 if x == r_uint(3):
130 return 9
131 elif x == r_uint(9):
132 return 27
133 elif x == r_uint(27):
134 return 3
135 return 0
136 fn = self.getcompiled(f, [r_uint])
137 for x in (0,1,2,3,9,27,48):
138 assert fn(r_uint(x)) == f(r_uint(x))
139
140 def test_longlong_switch(self):
141 def f(x):
142 if x == r_longlong(3):
143 return 9
144 elif x == r_longlong(9):
145 return 27
146 elif x == r_longlong(27):
147 return 3
148 return 0
149 fn = self.getcompiled(f, [r_longlong])
150 for x in (0,1,2,3,9,27,48, -9):
151 assert fn(r_longlong(x)) == f(r_longlong(x))
152
153 def test_ulonglong_switch(self):
154 def f(x):
155 if x == r_ulonglong(3):
156 return 9
157 elif x == r_ulonglong(9):
158 return 27
159 elif x == r_ulonglong(27):
160 return 3
161 return 0
162 fn = self.getcompiled(f, [r_ulonglong])
163 for x in (0,1,2,3,9,27,48, r_ulonglong(-9)):
164 assert fn(r_ulonglong(x)) == f(r_ulonglong(x))
165
166 def test_chr_switch(self):
167 def f(y):
168 x = chr(y)
169 if x == 'a':
170 return 'b'
171 elif x == 'b':
172 return 'c'
173 elif x == 'c':
174 return 'd'
175 return '@'
176 fn = self.getcompiled(f, [int])
177 for x in 'ABCabc@':
178 y = ord(x)
179 assert fn(y) == f(y)
180
181 def test_char_may_be_signed(self):
182 def f(n):
183 case = chr(n)
184 if case == '\xFF': return 1
185 if case == '\xFE': return 2
186 if case == '\xFD': return 3
187 if case == '\xFC': return 4
188 if case == '\xFB': return 5
189 if case == '\xFA': return 6
190 return 7
191 fn = self.getcompiled(f, [int])
192 for input, expected in [(255, 1), (253, 3), (251, 5), (161, 7)]:
193 res = fn(input)
194 assert res == expected
195
196 def test_unichr_switch(self):
197 def f(y):
198 x = unichr(y)
199 if x == u'a':
200 return 'b'
201 elif x == u'b':
202 return 'c'
203 elif x == u'c':
204 return 'd'
205 return '@'
206 fn = self.getcompiled(f, [int])
207 for x in u'ABCabc@':
208 y = ord(x)
209 assert fn(y) == f(y)
210
211
212 class TestTypedOptimizedRaisingOps:
213 def getcompiled(self, func, argtypes):
214 return compile(func, argtypes)
215
216 def test_int_floordiv_zer(self):
217 def f(x):
218 try:
219 y = 123 / x
220 except:
221 y = 456
222 return y
223 fn = self.getcompiled(f, [int])
224 for x in (0,1,2,3,9,27,48, -9):
225 assert fn(x) == f(x)
226
227 def test_ovf_op_in_loop(self):
228 # This checks whether the raising operations are implemented using
229 # unsigned arithmetic. The problem with using signed arithmetic is that
230 # signed overflow is undefined in C and the optimizer is allowed to
231 # remove the overflow check.
232 from sys import maxint
233 from rpython.rlib.rarithmetic import ovfcheck
234 def f(x, y):
235 ret = 0
236 for i in range(y):
237 try:
238 ret = ovfcheck(x + i)
239 except OverflowError:
240 break
241 return ret
242 fc = self.getcompiled(f, [int, int])
243 assert fc(10, 10) == 19
244 assert fc(maxint, 10) == maxint