test_rules_numerics.py 6.2 KB

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