test_rules_numerics.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. from src.rules.numerics import match_add_numerics, add_numerics, \
  2. match_divide_numerics, divide_numerics, match_multiply_numerics, \
  3. multiply_numerics
  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 TestRulesNumerics(RulesTestCase):
  8. def test_match_add_numerics(self):
  9. l1, l2 = root = tree('1 + 2')
  10. possibilities = match_add_numerics(root)
  11. self.assertEqualPos(possibilities,
  12. [P(root, add_numerics, (Scope(root), l1, l2))])
  13. (l1, b), l2 = root = tree('1 + b + 2')
  14. possibilities = match_add_numerics(root)
  15. self.assertEqualPos(possibilities,
  16. [P(root, add_numerics, (Scope(root), l1, l2))])
  17. def test_add_numerics(self):
  18. l0, a, l1 = tree('1,a,2')
  19. root = l0 + l1
  20. self.assertEqual(add_numerics(root, (Scope(root), l0, l1)), 3)
  21. root = l0 + a + l1
  22. self.assertEqual(add_numerics(root, (Scope(root), l0, l1)), L(3) + a)
  23. def test_add_numerics_negations(self):
  24. l1, a, l2 = tree('1,a,2')
  25. ml1, ml2 = -l1, -l2
  26. r = ml1 + l2
  27. self.assertEqual(add_numerics(r, (Scope(r), ml1, l2)), 1)
  28. r = l1 + ml2
  29. self.assertEqual(add_numerics(r, (Scope(r), l1, ml2)), -1)
  30. def test_match_divide_numerics(self):
  31. a, b, i2, i3, i6, f1, f2, f3 = tree('a,b,2,3,6,1.0,2.0,3.0')
  32. root = i6 / i2
  33. possibilities = match_divide_numerics(root)
  34. self.assertEqualPos(possibilities,
  35. [P(root, divide_numerics, (6, 2))])
  36. root = i3 / i2
  37. possibilities = match_divide_numerics(root)
  38. self.assertEqualPos(possibilities, [])
  39. root = f3 / i2
  40. possibilities = match_divide_numerics(root)
  41. self.assertEqualPos(possibilities,
  42. [P(root, divide_numerics, (3.0, 2))])
  43. root = i3 / f2
  44. possibilities = match_divide_numerics(root)
  45. self.assertEqualPos(possibilities,
  46. [P(root, divide_numerics, (3, 2.0))])
  47. root = f3 / f2
  48. possibilities = match_divide_numerics(root)
  49. self.assertEqualPos(possibilities,
  50. [P(root, divide_numerics, (3.0, 2.0))])
  51. root = i3 / f1
  52. possibilities = match_divide_numerics(root)
  53. self.assertEqualPos(possibilities,
  54. [P(root, divide_numerics, (3, 1))])
  55. root = a / b
  56. possibilities = match_divide_numerics(root)
  57. self.assertEqualPos(possibilities, [])
  58. def test_divide_numerics(self):
  59. i2, i3, i6, f2, f3 = tree('2,3,6,2.0,3.0')
  60. self.assertEqual(divide_numerics(i6 / i2, (6, 2)), 3)
  61. self.assertEqual(divide_numerics(f3 / i2, (3.0, 2)), 1.5)
  62. self.assertEqual(divide_numerics(i3 / f2, (3, 2.0)), 1.5)
  63. self.assertEqual(divide_numerics(f3 / f2, (3.0, 2.0)), 1.5)
  64. def test_match_multiply_numerics(self):
  65. i2, i3, i6, f2, f3, f6 = tree('2,3,6,2.0,3.0,6.0')
  66. root = i3 * i2
  67. self.assertEqual(match_multiply_numerics(root),
  68. [P(root, multiply_numerics, (Scope(root), i3, i2))])
  69. root = f3 * i2
  70. self.assertEqual(match_multiply_numerics(root),
  71. [P(root, multiply_numerics, (Scope(root), f3, i2))])
  72. root = i3 * f2
  73. self.assertEqual(match_multiply_numerics(root),
  74. [P(root, multiply_numerics, (Scope(root), i3, f2))])
  75. root = f3 * f2
  76. self.assertEqual(match_multiply_numerics(root),
  77. [P(root, multiply_numerics, (Scope(root), f3, f2))])
  78. def test_multiply_numerics(self):
  79. a, b, i2, i3, i6, f2, f3, f6 = tree('a,b,2,3,6,2.0,3.0,6.0')
  80. root = i3 * i2
  81. self.assertEqual(multiply_numerics(root, (Scope(root), i3, i2)), 6)
  82. root = f3 * i2
  83. self.assertEqual(multiply_numerics(root, (Scope(root), f3, i2)), 6.0)
  84. root = i3 * f2
  85. self.assertEqual(multiply_numerics(root, (Scope(root), i3, f2)), 6.0)
  86. root = f3 * f2
  87. self.assertEqual(multiply_numerics(root, (Scope(root), f3, f2)), 6.0)
  88. root = a * i3 * i2 * b
  89. self.assertEqualNodes(multiply_numerics(root,
  90. (Scope(root), i3, i2)), a * 6 * b)
  91. def test_multiply_numerics_negation(self):
  92. l1_neg, l2 = root = tree('-1 * 2')
  93. self.assertEqualNodes(multiply_numerics(root, (Scope(root), l1_neg,
  94. l2)), -l2)
  95. root, l6 = tree('1 + -2 * 3,6')
  96. l1, mul = root
  97. l2_neg, l3 = mul
  98. self.assertEqualNodes(multiply_numerics(mul, (Scope(mul),
  99. l2_neg, l3)), -l6)
  100. root, l30 = tree('-5 * x ^ 2 - -15x - 5 * 6,30')
  101. rest, mul = root
  102. l5_neg, l6 = mul
  103. self.assertEqualNodes(multiply_numerics(mul, (Scope(mul),
  104. l5_neg, l6)), -l30)