test_rules_goniometry.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. # vim: set fileencoding=utf-8 :
  2. from src.rules.goniometry import match_add_quadrants, add_quadrants, \
  3. factor_out_quadrant_negation, match_negated_parameter, \
  4. negated_sinus_parameter, is_pi_frac, negated_cosinus_parameter, \
  5. match_standard_radian, standard_radian
  6. from src.node import PI, OP_SIN, OP_COS, OP_TAN, sin, cos, tan, Scope
  7. from src.possibilities import Possibility as P
  8. from tests.rulestestcase import RulesTestCase, tree
  9. from src.rules import goniometry
  10. import doctest
  11. class TestRulesGoniometry(RulesTestCase):
  12. def test_doctest(self):
  13. self.assertEqual(doctest.testmod(m=goniometry)[0], 0)
  14. def test_match_add_quadrants(self):
  15. s, c = root = tree('sin(t) ^ 2 + cos(t) ^ 2')
  16. self.assertEqualPos(match_add_quadrants(root),
  17. [P(root, add_quadrants, (Scope(root), s, c))])
  18. c, s = root = tree('cos(t) ^ 2 + sin(t) ^ 2')
  19. self.assertEqualPos(match_add_quadrants(root),
  20. [P(root, add_quadrants, (Scope(root), s, c))])
  21. (s, a), c = root = tree('sin(t) ^ 2 + a + cos(t) ^ 2')
  22. self.assertEqualPos(match_add_quadrants(root),
  23. [P(root, add_quadrants, (Scope(root), s, c))])
  24. (s, c0), c1 = root = tree('sin(t) ^ 2 + cos(t) ^ 2 + cos(t) ^ 2')
  25. self.assertEqualPos(match_add_quadrants(root),
  26. [P(root, add_quadrants, (Scope(root), s, c0)),
  27. P(root, add_quadrants, (Scope(root), s, c1))])
  28. root = tree('sin(t) ^ 2 + cos(y) ^ 2')
  29. self.assertEqualPos(match_add_quadrants(root), [])
  30. root = tree('sin(t) ^ 2 - cos(t) ^ 2')
  31. self.assertEqualPos(match_add_quadrants(root), [])
  32. s, c = root = tree('-sin(t) ^ 2 - cos(t) ^ 2')
  33. self.assertEqualPos(match_add_quadrants(root),
  34. [P(root, factor_out_quadrant_negation, (Scope(root), s, c))])
  35. def test_add_quadrants(self):
  36. s, c = root = tree('sin(t) ^ 2 + cos(t) ^ 2')
  37. self.assertEqual(add_quadrants(root, (Scope(root), s, c)), 1)
  38. root, expect = tree('cos(t) ^ 2 + a + sin(t) ^ 2, a + 1')
  39. (c, a), s = root
  40. self.assertEqual(add_quadrants(root, (Scope(root), s, c)), expect)
  41. def test_factor_out_quadrant_negation(self):
  42. r, e = tree('-sin(t) ^ 2 - cos(t) ^ 2, -(sin(t) ^ 2 + cos(t) ^ 2)')
  43. s, c = r
  44. self.assertEqual(factor_out_quadrant_negation(r, (Scope(r), s, c)), e)
  45. def test_match_negated_parameter(self):
  46. s, c = tree('sin -t, cos -t')
  47. t = s[0]
  48. self.assertEqualPos(match_negated_parameter(s), \
  49. [P(s, negated_sinus_parameter, (t,))])
  50. self.assertEqualPos(match_negated_parameter(c), \
  51. [P(c, negated_cosinus_parameter, (t,))])
  52. def test_negated_sinus_parameter(self):
  53. s = tree('sin -t')
  54. t = s[0]
  55. self.assertEqual(negated_sinus_parameter(s, (t,)), -sin(+t))
  56. def test_negated_cosinus_parameter(self):
  57. c = tree('cos -t')
  58. t = c[0]
  59. self.assertEqual(negated_cosinus_parameter(c, (t,)), cos(+t))
  60. def test_is_pi_frac(self):
  61. l1, pi = tree('1,' + PI)
  62. self.assertTrue(is_pi_frac(l1 / 2 * pi, 2))
  63. self.assertFalse(is_pi_frac(l1 / 2 * pi, 3))
  64. self.assertFalse(is_pi_frac(l1 * pi, 3))
  65. def test_match_standard_radian(self):
  66. s, c, t = tree('sin(1 / 6 * pi), cos(1 / 2 * pi), tan(0)')
  67. self.assertEqualPos(match_standard_radian(s), \
  68. [P(s, standard_radian, (OP_SIN, 1))])
  69. self.assertEqualPos(match_standard_radian(c), \
  70. [P(c, standard_radian, (OP_COS, 4))])
  71. self.assertEqualPos(match_standard_radian(t), \
  72. [P(t, standard_radian, (OP_TAN, 0))])
  73. def test_standard_radian(self):
  74. l0, l1, sq3, pi6, pi4, pi2 = tree('0,1,sqrt(3),1/6*pi,1/4*pi,1/2*pi')
  75. self.assertEqual(standard_radian(sin(pi6), (OP_SIN, 1)), l1 / 2)
  76. self.assertEqual(standard_radian(sin(pi2), (OP_SIN, 4)), 1)
  77. self.assertEqual(standard_radian(cos(l0), (OP_COS, 0)), 1)
  78. self.assertEqual(standard_radian(tan(pi4), (OP_TAN, 3)), sq3)