Пример #1
0
class UpdateClockTest(unittest.TestCase):
    """Test logical clock in MessageLog.

    TODO this test case should be moved from here.
    """
    def setUp(self):
        with DeltaGraph() as self.test_graph:
            const_consumer(obj=forward.call(num=return_1()))

    def test_clock_updates(self):
        """Messages should be displayed at end of run in logical clock order.
        """
        self.rt = DeltaPySimulator(self.test_graph, msg_lvl=logging.DEBUG)
        self.rt.run()
        message_times = [
            msg.clk for node, _, msg in self.rt.msg_log.messages
            if node.startswith("const_consumer")
        ]
        self.assertGreater(len(message_times), 0)
        for time_1, time_2 in zip(message_times, message_times[1:]):
            self.assertLess(time_1, time_2)

    def test_no_message_logging(self):
        """Messages should not be logged at all.
        """
        self.rt = DeltaPySimulator(self.test_graph)
        self.rt.run()
        message_times = [
            msg.clk for node, _, msg in self.rt.msg_log.messages
            if node.startswith("const_consumer")
        ]
        self.assertEqual(message_times, [])
        for time_1, time_2 in zip(message_times, message_times[1:]):
            self.assertLess(time_1, time_2)
Пример #2
0
class ComplexGraph(unittest.TestCase):
    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)

    def test_run(self):
        """This graph should run, adding 2 to the number on every cycle."""
        self.runtime.run()
        self.assertEqual(self.saver.saved, [11])

    def test_graph_properties(self):
        self.graph.check()
Пример #3
0
 def test_method(self):
     foo = Foo()
     with DeltaGraph() as test_graph:
         foo.add_set_x(a=4, b=5)
     rt = DeltaPySimulator(test_graph)
     rt.run()
     self.assertEqual(foo.x, 9)
Пример #4
0
 def test_placeholder_method(self):
     foo = Foo()
     with DeltaGraph() as test_graph:
         n = placeholder_node_factory(a=4, b=5)
     n.specify_by_method(Foo.add_set_x, foo, node_key="node")
     rt = DeltaPySimulator(test_graph)
     rt.run()
     self.assertEqual(foo.x, 9)
Пример #5
0
    def test_func(self):
        @DeltaBlock(node_key="node")
        def add_assert(a: int, b: int, node: PythonNode = None) -> Void:
            self.assertEqual(node.receive('a') + node.receive('b'), 9)
            raise DeltaRuntimeExit

        with DeltaGraph() as test_graph:
            add_assert(a=4, b=5)
        rt = DeltaPySimulator(test_graph)
        rt.run()
Пример #6
0
 def test_migen_node_reset_shaper_constant(self, mock_stdout):
     """One PyMigenBody that takes as input a reset signal and extends the
     length of the reset to N clock cycles reset.
     """
     graph, _ = generate_graph_constant_input()
     rt = DeltaPySimulator(graph)
     rt.run()
     self.assertEqual(
         mock_stdout.getvalue(),
         'CHECK_SHAPE: reset has lasted at least 5 clk cycles\n')
Пример #7
0
    def test_placeholder_func(self):
        @DeltaBlock(node_key="node")
        def add_assert(a: int, b: int, node: PythonNode = None) -> Void:
            self.assertEqual(node.receive('a') + node.receive('b'), 9)
            raise DeltaRuntimeExit

        with DeltaGraph() as test_graph:
            n = placeholder_node_factory(a=4, b=5)
        n.specify_by_func(add_assert, node_key="node")
        rt = DeltaPySimulator(test_graph)
        rt.run()
Пример #8
0
 def test_no_message_logging(self):
     """Messages should not be logged at all.
     """
     self.rt = DeltaPySimulator(self.test_graph)
     self.rt.run()
     message_times = [
         msg.clk for node, _, msg in self.rt.msg_log.messages
         if node.startswith("const_consumer")
     ]
     self.assertEqual(message_times, [])
     for time_1, time_2 in zip(message_times, message_times[1:]):
         self.assertLess(time_1, time_2)
Пример #9
0
 def test_clock_updates(self):
     """Messages should be displayed at end of run in logical clock order.
     """
     self.rt = DeltaPySimulator(self.test_graph, msg_lvl=logging.DEBUG)
     self.rt.run()
     message_times = [
         msg.clk for node, _, msg in self.rt.msg_log.messages
         if node.startswith("const_consumer")
     ]
     self.assertGreater(len(message_times), 0)
     for time_1, time_2 in zip(message_times, message_times[1:]):
         self.assertLess(time_1, time_2)
Пример #10
0
    def test_migen_node_reset_shaper_pulse(self, mock_stdout):
        """One PyMigenBody that takes as input a reset signal and extends the
        length of the reset to N clock cycles reset. We generate a pulse of
        length 1 clock cycle (i.e. 0010) and check for (00000...111110)
        """

        graph, _ = generate_graph_interactive_input(verbose=False)
        rt = DeltaPySimulator(graph)
        rt.run()
        self.assertEqual(
            mock_stdout.getvalue(),
            'CHECK_SHAPE: reset has lasted exactly 5 clk cycles\n')
