def calculate_result(self): """Calculates the result placed in subresult.""" try: result = self.line_subresult.text() string = str(result) result = string.split() try: res = int(result[0]) except ValueError: res = float(result[0]) for x in range(1, len(result), 2): number = result[x + 1] try: number = int(number) except ValueError: number = float(number) if "+" in result[x]: res = round(MathLib.add(res, number), 9) if "-" in result[x]: res = round(MathLib.subtract(res, number), 9) if "*" in result[x]: res = round(MathLib.multiply(res, number), 9) if "/" in result[x]: res = round(MathLib.divide(res, number), 9) self.line_result.setText(str(res)) self.line_subresult.setText(self.line_subresult.text() + " " + "=") except ValueError: self.line_result.setText("Math Error") except OverflowError: self.line_result.setText("Overflow Error")
class matlibTest(unittest.TestCase): def setUp(self): self.matlib = MathLib() def test_add(self): self.assertEqual(self.matlib.add(1, 2), 3) self.assertEqual(self.matlib.add(-2, 2), 0) self.assertEqual(self.matlib.add(0.002, -0), 0.002) self.assertEqual(self.matlib.add(-2.5, -1.5), -4) self.assertEqual(self.matlib.add(0, 0), 0) self.assertRaises(TypeError, self.matlib.add) self.assertRaises(TypeError, self.matlib.add, 2) def test_sub(self): self.assertEqual(self.matlib.sub(4, 2), 2) self.assertEqual(self.matlib.sub(0, 2), -2) self.assertEqual(self.matlib.sub(-0.00005, 4), -4.00005) self.assertEqual(self.matlib.sub(4, -2), 6) self.assertEqual(self.matlib.sub(0, 0), 0) self.assertRaises(TypeError, self.matlib.sub) self.assertRaises(TypeError, self.matlib.sub, 2) def test_mul(self): self.assertEqual(self.matlib.mul(2, 5), 10) self.assertEqual(self.matlib.mul(0.0005, 0.1), 0.00005) self.assertEqual(self.matlib.mul(-2, 5), -10) self.assertEqual(self.matlib.mul(-2, -5), 10) self.assertEqual(self.matlib.mul(2, 0), 0) self.assertEqual(self.matlib.mul(0, 0), 0) self.assertRaises(TypeError, self.matlib.mul) self.assertRaises(TypeError, self.matlib.mul, 2) def test_div(self): self.assertEqual(self.matlib.div(8, 4), 2) self.assertEqual(self.matlib.div(8, -2), -4) self.assertEqual(self.matlib.div(8, 0.4), 20) self.assertEqual(self.matlib.div(0.8, 4), 0.2) self.assertEqual(self.matlib.div(0, 4), 0) self.assertEqual(self.matlib.div(-0, 4), 0) self.assertRaises(ZeroDivisionError, self.matlib.div, 5, 0) self.assertRaises(TypeError, self.matlib.div) self.assertRaises(TypeError, self.matlib.div, 2) def test_fac(self): self.assertEqual(self.matlib.fac(5), 120) self.assertEqual(self.matlib.fac(1), 1) self.assertEqual(self.matlib.fac(0), 1) self.assertEqual(self.matlib.fac(2), 2) self.assertRaises(TypeError, self.matlib.fac) self.assertRaises(Exception, self.matlib.fac, -2) self.assertRaises(Exception, self.matlib.fac, 1.55) def test_root_n(self): self.assertEqual(self.matlib.root_n(4, 2), 2) self.assertEqual(self.matlib.root_n(0, 5), 0) self.assertEqual(self.matlib.root_n(1, 6), 1) self.assertEqual(self.matlib.root_n(10.89, 2), 3.3) self.assertEqual(self.matlib.root_n(10.903204, 2), 3.302) self.assertRaises(TypeError, self.matlib.root_n) self.assertRaises(Exception, self.matlib.root_n, -2) def test_pow_x_y(self): self.assertEqual(self.matlib.pow_x_y(4, 2), 16) self.assertEqual(self.matlib.pow_x_y(0.5, 3), 0.125) self.assertEqual(self.matlib.pow_x_y(1, 200), 1) self.assertEqual(self.matlib.pow_x_y(0, 200), 0) self.assertEqual(self.matlib.pow_x_y(0, 0), 1) self.assertEqual(self.matlib.pow_x_y(251, 0), 1) self.assertEqual(self.matlib.pow_x_y(251, 0), 1) self.assertEqual(self.matlib.pow_x_y(2, -2), 0.25) self.assertEqual(self.matlib.pow_x_y(2, 2.2), 4.59479341998814) self.assertRaises(TypeError, self.matlib.pow_x_y) def test_log_n(self): self.assertEqual(self.matlib.log_n(2), 0.6931471805599454) self.assertEqual(self.matlib.log_n(1.1), 0.09531017980432499) self.assertEqual(self.matlib.log_n(1), 0) self.assertEqual(self.matlib.log_n(2.8), 1.0296194171811583) self.assertEqual(self.matlib.log_n(284), 5.648974238161206) self.assertRaises(Exception, self.matlib.log_n, 0) self.assertRaises(Exception, self.matlib.log_n, 0) self.assertRaises(Exception, self.matlib.log_n, -1) self.assertRaises(TypeError, self.matlib.log_n)
class MathLibTests(TestCase): def setUp(self): self.math = MathLib() # Tests for add function def test_add_positive(self): self.assertEqual(self.math.add(1, 3), 4) self.assertEqual(self.math.add(13, 19), 32) self.assertEqual(self.math.add(0, 1), 1) self.assertEqual(self.math.add(1, 0), 1) self.assertEqual(self.math.add(0, 0), 0) def test_add_negative(self): self.assertEqual(self.math.add(-1, -3), -4) self.assertEqual(self.math.add(-25, -56), -81) self.assertEqual(self.math.add(-1, 0), -1) self.assertEqual(self.math.add(0, -1), -1) def test_add_both(self): self.assertEqual(self.math.add(-1, 2), 1) self.assertEqual(self.math.add(1, -2), -1) self.assertEqual(self.math.add(-23, 56), 33) self.assertEqual(self.math.add(20, -20), 0) self.assertEqual(self.math.add(-1, 1), 0) def test_add_float(self): self.assertAlmostEqual(self.math.add(-1.5, -3.5), -5, 1) self.assertAlmostEqual(self.math.add(5.3, 10.4), 15.7, 1) self.assertAlmostEqual(self.math.add(5.4, -5.3), 0.1, 1) self.assertAlmostEqual(self.math.add(-1.3, 3.3), 2, 1) # Tests for sub function def test_sub_positive(self): self.assertEqual(self.math.subtract(7, 3), 4) self.assertEqual(self.math.subtract(3, 7), -4) self.assertEqual(self.math.subtract(0, 3), -3) self.assertEqual(self.math.subtract(3, 0), 3) self.assertEqual(self.math.subtract(0, 0), 0) self.assertEqual(self.math.subtract(10, 10), 0) self.assertEqual(self.math.subtract(54, 22), 32) def test_sub_negative(self): self.assertEqual(self.math.subtract(-4, -3), -1) self.assertEqual(self.math.subtract(-3, -4), 1) self.assertEqual(self.math.subtract(0, -3), 3) self.assertEqual(self.math.subtract(-3, 0), -3) self.assertEqual(self.math.subtract(-56, -45), -11) def test_sub_both(self): self.assertEqual(self.math.subtract(7, -3), 10) self.assertEqual(self.math.subtract(-7, 3), -10) self.assertEqual(self.math.subtract(54, -10), 64) self.assertEqual(self.math.subtract(-54, 10), -64) def test_sub_float(self): self.assertEqual(self.math.subtract(5.5, 3.3), 2.2) self.assertEqual(self.math.subtract(-7.4, -2.4), -5) self.assertEqual(self.math.subtract(10.5, -2.4), 12.9) self.assertEqual(self.math.subtract(-5.2, 10), -15.2) # Tests for multiply function def test_multiply_positive(self): self.assertEqual(self.math.multiply(3, 2), 6) self.assertEqual(self.math.multiply(2, 3), 6) self.assertEqual(self.math.multiply(0, 2), 0) self.assertEqual(self.math.multiply(4, 0), 0) self.assertEqual(self.math.multiply(0, 0), 0) self.assertEqual(self.math.multiply(45, 23), 1035) def test_multiply_negative(self): self.assertEqual(self.math.multiply(-3, -2), 6) self.assertEqual(self.math.multiply(0, -2), 0) self.assertEqual(self.math.multiply(-2, 0), 0) self.assertEqual(self.math.multiply(-12, -34), 408) def test_multiply_both(self): self.assertEqual(self.math.multiply(3, -2), -6) self.assertEqual(self.math.multiply(-3, 2), -6) self.assertEqual(self.math.multiply(-12, 2), -24) self.assertEqual(self.math.multiply(2, -12), -24) def test_multiply_float(self): self.assertAlmostEqual(self.math.multiply(3.3, 2.2), 7.26, 2) self.assertAlmostEqual(self.math.multiply(-6.2, -2.1), 13.02, 2) self.assertAlmostEqual(self.math.multiply(-3.3, 2.2), -7.26, 2) self.assertAlmostEqual(self.math.multiply(6.2, -2.1), -13.02, 2) # Tests for divide function def test_divide_positive(self): self.assertEqual(self.math.divide(5, 1), 5) self.assertEqual(self.math.divide(1, 2), 0.5) self.assertEqual(self.math.divide(0, 1), 0) self.assertEqual(self.math.divide(55, 2), 27.5) def test_divide_negative(self): self.assertEqual(self.math.divide(-10, -2), 5) self.assertEqual(self.math.divide(-1, -2), 0.5) self.assertEqual(self.math.divide(0, -2), 0) self.assertEqual(self.math.divide(-55, -11), 5) def test_divide_both(self): self.assertAlmostEqual(self.math.divide(-10, 2), -5, 1) self.assertAlmostEqual(self.math.divide(10, -2), -5, 1) self.assertAlmostEqual(self.math.divide(-55, 2), -27.5, 1) self.assertAlmostEqual(self.math.divide(55, -11), -5, 1) def test_divide_float(self): self.assertAlmostEqual(self.math.divide(5.5, 1.1), 5, 1) self.assertAlmostEqual(self.math.divide(-3.2, -0.5), 6.4, 1) self.assertAlmostEqual(self.math.divide(10.3, -2), -5.15, 2) self.assertAlmostEqual(self.math.divide(-10, 2.5), -4, 1) def test_divide_zero(self): with self.assertRaises(ValueError): self.math.divide(2, 0) # Tests for power function def test_power_even(self): self.assertEqual(self.math.power(1, 4), 1) self.assertEqual(self.math.power(2, 2), 4) self.assertEqual(self.math.power(-5, 2), 25) self.assertEqual(self.math.power(2.5, 2), 6.25) self.assertEqual(self.math.power(0, 0), 1) def test_power_odd(self): self.assertEqual(self.math.power(1, 3), 1) self.assertEqual(self.math.power(2, 3), 8) self.assertEqual(self.math.power(-2, 3), -8) self.assertEqual(self.math.power(2.5, 3), 15.625) def test_power_base(self): # Base need to be natural number with self.assertRaises(ValueError): self.math.power(2, 0) self.math.power(3, -5) self.math.power(-4, 1.5) self.math.power(5.5, -1) # Tests for root function def test_root_even(self): self.assertEqual(self.math.root(1, 2), 1) self.assertEqual(self.math.root(4, 2), 2) self.assertAlmostEqual(self.math.root(2, 2), 1.41, 2) def test_root_odd(self): self.assertEqual(self.math.root(1, 3), 1) self.assertEqual(self.math.root(27, 3), 3) self.assertEqual(self.math.root(8, 3), 2) def test_root_number(self): with self.assertRaises(ValueError): self.math.root(-5, 2) self.math.root(-1.5, 2) # Tests for sin function def test_sin(self): self.assertAlmostEqual(self.math.sin(1), 0.8415, 4) self.assertAlmostEqual(self.math.sin(5), -0.9589, 4) self.assertAlmostEqual(self.math.sin(-1), -0.8415, 4) self.assertAlmostEqual(self.math.sin(1.6), 0.9996, 4) # Tests for cos function def test_cos(self): self.assertAlmostEqual(self.math.cos(1), 0.5403, 4) self.assertAlmostEqual(self.math.cos(2), -0.4161, 4) self.assertAlmostEqual(self.math.cos(-4), -0.6536, 4) self.assertAlmostEqual(self.math.cos(1.5), 0.0707, 4) # Tests for tan function def test_tan(self): self.assertAlmostEqual(self.math.tan(1), 1.5574, 4) self.assertAlmostEqual(self.math.tan(0.8), 1.0296, 4) self.assertAlmostEqual(self.math.tan(4), 1.1578, 4) self.assertAlmostEqual(self.math.tan(3.5), 0.3746, 4) # Tests for exp function def test_exp(self): self.assertAlmostEqual(self.math.exp(1), 2.7183, 4) self.assertAlmostEqual(self.math.exp(2), 7.3891, 4) self.assertAlmostEqual(self.math.exp(-2), 0.1353, 4) self.assertAlmostEqual(self.math.exp(1.5), 4.4817, 4) # Tests for natural_log function def test_natural_log(self): self.assertEqual(self.math.natural_log(1), 0) self.assertAlmostEqual(self.math.natural_log(2), 0.6931, 4) self.assertAlmostEqual(self.math.natural_log(3.5), 1.2528, 4) def test_negative_natural_log(self): with self.assertRaises(ValueError): self.math.natural_log(0) self.math.natural_log(-5) self.math.natural_log(-2.5) # Tests for log function def test_log(self): self.assertAlmostEqual(self.math.log(1), 0, 1) self.assertAlmostEqual(self.math.log(100), 2, 1) self.assertAlmostEqual(self.math.log(2), 0.3010, 4) def test_negative_log(self): with self.assertRaises(ValueError): self.math.log(0) self.math.log(-1) self.math.log(-2.5) def test_factorial(self): self.assertEqual(self.math.factorial(12), 479001600) self.assertEqual(self.math.factorial(5), 120) self.assertEqual(self.math.factorial(1), 1) self.assertEqual(self.math.factorial(0), 1) with self.assertRaises(ValueError): self.math.factorial(-1) self.math.factorial(-100) self.math.factorial(-1.01) self.math.factorial(0.99) self.math.factorial(10.5)