示例#1
0
def test_dfs_graphs():
    g0 = Graph()
    in0 = Constant(g0)
    in1 = Constant(1)
    g0.return_ = in1
    value = Apply([in0], Graph())
    assert set(dfs(value)) == {value, in0}
    assert set(dfs(value, follow_graph=True)) == {value, in0, in1}
示例#2
0
def test_set_inputs_property():
    in0 = Constant(0)
    in1 = Constant(1)
    value = Apply([in0], Graph())
    value.inputs = [in1]
    assert in0.uses == set()
    assert in1.uses == {(value, 0)}
示例#3
0
def test_graph():
    """Construct a small graph.

    Note that this graph is strictly speaking nonsensical, because it doesn't
    use any actual primitive operations.
    """
    g = Graph()
    x = Parameter(g)
    assert x.value is PARAMETER
    one = Constant(1)
    add = Constant('add')
    return_ = Constant('return')
    value = Apply([add, x, one], g)
    return_ = Apply([return_, value], g)
    g.return_ = return_
    g.parameters.append(x)
示例#4
0
def test_del_inputs(index):
    in0 = Constant(0)
    in1 = Constant(1)
    value = Apply([in0, in1], Graph())
    del value.inputs[index]
    assert in0.uses == set()
    assert in1.uses == {(value, 0)}
    assert value.inputs[0] is in1
示例#5
0
def test_set_inputs(index):
    in0 = Constant(0)
    in1 = Constant(1)
    value = Apply([in0], Graph())
    value.inputs[index] = in1
    assert value.inputs[0] is in1
    assert in0.uses == set()
    assert in1.uses == {(value, 0)}
示例#6
0
def test_graph_output():
    g = Graph()
    with pytest.raises(Exception):
        print(g.output)
    one = Constant(1)
    g.output = one
    assert g.output is one
    assert isinstance(g.return_, Apply) and \
        len(g.return_.inputs) == 2 and \
        isinstance(g.return_.inputs[0], Constant) and \
        g.return_.inputs[0].value is primops.return_ and \
        g.return_.inputs[1] is one
    old_return = g.return_
    two = Constant(2)
    g.output = two
    assert g.return_ is old_return
    assert g.return_.inputs[1] is two
示例#7
0
def test_insert_inputs(index):
    in0 = Constant(0)
    in1 = Constant(1)
    value = Apply([in1], Graph())
    value.inputs.insert(index, in0)
    assert value.inputs[0] is in0
    assert value.inputs[1] is in1
    assert in0.uses == {(value, 0)}
    assert in1.uses == {(value, 1)}
示例#8
0
def test_slice_inputs():
    in0 = Constant(0)
    in1 = Constant(1)
    value = Apply([in0, in1], Graph())
    assert value.inputs[:] == [in0, in1]
    with pytest.raises(ValueError):
        del value.inputs[:]
    with pytest.raises(ValueError):
        value.inputs[:] = [in0]
示例#9
0
def test_copy():
    in0 = Constant(0)
    value0 = Apply([in0], Graph())
    value1 = copy.copy(value0)
    in1 = copy.copy(in0)
    assert value1.inputs == value0.inputs
    assert value1.inputs is not value0.inputs
    assert in0.uses == {(value0, 0), (value1, 0)}
    assert in1.uses == set()
示例#10
0
def test_graph_helpers():
    """Test the helper methods on graphs."""
    g = Graph()
    x = g.add_parameter()
    y = g.add_parameter()
    assert g.parameters == [x, y]
    one = g.constant(1)
    add = g.constant('add')
    temp = g.apply('mul', one, 2)
    assert temp.graph is g
    assert all(isinstance(x, Constant) for x in temp.inputs)
    assert list(x.value for x in temp.inputs) == ['mul', 1, 2]
    g.output = g.apply(add, temp, x)
    assert g.output.graph is g
    assert list(g.output.inputs) == [add, temp, x]
示例#11
0
def test_helpers():
    g = Graph()
    cg = Constant(g)
    assert is_constant_graph(cg)

    one = Constant(1)
    assert not is_constant_graph(one)
    assert is_constant(one)

    a = Apply([cg, one], g)
    assert is_apply(a)

    p = Parameter(g)
    assert is_parameter(p)
示例#12
0
def test_str_coverage():
    """Just a coverage test for __str__ and __repr__

    Doesn't check that they take particular values since that could change
    easily.
    """
    g = Graph()
    p = Parameter(g)
    p.name = 'param'
    objects = [g, Apply([], g), p, Parameter(g), Constant(0), Constant(g)]
    for o in objects:
        str(o)
        repr(o)
        o.debug.debug_name
示例#13
0
    def __init__(self, parser: Parser) -> None:
        """Construct a basic block.

        Constructing a basic block also constructs a corresponding function,
        and a constant that can be used to call this function.

        """
        self.parser = parser

        self.matured: bool = False
        self.variables: Dict[str, ANFNode] = {}
        self.preds: List[Block] = []
        self.phi_nodes: Dict[Parameter, str] = {}
        self.jumps: Dict[Block, Apply] = {}
        self.graph: Graph = Graph()
示例#14
0
def test_toposort():
    g0 = Graph()
    g0.output = Constant(1)
    g1 = Graph()
    in0 = Constant(g0)
    value = Apply([in0], g1)
    g1.output = value

    order = list(toposort(g1.return_))
    _check_toposort(order, g1.return_, succ_incoming)
示例#15
0
def test_toposort2():
    g0 = Graph()
    g0.output = Constant(33)
    g1 = Graph()
    in0 = Constant(g0)
    in1 = Constant(1)
    v1 = Apply([in0, in1], g1)
    v2 = Apply([in0, v1, in1], g1)
    g1.output = v2

    order = list(toposort(g1.return_))
    _check_toposort(order, g1.return_, succ_incoming)
示例#16
0
def test_repr_inputs():
    in0 = Constant(0)
    in1 = Constant(1)
    value = Apply([in0, in1], Graph())
    assert repr(value.inputs)
    assert str(value.inputs)
示例#17
0
def test_get_inputs(index):
    in0 = Constant(0)
    value = Apply([in0], Graph())
    assert value.inputs[index] == in0
示例#18
0
def test_append_inputs():
    in0 = Constant(0)
    value = Apply([], Graph())
    value.inputs.append(in0)
    assert in0.uses == {(value, 0)}
示例#19
0
def test_dfs():
    in0 = Constant(0)
    in1 = Constant(1)
    value = Apply([in0, in1], Graph())
    assert next(dfs(value)) == value
    assert set(dfs(value)) == {value, in0, in1}
示例#20
0
def test_incoming():
    in0 = Constant(0)
    value = Apply([in0], Graph())
    assert list(value.incoming) == [in0]
    assert list(in0.incoming) == []
示例#21
0
def test_outgoing():
    in0 = Constant(0)
    value = Apply([in0], Graph())
    assert list(value.outgoing) == []
    assert list(in0.outgoing) == [value]
示例#22
0
def test_len_inputs():
    in0 = Constant(0)
    in1 = Constant(1)
    value = Apply([in0, in1], Graph())
    assert len(value.inputs) == 2
示例#23
0
def test_init_inputs():
    in0 = Constant(0)
    value = Apply([in0], Graph())
    assert in0.uses == {(value, 0)}