Пример #11
0
    def test_splitting_of_one_forked_non_const(self):
        """One of forked outputs of a non-constant node is splitted."""
        s1 = StateSaver(int)
        s2 = StateSaver(int)
        with DeltaGraph() as graph:
            val = return_12_non_const()
            val_x = val.x
            s1.save(val_x)
            s2.save_and_exit(val_x)

        rt = DeltaPySimulator(graph)
        rt.run()
        self.assertEqual(s2.saved, [1])
Пример #12
0
    def test_one_migen_node_with_2_outs(self):
        """One PyMigenBody with 2 out ports produces what we expect."""
        s = StateSaver(int, verbose=True)

        with DeltaGraph() as graph:
            counter = TestMigen(tb_num_iter=2000,
                                name='counter',
                                lvl=logging.INFO).call(in1=40, in2=2)
            s.save_and_exit(adder(counter.out1, multiplier(counter.out2)))

        rt = DeltaPySimulator(graph)
        rt.run()

        self.assertEqual(s.saved, [5042])
Пример #13
0
    def test_merge_use_in_graph(self):
        """Test to ensure in params must match when associating
        constructors with a NodeTemplate.
        """
        test_template3 = NodeTemplate(name="test_3",
                                      inputs=[('a', int), ('b', int)],
                                      outputs=[('output', int)])

        @DeltaBlock(template=test_template3, allow_const=False)
        def _simple_add_2(a: int, b: int) -> int:
            return a + b

        test_template2 = NodeTemplate(name="test_2",
                                      inputs=[('a', int), ('b', int)],
                                      outputs=[('output', int)])

        @DeltaBlock(template=test_template2, allow_const=False)
        def simple_add_3(a: int, b: int) -> int:
            return a + b

        test_template3.merge(test_template2)

        saver = StateSaver()
        with DeltaGraph() as graph:
            n1 = simple_add_3(4, 73)
            saver.save_and_exit(n1)

        self.assertEqual(len(n1.bodies), 2)

        graph.select_bodies(preferred=['_simple_add_2'])
        self.assertIn("_simple_add_2", n1.body.access_tags)
        DeltaPySimulator(graph).run()
        self.assertEqual([77], saver.saved)
Пример #14
0
 def test_split_nodes(self):
     """Test that there is the correct number of nodes _before_
     and _after_ the runtime adds any splitter nodes.
     """
     self.assertEqual(len(self.graph.nodes), len(self.savers) + 1)
     DeltaPySimulator(self.graph)
     self.assertEqual(len(self.graph.nodes), len(self.savers) + 2)
Пример #15
0
 def test_via_Interactive(self):
     saver = StateSaver()
     with DeltaGraph() as graph:
         n1 = broken_adder.call(a=1, b=3)
         saver.save_and_exit(n1)
     self.assertEqual(len(n1.bodies), 3)
     self.assertIn("broken_adder", n1.body.access_tags)
     DeltaPySimulator(graph).run()
     self.assertEqual([5], saver.saved)
Пример #16
0
 def test_via_DeltaBlock(self):
     saver = StateSaver()
     with DeltaGraph() as graph:
         n1 = simple_add(4, 3)
         saver.save_and_exit(n1)
     self.assertEqual(len(n1.bodies), 3)
     self.assertIn("simple_add", n1.body.access_tags)
     DeltaPySimulator(graph).run()
     self.assertEqual([7], saver.saved)
Пример #17
0
 def test_via_default_call(self):
     saver = StateSaver()
     with DeltaGraph() as graph:
         n1 = test_template1.call(9, 2)
         saver.save_and_exit(n1)
     self.assertEqual(len(n1.bodies), 3)
     self.assertIn("simple_add", n1.body.access_tags)
     DeltaPySimulator(graph).run()
     self.assertEqual([11], saver.saved)
Пример #18
0
    def test_forked_return_non_const_to_non_const(self):
        """Non-constant node w/ forked return -> non-constant -> exit."""
        s = StateSaver(int)
        with DeltaGraph() as graph:
            val = return_12_non_const()
            s.save_and_exit(increment_non_const(val.x))

        DeltaPySimulator(graph).run()
        self.assertEqual(s.saved, [2])
Пример #19
0
    def test_one_migen_node_with_separate_ctrl_on_output_valid(self):
        """One PyMigenBody with 2 optional output ports produces
        the correct valid values (for the different ports).
        The migen node should be generating a sequence of outputs
        (1, None), (None, 2), (3, None) etc... """
        with DeltaGraph() as graph:
            alt = AlternatingOutputsMigen(tb_num_iter=100,
                                          name='alternatingOutput',
                                          lvl=logging.INFO).call(start=1)
            my_adder = add_non_const(alt.out_a, alt.out_b)
            # Checking that we have received a 1 and a 2
            checker = InputCheckerWithExit(lambda x: (x == 3))
            checker.check(my_adder)

        rt = DeltaPySimulator(graph)
        rt.run()

        self.assertEqual(checker.cond_met, True)
