[LABI] tema02: implementation of a calculater with the respective functionals and unitaries tests

This commit is contained in:
brunombpereira 2023-05-16 18:46:54 +01:00
parent a3df5126a9
commit 691ce81ea3
11 changed files with 365 additions and 0 deletions

View File

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="DiscordProjectSettings">
<option name="show" value="ASK" />
<option name="description" value="" />
</component>
</project>

View File

@ -0,0 +1,10 @@
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="DuplicatedCode" enabled="true" level="WEAK WARNING" enabled_by_default="true">
<Languages>
<language minSize="55" name="Python" />
</Languages>
</inspection_tool>
</profile>
</component>

View File

@ -0,0 +1,6 @@
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>

View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.11" project-jdk-type="Python SDK" />
</project>

View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/Calculater.iml" filepath="$PROJECT_DIR$/.idea/Calculater.iml" />
</modules>
</component>
</project>

View File

@ -0,0 +1,25 @@
import math
def soma(a, b):
return round(a + b, 2)
def subtracao(a, b):
return round(a - b, 2)
def multiplicacao(a, b):
return round(a * b, 2)
def divisao(a, b):
if b == 0:
raise ZeroDivisionError("Não é possível dividir por zero.")
return round(a / b, 2)
def resto_divisao_inteira(a, b):
if b == 0:
raise ZeroDivisionError("Não é possível dividir por zero.")
return round(a % b, 2)
def raiz_quadrada(a):
if a < 0:
raise ValueError("Não é possível calcular a raiz quadrada de um número negativo.")
return round(math.sqrt(a), 2)

View File

@ -0,0 +1,138 @@
import pytest
import aritmetica
def main():
print("Welcome to the Calculator!")
while True:
print("Please choose an operation:")
print("1. Addition (+)")
print("2. Subtraction (-)")
print("3. Multiplication (*)")
print("4. Division (/)")
print("5. Resto (%)")
print("6. Square Root (sqrt)")
print("7. Exit")
choice = input("Enter your choice (1-6): ")
if choice == '7':
print("Goodbye!")
break
elif choice == '1':
while True:
try:
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
except ValueError as e:
print("Invalid input. Please enter a number.")
print("Error: ", e.__doc__)
continue
result = aritmetica.soma(num1, num2)
print(f"{num1} + {num2} = {result}")
exit = input('To continue operation press ( y ) ?')
if exit.upper() != 'Y':
break
elif choice == '2':
while True:
try:
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
except ValueError as e:
print("Invalid input. Please enter a number.")
print("Error: ", e.__doc__)
continue
result = aritmetica.subtracao(num1, num2)
print(f"{num1} - {num2} = {result}")
exit = input('To continue operation press ( y ) ?')
if exit.upper() != 'Y':
break
elif choice == '3':
while True:
try:
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
except ValueError as e:
print("Invalid input. Please enter a number.")
print("Error: ", e.__doc__)
continue
result = aritmetica.multiplicacao(num1, num2)
print(f"{num1} * {num2} = {result}")
exit = input('To continue operation press ( y ) ?')
if exit.upper() != 'Y':
break
elif choice == '4':
while True:
try:
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
except ValueError as e:
print("Invalid input. Please enter a number.")
print("Error: ", e.__doc__)
continue
result = aritmetica.divisao(num1, num2)
print(f"{num1} / {num2} = {result}")
exit = input('To continue operation press ( y ) ?')
if exit.upper() != 'Y':
break
elif choice == '5' :
while True:
try:
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
except ValueError as e:
print("Invalid input. Please enter a number.")
print("Error: ", e.__doc__)
continue
result = aritmetica.resto_divisao_inteira(num1, num2)
print(f"{num1} % {num2} = {result}")
exit = input('To continue operation press ( y ) ?')
if exit.upper() != 'Y':
break
elif choice == '6':
while True:
try:
num1 = float(input("Enter the first number: "))
except ValueError as e:
print("Invalid input. Please enter a number.")
print("Error: ", e.__doc__)
continue
result = aritmetica.raiz_quadrada(num1)
print(f"sqrt({num1}) = {result}")
exit = input('To continue operation press ( y ) ?')
if exit.upper() != 'Y':
break
else:
print("Invalid input. Please enter a number between 1 and 6.")
if __name__ == "__main__":
pytest.main(['-x', 'test_unitarios.py'])
pytest.main(['-x', 'test_funcionais.py'])
main()

View File

