Repository: LovetheFrogs/PyCircTools (language: python)

Issue:
Test sr latch
Implementation of the SRLatch tests


Discussion / hints:

Fix patch:
diff --git a/CTools/Exceptions/__init__.py b/CTools/Exceptions/__init__.py
deleted file mode 100644
index 88512b4..0000000
--- a/CTools/Exceptions/__init__.py
+++ /dev/null
@@ -1,7 +0,0 @@
-"""
-CTools custom exceptions.
-"""
-
-from CTools.Exceptions.CircuitToolsExceptions import *
-from CTools.Exceptions.LogicGateExceptions import *
-from CTools.Exceptions.LatchesExceptions import *
diff --git a/CTools/LogicGates/__init__.py b/CTools/LogicGates/__init__.py
deleted file mode 100644
index 56825f1..0000000
--- a/CTools/LogicGates/__init__.py
+++ /dev/null
@@ -1,13 +0,0 @@
-"""
-CTools Logic Gates package.
-
-This package includes the  **NOT, AND, NAND, OR, XOR, NOR** logic gates.
-"""
-
-
-from CTools.LogicGates.Not import *
-from CTools.LogicGates.And import *
-from CTools.LogicGates.Nand import *
-from CTools.LogicGates.Or import *
-from CTools.LogicGates.Xor import *
-from CTools.LogicGates.Nor import *
diff --git a/CTools/Multiplexers/__init__.py b/CTools/Multiplexers/__init__.py
deleted file mode 100644
index 8365d73..0000000
--- a/CTools/Multiplexers/__init__.py
+++ /dev/null
@@ -1,10 +0,0 @@
-"""
-PyCircTools Multiplexers package.
-
-This package includes some Multiplexers and Demultiplexers (from 1 to 2 to 1 to 16)
-"""
-
-from CTools.Multiplexers.Mux2to1 import *
-from CTools.Multiplexers.Mux4to1 import *
-from CTools.Multiplexers.Mux8to1 import *
-from CTools.Multiplexers.Mux16to1 import *
diff --git a/CTools/__init__.py b/CTools/__init__.py
deleted file mode 100644
index f78130b..0000000
--- a/CTools/__init__.py
+++ /dev/null
@@ -1,8 +0,0 @@
-"""
-CTools is a python 3 package that provides the user with common circuit elements. It aims to help the implementation of
-circuits using code, serving as an alternative to programs like ModelSIM.
-"""
-
-from CTools.Exceptions import *
-from CTools.LogicGates import *
-from CTools.Latches import *
diff --git a/CTools/Exceptions/CircuitToolsExceptions.py b/PyCircTools/Exceptions/CircuitToolsExceptions.py
similarity index 81%
rename from CTools/Exceptions/CircuitToolsExceptions.py
rename to PyCircTools/Exceptions/CircuitToolsExceptions.py
index 9e6c573..47dc2c1 100644
--- a/CTools/Exceptions/CircuitToolsExceptions.py
+++ b/PyCircTools/Exceptions/CircuitToolsExceptions.py
@@ -1,11 +1,11 @@
 """
-General CTools exceptions.
+General PyCircTools exceptions.
 """
 
 
 class CircuitToolsException(Exception):
     """
-    Parent Exception class. All general CTools exceptions inherit from this class.
+    Parent Exception class. All general PyCircTools exceptions inherit from this class.
     """
     def __init__(self, msg="CircuitTools Exception."):
         self.msg = msg
diff --git a/CTools/Exceptions/LatchesExceptions.py b/PyCircTools/Exceptions/LatchesExceptions.py
similarity index 100%
rename from CTools/Exceptions/LatchesExceptions.py
rename to PyCircTools/Exceptions/LatchesExceptions.py
diff --git a/CTools/Exceptions/LogicGateExceptions.py b/PyCircTools/Exceptions/LogicGateExceptions.py
similarity index 84%
rename from CTools/Exceptions/LogicGateExceptions.py
rename to PyCircTools/Exceptions/LogicGateExceptions.py
index f1efd14..9fa55b8 100644
--- a/CTools/Exceptions/LogicGateExceptions.py
+++ b/PyCircTools/Exceptions/LogicGateExceptions.py
@@ -1,11 +1,11 @@
 """
-CTools LogicGates module Exceptions.
+PyCircTools LogicGates module Exceptions.
 """
 
 
 class LogicGateException(Exception):
     """
-    LogicGateException is the parent Exception class for all CTools.LogicGates related Exceptions
+    LogicGateException is the parent Exception class for all PyCircTools.LogicGates related Exceptions
     """
     def __init__(self, msg="Logic gate Exception."):
         self.msg = msg
