test_rules_numerics.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. from src.rules.numerics import match_add_numerics, add_numerics, \
  2. match_divide_numerics, divide_numerics, reduce_fraction_constants, \
  3. match_multiply_numerics, multiply_numerics, multiply_zero, \
  4. multiply_one, raise_numerics
  5. from src.node import ExpressionLeaf as L, Scope
  6. from src.possibilities import Possibility as P
  7. from tests.rulestestcase import RulesTestCase, tree
  8. class TestRulesNumerics(RulesTestCase):
  9. def test_match_add_numerics(self):
  10. l1, l2 = root = tree('1 + 2')
  11. possibilities = match_add_numerics(root)
  12. self.assertEqualPos(possibilities,
  13. [P(root, add_numerics, (Scope(root), l1, l2))])
  14. (l1, b), l2 = root = tree('1 + b + 2')
  15. possibilities = match_add_numerics(root)
  16. self.assertEqualPos(possibilities,
  17. [P(root, add_numerics, (Scope(root), l1, l2))])
  18. def test_add_numerics(self):
  19. l1, l2 = root = tree('1 + 2')
  20. self.assertEqual(add_numerics(root, (Scope(root), l1, l2)), 3)
  21. (l1, a), l2 = root = tree('1 + a + 2')
  22. self.assertEqual(add_numerics(root, (Scope(root), l1, l2)), L(3) + a)
  23. def test_add_numerics_negations(self):
  24. ml1, l2 = root = tree('-1 + 2')
  25. self.assertEqual(add_numerics(root, (Scope(root), ml1, l2)), 1)
  26. l1, ml2 = root = tree('1 - 2')
  27. self.assertEqual(add_numerics(root, (Scope(root), l1, ml2)), -1)
  28. def test_match_divide_numerics(self):
  29. a, b, i2, i3, i4, i6, f1, f2, f3 = tree('a,b,2,3,4,6,1.0,2.0,3.0')
  30. root = i6 / i2
  31. possibilities = match_divide_numerics(root)
  32. self.assertEqualPos(possibilities, [P(root, divide_numerics)])
  33. root = -i6 / i2
  34. self.assertEqualPos(match_divide_numerics(root), [])
  35. root = i6 / -i2
  36. self.assertEqualPos(match_divide_numerics(root), [])
  37. root = i2 / i4
  38. self.assertEqualPos(match_divide_numerics(root),
  39. [P(root, reduce_fraction_constants, (2,))])
  40. root = f3 / i2
  41. self.assertEqualPos(match_divide_numerics(root),
  42. [P(root, divide_numerics)])
  43. root = i3 / f2
  44. self.assertEqualPos(match_divide_numerics(root),
  45. [P(root, divide_numerics)])
  46. root = f3 / f2
  47. self.assertEqualPos(match_divide_numerics(root),
  48. [P(root, divide_numerics)])
  49. root = i3 / f1
  50. self.assertEqualPos(match_divide_numerics(root),
  51. [P(root, divide_numerics)])
  52. root = a / b
  53. self.assertEqualPos(match_divide_numerics(root), [])
  54. def test_divide_numerics(self):
  55. i2, i3, i6, f2, f3 = tree('2,3,6,2.0,3.0')
  56. self.assertEqual(divide_numerics(i6 / i2, ()), 3)
  57. self.assertEqual(divide_numerics(f3 / i2, ()), 1.5)
  58. self.assertEqual(divide_numerics(i3 / f2, ()), 1.5)
  59. self.assertEqual(divide_numerics(f3 / f2, ()), 1.5)
  60. self.assertEqual(divide_numerics(-(i6 / i2), ()), -i3)
  61. def test_reduce_fraction_constants(self):
  62. l1, l2 = tree('1,2')
  63. self.assertEqual(reduce_fraction_constants(l2 / 4, (2,)), l1 / l2)
  64. #def test_fraction_to_int_fraction(self):
  65. # l1, l4 = tree('1,4')
  66. # self.assertEqual(fraction_to_int_fraction(l4 / 3, (1, 1, 3)),
  67. # l1 + l1 / 3)
  68. def test_match_multiply_numerics(self):
  69. i2, i3, i6, f2, f3, f6 = tree('2,3,6,2.0,3.0,6.0')
  70. root = i3 * i2
  71. self.assertEqual(match_multiply_numerics(root),
  72. [P(root, multiply_numerics, (Scope(root), i3, i2))])
  73. root = f3 * i2
  74. self.assertEqual(match_multiply_numerics(root),
  75. [P(root, multiply_numerics, (Scope(root), f3, i2))])
  76. root = i3 * f2
  77. self.assertEqual(match_multiply_numerics(root),
  78. [P(root, multiply_numerics, (Scope(root), i3, f2))])
  79. root = f3 * f2
  80. self.assertEqual(match_multiply_numerics(root),
  81. [P(root, multiply_numerics, (Scope(root), f3, f2))])
  82. def test_match_multiply_zero(self):
  83. l0, x = root = tree('0x')
  84. self.assertEqual(match_multiply_numerics(root),
  85. [P(root, multiply_zero, (l0,))])
  86. def test_match_multiply_one(self):
  87. l1, x = root = tree('1x')
  88. self.assertEqual(match_multiply_numerics(root),
  89. [P(root, multiply_one, (Scope(root), l1))])
  90. (x, l1), x = root = tree('x * 1x')
  91. self.assertEqual(match_multiply_numerics(root),
  92. [P(root, multiply_one, (Scope(root), l1))])
  93. def test_multiply_numerics(self):
  94. a, b, i2, i3, i6, f2, f3, f6 = tree('a,b,2,3,6,2.0,3.0,6.0')
  95. i3, i2 = root = tree('3 * 2')
  96. self.assertEqual(multiply_numerics(root, (Scope(root), i3, i2)), 6)
  97. f3, i2 = root = tree('3.0 * 2')
  98. self.assertEqual(multiply_numerics(root, (Scope(root), f3, i2)), 6.0)
  99. i3, f2 = root = tree('3 * 2.0')
  100. self.assertEqual(multiply_numerics(root, (Scope(root), i3, f2)), 6.0)
  101. f3, f2 = root = tree('3.0 * 2.0')
  102. self.assertEqual(multiply_numerics(root, (Scope(root), f3, f2)), 6.0)
  103. ((a, i3), i2), b = root = tree('a * 3 * 2 * b')
  104. self.assertEqualNodes(multiply_numerics(root,
  105. (Scope(root), i3, i2)), a * 6 * b)
  106. def test_multiply_numerics_negation(self):
  107. l1_neg, l2 = root = tree('-1 * 2')
  108. self.assertEqualNodes(multiply_numerics(root, (Scope(root), l1_neg,
  109. l2)), -l2)
  110. root, l6 = tree('1 + -2 * 3,6')
  111. l1, mul = root
  112. l2_neg, l3 = mul
  113. self.assertEqualNodes(multiply_numerics(mul, (Scope(mul),
  114. l2_neg, l3)), -l6)
  115. root, l30 = tree('-5 * x ^ 2 - -15x - 5 * 6,30')
  116. rest, mul = root
  117. l5_neg, l6 = mul
  118. self.assertEqualNodes(multiply_numerics(mul, (Scope(mul),
  119. l5_neg, l6)), -l30)
  120. def test_raise_numerics(self):
  121. l1, l2 = root = tree('2 ^ 3')
  122. self.assertEqualNodes(raise_numerics(root, (l1, l2, 0)), L(8))
  123. l1_neg, l2 = root = tree('(-2) ^ 2')
  124. self.assertEqualNodes(raise_numerics(root, (l1_neg, l2, 0)), --L(4))
  125. l1_neg, l2 = root = tree('(-2) ^ 3')
  126. self.assertEqualNodes(raise_numerics(root, (l1_neg, l2, 0)), ---L(8))
  127. l1_neg, l2 = root = tree('-((-2) ^ 3)')
  128. self.assertEqualNodes(raise_numerics(root, (l1_neg, l2, 1)), ----L(8))
  129. l1_neg, l2 = root = tree('-(2 ^ 3)')
  130. self.assertEqualNodes(raise_numerics(root, (l1_neg, l2, 1)), -L(8))