Debian Codegraph @

rpython/flowspace/test/test_unroll.py

raw symbols SCIP
1 import operator
2
3 from rpython.flowspace.test.test_objspace import Base
4 from rpython.rlib.unroll import unrolling_iterable
5
6
7 class TestUnroll(Base):
8 def test_unroller(self):
9 l = unrolling_iterable(range(10))
10 def f(tot):
11 for v in l:
12 tot += v
13 return tot*3
14 assert f(0) == sum(l)*3
15
16 graph = self.codetest(f)
17 ops = self.all_operations(graph)
18 assert ops == {'inplace_add': 10, 'mul': 1}
19
20 def test_unroll_setattrs(self):
21 values_names = unrolling_iterable(enumerate(['a', 'b', 'c']))
22 def f(x):
23 for v, name in values_names:
24 setattr(x, name, v)
25
26 graph = self.codetest(f)
27 ops = self.all_operations(graph)
28 assert ops == {'setattr': 3}
29
30 def test_unroll_ifs(self):
31 operations = unrolling_iterable([operator.lt,
32 operator.le,
33 operator.eq,
34 operator.ne,
35 operator.gt,
36 operator.ge])
37 def accept(n):
38 "stub"
39 def f(x, y):
40 for op in operations:
41 if accept(op):
42 op(x, y)
43
44 graph = self.codetest(f)
45 ops = self.all_operations(graph)
46 assert ops == {'simple_call': 6,
47 'bool': 6,
48 'lt': 1,
49 'le': 1,
50 'eq': 1,
51 'ne': 1,
52 'gt': 1,
53 'ge': 1}
54
55 def test_unroll_twice(self):
56 operations = unrolling_iterable([1, 2, 3])
57 def f(x):
58 for num1 in operations:
59 for num2 in operations:
60 x = x + (num1 + num2)
61 return x
62
63 graph = self.codetest(f)
64 ops = self.all_operations(graph)
65 assert ops['add'] == 9