Debian Codegraph @

rpython/translator/backendopt/test/test_removeassert.py

raw symbols SCIP
1 from rpython.flowspace.model import summary
2 from rpython.translator.backendopt.removeassert import remove_asserts
3 from rpython.translator.backendopt.constfold import constant_fold_graph
4 from rpython.translator.backendopt.test import test_constfold
5 from rpython.translator.backendopt.test.test_constfold import check_graph
6
7
8 def get_graph(fn, signature):
9 graph, t = test_constfold.get_graph(fn, signature)
10 constant_fold_graph(graph)
11 return graph, t
12
13 def contains_raise(graph):
14 for link in graph.iterlinks():
15 if link.target is graph.exceptblock:
16 return True
17 else:
18 return False
19
20 def check(fn, args, expected_result, remaining_raise=False):
21 signature = [int] * len(args) # for now
22 graph, t = get_graph(fn, signature)
23 remove_asserts(t, [graph])
24 assert contains_raise(graph) == remaining_raise
25 check_graph(graph, args, expected_result, t)
26 return t, graph
27
28
29 def test_simple():
30 def fn(n):
31 assert n >= 1
32 return n-1
33 check(fn, [125], 124)
34
35 def test_simple_melting_away():
36 def fn(n):
37 assert n >= 1
38 return n-1
39 graph, t = get_graph(fn, [int])
40 assert summary(graph) == {'int_ge': 1, 'int_sub': 1}
41 remove_asserts(t, [graph])
42 assert summary(graph) == {'int_ge': 1, 'debug_assert': 1, 'int_sub': 1}
43 check_graph(graph, [1], 0, t)
44 from rpython.translator.backendopt.removenoops import remove_debug_assert
45 remove_debug_assert(graph)
46 assert summary(graph) == {'int_ge': 1, 'int_sub': 1}
47 from rpython.translator.simplify import transform_dead_op_vars
48 transform_dead_op_vars(graph)
49 assert summary(graph) == {'int_sub': 1}
50
51 def test_and():
52 def fn(n):
53 assert n >= 1 and n < 10
54 return n-1
55 check(fn, [1], 0)
56
57 def test_or():
58 def fn(n):
59 assert n >= 1 or n % 2 == 0
60 return n-1
61 check(fn, [-120], -121)
62
63 def test_isinstance():
64 class A:
65 pass
66 class B(A):
67 pass
68 def g(n):
69 if n > 10:
70 return A()
71 else:
72 b = B()
73 b.value = 321
74 return b
75 def fn(n):
76 x = g(n)
77 assert isinstance(x, B)
78 return x.value
79 t, graph = check(fn, [5], 321)
80 assert summary(graph)['debug_assert'] == 1
81 from rpython.translator.backendopt.removenoops import remove_debug_assert
82 remove_debug_assert(graph)
83 assert "debug_assert" not in summary(graph)
84 from rpython.translator.simplify import transform_dead_op_vars
85 transform_dead_op_vars(graph, t)
86 assert summary(graph)["direct_call"] == 1
87
88 def test_with_exception():
89 def g(n):
90 if n < 0:
91 raise ValueError
92 def fn(n):
93 try:
94 g(n)
95 assert False
96 except ValueError:
97 return 42
98 check(fn, [-8], 42, remaining_raise=True)
99
100