test_rules_groups.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. from src.rules.groups import match_combine_groups, combine_groups
  2. from src.possibilities import Possibility as P
  3. from tests.rulestestcase import RulesTestCase, tree
  4. class TestRulesGroups(RulesTestCase):
  5. def test_match_combine_groups_no_const(self):
  6. a0, a1 = root = tree('a + a')
  7. possibilities = match_combine_groups(root)
  8. self.assertEqualPos(possibilities,
  9. [P(root, combine_groups, (1, a0, a0, 1, a1, a1))])
  10. def test_match_combine_groups_single_const(self):
  11. a0, mul = root = tree('a + 2a')
  12. l2, a1 = mul
  13. possibilities = match_combine_groups(root)
  14. self.assertEqualPos(possibilities,
  15. [P(root, combine_groups, (1, a0, a0, l2, a1, mul))])
  16. def test_match_combine_groups_two_const(self):
  17. ((l2, a0), b), (l3, a1) = (m0, b), m1 = root = tree('2a + b + 3a')
  18. possibilities = match_combine_groups(root)
  19. self.assertEqualPos(possibilities,
  20. [P(root, combine_groups, (l2, a0, m0, l3, a1, m1))])
  21. def test_match_combine_groups_n_const(self):
  22. ((l2, a0), (l3, a1)), (l4, a2) = (m0, m1), m2 = root = tree('2a+3a+4a')
  23. possibilities = match_combine_groups(root)
  24. self.assertEqualPos(possibilities,
  25. [P(root, combine_groups, (l2, a0, m0, l3, a1, m1)),
  26. P(root, combine_groups, (l2, a0, m0, l4, a2, m2)),
  27. P(root, combine_groups, (l3, a1, m1, l4, a2, m2))])
  28. def test_match_combine_groups_identifier_group_no_const(self):
  29. ab0, ab1 = root = tree('ab + ab')
  30. possibilities = match_combine_groups(root)
  31. self.assertEqualPos(possibilities,
  32. [P(root, combine_groups, (1, ab0, ab0, 1, ab1, ab1))])
  33. def test_match_combine_groups_identifier_group_single_const(self):
  34. m0, m1 = root = tree('ab + 2ab')
  35. (l2, a), b = m1
  36. possibilities = match_combine_groups(root)
  37. self.assertEqualPos(possibilities,
  38. [P(root, combine_groups, (1, m0, m0, l2, a * b, m1))])
  39. def test_match_combine_groups_identifier_group_unordered(self):
  40. m0, m1 = root = tree('ab + ba')
  41. b, a = m1
  42. possibilities = match_combine_groups(root)
  43. self.assertEqualPos(possibilities,
  44. [P(root, combine_groups, (1, m0, m0, 1, b * a, m1))])
  45. def test_combine_groups_simple(self):
  46. root, l1 = tree('a + a,1')
  47. a0, a1 = root
  48. self.assertEqualNodes(combine_groups(root, (1, a0, a0, 1, a1, a1)),
  49. (l1 + 1) * a0)
  50. def test_combine_groups_nary(self):
  51. root, l1 = tree('ab + b + ba,1')
  52. abb, ba = root
  53. ab, b = abb
  54. self.assertEqualNodes(combine_groups(root, (1, ab, ab, 1, ba, ba)),
  55. (l1 + 1) * ab + b)