test_rules_sort.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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.sort import match_sort_polynome, swap_factors, get_poly_prop, \
  16. match_sort_monomial, iter_pairs, swap_mono, swap_poly, get_power_prop
  17. from src.node import Scope
  18. from src.possibilities import Possibility as P
  19. from tests.rulestestcase import RulesTestCase, tree
  20. class TestRulesSort(RulesTestCase):
  21. def test_swap_mono(self):
  22. self.assertTrue(swap_mono(tree('x, 1 / 2')))
  23. self.assertFalse(swap_mono(tree('2, 1 / 2')))
  24. self.assertTrue(swap_mono(tree('x, 2')))
  25. self.assertFalse(swap_mono(tree('2, x')))
  26. self.assertTrue(swap_mono(tree('x ^ 2, 2')))
  27. self.assertTrue(swap_mono(tree('y, x')))
  28. self.assertFalse(swap_mono(tree('x, y')))
  29. self.assertFalse(swap_mono(tree('x, x')))
  30. self.assertTrue(swap_mono(tree('x ^ 3, x ^ 2')))
  31. self.assertFalse(swap_mono(tree('x ^ 2, x ^ 3')))
  32. def test_swap_poly(self):
  33. self.assertTrue(swap_poly(tree('2, x')))
  34. self.assertFalse(swap_poly(tree('x, 2')))
  35. self.assertTrue(swap_poly(tree('a, a^2')))
  36. self.assertFalse(swap_poly(tree('a^2, a')))
  37. self.assertTrue(swap_poly(tree('y, x')))
  38. self.assertFalse(swap_poly(tree('x, y')))
  39. self.assertFalse(swap_poly(tree('x, x')))
  40. self.assertFalse(swap_poly(tree('x ^ 3, x ^ 2')))
  41. self.assertTrue(swap_poly(tree('x ^ 2, x ^ 3')))
  42. def test_get_power_prop(self):
  43. self.assertEqual(get_power_prop(tree('a')), ('a', 1))
  44. self.assertEqual(get_power_prop(tree('a ^ b')), ('a', 1))
  45. self.assertEqual(get_power_prop(tree('a ^ 2')), ('a', 2))
  46. self.assertEqual(get_power_prop(tree('a ^ -2')), ('a', -2))
  47. self.assertIsNone(get_power_prop(tree('1')))
  48. def test_get_poly_prop(self):
  49. self.assertEqual(get_poly_prop(tree('a ^ 2')), ('a', 2))
  50. self.assertEqual(get_poly_prop(tree('2a ^ 2')), ('a', 2))
  51. self.assertEqual(get_poly_prop(tree('ca ^ 2 * 2')), ('a', 2))
  52. self.assertEqual(get_poly_prop(tree('ab ^ 2')), ('a', 1))
  53. self.assertEqual(get_poly_prop(tree('a^3 * a^2')), ('a', 3))
  54. self.assertEqual(get_poly_prop(tree('a^2 * a^3')), ('a', 3))
  55. self.assertIsNone(get_poly_prop(tree('1')))
  56. def test_match_sort_monomial_constant(self):
  57. x, l2 = root = tree('x * 2')
  58. self.assertEqualPos(match_sort_monomial(root),
  59. [P(root, swap_factors, (Scope(root), x, l2))])
  60. root = tree('2x')
  61. self.assertEqualPos(match_sort_monomial(root), [])
  62. def test_match_sort_monomial_variables(self):
  63. y, x = root = tree('yx')
  64. self.assertEqualPos(match_sort_monomial(root),
  65. [P(root, swap_factors, (Scope(root), y, x))])
  66. root = tree('xy')
  67. self.assertEqualPos(match_sort_monomial(root), [])
  68. def test_match_sort_polynome(self):
  69. x, x2 = root = tree('x + x ^ 2')
  70. self.assertEqualPos(match_sort_polynome(root),
  71. [P(root, swap_factors, (Scope(root), x, x2))])
  72. root = tree('x + 2')
  73. self.assertEqualPos(match_sort_polynome(root), [])
  74. def test_swap_factors(self):
  75. x, l2 = root = tree('x * 2')
  76. self.assertEqualNodes(swap_factors(root, (Scope(root), x, l2)),
  77. l2 * x)