@ -0,0 +1,114 @@
import unittest
from aritmetica import soma, subtracao, multiplicacao, divisao, raiz_quadrada, resto_divisao_inteira
class TestCalculadora(unittest.TestCase):
def test_soma_numeros_validos(self):
self.assertEqual(soma(2, 3), 5)
self.assertEqual(soma(-2.5, 3.5), 1)
self.assertEqual(soma(0, 0), 0)
self.assertEqual(soma(0.1, 0.2), 0.3)
def test_soma_argumentos_invalidos(self):
with self.assertRaises(TypeError):
soma("2", 3)
with self.assertRaises(TypeError):
soma(2, "3")
with self.assertRaises(TypeError):
soma("2", "3")
with self.assertRaises(TypeError):
soma(2)
with self.assertRaises(TypeError):
soma()
def test_subtracao_numeros_validos(self):
self.assertEqual(subtracao(5, 3), 2)
self.assertEqual(subtracao(3, 5), -2)
self.assertEqual(subtracao(-2.5, 3.5), -6)
self.assertEqual(subtracao(0, 0), 0)
self.assertEqual(subtracao(0.1, 0.2), -0.1)
def test_subtracao_argumentos_invalidos(self):
with self.assertRaises(TypeError):
subtracao("2", 3)
with self.assertRaises(TypeError):
subtracao(2, "3")
with self.assertRaises(TypeError):
subtracao("2", "3")
with self.assertRaises(TypeError):
subtracao(2)
with self.assertRaises(TypeError):
subtracao()
def test_multiplicacao_numeros_validos(self):
self.assertEqual(multiplicacao(2, 3), 6)
self.assertEqual(multiplicacao(-2.5, 3.5), -8.75)
self.assertEqual(multiplicacao(0, 0), 0)
self.assertEqual(multiplicacao(0.1, 0.2), 0.02)
def test_multiplicacao_argumentos_invalidos(self):
with self.assertRaises(TypeError):
multiplicacao("2", 3)
with self.assertRaises(TypeError):
multiplicacao(2, "3")
with self.assertRaises(TypeError):
multiplicacao("2", "3")
with self.assertRaises(TypeError):
multiplicacao(2)
with self.assertRaises(TypeError):
multiplicacao()
def test_divisao_numeros_validos(self):
self.assertEqual(divisao(4, 2), 2)
self.assertEqual(divisao(3, 5), 0.6)
self.assertEqual(divisao(-10, 2), -5)
self.assertEqual(divisao(0, 5), 0)
def test_divisao_argumentos_invalidos(self):
with self.assertRaises(TypeError):
divisao("2", 3)
with self.assertRaises(TypeError):
divisao(2, "3")
with self.assertRaises(TypeError):
divisao("2", "3")
with self.assertRaises(TypeError):
divisao(2)
with self.assertRaises(TypeError):
divisao()
with self.assertRaises(ZeroDivisionError):
divisao(5, 0)
def test_resto_divisao_inteira_numeros_validos(self):
self.assertEqual(resto_divisao_inteira(7, 3), 1)
self.assertEqual(resto_divisao_inteira(10, 2), 0)
self.assertEqual(resto_divisao_inteira(-7, 3), 2)
self.assertEqual(resto_divisao_inteira(0, 5), 0)
def test_resto_divisao_inteira_argumentos_invalidos(self):
with self.assertRaises(TypeError):
resto_divisao_inteira("2", 3)
with self.assertRaises(TypeError):
resto_divisao_inteira(2, "3")
with self.assertRaises(TypeError):
resto_divisao_inteira("2", "3")
with self.assertRaises(TypeError):
resto_divisao_inteira(2)
with self.assertRaises(TypeError):
resto_divisao_inteira()
def test_raiz_quadrada_numeros_validos(self):
self.assertAlmostEqual(raiz_quadrada(4), 2)
self.assertAlmostEqual(raiz_quadrada(9), 3)
self.assertAlmostEqual(raiz_quadrada(2), 1.41)
self.assertAlmostEqual(raiz_quadrada(0), 0)
def test_raiz_quadrada_argumentos_invalidos(self):
with self.assertRaises(ValueError):
raiz_quadrada(-4)
with self.assertRaises(TypeError):
raiz_quadrada("2")
with self.assertRaises(TypeError):
raiz_quadrada()
if __name__ == '__main__':
unittest.main()

View File

@ -0,0 +1,37 @@
import unittest
import aritmetica
class TestAritmetica(unittest.TestCase):
def test_soma(self):
self.assertEqual(aritmetica.soma(2, 3), 5)
self.assertEqual(aritmetica.soma(-2, 3), 1)
self.assertEqual(aritmetica.soma(2.5, 3.7), 6.2)
def test_subtracao(self):
self.assertEqual(aritmetica.subtracao(2, 3), -1)
self.assertEqual(aritmetica.subtracao(-2, 3), -5)
self.assertEqual(aritmetica.subtracao(2.5, 3.7), -1.2)
def test_multiplicacao(self):
self.assertEqual(aritmetica.multiplicacao(2, 3), 6)
self.assertEqual(aritmetica.multiplicacao(-2, 3), -6)
self.assertEqual(aritmetica.multiplicacao(2.5, 3.7), 9.25)
def test_divisao(self):
self.assertEqual(aritmetica.divisao(6, 3), 2)
self.assertEqual(aritmetica.divisao(7, 3), 2.33)
self.assertRaises(ZeroDivisionError, aritmetica.divisao, 6, 0)
def test_resto_divisao_inteira(self):
self.assertEqual(aritmetica.resto_divisao_inteira(7, 3), 1)
self.assertEqual(aritmetica.resto_divisao_inteira(10, 4), 2)
self.assertEqual(aritmetica.resto_divisao_inteira(15, 5), 0)
def test_raiz_quadrada(self):
self.assertEqual(aritmetica.raiz_quadrada(9), 3)
self.assertEqual(aritmetica.raiz_quadrada(16), 4)
self.assertEqual(aritmetica.raiz_quadrada(25), 5)
if __name__ == '__main__':
unittest.main()