test_rules_lineq.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. '5 / 5 * x = (-5) / 5',
  64. '1x = (-5) / 5',
  65. 'x = (-5) / 5',
  66. 'x = -5 / 5',
  67. 'x = -1',
  68. ])
  69. def test_match_move_term_chain_advanced(self):
  70. self.assertRewrite([
  71. '-x = a',
  72. '(-x)(-1) = a(-1)',
  73. '-x(-1) = a(-1)',
  74. '--x * 1 = a(-1)',
  75. '--x = a(-1)',
  76. 'x = a(-1)',
  77. 'x = -a * 1',
  78. 'x = -a',
  79. ])