Пример #20
0
 def setUp(self):
     with DeltaGraph() as graph:
         ident_ph = placeholder_node_factory()
         inter = interactive_func.call(num=ident_ph)
         ident = int_to_str(inter)
         ident_ph.specify_by_node(ident)
     self.graph = graph
     self.runtime = DeltaPySimulator(self.graph)
     self.dummy_node = ident
     self.inter_node = inter
Пример #21
0
    def test_tuple_first_sending_alone(self):
        """Demonstration of how a user can run into an error when sending
        tuples as the first part of an output, and not specifing the other
        outputs.
        """
        with DeltaGraph() as graph:
            tuple_first_alone()

        with self.assertRaises(RuntimeError):
            DeltaPySimulator(graph).run()
Пример #22
0
    def test_migen_trigger_succeeds(self):
        """Assert that when the `test_bench_ye_trigger` node sends data, the
        ouput of the migen node is 15 since the data signal is available on the
        particular clock cycle that loads in the data.
        """

        with DeltaGraph() as graph:

            test_bench_output = test_bench_yes_trigger.call()

            c1 = TestMigenNode(tb_num_iter=10).call(
                inp=test_bench_output.inp, trigger=test_bench_output.trigger)

            self.saver.save_and_exit(c1.out)

        rt = DeltaPySimulator(graph)
        rt.run()

        self.assertEqual(self.saver.saved[0], 15)
Пример #23
0
    def test_splitting_to_one_node_non_const(self):
        """Splitted output of a non-constant node and sent to another node's
        different inputs."""
        s = StateSaver(int)
        with DeltaGraph() as graph:
            val = return_1_non_const()
            s.save_and_exit(add_non_const(val, val))

        DeltaPySimulator(graph).run()
        self.assertEqual(s.saved, [2])
Пример #24
0
    def test_migen_trigger_fails(self):
        """Assert that when the `test_bench_no_trigger` node sends data, the
        ouput of the migen node is 0 due to the data not loading into output
        signal properly.
        """

        with DeltaGraph() as graph:

            test_bench_output = test_bench_no_trigger.call()

            c1 = TestMigenNode(tb_num_iter=10).call(
                inp=test_bench_output.inp, trigger=test_bench_output.trigger)

            self.saver.save_and_exit(c1.out)

        rt = DeltaPySimulator(graph)
        rt.run()

        self.assertEqual(self.saver.saved[0], 0)
Пример #25
0
 def test_via_DeltaMethodBlock(self):
     saver = StateSaver()
     cacher = OpCacher()
     with DeltaGraph() as graph:
         n1 = cacher.cached_add(1, 3)
         saver.save_and_exit(n1)
     self.assertEqual(len(n1.bodies), 4)
     self.assertIn("cached_add", n1.body.access_tags)
     DeltaPySimulator(graph).run()
     self.assertEqual([4], saver.saved)
Пример #26
0
    def test_splitting_of_multiple_forked_non_const(self):
        """Multiple forked outputs of a non-constant node are splitted."""
        s = StateSaver(int)
        with DeltaGraph() as graph:
            val = return_12_non_const()
            s.save_and_exit(
                add_non_const(
                    add_non_const(add_non_const(val.x, val.x), val.y), val.y))

        DeltaPySimulator(graph).run()
        self.assertEqual(s.saved, [6])
Пример #27
0
    def test_queues(self):
        """Test that the queues out of the splitter node are DeltaQueues."""
        splitter_node = self.graph.find_node_by_name("splitter")
        self.assertTrue(splitter_node is None)

        runtime = DeltaPySimulator(self.graph)
        splitter_node = self.graph.find_node_by_name("splitter")
        out_qs = runtime.out_queues[splitter_node.name]

        self.assertEqual(len(out_qs), 3)
        for q in out_qs.values():
            self.assertIsInstance(q, DeltaQueue)
Пример #28
0
    def test_positional_block_send(self):
        ts = TripleStateSaver(11)
        fs = ForkedSendTester()

        with DeltaGraph() as graph:
            b_send = fs.positional_send()
            ts.multi_count_print_exit(b_send.x, b_send.y, b_send.z)

        DeltaPySimulator(graph).run()
        self.assertEqual(ts.x_store, [1, 3, 5, 6, 7, 10])
        self.assertEqual(ts.y_store, [2, 4])
        self.assertEqual(ts.z_store, [False, True, False])
Пример #29
0
    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)
Пример #30
0
    def test_template_different_selected(self):
        """Test that node key is still correctly used on a multi-body
        node that has its body selected different from the one that was
        specified via the constructor.
        """
        foo = Foo_T()
        with DeltaGraph() as test_graph:
            n1 = add_print_t(a=4, b=5)

        n1.add_body(foo.add_set_x_t)
        test_graph.select_bodies(preferred=["add_set_x_t"])
        DeltaPySimulator(test_graph).run()
        self.assertIn("add_set_x_t", n1.body.access_tags)
        self.assertEqual(foo.x, 9)