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)
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)
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
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]