diff --git a/b_asic/port.py b/b_asic/port.py index c22053df1928cf2f6ea32c4880816d551b85836a..d7a0cc17fcf6c998dc15ad270977702e7a7162cd 100644 --- a/b_asic/port.py +++ b/b_asic/port.py @@ -35,15 +35,6 @@ class Port(ABC): """Return a list of all connected signals.""" raise NotImplementedError - @abstractmethod - def signal(self, i: int = 0) -> Signal: - """Return the connected signal at index i. - - Keyword argumens: - i: integer index of the signal requsted. - """ - raise NotImplementedError - @property @abstractmethod def connected_ports(self) -> List["Port"]: @@ -119,24 +110,32 @@ class InputPort(AbstractPort): """ _source_signal: Optional[Signal] + _value_length: Optional[int] + def __init__(self, port_id: PortIndex, operation: Operation): super().__init__(port_id, operation) self._source_signal = None + self._value_length = None @property def signals(self) -> List[Signal]: return [] if self._source_signal is None else [self._source_signal] - def signal(self, i: int = 0) -> Signal: - assert 0 <= i < self.signal_count(), "Signal index out of bound." - assert self._source_signal is not None, "No Signal connect to InputPort." - return self._source_signal + @property + def value_length(self) -> Optional[int]: + return self._value_length @property def connected_ports(self) -> List[Port]: return [] if self._source_signal is None or self._source_signal.source is None \ else [self._source_signal.source] + + @value_length.setter + def value_length(self, bits: Optional[int]) -> None: + assert isinstance(bits, int) or bits is None, "Value length on input port has to be of type Int or NoneType." + assert bits is None or bits >= 0, "Value length on input port has to be a non-negative integer or None." + self._value_length = bits def signal_count(self) -> int: return 0 if self._source_signal is None else 1 diff --git a/test/test_inputport.py b/test/test_inputport.py index a43240693ac632b48461023536ff46b0ea379c5c..910b2700e25f4cb8d4148cc8bfb6b89126576a7b 100644 --- a/test/test_inputport.py +++ b/test/test_inputport.py @@ -30,14 +30,12 @@ def s_w_source(): @pytest.fixture def sig_with_dest(): - inp_port = InputPort(0, None) - return Signal(destination=out_port) + #inp_port = InputPort(0, None) + return Signal(destination=inp_port()) @pytest.fixture def connected_sig(): - out_port = OutputPort(0, None) - inp_port = InputPort(0, None) - return Signal(source=out_port, destination=inp_port) + return Signal(source=out_port(), destination=inp_port()) def test_connect_then_disconnect(inp_port, out_port): """Test connect unused port to port.""" @@ -83,13 +81,27 @@ def test_add_signal_then_disconnect(inp_port, s_w_source): assert s_w_source.source.signals == [s_w_source] assert s_w_source.destination is None -def test_connect_then_disconnect(inp_port, out_port): - """Can port be connected and then disconnected properly?""" - inp_port.connect(out_port) +def test_set_value_length_pos_int(inp_port): + inp_port.value_length = 10 + assert inp_port.value_length == 10 + +def test_set_value_length_zero(inp_port): + inp_port.value_length = 0 + assert inp_port.value_length == 0 + +def test_set_value_length_neg_int(inp_port): + with pytest.raises(Exception): + inp_port.value_length = -10 + +def test_set_value_length_complex(inp_port): + with pytest.raises(Exception): + inp_port.value_length = (2+4j) - inp_port.disconnect(out_port) +def test_set_value_length_float(inp_port): + with pytest.raises(Exception): + inp_port.value_length = 3.2 - print("outport signals:", out_port.signals, "count:", out_port.signal_count()) - assert inp_port.signal_count() == 1 - assert len(inp_port.connected_ports) == 0 - assert out_port.signal_count() == 0 +def test_set_value_length_pos_then_none(inp_port): + inp_port.value_length = 10 + inp_port.value_length = None + assert inp_port.value_length == None \ No newline at end of file