test_rules_absolute.py 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. from src.rules.absolute import match_factor_out_abs_term, \
  2. remove_absolute_negation, factor_out_abs_sqrt, absolute_numeric, \
  3. factor_out_abs_term, factor_out_abs_exponent
  4. from src.node import ExpressionLeaf as L, Scope
  5. from src.possibilities import Possibility as P
  6. from tests.rulestestcase import RulesTestCase, tree
  7. class TestRulesAbsolute(RulesTestCase):
  8. def test_match_factor_out_abs_term_negation(self):
  9. root = tree('|-a|')
  10. self.assertEqualPos(match_factor_out_abs_term(root),
  11. [P(root, remove_absolute_negation)])
  12. def test_match_factor_out_abs_term_numeric(self):
  13. root = tree('|2|')
  14. self.assertEqualPos(match_factor_out_abs_term(root),
  15. [P(root, absolute_numeric)])
  16. root = tree('|a|')
  17. self.assertEqualPos(match_factor_out_abs_term(root), [])
  18. def test_match_factor_out_abs_term_mult(self):
  19. ((a, b),) = (ab,) = root = tree('|ab|')
  20. self.assertEqualPos(match_factor_out_abs_term(root),
  21. [P(root, factor_out_abs_term, (Scope(ab), a)),
  22. P(root, factor_out_abs_term, (Scope(ab), b))])
  23. (((a, b), c),) = (abc,) = root = tree('|abc|')
  24. self.assertEqualPos(match_factor_out_abs_term(root),
  25. [P(root, factor_out_abs_term, (Scope(abc), a)),
  26. P(root, factor_out_abs_term, (Scope(abc), b)),
  27. P(root, factor_out_abs_term, (Scope(abc), c))])
  28. def test_match_factor_out_abs_term_sqrt(self):
  29. root = tree('|sqrt a|')
  30. self.assertEqualPos(match_factor_out_abs_term(root),
  31. [P(root, factor_out_abs_sqrt)])
  32. def test_match_factor_out_abs_term_exponent(self):
  33. root = tree('|a ^ 2|')
  34. self.assertEqualPos(match_factor_out_abs_term(root),
  35. [P(root, factor_out_abs_exponent)])
  36. root = tree('|a ^ b|')
  37. self.assertEqualPos(match_factor_out_abs_term(root), [])
  38. def test_remove_absolute_negation(self):
  39. root, expect = tree('|-a|, |a|')
  40. self.assertEqual(remove_absolute_negation(root, ()), expect)
  41. root, expect = tree('-|-a|, -|a|')
  42. self.assertEqual(remove_absolute_negation(root, ()), expect)
  43. def test_absolute_numeric(self):
  44. root, expect = tree('|2|, 2')
  45. self.assertEqual(absolute_numeric(root, ()), expect)
  46. root, expect = tree('-|2|, -2')
  47. self.assertEqual(absolute_numeric(root, ()), expect)
  48. def test_factor_out_abs_term(self):
  49. root, expect = tree('|abc|, |a||bc|')
  50. (((a, b), c),) = (abc,) = root
  51. self.assertEqual(factor_out_abs_term(root, (Scope(abc), a)), expect)
  52. root, expect = tree('|abc|, |b||ac|')
  53. (((a, b), c),) = (abc,) = root
  54. self.assertEqual(factor_out_abs_term(root, (Scope(abc), b)), expect)
  55. root, expect = tree('-|abc|, -|a||bc|')
  56. (((a, b), c),) = (abc,) = root
  57. self.assertEqual(factor_out_abs_term(root, (Scope(abc), a)), expect)
  58. def test_factor_out_abs_sqrt(self):
  59. root, expect = tree('|sqrt a|, sqrt|a|')
  60. self.assertEqual(factor_out_abs_sqrt(root, ()), expect)
  61. root, expect = tree('-|sqrt a|, -sqrt|a|')
  62. self.assertEqual(factor_out_abs_sqrt(root, ()), expect)
  63. def test_factor_out_abs_exponent(self):
  64. root, expect = tree('|a ^ 2|, |a| ^ 2')
  65. self.assertEqual(factor_out_abs_exponent(root, ()), expect)
  66. root, expect = tree('-|a ^ 2|, -|a| ^ 2')
  67. self.assertEqual(factor_out_abs_exponent(root, ()), expect)