From 8835c9478b3ceae2c142186e86a04514d61db8a3 Mon Sep 17 00:00:00 2001 From: fdai7763 Date: Tue, 23 Jan 2024 20:41:46 +0100 Subject: [PATCH] added test for calculating string with pow as well as corresponding functionality --- src/main/py/read_math_function.py | 3 +++ src/test/py/test_read_math_function.py | 2 ++ 2 files changed, 5 insertions(+) diff --git a/src/main/py/read_math_function.py b/src/main/py/read_math_function.py index 89ea18d..3f7deea 100644 --- a/src/main/py/read_math_function.py +++ b/src/main/py/read_math_function.py @@ -1,3 +1,5 @@ +import math + def read_constant(type, value): if type != "c": return False @@ -13,6 +15,7 @@ def is_allowed_string(type, value): return all(character.isdigit() or character in allowed for character in value) def calculate_string(string): + string = string.replace('^', '**') try: return eval(string) except Exception as e: diff --git a/src/test/py/test_read_math_function.py b/src/test/py/test_read_math_function.py index 6f2bfd8..dab7400 100644 --- a/src/test/py/test_read_math_function.py +++ b/src/test/py/test_read_math_function.py @@ -17,6 +17,8 @@ class read_function(unittest.TestCase): self.assertEqual(is_allowed_string("c", "42^(2)+1-3*4/3x"), False) def test_constant_calculate_without_pow(self): self.assertEqual(calculate_string("42+1-3*4/3"), 39) + def test_constant_calculate_with_pow(self): + self.assertEqual(calculate_string("42^(2)+1-3*4/3"), 1761) if __name__ == '__main__': unittest.main()