test_rules_factors.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. from src.rules.factors import match_expand, expand_double, expand_single
  2. from src.node import Scope
  3. from src.possibilities import Possibility as P
  4. from tests.rulestestcase import RulesTestCase, tree
  5. class TestRulesFactors(RulesTestCase):
  6. def test_match_expand(self):
  7. a, bc, d = tree('a,b + c,d')
  8. b, c = bc
  9. root = a * bc
  10. self.assertEqualPos(match_expand(root),
  11. [P(root, expand_single, (Scope(root), a, bc))])
  12. root = bc * a
  13. self.assertEqualPos(match_expand(root),
  14. [P(root, expand_single, (Scope(root), bc, a))])
  15. root = a * bc * d
  16. self.assertEqualPos(match_expand(root),
  17. [P(root, expand_single, (Scope(root), a, bc)),
  18. P(root, expand_single, (Scope(root), bc, d))])
  19. ab, cd = root = (a + b) * (c + d)
  20. self.assertEqualPos(match_expand(root),
  21. [P(root, expand_double, (Scope(root), ab, cd))])
  22. (ab, cd), e = root = tree('(a + b)(c + d)e')
  23. self.assertEqualPos(match_expand(root),
  24. [P(root, expand_double, (Scope(root), ab, cd)),
  25. P(root, expand_single, (Scope(root), cd, e)),
  26. P(root, expand_single, (Scope(root), ab, e))])
  27. def test_expand_single(self):
  28. root, expect = tree('a(b + c), ab + ac')
  29. a, bc = root
  30. self.assertEqualNodes(expand_single(root, (Scope(root), a, bc)),
  31. expect)
  32. root, expect = tree('a(b+c)d, a(bd + cd)')
  33. (a, bc), d = root
  34. self.assertEqualNodes(expand_single(root, (Scope(root), bc, d)),
  35. expect)
  36. def test_expand_double(self):
  37. (a, b), (c, d) = ab, cd = tree('a + b,c + d')
  38. root, expect = tree('(a + b)(c + d), ac + ad + bc + bd')
  39. ab, cd = root
  40. self.assertEqualNodes(expand_double(root, (Scope(root), ab, cd)),
  41. expect)
  42. root, expect = tree('a(a + b)b(c + d)c, a(ac + ad + bc + bd)bc')
  43. (((a, ab), b), cd), c = root
  44. self.assertEqualNodes(expand_double(root, (Scope(root), ab, cd)),
  45. expect)