diff --git a/CTools/Exceptions/MultiplexersExceptions.py b/PyCircTools/Exceptions/MultiplexersExceptions.py
similarity index 100%
rename from CTools/Exceptions/MultiplexersExceptions.py
rename to PyCircTools/Exceptions/MultiplexersExceptions.py
diff --git a/PyCircTools/Exceptions/__init__.py b/PyCircTools/Exceptions/__init__.py
new file mode 100644
index 0000000..124a40d
--- /dev/null
+++ b/PyCircTools/Exceptions/__init__.py
@@ -0,0 +1,7 @@
+"""
+PyCircTools custom exceptions.
+"""
+
+from PyCircTools.Exceptions.CircuitToolsExceptions import *
+from PyCircTools.Exceptions.LogicGateExceptions import *
+from PyCircTools.Exceptions.LatchesExceptions import *
diff --git a/PyCircTools/Latches/SRLatch.py b/PyCircTools/Latches/SRLatch.py
new file mode 100644
index 0000000..24d28ed
--- /dev/null
+++ b/PyCircTools/Latches/SRLatch.py
@@ -0,0 +1,122 @@
+from  PyCircTools.Exceptions.CircuitToolsExceptions import NotTruthValue
+from PyCircTools.LogicGates import And, Nor
+
+
+class SRLatch:
+    """
+    SR Latch module. Takes 3 inputs (R, S and Enable).
+    """
+    def __init__(self):
+        """
+        SRLatch class constructor. Initialises all inputs and outputs to False.
+        """
+        self.R = False
+        self.S = False
+        self.enable = False
+        self.Q = False
+        self.Qp = False
+
+    def get_R(self):
+        """
+        Method get_R gets the value of the R input.
+
+        :return: Value of the gate's R input.
+        :rtype: bool
+        """
+        return self.R
+
+    def get_S(self):
+        """
+        Method get_S gets the value of the S input.
+
+        :return: Value of the gate's S input.
+        :rtype: bool
+        """
+        return self.S
+
+    def get_enable(self):
+        """
+        Method get_enable gets the value of enable input.
+
+        :return: Value of the gate's enable input.
+        :rtype: bool
+        """
+        return self.enable
+
+    def get_Q(self):
+        """
+        Method get_Q gets the value of the Q output.
+
+        :return: Value of the gate's Q output.
+        :rtype: bool
+        """
+        return self.Q
+
+    def get_Qp(self):
+        """
+        Method get_Qp gets the value of the Qp output.
+
+        :return: Value of the gate's Qp output.
+        :rtype: bool
+        """
+        return self.Qp
+
+    def set_R(self, value):
+        """
+        Method set_R sets the value of the R input to the bool value.
+
+        :param value: Desired value of the gate's R input.
+        :type value: bool
+        :raises NotTruthValue: Raised when a variable type is not bool.
+        """
+        if type(value) is not bool:
+            raise NotTruthValue
+
+        self.R = value
+        self.__calculate_output()
+        return self
+
+    def set_S(self, value):
+        """
+        Method set_S sets the value of the S input to the bool value.
+
+        :param value: Desired value of the gate's S input.
+        :type value: bool
+        :raises NotTruthValue: Raised when a variable type is not bool.
+        """
+        if type(value) is not bool:
+            raise NotTruthValue
+
+        self.S = value
+        self.__calculate_output()
+        return self
+
+    def set_enable(self, value):
+        """
+        Method set_enable sets the value of Enable to the bool value.
+
+        :param value: Desired value of the gate's Enable input.
+        :type value: bool
+        :raises NotTruthValue: Raised when a variable type is not bool.
+        """
+        if type(value) is not bool:
+            raise NotTruthValue
+
+        self.enable = value
+        self.__calculate_output()
+        return self
+
+    def __calculate_output(self):
+        """
+        Method __calculate_output calculates the value of both the Q and the Qp signal.
+        """
+        and_r = And().set_input(0, self.R).set_input(1, self.enable)
+        and_s = And().set_input(0, self.S).set_input(1, self.enable)
+
+        nor_r = Nor().set_input(0, and_r.get_output()).set_input(1, self.Qp)
+        nor_s = Nor().set_input(0, and_s.get_output()).set_input(1, self.Q)
+
+        self.Q = nor_r.get_output()
+        self.Qp = nor_s.get_output()
+
+        return self
diff --git a/CTools/Latches/__init__.py b/PyCircTools/Latches/__init__.py
similarity index 59%
rename from CTools/Latches/__init__.py
rename to PyCircTools/Latches/__init__.py
index 0520d20..d092c75 100644
--- a/CTools/Latches/__init__.py
+++ b/PyCircTools/Latches/__init__.py
@@ -2,4 +2,6 @@
 PyCircTools Latches package.
 
 This package includes **** latches.
