test_rules_goniometry.py 3.6 KB

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