test_b1_ch10.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 run_expressions
  5. class TestB1Ch10(unittest.TestCase):
  6. def test_diagnostic_test(self):
  7. run_expressions(Parser, [
  8. ('5(a-2b)', N('*', L(5), N('-', L('a'),
  9. N('*', L(2), L('b'))))),
  10. ('-(3a+6b)', N('-', N('+', N('*', L(3), L('a')),
  11. N('*', L(6), L('b'))))),
  12. ('18-(a-12)', N('-', L(18),
  13. N('-', L('a'), L(12)))),
  14. ('-p-q+5(p-q)-3q-2(p-q)',
  15. N('-',
  16. N('-',
  17. N('+', N('-', N('-', L('p')), L('q')),
  18. N('*', L(5), N('-', L('p'), L('q')))),
  19. N('*', L(3), L('q'))
  20. ),
  21. N('*', L(2), N('-', L('p'), L('q')))
  22. )
  23. ),
  24. ('(2+3/7)^4',
  25. N('^', N('+', L(2), N('/', L(3), L(7))), L(4))
  26. ),
  27. ('x3*x2*x',
  28. N('*',
  29. N('*',
  30. N('^', L('x'), L(3)),
  31. N('^', L('x'), L(2))),
  32. L('x')
  33. )
  34. ),
  35. ('-x3*-2x5',
  36. N('*',
  37. N('*',
  38. N('-', N('^', L('x'), L(3))),
  39. N('-', L(2))),
  40. N('^', L('x'), L(5))
  41. )
  42. ),
  43. ('(7x2y3)^2/(7x2y3)',
  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. ])