-"""
\ No newline at end of file
+"""
+
+from PyCircTools.Latches.SRLatch import *
diff --git a/CTools/LogicGates/And.py b/PyCircTools/LogicGates/And.py
similarity index 94%
rename from CTools/LogicGates/And.py
rename to PyCircTools/LogicGates/And.py
index 22ad8cb..50b1c0c 100644
--- a/CTools/LogicGates/And.py
+++ b/PyCircTools/LogicGates/And.py
@@ -1,5 +1,5 @@
-from CTools.Exceptions.CircuitToolsExceptions import NotTruthValue
-from CTools.Exceptions.LogicGateExceptions import NonPositiveInput, NotAnInput
+from PyCircTools.Exceptions.CircuitToolsExceptions import NotTruthValue
+from PyCircTools.Exceptions.LogicGateExceptions import NonPositiveInput, NotAnInput
 
 
 class And:
diff --git a/CTools/LogicGates/Nand.py b/PyCircTools/LogicGates/Nand.py
similarity index 94%
rename from CTools/LogicGates/Nand.py
rename to PyCircTools/LogicGates/Nand.py
index d690195..db4c78c 100644
--- a/CTools/LogicGates/Nand.py
+++ b/PyCircTools/LogicGates/Nand.py
@@ -1,5 +1,5 @@
-from CTools.Exceptions.CircuitToolsExceptions import NotTruthValue
-from CTools.Exceptions.LogicGateExceptions import NonPositiveInput, NotAnInput
+from PyCircTools.Exceptions.CircuitToolsExceptions import NotTruthValue
+from PyCircTools.Exceptions.LogicGateExceptions import NonPositiveInput, NotAnInput
 
 
 class Nand:
diff --git a/CTools/LogicGates/Nor.py b/PyCircTools/LogicGates/Nor.py
similarity index 94%
rename from CTools/LogicGates/Nor.py
rename to PyCircTools/LogicGates/Nor.py
index 034106e..3ba1d85 100644
--- a/CTools/LogicGates/Nor.py
+++ b/PyCircTools/LogicGates/Nor.py
@@ -1,5 +1,5 @@
-from CTools.Exceptions.CircuitToolsExceptions import NotTruthValue
-from CTools.Exceptions.LogicGateExceptions import NonPositiveInput, NotAnInput
+from PyCircTools.Exceptions.CircuitToolsExceptions import NotTruthValue
+from PyCircTools.Exceptions.LogicGateExceptions import NonPositiveInput, NotAnInput
 
 
 class Nor:
diff --git a/CTools/LogicGates/Not.py b/PyCircTools/LogicGates/Not.py
similarity index 94%
rename from CTools/LogicGates/Not.py
rename to PyCircTools/LogicGates/Not.py
index fd958ed..8011104 100644
--- a/CTools/LogicGates/Not.py
+++ b/PyCircTools/LogicGates/Not.py
@@ -1,4 +1,4 @@
-from CTools.Exceptions.CircuitToolsExceptions import NotTruthValue
+from PyCircTools.Exceptions.CircuitToolsExceptions import NotTruthValue
 
 
 class Not:
