def evaluate(): expression = request.GET["expression"] response.content_type = 'text/json' try: result = edsger.evaluate(expression) return json.dumps(result) except Exception, err: return json.dumps(err.__class__.__name__ + ": " + str(err))
def test_unary_minus(self): self.assertAlmostEqual(evaluate('1--2'), 3) self.assertAlmostEqual(evaluate('-3+-2'), -5) self.assertAlmostEqual(evaluate('1+(-2*2)'), -3) self.assertAlmostEqual(evaluate('1--(-2^2)'), -3) self.assertAlmostEqual(evaluate('(1)-2'), -1)
def test_order_of_operations(self): self.assertAlmostEqual(evaluate("(1 + 2) * (4 - 1) ^ 2"), 27) self.assertAlmostEqual(evaluate("(1 + 2) * (4 - 2 ^ 3)"), -12) self.assertAlmostEqual(evaluate("10 / 2 / 3"), 10.0 / 2.0 / 3.0)
def test_nested_par(self): self.assertAlmostEqual(evaluate("((10 - 8) * (2)) + (5 / 3)"), ((10 - 8) * (2)) + (5.0 / 3))
def test_assoc(self): self.assertAlmostEqual(evaluate('1 + 2 * 3'), 7) self.assertAlmostEqual(evaluate('1 + 2 * 3 ^ 2'), 19)
def test_division(self): self.assertAlmostEqual(evaluate('10 / 2'), 5) self.assertAlmostEqual(evaluate('3 / 2'), 3.0/2.0)
def test_multiplication(self): self.assertAlmostEqual(evaluate('2 * 3'), 6)
def test_subtraction(self): self.assertAlmostEqual(evaluate('1 - 2'), -1)
def test_addition(self): self.assertAlmostEqual(evaluate('1 + 2'), 3)