def test_simple_folding_chain(self): """Test that a chain of node tagged as constant will turn to constant nodes. """ with DeltaGraph() as graph: n0 = return_2() n1 = foo_const(n0) n2 = foo_const(n1) self.assertIsInstance(n0.body, PyConstBody) self.assertIsInstance(n1.body, PyConstBody) self.assertIsInstance(n2.body, PyConstBody) self.assertTrue(graph.check())
def setUp(self): """ Create the graph: return_4 -> add -> add_member -> print -> saver / return_2 / """ self.saver = dl.lib.StateSaver(int) with dl.DeltaGraph() as graph: inst = OtherClass(2) const = add(return_4(), return_2()) num = inst.add_member(const) self.saver.save_and_exit(num) self.graph = graph self.runtime = dl.DeltaPySimulator(graph)
def test_simple_folding_chain_splitting(self): """Test that if an output of a constant node can be split between a constant and non-constan nodes, i.e. the source stays constant and only one of the destinations is constant. """ with DeltaGraph() as graph: n0 = return_2() n1 = foo_const(n0) n2 = foo_const(n1) n3 = foo_non_const(n1) self.assertIsInstance(n0.body, PyConstBody) self.assertIsInstance(n1.body, PyConstBody) self.assertIsInstance(n2.body, PyConstBody) self.assertIsInstance(n3.body, PyFuncBody) self.assertTrue(graph.check())
def test_serialize_state_saver(self): """Serialise a state saver and load it in a new instance.""" saver = StateSaver(t=int, verbose=True) with DeltaGraph() as test_graph: saver_node = saver.save(return_2()) saver_body = saver_node.body.as_serialized python_string = f""" import dill saver_body = dill.loads({saver_body}) saver_body.eval(5) """ p = subprocess.run([r"python"], input=str.encode(python_string), stdout=subprocess.PIPE, check=False) output = p.stdout.decode() self.assertEqual(output, "saving 5\n")
def setUp(self): """Constructs the following graph: +---------------------------+ | SAVE | | +-----+ ^ | +--->Add +----+ | |to 10| +----+ | | +--------> | | +-----+ |ADD +---+ 2 --->| | +----+ """ self.saver = StateSaver(int) with DeltaGraph() as graph: add_ph = placeholder_node_factory() b = return_2() self.int_node = add_until_10.call(num=add_ph) add_node = add_non_const(b, self.int_node.x) add_ph.specify_by_node(add_node) self.saver.save_and_exit(self.int_node.y) self.graph = graph self.runtime = DeltaPySimulator(self.graph)