test_calc.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. # This file is part of TRS (http://math.kompiler.org)
  2. #
  3. # TRS is free software: you can redistribute it and/or modify it under the
  4. # terms of the GNU Affero General Public License as published by the Free
  5. # Software Foundation, either version 3 of the License, or (at your option) any
  6. # later version.
  7. #
  8. # TRS is distributed in the hope that it will be useful, but WITHOUT ANY
  9. # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  10. # A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  11. # details.
  12. #
  13. # You should have received a copy of the GNU Affero General Public License
  14. # along with TRS. If not, see <http://www.gnu.org/licenses/>.
  15. import unittest
  16. from src.parser import Parser
  17. from src.node import ExpressionNode as N, ExpressionLeaf as L
  18. from tests.parser import ParserWrapper, run_expressions
  19. class TestCalc(unittest.TestCase):
  20. def test_constructor(self):
  21. assert ParserWrapper(Parser).run(['1+4']) \
  22. == N('+', L(1), L(4))
  23. def test_basic_on_exp(self):
  24. expressions = [('4', L(4)),
  25. ('3+4', L(3) + L(4)),
  26. ('3-4', L(3) + -L(4)),
  27. ('3/4', L(3) / L(4)),
  28. ('-4', -L(4)),
  29. ('3^4', N('^', L(3), L(4))),
  30. ('(2)', L(2))]
  31. run_expressions(Parser, expressions)
  32. def test_infinity(self):
  33. expressions = [('2^3000', N('^', L(2), L(3000))),
  34. ('2^-3000', N('^', L(2), -L(3000)))]
  35. # ('2^99999999999', None),
  36. # ('2^-99999999999', 0.0)]
  37. run_expressions(Parser, expressions)
  38. def test_concat_easy(self):
  39. expressions = [
  40. ('xy', N('*', L('x'), L('y'))),
  41. ('2x', N('*', L(2), L('x'))),
  42. ('x4', N('*', L('x'), L(4))),
  43. ('3 4', N('*', L(3), L(4))),
  44. ('(x)4', N('*', L('x'), L(4))),
  45. ('(3+4)2', N('*', N('+', L(3), L(4)), L(2))),
  46. ]
  47. run_expressions(Parser, expressions)
  48. def test_concat_intermediate(self):
  49. expressions = [
  50. ('(3+4)(5+7)', N('*', N('+', L(3), L(4)),
  51. N('+', L(5), L(7)))),
  52. ('(a+b)(c+d)', N('*', N('+', L('a'), L('b')),
  53. N('+', L('c'), L('d')))),
  54. ('a+b(c+d)', N('+', L('a'), N('*', L('b'),
  55. N('+', L('c'), L('d'))))),
  56. ('abcd', N('*', N('*', N('*', L('a'), L('b')),
  57. L('c')), L('d'))),
  58. ('ab(c)d', N('*', N('*', N('*', L('a'), L('b')),
  59. L('c')), L('d'))),
  60. ('ab*(c)*d', N('*', N('*', N('*', L('a'), L('b')),
  61. L('c')), L('d'))),
  62. ('ab*(c)^d', N('*', N('*', L('a'), L('b')),
  63. N('^', L('c'), L('d')))),
  64. ]
  65. run_expressions(Parser, expressions)
  66. def test_pow_nested(self):
  67. # a^b^c = a^(b^c) != (a^b)^c
  68. a, b, c, d, e = L('a'), L('b'), L('c'), L('d'), L('e')
  69. expressions = [
  70. ('a^b^c', N('^', a, N('^', b, c))),
  71. ('-1^b^c', -N('^', L(1), N('^', b, c))),
  72. ('ab^c', N('*', a, N('^', b, c))),
  73. ('a(b)^c', N('*', a, N('^', b, c))),
  74. ('a(b+c)^(d+e)', N('*', a, N('^', N('+', b, c),
  75. N('+', d, e)))),
  76. ('(a(b+c))^(d+e)', N('^', N('*', a, N('+', b, c)),
  77. N('+', d, e))),
  78. ]
  79. run_expressions(Parser, expressions)
  80. def test_negation(self):
  81. run_expressions(Parser, [
  82. ('-9', -L(9)),
  83. ('--9', --L(9)),
  84. ('a--9', L('a') + -(-L(9))),
  85. ])