test_rules_numerics.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. from src.rules.numerics import add_numerics, match_divide_numerics, \
  2. divide_numerics, match_multiply_numerics, multiply_numerics
  3. from src.possibilities import Possibility as P
  4. from src.node import ExpressionLeaf as L
  5. from tests.rulestestcase import RulesTestCase
  6. from tests.test_rules_poly import tree
  7. class TestRulesNumerics(RulesTestCase):
  8. def test_add_numerics(self):
  9. l0, a, l1 = tree('1,a,2')
  10. self.assertEqual(add_numerics(l0 + l1, (l0, l1, 1, 2)), 3)
  11. self.assertEqual(add_numerics(l0 + a + l1, (l0, l1, 1, 2)), L(3) + a)
  12. def test_match_divide_numerics(self):
  13. a, b, i2, i3, i6, f1, f2, f3 = tree('a,b,2,3,6,1.0,2.0,3.0')
  14. root = i6 / i2
  15. possibilities = match_divide_numerics(root)
  16. self.assertEqualPos(possibilities,
  17. [P(root, divide_numerics, (6, 2))])
  18. root = i3 / i2
  19. possibilities = match_divide_numerics(root)
  20. self.assertEqualPos(possibilities, [])
  21. root = f3 / i2
  22. possibilities = match_divide_numerics(root)
  23. self.assertEqualPos(possibilities,
  24. [P(root, divide_numerics, (3.0, 2))])
  25. root = i3 / f2
  26. possibilities = match_divide_numerics(root)
  27. self.assertEqualPos(possibilities,
  28. [P(root, divide_numerics, (3, 2.0))])
  29. root = f3 / f2
  30. possibilities = match_divide_numerics(root)
  31. self.assertEqualPos(possibilities,
  32. [P(root, divide_numerics, (3.0, 2.0))])
  33. root = i3 / f1
  34. possibilities = match_divide_numerics(root)
  35. self.assertEqualPos(possibilities,
  36. [P(root, divide_numerics, (3, 1))])
  37. root = a / b
  38. possibilities = match_divide_numerics(root)
  39. self.assertEqualPos(possibilities, [])
  40. def test_divide_numerics(self):
  41. i2, i3, i6, f2, f3 = tree('2,3,6,2.0,3.0')
  42. self.assertEqual(divide_numerics(i6 / i2, (6, 2)), 3)
  43. self.assertEqual(divide_numerics(f3 / i2, (3.0, 2)), 1.5)
  44. self.assertEqual(divide_numerics(i3 / f2, (3, 2.0)), 1.5)
  45. self.assertEqual(divide_numerics(f3 / f2, (3.0, 2.0)), 1.5)
  46. def test_match_multiply_numerics(self):
  47. i2, i3, i6, f2, f3, f6 = tree('2,3,6,2.0,3.0,6.0')
  48. root = i3 * i2
  49. self.assertEqual(match_multiply_numerics(root),
  50. [P(root, multiply_numerics, (i3, i2))])
  51. root = f3 * i2
  52. self.assertEqual(match_multiply_numerics(root),
  53. [P(root, multiply_numerics, (f3, i2))])
  54. root = i3 * f2
  55. self.assertEqual(match_multiply_numerics(root),
  56. [P(root, multiply_numerics, (i3, f2))])
  57. root = f3 * f2
  58. self.assertEqual(match_multiply_numerics(root),
  59. [P(root, multiply_numerics, (f3, f2))])
  60. def test_multiply_numerics(self):
  61. a, b, i2, i3, i6, f2, f3, f6 = tree('a,b,2,3,6,2.0,3.0,6.0')
  62. self.assertEqual(multiply_numerics(i3 * i2, (i3, i2)), 6)
  63. self.assertEqual(multiply_numerics(f3 * i2, (f3, i2)), 6.0)
  64. self.assertEqual(multiply_numerics(i3 * f2, (i3, f2)), 6.0)
  65. self.assertEqual(multiply_numerics(f3 * f2, (f3, f2)), 6.0)
  66. self.assertEqualNodes(multiply_numerics(a * i3 * i2 * b, (i3, i2)),
  67. a * 6 * b)