test_calc.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import unittest
  2. from src.parser import Parser
  3. from src.node import ExpressionNode as N, ExpressionLeaf as L
  4. from tests.parser import ParserWrapper, run_expressions
  5. class TestCalc(unittest.TestCase):
  6. def test_constructor(self):
  7. assert ParserWrapper(Parser).run(['1+4']) \
  8. == N('+', L(1), L(4))
  9. def test_basic_on_exp(self):
  10. expressions = [('4', L(4)),
  11. ('3+4', N('+', L(3), L(4))),
  12. ('3-4', N('-', L(3), L(4))),
  13. ('3/4', N('/', L(3), L(4))),
  14. ('-4', N('-', L(4))),
  15. ('3^4', N('^', L(3), L(4))),
  16. ('(2)', L(2))]
  17. run_expressions(Parser, expressions)
  18. def test_infinity(self):
  19. expressions = [('2^3000', N('^', L(2), L(3000))),
  20. ('2^-3000', N('^', L(2), N('-', L(3000))))]
  21. # ('2^99999999999', None),
  22. # ('2^-99999999999', 0.0)]
  23. run_expressions(Parser, expressions)
  24. def test_concat_easy(self):
  25. expressions = [
  26. ('xy', N('*', L('x'), L('y'))),
  27. ('2x', N('*', L(2), L('x'))),
  28. ('x4', N('^', L('x'), L(4))),
  29. ('xy4', N('*', L('x'), N('^', L('y'), L(4)))),
  30. ('(x)4', N('*', L('x'), L(4))),
  31. ('(3+4)2', N('*', N('+', L(3), L(4)), L(2))),
  32. ]
  33. run_expressions(Parser, expressions)
  34. def test_concat_intermediate(self):
  35. expressions = [
  36. ('(3+4)(5+7)', N('*', N('+', L(3), L(4)),
  37. N('+', L(5), L(7)))),
  38. ('(a+b)(c+d)', N('*', N('+', L('a'), L('b')),
  39. N('+', L('c'), L('d')))),
  40. ('a+b(c+d)', N('+', L('a'), N('*', L('b'),
  41. N('+', L('c'), L('d'))))),
  42. ('ab(c)d', N('*', L('a'), L('b'), L('c'), L('d'))),
  43. #('ab(c)d', N('*', L('a'), N('*', L('b'),
  44. # N('*', L('c'), L('d'))))),
  45. ]
  46. run_expressions(Parser, expressions)