test_rules_lineq.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. from src.rules.lineq import match_move_term, swap_sides, subtract_term, \
  2. divide_term, multiply_term
  3. from src.node import Scope
  4. from src.possibilities import Possibility as P
  5. from tests.rulestestcase import RulesTestCase, tree
  6. class TestRulesLineq(RulesTestCase):
  7. def test_match_move_term_swap(self):
  8. root = tree('x = b')
  9. self.assertEqualPos(match_move_term(root), [])
  10. root = tree('a = bx')
  11. self.assertEqualPos(match_move_term(root), [P(root, swap_sides)])
  12. def test_match_move_term_subtract(self):
  13. root, a = tree('x + a = b, a')
  14. self.assertEqualPos(match_move_term(root),
  15. [P(root, subtract_term, (a,))])
  16. root, cx = tree('x = b + cx, cx')
  17. self.assertEqualPos(match_move_term(root),
  18. [P(root, subtract_term, (cx,))])
  19. def test_match_move_term_divide(self):
  20. root, a = tree('ax = b, a')
  21. self.assertEqualPos(match_move_term(root),
  22. [P(root, divide_term, (a,))])
  23. def test_match_move_term_multiply(self):
  24. root, a = tree('x / a = b, a')
  25. self.assertEqualPos(match_move_term(root),
  26. [P(root, multiply_term, (a,))])
  27. root, x = tree('a / x = b, x')
  28. self.assertEqualPos(match_move_term(root),
  29. [P(root, multiply_term, (x,))])
  30. root, l1 = tree('-x = b, -1')
  31. self.assertEqualPos(match_move_term(root),
  32. [P(root, multiply_term, (l1,))])
  33. def test_swap_sides(self):
  34. root, expect = tree('a = bx, bx = a')
  35. self.assertEqual(swap_sides(root, ()), expect)
  36. def test_subtract_term(self):
  37. root, a, expect = tree('x + a = b, a, x + a - a = b - a')
  38. self.assertEqual(subtract_term(root, (a,)), expect)
  39. def test_divide_term(self):
  40. root, a, expect = tree('x * a = b, a, x * a / a = b / a')
  41. self.assertEqual(divide_term(root, (a,)), expect)
  42. def test_multiply_term(self):
  43. root, a, expect = tree('x / a = b, a, x / a * a = b * a')
  44. self.assertEqual(multiply_term(root, (a,)), expect)
  45. def test_match_move_term_chain_negation(self):
  46. self.assertRewrite([
  47. '2x + 3 = -3x - 2',
  48. '2x + 3 - 3 = -3x - 2 - 3',
  49. '2x + 0 = -3x - 2 - 3',
  50. '2x = -3x - 2 - 3',
  51. '2x = -3x - 5',
  52. '2x - -3x = -3x - 5 - -3x',
  53. '2x + 3x = -3x - 5 - -3x',
  54. '(2 + 3)x = -3x - 5 - -3x',
  55. '5x = -3x - 5 - -3x',
  56. '5x = -3x - 5 + 3x',
  57. '5x = (-1 + 1)3x - 5',
  58. '5x = 0 * 3x - 5',
  59. '5x = 0x - 5',
  60. '5x = 0 - 5',
  61. '5x = -5',
  62. '5x / 5 = (-5) / 5',
  63. 'x / 1 = (-5) / 5',
  64. 'x = (-5) / 5',
  65. 'x = -5 / 5',
  66. 'x = -1',
  67. ])
  68. def test_match_move_term_chain_advanced(self):
  69. self.assertRewrite([
  70. '-x = a',
  71. '(-x)(-1) = a(-1)',
  72. '-x(-1) = a(-1)',
  73. '--x * 1 = a(-1)',
  74. '--x = a(-1)',
  75. 'x = a(-1)',
  76. 'x = -a * 1',
  77. 'x = -a',
  78. ])