test_rules_factors.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. # This file is part of TRS (http://math.kompiler.org)
  2. #
  3. # TRS is free software: you can redistribute it and/or modify it under the
  4. # terms of the GNU Affero General Public License as published by the Free
  5. # Software Foundation, either version 3 of the License, or (at your option) any
  6. # later version.
  7. #
  8. # TRS is distributed in the hope that it will be useful, but WITHOUT ANY
  9. # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  10. # A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  11. # details.
  12. #
  13. # You should have received a copy of the GNU Affero General Public License
  14. # along with TRS. If not, see <http://www.gnu.org/licenses/>.
  15. from src.rules.factors import match_expand, expand_double, expand_single
  16. from src.node import Scope
  17. from src.possibilities import Possibility as P
  18. from tests.rulestestcase import RulesTestCase, tree
  19. class TestRulesFactors(RulesTestCase):
  20. def test_match_expand(self):
  21. a, bc, d = tree('a,b + c,d')
  22. b, c = bc
  23. root = a * bc
  24. self.assertEqualPos(match_expand(root),
  25. [P(root, expand_single, (Scope(root), a, bc))])
  26. root = bc * a
  27. self.assertEqualPos(match_expand(root),
  28. [P(root, expand_single, (Scope(root), bc, a))])
  29. root = a * bc * d
  30. self.assertEqualPos(match_expand(root),
  31. [P(root, expand_single, (Scope(root), a, bc)),
  32. P(root, expand_single, (Scope(root), bc, d))])
  33. ab, cd = root = (a + b) * (c + d)
  34. self.assertEqualPos(match_expand(root),
  35. [P(root, expand_double, (Scope(root), ab, cd))])
  36. (ab, cd), e = root = tree('(a + b)(c + d)e')
  37. self.assertEqualPos(match_expand(root),
  38. [P(root, expand_double, (Scope(root), ab, cd)),
  39. P(root, expand_single, (Scope(root), cd, e)),
  40. P(root, expand_single, (Scope(root), ab, e))])
  41. def test_expand_single(self):
  42. root, expect = tree('a(b + c), ab + ac')
  43. a, bc = root
  44. self.assertEqualNodes(expand_single(root, (Scope(root), a, bc)),
  45. expect)
  46. root, expect = tree('a(b+c)d, a(bd + cd)')
  47. (a, bc), d = root
  48. self.assertEqualNodes(expand_single(root, (Scope(root), bc, d)),
  49. expect)
  50. def test_expand_double(self):
  51. (a, b), (c, d) = ab, cd = tree('a + b,c + d')
  52. root, expect = tree('(a + b)(c + d), ac + ad + bc + bd')
  53. ab, cd = root
  54. self.assertEqualNodes(expand_double(root, (Scope(root), ab, cd)),
  55. expect)
  56. root, expect = tree('a(a + b)b(c + d)c, a(ac + ad + bc + bd)bc')
  57. (((a, ab), b), cd), c = root
  58. self.assertEqualNodes(expand_double(root, (Scope(root), ab, cd)),
  59. expect)