| 1 |
import inspect |
| 2 |
|
| 3 |
from rpython.flowspace.model import * |
| 4 |
|
| 5 |
|
| 6 |
def sample_function(i): |
| 7 |
sum = 0 |
| 8 |
while i > 0: |
| 9 |
sum = sum + i |
| 10 |
i = i - 1 |
| 11 |
return sum |
| 12 |
|
| 13 |
class pieces: |
| 14 |
""" The manually-built graph corresponding to the sample_function(). |
| 15 |
""" |
| 16 |
i0 = Variable("i0") |
| 17 |
i1 = Variable("i1") |
| 18 |
i2 = Variable("i2") |
| 19 |
i3 = Variable("i3") |
| 20 |
sum1 = Variable("sum1") |
| 21 |
sum2 = Variable("sum2") |
| 22 |
sum3 = Variable("sum3") |
| 23 |
|
| 24 |
conditionres = Variable("conditionres") |
| 25 |
conditionop = SpaceOperation("gt", [i1, Constant(0)], conditionres) |
| 26 |
addop = SpaceOperation("add", [sum2, i2], sum3) |
| 27 |
decop = SpaceOperation("sub", [i2, Constant(1)], i3) |
| 28 |
startblock = Block([i0]) |
| 29 |
headerblock = Block([i1, sum1]) |
| 30 |
whileblock = Block([i2, sum2]) |
| 31 |
|
| 32 |
graph = FunctionGraph("f", startblock) |
| 33 |
startblock.closeblock(Link([i0, Constant(0)], headerblock)) |
| 34 |
headerblock.operations.append(conditionop) |
| 35 |
headerblock.exitswitch = conditionres |
| 36 |
headerblock.closeblock(Link([sum1], graph.returnblock, False), |
| 37 |
Link([i1, sum1], whileblock, True)) |
| 38 |
whileblock.operations.append(addop) |
| 39 |
whileblock.operations.append(decop) |
| 40 |
whileblock.closeblock(Link([i3, sum3], headerblock)) |
| 41 |
|
| 42 |
graph.func = sample_function |
| 43 |
|
| 44 |
graph = pieces.graph |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
def test_checkgraph(): |
| 49 |
checkgraph(graph) |
| 50 |
|
| 51 |
def test_copygraph(): |
| 52 |
graph2 = copygraph(graph) |
| 53 |
checkgraph(graph2) |
| 54 |
|
| 55 |
def test_graphattributes(): |
| 56 |
assert graph.startblock is pieces.startblock |
| 57 |
assert graph.returnblock is pieces.headerblock.exits[0].target |
| 58 |
assert graph.getargs() == [pieces.i0] |
| 59 |
assert [graph.getreturnvar()] == graph.returnblock.inputargs |
| 60 |
assert graph.source == inspect.getsource(sample_function) |
| 61 |
|
| 62 |
def test_iterblocks(): |
| 63 |
assert list(graph.iterblocks()) == [pieces.startblock, |
| 64 |
pieces.headerblock, |
| 65 |
graph.returnblock, |
| 66 |
pieces.whileblock] |
| 67 |
|
| 68 |
def test_iterlinks(): |
| 69 |
assert list(graph.iterlinks()) == [pieces.startblock.exits[0], |
| 70 |
pieces.headerblock.exits[0], |
| 71 |
pieces.headerblock.exits[1], |
| 72 |
pieces.whileblock.exits[0]] |
| 73 |
|
| 74 |
def test_mkentrymap(): |
| 75 |
entrymap = mkentrymap(graph) |
| 76 |
startlink = entrymap[graph.startblock][0] |
| 77 |
assert entrymap == { |
| 78 |
pieces.startblock: [startlink], |
| 79 |
pieces.headerblock: [pieces.startblock.exits[0], |
| 80 |
pieces.whileblock.exits[0]], |
| 81 |
graph.returnblock: [pieces.headerblock.exits[0]], |
| 82 |
pieces.whileblock: [pieces.headerblock.exits[1]], |
| 83 |
} |
| 84 |
|
| 85 |
def test_blockattributes(): |
| 86 |
block = pieces.whileblock |
| 87 |
assert block.getvariables() == [pieces.i2, |
| 88 |
pieces.sum2, |
| 89 |
pieces.sum3, |
| 90 |
pieces.i3] |
| 91 |
assert block.getconstants() == [Constant(1)] |
| 92 |
|
| 93 |
def test_renamevariables(): |
| 94 |
block = pieces.whileblock |
| 95 |
v = Variable() |
| 96 |
block.renamevariables({pieces.sum2: v}) |
| 97 |
assert block.getvariables() == [pieces.i2, |
| 98 |
v, |
| 99 |
pieces.sum3, |
| 100 |
pieces.i3] |
| 101 |
block.renamevariables({v: pieces.sum2}) |
| 102 |
assert block.getvariables() == [pieces.i2, |
| 103 |
pieces.sum2, |
| 104 |
pieces.sum3, |
| 105 |
pieces.i3] |
| 106 |
|
| 107 |
def test_variable(): |
| 108 |
v = Variable() |
| 109 |
assert v.name[0] == 'v' and v.name[1:].isdigit() |
| 110 |
assert not v.renamed |
| 111 |
v.rename("foobar") |
| 112 |
name1 = v.name |
| 113 |
assert name1.startswith('foobar_') |
| 114 |
assert name1.split('_', 1)[1].isdigit() |
| 115 |
assert v.renamed |
| 116 |
v.rename("not again") |
| 117 |
assert v.name == name1 |
| 118 |
v2 = Variable(v) |
| 119 |
assert v2.renamed |
| 120 |
assert v2.name.startswith("foobar_") and v2.name != v.name |
| 121 |
assert v2.name.split('_', 1)[1].isdigit() |