diff --git a/CTools/LogicGates/Or.py b/PyCircTools/LogicGates/Or.py
similarity index 94%
rename from CTools/LogicGates/Or.py
rename to PyCircTools/LogicGates/Or.py
index df03411..ac9605e 100644
--- a/CTools/LogicGates/Or.py
+++ b/PyCircTools/LogicGates/Or.py
@@ -1,5 +1,5 @@
-from CTools.Exceptions.CircuitToolsExceptions import NotTruthValue
-from CTools.Exceptions.LogicGateExceptions import NonPositiveInput, NotAnInput
+from PyCircTools.Exceptions.CircuitToolsExceptions import NotTruthValue
+from PyCircTools.Exceptions.LogicGateExceptions import NonPositiveInput, NotAnInput
 
 
 class Or:
diff --git a/CTools/LogicGates/Xor.py b/PyCircTools/LogicGates/Xor.py
similarity index 94%
rename from CTools/LogicGates/Xor.py
rename to PyCircTools/LogicGates/Xor.py
index 472277f..057ec24 100644
--- a/CTools/LogicGates/Xor.py
+++ b/PyCircTools/LogicGates/Xor.py
@@ -1,5 +1,5 @@
-from CTools.Exceptions.CircuitToolsExceptions import NotTruthValue
-from CTools.Exceptions.LogicGateExceptions import NonPositiveInput, NotAnInput
+from PyCircTools.Exceptions.CircuitToolsExceptions import NotTruthValue
+from PyCircTools.Exceptions.LogicGateExceptions import NonPositiveInput, NotAnInput
 
 
 class Xor:
diff --git a/PyCircTools/LogicGates/__init__.py b/PyCircTools/LogicGates/__init__.py
new file mode 100644
index 0000000..c7697cf
--- /dev/null
+++ b/PyCircTools/LogicGates/__init__.py
@@ -0,0 +1,13 @@
+"""
+PyCircTools Logic Gates package.
+
+This package includes the  **NOT, AND, NAND, OR, XOR, NOR** logic gates.
+"""
+
+
+from PyCircTools.LogicGates.Not import *
+from PyCircTools.LogicGates.And import *
+from PyCircTools.LogicGates.Nand import *
+from PyCircTools.LogicGates.Or import *
+from PyCircTools.LogicGates.Xor import *
+from PyCircTools.LogicGates.Nor import *
diff --git a/CTools/Multiplexers/Mux16to1.py b/PyCircTools/Multiplexers/Mux16to1.py
similarity index 96%
rename from CTools/Multiplexers/Mux16to1.py
rename to PyCircTools/Multiplexers/Mux16to1.py
index 3e135f1..59915e1 100644
--- a/CTools/Multiplexers/Mux16to1.py
+++ b/PyCircTools/Multiplexers/Mux16to1.py
@@ -1,5 +1,5 @@
-from CTools.Exceptions.MultiplexersExceptions import NonExistingInput, NonExistingControlSignal
-from CTools.Exceptions.CircuitToolsExceptions import NotTruthValue
+from PyCircTools.Exceptions.MultiplexersExceptions import NonExistingInput, NonExistingControlSignal
+from PyCircTools.Exceptions.CircuitToolsExceptions import NotTruthValue
 
 
 class Mux16to1:
diff --git a/CTools/Multiplexers/Mux2to1.py b/PyCircTools/Multiplexers/Mux2to1.py
similarity index 94%
rename from CTools/Multiplexers/Mux2to1.py
rename to PyCircTools/Multiplexers/Mux2to1.py
index 4114ced..735ba59 100644
--- a/CTools/Multiplexers/Mux2to1.py
+++ b/PyCircTools/Multiplexers/Mux2to1.py
@@ -1,5 +1,5 @@
-from CTools.Exceptions.MultiplexersExceptions import NonExistingInput
-from CTools.Exceptions.CircuitToolsExceptions import NotTruthValue
+from PyCircTools.Exceptions.MultiplexersExceptions import NonExistingInput
+from PyCircTools.Exceptions.CircuitToolsExceptions import NotTruthValue
 
 
 class Mux2to1:
