diff --git a/PyCircTools/Exceptions/OperatorsExceptions.py b/PyCircTools/Exceptions/OperatorsExceptions.py index 5f3e9eb..13732af 100644 --- a/PyCircTools/Exceptions/OperatorsExceptions.py +++ b/PyCircTools/Exceptions/OperatorsExceptions.py @@ -41,6 +41,15 @@ def __init__(self, curr_size, req_size): super().__init__(self.msg) +class WrongRange(OperatorException): + """ + WrongRange is raised when the number of inputs is lower or equal than the number of inputs in certain modules. + """ + def __init(self, obj): + self.msg = "Number of inputs of " + obj + " must be lower than the number of outputs." + super().__init__(self.msg) + + def get_adder_type(addr_class): """ Method used to get the type of adder used. diff --git a/PyCircTools/Operators/Extender.py b/PyCircTools/Operators/Extender.py index e6297dc..d61900c 100644 --- a/PyCircTools/Operators/Extender.py +++ b/PyCircTools/Operators/Extender.py @@ -1,4 +1,4 @@ -from PyCircTools import NotTruthValue, NotControlList, WrongSize +from PyCircTools import NotTruthValue, NotControlList, WrongSize, WrongRange class Extender: @@ -18,11 +18,14 @@ def __init__(self, into=2, out=4, pure=True): :param pure: Used to know if the input is in pure binary or two's complement. :type pure: bool """ + if into >= out: + raise WrongRange("Extender") + self.numInto = into self.numOut = out self.pure = pure self.input = [False] * into - self.output = self.__calculate_output() + self.output = [False] * out def get_numIn(self): """ @@ -96,7 +99,7 @@ def set_input(self, values, pure=True): self.input = values self.__set_pure(pure) - self.output == self.__calculate_output() + self.__calculate_output() return self @@ -135,4 +138,5 @@ def __calculate_output(self): else: output.append(self.input[self.numInto - 1]) - return output + self.output = output + return self diff --git a/PyCircTools/Operators/__init__.py b/PyCircTools/Operators/__init__.py index 9e8f4fd..7d98f55 100644 --- a/PyCircTools/Operators/__init__.py +++ b/PyCircTools/Operators/__init__.py @@ -8,3 +8,4 @@ from PyCircTools.Operators.Subtractor import * from PyCircTools.Operators.Alu import * from PyCircTools.Operators.Shifter import * +from PyCircTools.Operators.Extender import * diff --git a/tests/Operators_tests/test_extender.py b/tests/Operators_tests/test_extender.py new file mode 100644 index 0000000..e3d736b --- /dev/null +++ b/tests/Operators_tests/test_extender.py @@ -0,0 +1,18 @@ +from PyCircTools import Extender + + +def test_extender(): + ext = Extender() + + assert ext.set_input([True, True]).get_output() == [True, True, False, False] + assert ext.get_numIn() == 2 + assert ext.get_numOut() == 4 + + _ext = Extender(4, 8) + + assert _ext.numInto == 4 + assert _ext.numOut == 8 + assert _ext.set_input([False, False, True, False]).get_output() == [False, False, True, False, False, False, False, + False] + assert _ext.set_input([True, False, True, True], False).get_output() == [True, False, True, True, True, True, True, + True]