test_rules_groups.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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.groups import match_combine_groups, combine_groups
  16. from src.node import Scope
  17. from src.possibilities import Possibility as P
  18. from tests.rulestestcase import RulesTestCase, tree
  19. class TestRulesGroups(RulesTestCase):
  20. def test_match_combine_groups_no_const(self):
  21. root, l1 = tree('a + a,1')
  22. a0, a1 = root
  23. possibilities = match_combine_groups(root)
  24. self.assertEqualPos(possibilities,
  25. [P(root, combine_groups, (Scope(root), l1, a0, a0,
  26. l1, a1, a1))])
  27. def test_match_combine_groups_negation(self):
  28. root, l1 = tree('-a + a,1')
  29. a0, a1 = root
  30. possibilities = match_combine_groups(root)
  31. self.assertEqualPos(possibilities,
  32. [P(root, combine_groups, (Scope(root), -l1, +a0, a0,
  33. l1, a1, a1))])
  34. def test_match_combine_groups_single_const(self):
  35. root, l1 = tree('a + 2a,1')
  36. a0, mul = root
  37. l2, a1 = mul
  38. possibilities = match_combine_groups(root)
  39. self.assertEqualPos(possibilities,
  40. [P(root, combine_groups, (Scope(root), l1, a0, a0,
  41. l2, a1, mul))])
  42. def test_match_combine_groups_two_const(self):
  43. ((l2, a0), b), (l3, a1) = (m0, b), m1 = root = tree('2a + b + 3a')
  44. possibilities = match_combine_groups(root)
  45. self.assertEqualPos(possibilities,
  46. [P(root, combine_groups, (Scope(root), l2, a0, m0,
  47. l3, a1, m1))])
  48. def test_match_combine_groups_n_const(self):
  49. ((l2, a0), (l3, a1)), (l4, a2) = (m0, m1), m2 = root = tree('2a+3a+4a')
  50. possibilities = match_combine_groups(root)
  51. self.assertEqualPos(possibilities,
  52. [P(root, combine_groups, (Scope(root), l2, a0, m0,
  53. l3, a1, m1)),
  54. P(root, combine_groups, (Scope(root), l2, a0, m0,
  55. l4, a2, m2)),
  56. P(root, combine_groups, (Scope(root), l3, a1, m1,
  57. l4, a2, m2))])
  58. def test_match_combine_groups_identifier_group_no_const(self):
  59. root, l1 = tree('ab + ab,1')
  60. ab0, ab1 = root
  61. possibilities = match_combine_groups(root)
  62. self.assertEqualPos(possibilities,
  63. [P(root, combine_groups, (Scope(root), l1, ab0, ab0,
  64. l1, ab1, ab1))])
  65. def test_match_combine_groups_identifier_group_single_const(self):
  66. root, l1 = tree('ab + 2ab,1')
  67. m0, m1 = root
  68. (l2, a), b = m1
  69. possibilities = match_combine_groups(root)
  70. self.assertEqualPos(possibilities,
  71. [P(root, combine_groups, (Scope(root), l1, m0, m0,
  72. l2, a * b, m1))])
  73. def test_match_combine_groups_identifier_group_unordered(self):
  74. root, l1 = tree('ab + ba,1')
  75. m0, m1 = root
  76. b, a = m1
  77. possibilities = match_combine_groups(root)
  78. self.assertEqualPos(possibilities,
  79. [P(root, combine_groups, (Scope(root), l1, m0, m0,
  80. l1, b * a, m1))])
  81. def test_combine_groups_simple(self):
  82. root, l1 = tree('a + a,1')
  83. a0, a1 = root
  84. self.assertEqualNodes(combine_groups(root,
  85. (Scope(root), l1, a0, a0, l1, a1, a1)),
  86. (l1 + 1) * a0)
  87. def test_combine_groups_nary(self):
  88. root, l1 = tree('ab + b + ba,1')
  89. abb, ba = root
  90. ab, b = abb
  91. self.assertEqualNodes(combine_groups(root,
  92. (Scope(root), l1, ab, ab, l1, ba, ba)),
  93. (l1 + 1) * ab + b)
  94. def test_combine_groups_negation(self):
  95. root, expect = tree('3a - 2a, -(3 + 2)a')
  96. (l3, a0), (l2, a1) = n0, n1 = root
  97. self.assertEqualNodes(combine_groups(root,
  98. (Scope(root), l3, a0, n0, l2, a1, n1)),
  99. expect)