test_rules_absolute.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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.absolute import match_factor_out_abs_term, \
  16. remove_absolute_negation, factor_out_abs_sqrt, absolute_numeric, \
  17. factor_out_abs_term, factor_out_abs_exponent
  18. from src.node import Scope
  19. from src.possibilities import Possibility as P
  20. from tests.rulestestcase import RulesTestCase, tree
  21. class TestRulesAbsolute(RulesTestCase):
  22. def test_match_factor_out_abs_term_negation(self):
  23. root = tree('|-a|')
  24. self.assertEqualPos(match_factor_out_abs_term(root),
  25. [P(root, remove_absolute_negation)])
  26. def test_match_factor_out_abs_term_numeric(self):
  27. root = tree('|2|')
  28. self.assertEqualPos(match_factor_out_abs_term(root),
  29. [P(root, absolute_numeric)])
  30. root = tree('|a|')
  31. self.assertEqualPos(match_factor_out_abs_term(root), [])
  32. def test_match_factor_out_abs_term_mult(self):
  33. ((a, b),) = (ab,) = root = tree('|ab|')
  34. self.assertEqualPos(match_factor_out_abs_term(root),
  35. [P(root, factor_out_abs_term, (Scope(ab), a)),
  36. P(root, factor_out_abs_term, (Scope(ab), b))])
  37. (((a, b), c),) = (abc,) = root = tree('|abc|')
  38. self.assertEqualPos(match_factor_out_abs_term(root),
  39. [P(root, factor_out_abs_term, (Scope(abc), a)),
  40. P(root, factor_out_abs_term, (Scope(abc), b)),
  41. P(root, factor_out_abs_term, (Scope(abc), c))])
  42. def test_match_factor_out_abs_term_sqrt(self):
  43. root = tree('|sqrt a|')
  44. self.assertEqualPos(match_factor_out_abs_term(root),
  45. [P(root, factor_out_abs_sqrt)])
  46. def test_match_factor_out_abs_term_exponent(self):
  47. root = tree('|a ^ 2|')
  48. self.assertEqualPos(match_factor_out_abs_term(root),
  49. [P(root, factor_out_abs_exponent)])
  50. root = tree('|a ^ b|')
  51. self.assertEqualPos(match_factor_out_abs_term(root), [])
  52. def test_remove_absolute_negation(self):
  53. root, expect = tree('|-a|, |a|')
  54. self.assertEqual(remove_absolute_negation(root, ()), expect)
  55. root, expect = tree('-|-a|, -|a|')
  56. self.assertEqual(remove_absolute_negation(root, ()), expect)
  57. def test_absolute_numeric(self):
  58. root, expect = tree('|2|, 2')
  59. self.assertEqual(absolute_numeric(root, ()), expect)
  60. root, expect = tree('-|2|, -2')
  61. self.assertEqual(absolute_numeric(root, ()), expect)
  62. def test_factor_out_abs_term(self):
  63. root, expect = tree('|abc|, |a||bc|')
  64. (((a, b), c),) = (abc,) = root
  65. self.assertEqual(factor_out_abs_term(root, (Scope(abc), a)), expect)
  66. root, expect = tree('|abc|, |b||ac|')
  67. (((a, b), c),) = (abc,) = root
  68. self.assertEqual(factor_out_abs_term(root, (Scope(abc), b)), expect)
  69. root, expect = tree('-|abc|, -|a||bc|')
  70. (((a, b), c),) = (abc,) = root
  71. self.assertEqual(factor_out_abs_term(root, (Scope(abc), a)), expect)
  72. def test_factor_out_abs_sqrt(self):
  73. root, expect = tree('|sqrt a|, sqrt|a|')
  74. self.assertEqual(factor_out_abs_sqrt(root, ()), expect)
  75. root, expect = tree('-|sqrt a|, -sqrt|a|')
  76. self.assertEqual(factor_out_abs_sqrt(root, ()), expect)
  77. def test_factor_out_abs_exponent(self):
  78. root, expect = tree('|a ^ 2|, |a| ^ 2')
  79. self.assertEqual(factor_out_abs_exponent(root, ()), expect)
  80. root, expect = tree('-|a ^ 2|, -|a| ^ 2')
  81. self.assertEqual(factor_out_abs_exponent(root, ()), expect)