Esempio n. 1
0
 def test_single_value(self):
     self.assertAlmostEqual(1, Calculator.evaluate('1'))
Esempio n. 2
0
 def test_simple_arithmetic(self):
     self.assertAlmostEqual(2, Calculator.evaluate('1 + 1'))
     self.assertAlmostEqual(-1, Calculator.evaluate('3 - 4'))
     self.assertAlmostEqual(12, Calculator.evaluate('3 * 4'))
     self.assertAlmostEqual(3.75, Calculator.evaluate('15 / 4'))
Esempio n. 3
0
 def test_mismatched_parenthesis_right(self):
     with self.assertRaises(Exception):
         Calculator.evaluate('( 3 - 2 ) * 4 )')
Esempio n. 4
0
 def test_parenthesis(self):
     self.assertAlmostEqual(-4, Calculator.evaluate('( 2 - 3 ) * 4'))
     self.assertAlmostEqual(-4, Calculator.evaluate('( ( 2 - 3 ) ) * 4'))
     self.assertAlmostEqual(4, Calculator.evaluate('2 * ( 3 - 1 )'))
Esempio n. 5
0
 def test_missing_operator(self):
     with self.assertRaises(Exception):
         Calculator.evaluate('1 2')
Esempio n. 6
0
 def test_exponent_precedence(self):
     self.assertAlmostEqual(32, Calculator.evaluate('2 ^ 3 * 4'))
     self.assertAlmostEqual(-5, Calculator.evaluate('3 - 2 ^ 3'))
Esempio n. 7
0
 def test_multiplication_division_precedence(self):
     self.assertAlmostEqual(14, Calculator.evaluate('2 + 3 * 4'))
     self.assertAlmostEqual(2.66, Calculator.evaluate('2 / 3 * 4'), 1)