From 06a42d159b1bb24d8067653d06c6a0ad10be397a Mon Sep 17 00:00:00 2001 From: fdai7764 Date: Sat, 27 Jan 2024 14:34:20 +0100 Subject: [PATCH] added test to use sqrt_power with a negative base as well as corresponding functionality to catch math error --- src/main/py/calculations_with_roots.py | 2 ++ src/test/py/test_calculations_with_roots.py | 3 +++ 2 files changed, 5 insertions(+) diff --git a/src/main/py/calculations_with_roots.py b/src/main/py/calculations_with_roots.py index f453bff..3b5f9ea 100644 --- a/src/main/py/calculations_with_roots.py +++ b/src/main/py/calculations_with_roots.py @@ -6,4 +6,6 @@ def multiplyRoots(first_number, second_number): return sqrt(first_number * second_number) def sqrt_power(base, exponent): + if base < 0: + return -1 return potentiate(sqrt(base), exponent) \ No newline at end of file diff --git a/src/test/py/test_calculations_with_roots.py b/src/test/py/test_calculations_with_roots.py index deaec8b..1928c80 100644 --- a/src/test/py/test_calculations_with_roots.py +++ b/src/test/py/test_calculations_with_roots.py @@ -21,6 +21,9 @@ class calculationsWithRoots(unittest.TestCase): def test_sqrt_4_to_power_of_3_should_be_8(self): self.assertEqual(sqrt_power(4, 3), 8) + def test_sqrt_negative_4_to_power_of_3_should_be_negative_one_for_error(self): + self.assertEqual(sqrt_power(-4, 3), -1) + if __name__ == '__main__': unittest.main()