Exemple #1
0
    def test_last_state(self):
        """
        Tests that Set=low & Reset=Low provides the previous state
        """
        set_node = Node(State.low, name="Set")
        reset_node = Node(State.high, name="Reset")
        latch = SRNandLatch([set_node, reset_node])
        out_nodes = latch.calculate()
        assert out_nodes["Q"] == State.high
        assert out_nodes["QBar"] == State.low

        set_node.state = State.high
        reset_node.state = State.high
        out_nodes = latch.calculate()
        assert out_nodes["Q"] == State.high
        assert out_nodes["QBar"] == State.low

        set_node.state = State.high
        reset_node.state = State.low
        out_nodes = latch.calculate()
        assert out_nodes["Q"] == State.low
        assert out_nodes["QBar"] == State.high

        set_node.state = State.high
        reset_node.state = State.high
        out_nodes = latch.calculate()
        assert out_nodes["Q"] == State.low
        assert out_nodes["QBar"] == State.high
Exemple #2
0
    def test_no_change_state_high_output(self):
        d = Node(State.high, name="D")
        clk = Node(State.high, name="Clk")
        ff = DTypeFlipFlop([d, clk])
        ff.calculate()
        assert ff.outputs["Q"].state == State.high
        assert ff.outputs["QBar"].state == State.low

        d.state = State.low
        clk.state = State.low
        ff.calculate()
        assert ff.outputs["Q"].state == State.high
        assert ff.outputs["QBar"].state == State.low

        d.state = State.high
        clk.state = State.low
        ff.calculate()
        assert ff.outputs["Q"].state == State.high
        assert ff.outputs["QBar"].state == State.low

        d.state = State.low
        clk.state = State.low
        ff.calculate()
        assert ff.outputs["Q"].state == State.high
        assert ff.outputs["QBar"].state == State.low
Exemple #3
0
 def test_get_object_by_name_success(self):
     node1 = Node(name="set")
     node2 = Node(name="Reset")
     node3 = Node(name="3")
     nodes = NodeList([node1, node2, node3])
     assert nodes.get_object_by_name("set") == node1
     assert nodes.get_object_by_name("Reset") == node2
     assert nodes.get_object_by_name("3") == node3
Exemple #4
0
 def test_get_object_by_name_fail_case_sensitive(self):
     node1 = Node(name="set")
     node2 = Node(name="2")
     node3 = Node(name="3")
     nodes = NodeList([node1, node2, node3])
     with pytest.raises(ValueError) as ex:
         nodes.get_object_by_name("Set")
     assert str(ex.value
                ) == f"Node Set not found. Valid node names are (set, 2, 3)"
Exemple #5
0
 def test_get_object_by_name_fails(self):
     node1 = Node(name="1")
     node2 = Node(name="2")
     node3 = Node(name="3")
     nodes = NodeList([node1, node2, node3])
     with pytest.raises(ValueError) as ex:
         nodes.get_object_by_name("4")
     assert str(
         ex.value) == f"Node 4 not found. Valid node names are (1, 2, 3)"
Exemple #6
0
 def test_str(self):
     a = OneOutputComponent(
         inputs=[Node(name="node1"),
                 Node(name="node2", state=State.high)],
         name="my component",
     )
     a._outputs = NodeList([Node(name="output")])
     assert (
         str(a) ==
         "my component: (node1: State.low, node2: State.high) -> (output: State.low)"
     )
Exemple #7
0
 def test_str(self):
     a = MultipleOutputComponent(
         inputs=[Node(name="node1"),
                 Node(name="node2", state=State.high)],
         name="my component",
     )
     a._outputs = NodeList(
         [Node(name="output1"),
          Node(name="output2", state=State.high)])
     assert str(a) == (
         "my component: (node1: State.low, node2: State.high) -> (output1: State.low, output2: State.high)"
     )
Exemple #8
0
    def test_basic(self):
        d = Node(State.low, name="D")
        clk = Node(State.high, name="Clk")
        ff = DTypeFlipFlop([d, clk])
        ff.calculate()
        assert ff.outputs["Q"].state == State.low
        assert ff.outputs["QBar"].state == State.high

        d.state = State.high
        clk.state = State.high
        ff.calculate()
        assert ff.outputs["Q"].state == State.high
        assert ff.outputs["QBar"].state == State.low
Exemple #9
0
 def test_validation_expected_names_success(self):
     nodes = NodeList([Node(name="1"), Node(name="2")])
     nodes.validate("element", expected_names=["1", "2"])
Exemple #10
0
 def test_validation_maximum_fails(self):
     nodes = NodeList([Node(name="1"), Node(name="2")])
     with pytest.raises(ValueError) as ex:
         nodes.validate("element", max_length=1)
     assert str(ex.value) == f"element must have a maximum of 1 inputs."
Exemple #11
0
 def test_validation_maximum_success(self):
     nodes = NodeList([Node(name="1"), Node(name="2")])
     nodes.validate("element", max_length=2)
Exemple #12
0
 def test_name(self):
     c = self.component([Node(State.low), Node(State.low)], name="testname")
     assert c.name == "testname"
Exemple #13
0
 def test_one_input_fails_set_inputs(self):
     with pytest.raises(ValueError) as ex:
         c = self.component(name="testname")
         c.inputs = [Node(State.low)]
     assert (str(ex.value) ==
             f"{self.component.__name__} must have two or more inputs.")
Exemple #14
0
 def test_names(self):
     c = self.component([Node(State.low)], name="testname")
     assert c.name == "testname"
     assert c.outputs[0].name == "testname_out"
Exemple #15
0
 def test_not_gate(self, ina, result):
     a = Node(ina)
     b = self.component([a])
     assert b.calculate()[0].state == result
     assert b.outputs[0].state == result
Exemple #16
0
 def test_xnor_gate(self, ina, inb, inc, result):
     c = self.component([Node(ina), Node(inb), Node(inc)])
     assert c.calculate()[0].state == result
     assert c.outputs[0].state == result
Exemple #17
0
 def test_comparing_nodes_with_states(self):
     a = Node(state=State.high)
     assert a == State.high
Exemple #18
0
 def test_validation_expected_names_missing(self):
     nodes = NodeList([Node(name="2")])
     with pytest.raises(ValueError) as ex:
         nodes.validate("element", expected_names=["2", "3"])
     assert str(ex.value) == f"The following node names were missing: 3"
Exemple #19
0
 def test_gate(self, s, r, q, qbar):
     latch = SRNandLatch([Node(s, name="Set"), Node(r, name="Reset")])
     out_nodes = latch.calculate()
     assert [i.state for i in out_nodes] == [q, qbar], latch
Exemple #20
0
 def test_too_many_inputs(self):
     with pytest.raises(ValueError) as ex:
         self.component([Node(), Node()])
     assert str(ex.value) == "A not gate can only have one input."
Exemple #21
0
 def test_name_null(self):
     c = self.component([Node(State.low), Node(State.low)], name=None)
     assert c.name == self.component.__name__
Exemple #22
0
 def test_str(self):
     a = Node(name="hello world", state=State.high)
     assert str(a) == f"hello world: State.high"
Exemple #23
0
 def test_str(self):
     a = NodeList([Node(name="node", state=State.high)])
     assert str(a) == "[node: State.high]"