diff --git a/CTools/Multiplexers/Mux4to1.py b/PyCircTools/Multiplexers/Mux4to1.py
similarity index 95%
rename from CTools/Multiplexers/Mux4to1.py
rename to PyCircTools/Multiplexers/Mux4to1.py
index 9473b88..982b32d 100644
--- a/CTools/Multiplexers/Mux4to1.py
+++ b/PyCircTools/Multiplexers/Mux4to1.py
@@ -1,5 +1,5 @@
-from CTools.Exceptions.MultiplexersExceptions import NonExistingInput, NonExistingControlSignal
-from CTools.Exceptions.CircuitToolsExceptions import NotTruthValue
+from PyCircTools.Exceptions.MultiplexersExceptions import NonExistingInput, NonExistingControlSignal
+from PyCircTools.Exceptions.CircuitToolsExceptions import NotTruthValue
 
 
 class Mux4to1:
diff --git a/CTools/Multiplexers/Mux8to1.py b/PyCircTools/Multiplexers/Mux8to1.py
similarity index 95%
rename from CTools/Multiplexers/Mux8to1.py
rename to PyCircTools/Multiplexers/Mux8to1.py
index 46b3389..b5d87cc 100644
--- a/CTools/Multiplexers/Mux8to1.py
+++ b/PyCircTools/Multiplexers/Mux8to1.py
@@ -1,5 +1,5 @@
-from CTools.Exceptions.MultiplexersExceptions import NonExistingInput, NonExistingControlSignal
-from CTools.Exceptions.CircuitToolsExceptions import NotTruthValue
+from PyCircTools.Exceptions.MultiplexersExceptions import NonExistingInput, NonExistingControlSignal
+from PyCircTools.Exceptions.CircuitToolsExceptions import NotTruthValue
 
 
 class Mux8to1:
diff --git a/PyCircTools/Multiplexers/__init__.py b/PyCircTools/Multiplexers/__init__.py
new file mode 100644
index 0000000..2caae35
--- /dev/null
+++ b/PyCircTools/Multiplexers/__init__.py
@@ -0,0 +1,10 @@
+"""
+PyCircTools Multiplexers package.
+
+This package includes some Multiplexers and Demultiplexers (from 1 to 2 to 1 to 16)
+"""
+
+from PyCircTools.Multiplexers.Mux2to1 import *
+from PyCircTools.Multiplexers.Mux4to1 import *
+from PyCircTools.Multiplexers.Mux8to1 import *
+from PyCircTools.Multiplexers.Mux16to1 import *
diff --git a/PyCircTools/__init__.py b/PyCircTools/__init__.py
new file mode 100644
index 0000000..b1bd0bd
--- /dev/null
+++ b/PyCircTools/__init__.py
@@ -0,0 +1,8 @@
+"""
+PyCircTools is a python 3 package that provides the user with common circuit elements. It aims to help the implementation of
+circuits using code, serving as an alternative to programs like ModelSIM.
+"""
+
+from PyCircTools.Exceptions import *
+from PyCircTools.LogicGates import *
+from PyCircTools.Latches import *


Test patch:
diff --git a/tests/Latches_tests/test_SRLatch.py b/tests/Latches_tests/test_SRLatch.py
new file mode 100644
index 0000000..6e7238c
--- /dev/null
+++ b/tests/Latches_tests/test_SRLatch.py
@@ -0,0 +1,32 @@
+from PyCircTools.Latches import SRLatch
+
+
+def test_SRLatch():
+    errors = []
+    latch = SRLatch()
+
+    if latch.get_Q():
+        errors.append("Q output when generated test not passed!")
+
+    if latch.set_R(True).get_Q():
+        errors.append("Operation when enable is False!")
+
+    if not latch.set_enable(True).set_R(False).get_Q() and not latch.get_Qp():
+        pass
+    else:
+        errors.append("Set and all False test not passed!")
+
+    if not latch.set_R(True).get_Q() and latch.get_Qp():
+        pass
+    else:
+        errors.append("R and not S test not passed!")
+
+    if latch.set_R(False).set_S(True).get_Q() and not latch.get_Qp():
+        pass
+    else:
+        errors.append("not R and S test not passed!")
+
+    if latch.set_R(True).get_Q() and latch.get_Qp():
+        pass
+    else:
+        errors.append("R and S test not passed!")
diff --git a/tests/LogicGates_tests/test_and.py b/tests/LogicGates_tests/test_and.py
index cf52bfb..1870279 100644
--- a/tests/LogicGates_tests/test_and.py
+++ b/tests/LogicGates_tests/test_and.py
@@ -1,4 +1,4 @@
-from CTools.LogicGates import And
+from PyCircTools.LogicGates import And
 
 
 def test_and_gate():
