test_rules_factors.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. a, b, c, d = tree('a,b,c,d')
  29. bc = b + c
  30. root = a * bc
  31. self.assertEqualNodes(expand_single(root, (Scope(root), a, bc)),
  32. a * b + a * c)
  33. root = a * bc * d
  34. self.assertEqualNodes(expand_single(root, (Scope(root), bc, d)),
  35. a * (b * d + c * d))
  36. def test_expand_double(self):
  37. (a, b), (c, d) = ab, cd = tree('a + b,c + d')
  38. root = ab * cd
  39. self.assertEqualNodes(expand_double(root, (Scope(root), ab, cd)),
  40. a * c + a * d + b * c + b * d)
  41. root = a * ab * b * cd * c
  42. self.assertEqualNodes(expand_double(root, (Scope(root), ab, cd)),
  43. a * (a * c + a * d + b * c + b * d) * b * c)