Browse Source

added test for calculating string with pow as well as corresponding functionality

remotes/origin/feature/feature-read-math-functions
fdai7763 11 months ago
parent
commit
8835c9478b
  1. 3
      src/main/py/read_math_function.py
  2. 2
      src/test/py/test_read_math_function.py

3
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:

2
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()
Loading…
Cancel
Save