test_b1_ch10.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 run_expressions
  19. class TestB1Ch10(unittest.TestCase):
  20. def test_diagnostic_test(self):
  21. run_expressions(Parser, [
  22. ('5(a-2b)', L(5) * (L('a') + -(L(2) * 'b'))),
  23. ('-(3a+6b)', -(L(3) * L('a') + L(6) * 'b')),
  24. ('18-(a-12)', L(18) + -(L('a') + -L(12))),
  25. ('-p-q+5(p-q)-3q-2(p-q)',
  26. -L('p') + -L('q') + L(5) * (L('p') + -L('q')) + -(L(3) * 'q') \
  27. + -(L(2) * (L('p') + -L('q')))
  28. ),
  29. ('(2+3/7)^4',
  30. N('^', N('+', L(2), N('/', L(3), L(7))), L(4))
  31. ),
  32. ('x^3*x^2*x',
  33. N('*',
  34. N('*',
  35. N('^', L('x'), L(3)),
  36. N('^', L('x'), L(2))),
  37. L('x')
  38. )
  39. ),
  40. ('-x^3*-2x^5',
  41. -(L('x') ** L(3) * -L(2) * L('x') ** L(5))
  42. ),
  43. ('(7x^2y^3)^2/(7x^2y^3)',
  44. N('/',
  45. N('^',
  46. N('*',
  47. N('*', L(7), N('^', L('x'), L(2))),
  48. N('^', L('y'), L(3))
  49. ),
  50. L(2)),
  51. N('*',
  52. N('*', L(7), N('^', L('x'), L(2))),
  53. N('^', L('y'), L(3))
  54. )
  55. )
  56. ),
  57. ])