summaryrefslogtreecommitdiff
path: root/unitTests.py
diff options
context:
space:
mode:
Diffstat (limited to 'unitTests.py')
-rw-r--r--unitTests.py69
1 files changed, 47 insertions, 22 deletions
diff --git a/unitTests.py b/unitTests.py
index 17654e4..4e27776 100644
--- a/unitTests.py
+++ b/unitTests.py
@@ -129,35 +129,60 @@ class ParseFourFnTest(ParseTestCase):
import examples.fourFn as fourFn
def test(s, ans):
fourFn.exprStack = []
- results = (fourFn.BNF()).parseString(s)
- resultValue = fourFn.evaluateStack(fourFn.exprStack)
- self.assertTrue(resultValue == ans, "failed to evaluate %s, got %f" % (s, resultValue))
- print_(s, "->", resultValue)
+ results = fourFn.BNF().parseString(s)
+ try:
+ resultValue = fourFn.evaluate_stack(fourFn.exprStack)
+ except Exception:
+ self.assertIsNone(ans, "exception raised for expression {0!r}".format(s))
+ else:
+ self.assertTrue(resultValue == ans, "failed to evaluate %s, got %f" % (s, resultValue))
+ print_(s, "->", resultValue)
+
- from math import pi, exp
- e = exp(1)
+ import math
+ e = math.exp(1)
test("9", 9)
- test("9 + 3 + 6", 18)
- test("9 + 3 / 11", 9.0+3.0/11.0)
- test("(9 + 3)", 12)
- test("(9+3) / 11", (9.0+3.0)/11.0)
- test("9 - (12 - 6)", 3)
- test("2*3.14159", 6.28318)
- test("3.1415926535*3.1415926535 / 10", 3.1415926535*3.1415926535/10.0)
- test("PI * PI / 10", pi*pi/10.0)
- test("PI*PI/10", pi*pi/10.0)
+ test("-9", -9)
+ test("--9", 9)
+ test("-E", -math.e)
+ test("9 + 3 + 6", 9 + 3 + 6)
+ test("9 + 3 / 11", 9 + 3.0 / 11)
+ test("(9 + 3)", (9 + 3))
+ test("(9+3) / 11", (9 + 3.0) / 11)
+ test("9 - 12 - 6", 9 - 12 - 6)
+ test("9 - (12 - 6)", 9 - (12 - 6))
+ test("2*3.14159", 2 * 3.14159)
+ test("3.1415926535*3.1415926535 / 10", 3.1415926535 * 3.1415926535 / 10)
+ test("PI * PI / 10", math.pi * math.pi / 10)
+ test("PI*PI/10", math.pi * math.pi / 10)
+ test("PI^2", math.pi ** 2)
+ test("round(PI^2)", round(math.pi ** 2))
test("6.02E23 * 8.048", 6.02E23 * 8.048)
- test("e / 3", e/3.0)
- test("sin(PI/2)", 1.0)
- test("trunc(E)", 2.0)
- test("E^PI", e**pi)
- test("2^3^2", 2**3**2)
- test("2^3+2", 2**3+2)
- test("2^9", 2**9)
+ test("e / 3", math.e / 3)
+ test("sin(PI/2)", math.sin(math.pi / 2))
+ test("10+sin(PI/4)^2", 10 + math.sin(math.pi / 4) ** 2)
+ test("trunc(E)", int(math.e))
+ test("trunc(-E)", int(-math.e))
+ test("round(E)", round(math.e))
+ test("round(-E)", round(-math.e))
+ test("E^PI", math.e ** math.pi)
+ test("exp(0)", 1)
+ test("exp(1)", math.e)
+ test("2^3^2", 2 ** 3 ** 2)
+ test("(2^3)^2", (2 ** 3) ** 2)
+ test("2^3+2", 2 ** 3 + 2)
+ test("2^3+5", 2 ** 3 + 5)
+ test("2^9", 2 ** 9)
test("sgn(-2)", -1)
test("sgn(0)", 0)
test("sgn(0.1)", 1)
+ test("foo(0.1)", None)
+ test("round(E, 3)", round(math.e, 3))
+ test("round(PI^2, 3)", round(math.pi ** 2, 3))
+ test("sgn(cos(PI/4))", 1)
+ test("sgn(cos(PI/2))", 0)
+ test("sgn(cos(PI*3/4))", -1)
class ParseSQLTest(ParseTestCase):
def runTest(self):