test_rules_lineq.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. from src.rules.lineq import match_move_term, swap_sides, subtract_term, \
  2. divide_term, multiply_term, split_absolute_equation, \
  3. match_multiple_equations, substitute_variable, match_double_case, \
  4. double_case
  5. from src.node import Scope
  6. from src.possibilities import Possibility as P
  7. from tests.rulestestcase import RulesTestCase, tree
  8. class TestRulesLineq(RulesTestCase):
  9. def test_match_move_term_swap(self):
  10. root = tree('x = b')
  11. self.assertEqualPos(match_move_term(root), [])
  12. root = tree('a = bx')
  13. self.assertEqualPos(match_move_term(root), [P(root, swap_sides)])
  14. def test_match_move_term_subtract(self):
  15. root, a = tree('x + a = b, a')
  16. self.assertEqualPos(match_move_term(root),
  17. [P(root, subtract_term, (a,))])
  18. root, cx = tree('x = b + cx, cx')
  19. self.assertEqualPos(match_move_term(root),
  20. [P(root, subtract_term, (cx,))])
  21. def test_match_move_term_divide(self):
  22. root, a = tree('ax = b, a')
  23. self.assertEqualPos(match_move_term(root),
  24. [P(root, divide_term, (a,))])
  25. def test_match_move_term_multiply(self):
  26. root, a = tree('x / a = b, a')
  27. self.assertEqualPos(match_move_term(root),
  28. [P(root, multiply_term, (a,))])
  29. root, x = tree('a / x = b, x')
  30. self.assertEqualPos(match_move_term(root),
  31. [P(root, multiply_term, (x,))])
  32. root, l1 = tree('-x = b, -1')
  33. self.assertEqualPos(match_move_term(root),
  34. [P(root, multiply_term, (l1,))])
  35. def test_match_move_term_absolute(self):
  36. root = tree('|x| = 2')
  37. self.assertEqualPos(match_move_term(root),
  38. [P(root, split_absolute_equation)])
  39. root = tree('|x - 1| = 2')
  40. self.assertEqualPos(match_move_term(root),
  41. [P(root, split_absolute_equation)])
  42. def test_swap_sides(self):
  43. root, expect = tree('a = bx, bx = a')
  44. self.assertEqual(swap_sides(root, ()), expect)
  45. def test_subtract_term(self):
  46. root, a, expect = tree('x + a = b, a, x + a - a = b - a')
  47. self.assertEqual(subtract_term(root, (a,)), expect)
  48. def test_divide_term(self):
  49. root, a, expect = tree('x * a = b, a, (xa) / a = b / a')
  50. self.assertEqual(divide_term(root, (a,)), expect)
  51. def test_multiply_term(self):
  52. root, a, expect = tree('x / a = b, a, x / a * a = b * a')
  53. self.assertEqual(multiply_term(root, (a,)), expect)
  54. def test_split_absolute_equation(self):
  55. root, expect = tree('|x| = 2, x = 2 vv x = -2')
  56. self.assertEqual(split_absolute_equation(root, ()), expect)
  57. # FIXME: following call exeeds recursion limit
  58. # FIXME: self.assertValidate('|x - 1| = 2', 'x = -1 vv x = 3')
  59. def test_match_move_term_chain_negation(self):
  60. self.assertRewrite([
  61. '2x + 3 = -3x - 2',
  62. '2x + 3 - 3 = -3x - 2 - 3',
  63. '2x + 0 = -3x - 2 - 3',
  64. '2x = -3x - 2 - 3',
  65. '2x = -3x - 5',
  66. '2x - -3x = -3x - 5 - -3x',
  67. '2x + 3x = -3x - 5 - -3x',
  68. '(2 + 3)x = -3x - 5 - -3x',
  69. '5x = -3x - 5 - -3x',
  70. '5x = -3x - 5 + 3x',
  71. '5x = (-1 + 1)3x - 5',
  72. '5x = 0 * 3x - 5',
  73. '5x = 0 - 5',
  74. '5x = -5',
  75. '(5x) / 5 = (-5) / 5',
  76. '5 / 5 * x = (-5) / 5',
  77. '1x = (-5) / 5',
  78. 'x = (-5) / 5',
  79. 'x = -5 / 5',
  80. 'x = -1',
  81. ])
  82. def test_match_move_term_chain_advanced(self):
  83. self.assertRewrite([
  84. '-x = a',
  85. '(-x)(-1) = a(-1)',
  86. '-x(-1) = a(-1)',
  87. '--x * 1 = a(-1)',
  88. '--x = a(-1)',
  89. 'x = a(-1)',
  90. 'x = -a * 1',
  91. 'x = -a',
  92. ])
  93. def test_match_multiple_equations(self):
  94. eq0, eq1 = root = tree('x = 2 ^^ ay + x = 3')
  95. x = eq0[0]
  96. self.assertEqualPos(match_multiple_equations(root),
  97. [P(root, substitute_variable, (Scope(root), x, 2, eq1))])
  98. root = tree('x + y = 2 ^^ ay + x = 3')
  99. self.assertEqualPos(match_multiple_equations(root), [])
  100. root = tree('x + y ^^ ay + x = 3')
  101. self.assertEqualPos(match_multiple_equations(root), [])
  102. def test_substitute_variable(self):
  103. root, expect = tree('x = 2 ^^ ay + x = 3, x = 2 ^^ ay + 2 = 3')
  104. (x, l2), eq = root
  105. self.assertEqual(substitute_variable(root, ((Scope(root), x, l2, eq))),
  106. expect)
  107. def test_match_double_case(self):
  108. a, b = root = tree('x = 2 vv x = 2')
  109. self.assertEqualPos(match_double_case(root),
  110. [P(root, double_case, (Scope(root), a, b))])
  111. root = tree('x = 2 vv x = -2')
  112. self.assertEqualPos(match_double_case(root), [])
  113. def test_double_case(self):
  114. a, b = root = tree('x = 2 vv x = 2, x = 2')
  115. self.assertEqual(double_case(root, (Scope(root), a, b)), a)