Repository: LovetheFrogs/PyCircTools (language: python)

Issue:
Xor gate



Discussion / hints:

Fix patch:
diff --git a/BinaryTools/LogicGates/And.py b/BinaryTools/LogicGates/And.py
new file mode 100644
index 0000000..b078ea0
--- /dev/null
+++ b/BinaryTools/LogicGates/And.py
@@ -0,0 +1,42 @@
+class And:
+    def __init__(self, inputNumber=2):
+        self.input = self.__create_input(inputNumber)
+        self.output = False
+        self.numOfInputs = inputNumber
+
+    def get_input(self, num):
+        return self.input[num]
+
+    def get_output(self):
+        return self.output
+
+    def get_numOfInputs(self):
+        return self.numOfInputs
+
+    def set_input(self, num, value):
+        self.input[num] = value
+        self.__calculate_output()
+        return self
+
+    @staticmethod
+    def __create_input(number):
+        return [False] * number
+
+    def __calculate_output(self):
+        output = True
+        for value in self.input:
+            output = output and value
+        self.output = output
+        return self
+
+    def add_input(self):
+        self.input.append(False)
+        self.numOfInputs += 1
+        self.output = False
+        return self
+
+    def remove_input(self):
+        self.input.pop()
+        self.numOfInputs -= 1
+        self.__calculate_output()
+        return self
diff --git a/BinaryTools/LogicGates/Nand.py b/BinaryTools/LogicGates/Nand.py
new file mode 100644
index 0000000..bef2157
--- /dev/null
+++ b/BinaryTools/LogicGates/Nand.py
@@ -0,0 +1,42 @@
+class Nand:
+    def __init__(self, inputNumber=2):
+        self.input = self.__create_input(inputNumber)
+        self.output = True
+        self.numOfInputs = inputNumber
+
+    def get_input(self, num):
+        return self.input[num]
+
+    def get_output(self):
+        return self.output
+
+    def get_numOfInputs(self):
+        return self.numOfInputs
+
+    def set_input(self, num, value):
+        self.input[num] = value
+        self.__calculate_output()
+        return self
+
+    def __calculate_output(self):
+        output = True
+        for value in self.input:
+            output = output and value
+        self.output = not output
+        return self
+
+    def add_input(self):
+        self.input.append(False)
+        self.numOfInputs += 1
+        self.__calculate_output()
+        return self
+
+    def remove_input(self):
+        self.input.pop()
+        self.__calculate_output()
+        self.numOfInputs -= 1
+        return self
+
+    @staticmethod
+    def __create_input(num):
+        return [False] * num
diff --git a/BinaryTools/LogicGates/Nor.py b/BinaryTools/LogicGates/Nor.py
new file mode 100644
index 0000000..d65174e
--- /dev/null
+++ b/BinaryTools/LogicGates/Nor.py
@@ -0,0 +1,42 @@
+class Nor:
+    def __init__(self, inputNumber=2):
+        self.input = self.__create_input(inputNumber)
+        self.output = True
+        self.numOfInputs = inputNumber
+
+    def get_input(self, num):
+        return self.input[num]
+
+    def get_output(self):
+        return self.output
+
+    def get_numOfInputs(self):
+        return self.numOfInputs
+
+    def set_input(self, num, value):
+        self.input[num] = value
+        self.__calculate_output()
+        return self
+
+    def add_input(self):
+        self.input.append(False)
+        self.numOfInputs += 1
+        self.__calculate_output()
+        return self
+
+    def remove_input(self):
+        self.input.pop()
+        self.numOfInputs -= 1
+        self.__calculate_output()
+        return self
+
+    def __calculate_output(self):
+        output = False
+        for value in self.input:
+            output = output or value
+        self.output = not output
+        return self
+
+    @staticmethod
+    def __create_input(number):
+        return [False] * number
diff --git a/BinaryTools/LogicGates/Not.py b/BinaryTools/LogicGates/Not.py
new file mode 100644
index 0000000..9e6aa1b
--- /dev/null
+++ b/BinaryTools/LogicGates/Not.py
@@ -0,0 +1,19 @@
+class Not:
+    def __init__(self):
+        self.input = True
+        self.output = self.__calculate_output()
+
+    def get_input(self):
+        return self.input
+
+    def get_output(self):
+        return self.output
+
+    def set_input(self, value):
+        self.input = value
+        self.__calculate_output()
+        return self
+
+    def __calculate_output(self):
+        self.output = not self.input
+        return self
diff --git a/BinaryTools/LogicGates/Or.py b/BinaryTools/LogicGates/Or.py
new file mode 100644
index 0000000..6da3335
--- /dev/null
+++ b/BinaryTools/LogicGates/Or.py
@@ -0,0 +1,42 @@
+class Or:
+    def __init__(self, inputNumber=2):
+        self.input = self.__create_input(inputNumber)
+        self.output = False
+        self.numOfInputs = inputNumber
+
+    def get_input(self, num):
+        return self.input[num]
+
+    def get_output(self):
+        return self.output
+
+    def get_numOfInputs(self):
+        return self.numOfInputs
+
+    def set_input(self, num, value):
+        self.input[num] = value
+        self.__calculate_output()
+        return self
+
+    def add_input(self):
+        self.input.append(False)
+        self.numOfInputs += 1
+        self.__calculate_output()
+        return self
+
+    def remove_input(self):
+        self.input.pop()
+        self.numOfInputs -= 1
+        self.__calculate_output()
+        return self
+
+    def __calculate_output(self):
+        output = False
+        for value in self.input:
+            output = output or value
+        self.output = output
+        return self
+
+    @staticmethod
+    def __create_input(number):
+        return [False] * number
diff --git a/BinaryTools/LogicGates/Xor.py b/BinaryTools/LogicGates/Xor.py
new file mode 100644
index 0000000..b422abf
--- /dev/null
+++ b/BinaryTools/LogicGates/Xor.py
@@ -0,0 +1,42 @@
+class Xor:
+    def __init__(self, inputNumber=2):
+        self.input = self.__create_input(inputNumber)
+        self.output = False
+        self.numOfInputs = inputNumber
+
+    def get_input(self, num):
+        return self.input[num]
+
+    def get_output(self):
+        return self.output
+
+    def get_numOfInputs(self):
+        return self.numOfInputs
+
+    def set_input(self, num, value):
+        self.input[num] = value
+        self.__calculate_output()
+        return self
+
+    def add_input(self):
+        self.input.append(False)
+        self.numOfInputs += 1
+        self.__calculate_output()
+        return self
+
+    def remove_input(self):
+        self.input.pop()
+        self.numOfInputs -= 1
+        self.__calculate_output()
+        return self
+
+    def __calculate_output(self):
+        output = False
+        for value in self.input:
+            output = ((output and not value) or (not output and value))
+        self.output = output
+        return self
+
+    @staticmethod
+    def __create_input(number):
+        return [False] * number
diff --git a/BinaryTools/Logic_Gates/__init__.py b/BinaryTools/LogicGates/__init__.py
similarity index 100%
rename from BinaryTools/Logic_Gates/__init__.py
rename to BinaryTools/LogicGates/__init__.py


