File: test_model.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 (121 lines) | stat: -rw-r--r-- 4,062 bytes parent folder | download | duplicates (9)
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
import inspect

from rpython.flowspace.model import *


def sample_function(i):
    sum = 0
    while i > 0:
        sum = sum + i
        i = i - 1
    return sum

class pieces:
    """ The manually-built graph corresponding to the sample_function().
    """
    i0 = Variable("i0")
    i1 = Variable("i1")
    i2 = Variable("i2")
    i3 = Variable("i3")
    sum1 = Variable("sum1")
    sum2 = Variable("sum2")
    sum3 = Variable("sum3")

    conditionres = Variable("conditionres")
    conditionop = SpaceOperation("gt", [i1, Constant(0)], conditionres)
    addop = SpaceOperation("add", [sum2, i2], sum3)
    decop = SpaceOperation("sub", [i2, Constant(1)], i3)
    startblock = Block([i0])
    headerblock = Block([i1, sum1])
    whileblock = Block([i2, sum2])

    graph = FunctionGraph("f", startblock)
    startblock.closeblock(Link([i0, Constant(0)], headerblock))
    headerblock.operations.append(conditionop)
    headerblock.exitswitch = conditionres
    headerblock.closeblock(Link([sum1], graph.returnblock, False),
                           Link([i1, sum1], whileblock, True))
    whileblock.operations.append(addop)
    whileblock.operations.append(decop)
    whileblock.closeblock(Link([i3, sum3], headerblock))

    graph.func = sample_function

graph = pieces.graph

# ____________________________________________________________

def test_checkgraph():
    checkgraph(graph)

def test_copygraph():
    graph2 = copygraph(graph)
    checkgraph(graph2)

def test_graphattributes():
    assert graph.startblock is pieces.startblock
    assert graph.returnblock is pieces.headerblock.exits[0].target
    assert graph.getargs() == [pieces.i0]
    assert [graph.getreturnvar()] == graph.returnblock.inputargs
    assert graph.source == inspect.getsource(sample_function)

def test_iterblocks():
    assert list(graph.iterblocks()) == [pieces.startblock,
                                        pieces.headerblock,
                                        graph.returnblock,
                                        pieces.whileblock]

def test_iterlinks():
    assert list(graph.iterlinks()) == [pieces.startblock.exits[0],
                                       pieces.headerblock.exits[0],
                                       pieces.headerblock.exits[1],
                                       pieces.whileblock.exits[0]]

def test_mkentrymap():
    entrymap = mkentrymap(graph)
    startlink = entrymap[graph.startblock][0]
    assert entrymap == {
        pieces.startblock:  [startlink],
        pieces.headerblock: [pieces.startblock.exits[0],
                             pieces.whileblock.exits[0]],
        graph.returnblock:  [pieces.headerblock.exits[0]],
        pieces.whileblock:  [pieces.headerblock.exits[1]],
        }

def test_blockattributes():
    block = pieces.whileblock
    assert block.getvariables() == [pieces.i2,
                                    pieces.sum2,
                                    pieces.sum3,
                                    pieces.i3]
    assert block.getconstants() == [Constant(1)]

def test_renamevariables():
    block = pieces.whileblock
    v = Variable()
    block.renamevariables({pieces.sum2: v})
    assert block.getvariables() == [pieces.i2,
                                    v,
                                    pieces.sum3,
                                    pieces.i3]
    block.renamevariables({v: pieces.sum2})
    assert block.getvariables() == [pieces.i2,
                                    pieces.sum2,
                                    pieces.sum3,
                                    pieces.i3]

def test_variable():
    v = Variable()
    assert v.name[0] == 'v' and v.name[1:].isdigit()
    assert not v.renamed
    v.rename("foobar")
    name1 = v.name
    assert name1.startswith('foobar_')
    assert name1.split('_', 1)[1].isdigit()
    assert v.renamed
    v.rename("not again")
    assert v.name == name1
    v2 = Variable(v)
    assert v2.renamed
    assert v2.name.startswith("foobar_") and v2.name != v.name
    assert v2.name.split('_', 1)[1].isdigit()