test_rules_lineq.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. from src.rules.lineq import match_move_term, swap_sides, subtract_term, \
  2. divide_term, multiply_term, split_absolute_equation
  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_match_move_term_absolute(self):
  34. root = tree('|x| = 2')
  35. self.assertEqualPos(match_move_term(root),
  36. [P(root, split_absolute_equation)])
  37. root = tree('|x - 1| = 2')
  38. self.assertEqualPos(match_move_term(root),
  39. [P(root, split_absolute_equation)])
  40. def test_swap_sides(self):
  41. root, expect = tree('a = bx, bx = a')
  42. self.assertEqual(swap_sides(root, ()), expect)
  43. def test_subtract_term(self):
  44. root, a, expect = tree('x + a = b, a, x + a - a = b - a')
  45. self.assertEqual(subtract_term(root, (a,)), expect)
  46. def test_divide_term(self):
  47. root, a, expect = tree('x * a = b, a, x * a / a = b / a')
  48. self.assertEqual(divide_term(root, (a,)), expect)
  49. def test_multiply_term(self):
  50. root, a, expect = tree('x / a = b, a, x / a * a = b * a')
  51. self.assertEqual(multiply_term(root, (a,)), expect)
  52. def test_split_absolute_equation(self):
  53. root, expect = tree('|x| = 2, x = 2 vv x = -2')
  54. self.assertEqual(split_absolute_equation(root, ()), expect)
  55. # FIXME: following call exeeds recursion limit
  56. # FIXME: self.assertValidate('|x - 1| = 2', 'x = -1 vv x = 3')
  57. def test_match_move_term_chain_negation(self):
  58. self.assertRewrite([
  59. '2x + 3 = -3x - 2',
  60. '2x + 3 - 3 = -3x - 2 - 3',
  61. '2x + 0 = -3x - 2 - 3',
  62. '2x = -3x - 2 - 3',
  63. '2x = -3x - 5',
  64. '2x - -3x = -3x - 5 - -3x',
  65. '2x + 3x = -3x - 5 - -3x',
  66. '(2 + 3)x = -3x - 5 - -3x',
  67. '5x = -3x - 5 - -3x',
  68. '5x = -3x - 5 + 3x',
  69. '5x = (-1 + 1)3x - 5',
  70. '5x = 0 * 3x - 5',
  71. '5x = 0 - 5',
  72. '5x = -5',
  73. '5x / 5 = (-5) / 5',
  74. '5 / 5 * x = (-5) / 5',
  75. '1x = (-5) / 5',
  76. 'x = (-5) / 5',
  77. 'x = -5 / 5',
  78. 'x = -1',
  79. ])
  80. def test_match_move_term_chain_advanced(self):
  81. self.assertRewrite([
  82. '-x = a',
  83. '(-x)(-1) = a(-1)',
  84. '-x(-1) = a(-1)',
  85. '--x * 1 = a(-1)',
  86. '--x = a(-1)',
  87. 'x = a(-1)',
  88. 'x = -a * 1',
  89. 'x = -a',
  90. ])