test_rules_groups.py 4.2 KB

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