Debian Codegraph @

pypy/module/pypyjit/test_pypy_c/test_min_max.py

raw symbols SCIP
1 from pypy.module.pypyjit.test_pypy_c.test_00_model import BaseTestPyPyC
2
3
4 class TestMinMax(BaseTestPyPyC):
5 def test_min_max(self):
6 def main():
7 i=0
8 sa=0
9 while i < 300:
10 sa+=min(max(i, 3000), 4000)
11 i+=1
12 return sa
13 log = self.run(main, [])
14 assert log.result == 300*3000
15 loop, = log.loops_by_filename(self.filepath)
16 assert loop.match("""
17 i7 = int_lt(i4, 300)
18 guard_true(i7, descr=...)
19 guard_not_invalidated(descr=...)
20 i9 = int_add_ovf(i5, 3000)
21 guard_no_overflow(descr=...)
22 i11 = int_add(i4, 1)
23 --TICK--
24 jump(..., descr=...)
25 """)
26
27 def test_silly_max(self):
28 def main():
29 i = 13
30 sa = 0
31 while i < 30000:
32 lst = list(range(i % 1000 + 2))
33 sa += max(*lst) # ID: callmax
34 i += 1
35 return sa
36 log = self.run(main, [])
37 assert log.result == main()
38 loop, = log.loops_by_filename(self.filepath)
39 assert loop.match_by_id('callmax', """
40 ...
41 p76 = call_may_force_r(_, _, _, _, descr=...)
42 ...
43 """)
44
45 #----- the following logic used to check the content of the assembly
46 #----- generated for the loop in max(), but now we no longer produce
47 #----- any custom assembly in this case. It used to say
48 #----- 'call_assembler_r' above, and now it says 'call_may_force_r'.
49 #loop2 = log.loops[0]
50 #loop2.match('''
51 #...
52 #label(..., descr=...)
53 #...
54 #label(..., descr=...)
55 #guard_not_invalidated?
56 #i17 = int_ge(i11, i7)
57 #guard_false(i17, descr=...)
58 #p18 = getarrayitem_gc_r(p5, i11, descr=...)
59 #i19 = int_add(i11, 1)
60 #setfield_gc(p2, i19, descr=...)
61 #guard_nonnull_class(p18, ConstClass(W_IntObject), descr=...)
62 #i20 = getfield_gc_i(p18, descr=...)
63 #i21 = int_gt(i20, i14)
64 #guard_true(i21, descr=...)
65 #jump(..., descr=...)
66 #''')
67 ## XXX could be "guard_class(p18)" instead; we lost somewhere
68 ## the information that it cannot be null.
69
70 def test_iter_max(self):
71 def main():
72 i = 2
73 sa = 0
74 while i < 300:
75 lst = range(i)
76 sa += max(lst) # ID: max
77 i += 1
78 return sa
79 log = self.run(main, [])
80 assert log.result == main()
81 loop, = log.loops_by_filename(self.filepath)
82 # We dont want too many guards, but a residual call to min_max_loop
83 guards = [n for n in log.opnames(loop.ops_by_id("max")) if n.startswith('guard')]
84 assert len(guards) < 20
85 assert loop.match("""
86 ...
87 p76 = call_assembler_r(..., descr=...)
88 ...
89 """)