diff --git a/tests/LogicGates_tests/test_nand.py b/tests/LogicGates_tests/test_nand.py
index 2277d01..64ad821 100644
--- a/tests/LogicGates_tests/test_nand.py
+++ b/tests/LogicGates_tests/test_nand.py
@@ -1,4 +1,4 @@
-from CTools.LogicGates import Nand
+from PyCircTools.LogicGates import Nand
 
 
 def test_and_gate():
diff --git a/tests/LogicGates_tests/test_nor.py b/tests/LogicGates_tests/test_nor.py
index 3b2adc8..1707ae7 100644
--- a/tests/LogicGates_tests/test_nor.py
+++ b/tests/LogicGates_tests/test_nor.py
@@ -1,4 +1,4 @@
-from CTools.LogicGates import Nor
+from PyCircTools.LogicGates import Nor
 
 
 def test_or_gate():
diff --git a/tests/LogicGates_tests/test_not.py b/tests/LogicGates_tests/test_not.py
index a2f1aed..7e18ace 100644
--- a/tests/LogicGates_tests/test_not.py
+++ b/tests/LogicGates_tests/test_not.py
@@ -1,4 +1,4 @@
-from CTools.LogicGates import Not
+from PyCircTools.LogicGates import Not
 
 
 def test_not_gate():
diff --git a/tests/LogicGates_tests/test_or.py b/tests/LogicGates_tests/test_or.py
index 601778a..f861244 100644
--- a/tests/LogicGates_tests/test_or.py
+++ b/tests/LogicGates_tests/test_or.py
@@ -1,4 +1,4 @@
-from CTools.LogicGates import Or
+from PyCircTools.LogicGates import Or
 
 
 def test_or_gate():
diff --git a/tests/LogicGates_tests/test_xor.py b/tests/LogicGates_tests/test_xor.py
index e0bce95..6b261fc 100644
--- a/tests/LogicGates_tests/test_xor.py
+++ b/tests/LogicGates_tests/test_xor.py
@@ -1,4 +1,4 @@
-from CTools.LogicGates import Xor
+from PyCircTools.LogicGates import Xor
 
 
 def test_or_gate():
diff --git a/tests/Multiplexers_tests/test_Mux16to1.py b/tests/Multiplexers_tests/test_Mux16to1.py
index 107d347..f2382e0 100644
--- a/tests/Multiplexers_tests/test_Mux16to1.py
+++ b/tests/Multiplexers_tests/test_Mux16to1.py
@@ -1,7 +1,7 @@
 import itertools
 
-from CTools import NotTruthValue, NonExistingControlSignal, NonExistingInput
-from CTools.Multiplexers import Mux16to1
+from PyCircTools import NotTruthValue, NonExistingControlSignal, NonExistingInput
+from PyCircTools.Multiplexers import Mux16to1
 
 
 def generate_inputs(num):
diff --git a/tests/Multiplexers_tests/test_Mux4to1.py b/tests/Multiplexers_tests/test_Mux4to1.py
index e9cba7c..7d9f9e6 100644
--- a/tests/Multiplexers_tests/test_Mux4to1.py
+++ b/tests/Multiplexers_tests/test_Mux4to1.py
@@ -1,5 +1,5 @@
-from CTools import NotTruthValue, NonExistingControlSignal, NonExistingInput
-from CTools.Multiplexers import Mux4to1
+from PyCircTools import NotTruthValue, NonExistingControlSignal, NonExistingInput
+from PyCircTools.Multiplexers import Mux4to1
 
 
 def to_bool(value):
diff --git a/tests/Multiplexers_tests/test_Mux8to1.py b/tests/Multiplexers_tests/test_Mux8to1.py
index 84d9e1c..acd7848 100644
--- a/tests/Multiplexers_tests/test_Mux8to1.py
+++ b/tests/Multiplexers_tests/test_Mux8to1.py
@@ -1,7 +1,7 @@
 import itertools
 
-from CTools import NotTruthValue, NonExistingControlSignal, NonExistingInput
-from CTools.Multiplexers import Mux8to1
+from PyCircTools import NotTruthValue, NonExistingControlSignal, NonExistingInput
+from PyCircTools.Multiplexers import Mux8to1
 
 
 def generate_inputs(num):