Test patch:
diff --git a/tests/__init__.py b/tests/__init__.py
new file mode 100644
index 0000000..bb377e8
--- /dev/null
+++ b/tests/__init__.py
@@ -0,0 +1,1 @@
+import pytest
\ No newline at end of file
diff --git a/tests/test_and.py b/tests/test_and.py
new file mode 100644
index 0000000..cd9cecd
--- /dev/null
+++ b/tests/test_and.py
@@ -0,0 +1,37 @@
+from BinaryTools.LogicGates import And
+
+
+def test_and_gate():
+    errors = []
+    andGate = And.And()
+
+    if andGate.get_output():
+        errors.append("False and False not passed!")
+
+    andGate.set_input(0, True)
+    if andGate.get_output():
+        errors.append("True and False not passed!")
+
+    andGate.set_input(1, True)
+    if not andGate.get_output():
+        errors.append("True and True not passed!")
+
+    andGate.add_input()
+    if not andGate.get_numOfInputs() == 3:
+        errors.append("Input adding not passed!")
+
+    andGate2 = And.And(3)
+    if not andGate2.get_numOfInputs() == 3:
+        errors.append("Multiple inputs when building not passed!")
+
+    if andGate2.set_input(0, True).set_input(1, True).get_output():
+        errors.append("True and True and False not passed!")
+
+    andGate2.remove_input()
+    if not andGate2.get_numOfInputs() == 2:
+        errors.append("Removing item not passed!")
+
+    if not andGate2.get_output():
+        errors.append("Testing output after removing input not passed!")
+
+    assert not errors, "errors occured:\n{}".format("\n".join(errors))
diff --git a/tests/test_nand.py b/tests/test_nand.py
new file mode 100644
index 0000000..baa3b7d
--- /dev/null
+++ b/tests/test_nand.py
@@ -0,0 +1,43 @@
+from BinaryTools.LogicGates import Nand
+
+
+def test_and_gate():
+    errors = []
+    nandGate = Nand.Nand()
+
+    if not nandGate.get_output():
+        errors.append("False and False not passed!")
+
+    nandGate.set_input(0, True)
+    if not nandGate.get_output():
+        errors.append("True and False not passed!")
+
+    nandGate.set_input(1, True)
+    if nandGate.get_output():
+        errors.append("True and True not passed!")
+
+    nandGate.add_input()
+    if not nandGate.get_numOfInputs() == 3:
+        errors.append("Input adding not passed!")
+
+    nandGate2 = Nand.Nand(3)
+    if not nandGate2.get_numOfInputs() == 3:
+        errors.append("Multiple inputs when building not passed!")
+
+    if not nandGate2.get_output():
+        errors.append("Output when 3 inputs not passed!")
+
+    if not nandGate2.set_input(0, True).set_input(1, True).get_output():
+        errors.append("True and True and False not passed!")
+
+    nandGate2.remove_input()
+    if not nandGate2.get_numOfInputs() == 2:
+        errors.append("Removing item not passed!")
+
+    if nandGate2.get_output():
+        errors.append("Testing output after removing input not passed!")
+
+    if not nandGate2.set_input(0, False).get_output():
+        errors.append("Testing output after removing input not passed!")
+
+    assert not errors, "errors occured:\n{}".format("\n".join(errors))
diff --git a/tests/test_nor.py b/tests/test_nor.py
new file mode 100644
index 0000000..4ed5302
--- /dev/null
+++ b/tests/test_nor.py
@@ -0,0 +1,37 @@
+from BinaryTools.LogicGates import Nor
+
+
+def test_or_gate():
+    errors = []
+    norGate = Nor.Nor()
+
+    if not norGate.get_output():
+        errors.append("False and False not passed!")
+
+    norGate.set_input(0, True)
+    if norGate.get_output():
+        errors.append("True and False not passed!")
+
+    norGate.set_input(1, True)
+    if norGate.get_output():
+        errors.append("True and True not passed!")
+
+    norGate.add_input()
+    if not norGate.get_numOfInputs() == 3:
+        errors.append("Input adding not passed!")
+
+    norGate2 = Nor.Nor(3)
+    if not norGate2.get_numOfInputs() == 3:
+        errors.append("Multiple inputs when building not passed!")
+
+    if norGate2.set_input(0, True).set_input(1, True).get_output():
+        errors.append("True and True and False not passed!")
+
+    norGate2.remove_input()
+    if not norGate2.get_numOfInputs() == 2:
+        errors.append("Removing item not passed!")
+
+    if norGate2.get_output():
+        errors.append("Testing output after removing input not passed!")
+
+    assert not errors, "errors occured:\n{}".format("\n".join(errors))
diff --git a/tests/test_not.py b/tests/test_not.py
new file mode 100644
index 0000000..c92c8e2
--- /dev/null
+++ b/tests/test_not.py
@@ -0,0 +1,18 @@
+from BinaryTools.LogicGates import Not
+
+
+def test_not_gate():
+    errors = []
+    notGate = Not.Not()
+
+    if notGate.get_output():
+        errors.append("True not passed!")
+
+    if not notGate.set_input(False).get_output():
+        errors.append("False not passed!")
+
+    if notGate.get_input():
+        errors.append("Get input method for False not passed!")
+
+    if not notGate.set_input(True).get_input():
+        errors.append("Get input method for True not passed!")
diff --git a/tests/test_or.py b/tests/test_or.py
new file mode 100644
index 0000000..2ec6b16
--- /dev/null
+++ b/tests/test_or.py
@@ -0,0 +1,37 @@
+from BinaryTools.LogicGates import Or
+
+
+def test_or_gate():
+    errors = []
+    orGate = Or.Or()
+
+    if orGate.get_output():
+        errors.append("False and False not passed!")
+
+    orGate.set_input(0, True)
+    if not orGate.get_output():
+        errors.append("True and False not passed!")
+
+    orGate.set_input(1, True)
+    if not orGate.get_output():
+        errors.append("True and True not passed!")
+
+    orGate.add_input()
+    if not orGate.get_numOfInputs() == 3:
+        errors.append("Input adding not passed!")
+
+    orGate2 = Or.Or(3)
+    if not orGate2.get_numOfInputs() == 3:
+        errors.append("Multiple inputs when building not passed!")
+
+    if not orGate2.set_input(0, True).set_input(1, True).get_output():
+        errors.append("True and True and False not passed!")
+
+    orGate2.remove_input()
+    if not orGate2.get_numOfInputs() == 2:
+        errors.append("Removing item not passed!")
+
+    if not orGate2.get_output():
+        errors.append("Testing output after removing input not passed!")
+
+    assert not errors, "errors occured:\n{}".format("\n".join(errors))
diff --git a/tests/test_xor.py b/tests/test_xor.py
new file mode 100644
index 0000000..319027e
--- /dev/null
+++ b/tests/test_xor.py
@@ -0,0 +1,37 @@
+from BinaryTools.LogicGates import Xor
+
+
+def test_or_gate():
+    errors = []
+    xorGate = Xor.Xor()
+
+    if xorGate.get_output():
+        errors.append("False and False not passed!")
+
+    xorGate.set_input(0, True)
+    if not xorGate.get_output():
+        errors.append("True and False not passed!")
+
+    xorGate.set_input(1, True)
+    if xorGate.get_output():
+        errors.append("True and True not passed!")
+
+    xorGate.add_input()
+    if not xorGate.get_numOfInputs() == 3:
+        errors.append("Input adding not passed!")
+
+    xorGate2 = Xor.Xor(3)
+    if not xorGate2.get_numOfInputs() == 3:
+        errors.append("Multiple inputs when building not passed!")
+
+    if xorGate2.set_input(0, True).set_input(1, True).get_output():
+        errors.append("True and True and False not passed!")
+
+    xorGate2.remove_input()
+    if not xorGate2.get_numOfInputs() == 2:
+        errors.append("Removing item not passed!")
+
+    if xorGate2.get_output():
+        errors.append("Testing output after removing input not passed!")
+
+    assert not errors, "errors occured:\n{}".format("\n".join(errors))