test_rules_negation.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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.negation import match_negated_factor, negated_factor, \
  16. match_negate_polynome, negate_polynome, negated_zero, \
  17. double_negation, match_negated_division, negated_nominator, \
  18. negated_denominator
  19. from src.node import Scope
  20. from src.possibilities import Possibility as P
  21. from tests.rulestestcase import RulesTestCase, tree
  22. class TestRulesNegation(RulesTestCase):
  23. def test_match_negated_factor(self):
  24. a, b = root = tree('a * -b')
  25. self.assertEqualPos(match_negated_factor(root),
  26. [P(root, negated_factor, (Scope(root), b))])
  27. (a, b), c = root = tree('a * (-b) * -c')
  28. scope = Scope(root)
  29. self.assertEqualPos(match_negated_factor(root),
  30. [P(root, negated_factor, (scope, b)),
  31. P(root, negated_factor, (scope, c))])
  32. def test_negated_factor(self):
  33. a, b = root = tree('a * -b')
  34. self.assertEqual(negated_factor(root, (Scope(root), b)), -(a * +b))
  35. (a, b), c = root = tree('a * (-b) * -c')
  36. self.assertEqual(negated_factor(root, (Scope(root), b)), -(a * +b * c))
  37. self.assertEqual(negated_factor(root, (Scope(root), c)), -(a * b * +c))
  38. def test_match_negate_polynome(self):
  39. root = tree('--a')
  40. self.assertEqualPos(match_negate_polynome(root),
  41. [P(root, double_negation)])
  42. root = tree('-0')
  43. self.assertEqualPos(match_negate_polynome(root),
  44. [P(root, negated_zero)])
  45. root = tree('--0')
  46. self.assertEqualPos(match_negate_polynome(root),
  47. [P(root, double_negation),
  48. P(root, negated_zero)])
  49. root = tree('-(a + b)')
  50. self.assertEqualPos(match_negate_polynome(root),
  51. [P(root, negate_polynome)])
  52. def test_double_negation(self):
  53. root = tree('--a')
  54. self.assertEqual(double_negation(root, ()), ++root)
  55. def test_negated_zero(self):
  56. root = tree('-0')
  57. self.assertEqual(negated_zero(root, ()), 0)
  58. def test_negate_polynome(self):
  59. a, b = root = tree('-(a + b)')
  60. self.assertEqual(negate_polynome(root, ()), -a + -b)
  61. a, b = root = tree('-(a - b)')
  62. self.assertEqual(negate_polynome(root, ()), -a + -b)
  63. def test_match_negated_division_none(self):
  64. self.assertEqual(match_negated_division(tree('1 / 2')), [])
  65. def test_match_negated_division_single(self):
  66. l1, l2 = root = tree('-1 / 2')
  67. self.assertEqualPos(match_negated_division(root), [])
  68. l1, l2 = root = tree('(-1) / 2')
  69. self.assertEqualPos(match_negated_division(root),
  70. [P(root, negated_nominator)])
  71. l1, l2 = root = tree('1 / -2')
  72. self.assertEqualPos(match_negated_division(root),
  73. [P(root, negated_denominator)])
  74. def test_match_negated_division_double(self):
  75. root = tree('(-1) / -2')
  76. self.assertEqualPos(match_negated_division(root),
  77. [P(root, negated_nominator),
  78. P(root, negated_denominator)])
  79. def test_negated_nominator(self):
  80. l1, l2 = root = tree('(-1) / 2')
  81. self.assertEqual(negated_nominator(root, ()), -(+l1 / l2))
  82. def test_negated_denominator(self):
  83. l1, l2 = root = tree('1 / -2')
  84. self.assertEqual(negated_denominator(root, ()), -(l1 / +l2))
  85. def test_double_negated_division(self):
  86. self.assertRewrite([
  87. '(-a) / -b',
  88. '-a / -b',
  89. '--a / b',
  90. 'a / b',
  91. ])