test_rules_lineq.py 3.7 KB

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