From a64a75f92677c24410cb24f4af59e092980cd93f Mon Sep 17 00:00:00 2001 From: fdai7764 Date: Sat, 27 Jan 2024 16:01:39 +0100 Subject: [PATCH] added test for dividing by zero if the second root is zero as well as functionality to avoid this --- src/main/py/calculations_with_roots.py | 2 +- src/test/py/test_calculations_with_roots.py | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main/py/calculations_with_roots.py b/src/main/py/calculations_with_roots.py index da59537..7bceb34 100644 --- a/src/main/py/calculations_with_roots.py +++ b/src/main/py/calculations_with_roots.py @@ -6,7 +6,7 @@ def multiplyRoots(first_number, second_number): return sqrt(first_number * second_number) def divideRoots(first_number, second_number): - if first_number < 0 or second_number < 0: + if first_number < 0 or second_number <= 0: return -1 return sqrt(first_number / second_number) diff --git a/src/test/py/test_calculations_with_roots.py b/src/test/py/test_calculations_with_roots.py index 6e10384..662401a 100644 --- a/src/test/py/test_calculations_with_roots.py +++ b/src/test/py/test_calculations_with_roots.py @@ -24,6 +24,9 @@ class calculationsWithRoots(unittest.TestCase): def test_divide_sqrt_negative_16_by_sqrt_negative_4_should_be_negative_1_for_error(self): self.assertEqual(divideRoots(-16,-4), -1) + def test_divide_sqrt_16_by_sqrt_0_should_be_negative_1_for_error(self): + self.assertEqual(divideRoots(16,0), -1) + def test_sqrt_4_to_power_of_3_should_be_8(self): self.assertEqual(sqrt_power(4, 3), 8)