test_rules_numerics.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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_add_numerics(self):
  13. l0, a, l1 = tree('1,a,2')
  14. self.assertEqual(add_numerics(l0 + l1, (l0, l1, 1, 2)), 3)
  15. self.assertEqual(add_numerics(l0 + a + l1, (l0, l1, 1, 2)), L(3) + a)
  16. def test_match_divide_numerics(self):
  17. a, b, i2, i3, i6, f1, f2, f3 = tree('a,b,2,3,6,1.0,2.0,3.0')
  18. root = i6 / i2
  19. possibilities = match_divide_numerics(root)
  20. self.assertEqualPos(possibilities,
  21. [P(root, divide_numerics, (6, 2))])
  22. root = i3 / i2
  23. possibilities = match_divide_numerics(root)
  24. self.assertEqualPos(possibilities, [])
  25. root = f3 / i2
  26. possibilities = match_divide_numerics(root)
  27. self.assertEqualPos(possibilities,
  28. [P(root, divide_numerics, (3.0, 2))])
  29. root = i3 / f2
  30. possibilities = match_divide_numerics(root)
  31. self.assertEqualPos(possibilities,
  32. [P(root, divide_numerics, (3, 2.0))])
  33. root = f3 / f2
  34. possibilities = match_divide_numerics(root)
  35. self.assertEqualPos(possibilities,
  36. [P(root, divide_numerics, (3.0, 2.0))])
  37. root = i3 / f1
  38. possibilities = match_divide_numerics(root)
  39. self.assertEqualPos(possibilities,
  40. [P(root, divide_numerics, (3, 1))])
  41. root = a / b
  42. possibilities = match_divide_numerics(root)
  43. self.assertEqualPos(possibilities, [])
  44. def test_divide_numerics(self):
  45. i2, i3, i6, f2, f3 = tree('2,3,6,2.0,3.0')
  46. self.assertEqual(divide_numerics(i6 / i2, (6, 2)), 3)
  47. self.assertEqual(divide_numerics(f3 / i2, (3.0, 2)), 1.5)
  48. self.assertEqual(divide_numerics(i3 / f2, (3, 2.0)), 1.5)
  49. self.assertEqual(divide_numerics(f3 / f2, (3.0, 2.0)), 1.5)
  50. def test_match_multiply_numerics(self):
  51. i2, i3, i6, f2, f3, f6 = tree('2,3,6,2.0,3.0,6.0')
  52. root = i3 * i2
  53. self.assertEqual(match_multiply_numerics(root),
  54. [P(root, multiply_numerics, (i3, i2))])
  55. root = f3 * i2
  56. self.assertEqual(match_multiply_numerics(root),
  57. [P(root, multiply_numerics, (f3, i2))])
  58. root = i3 * f2
  59. self.assertEqual(match_multiply_numerics(root),
  60. [P(root, multiply_numerics, (i3, f2))])
  61. root = f3 * f2
  62. self.assertEqual(match_multiply_numerics(root),
  63. [P(root, multiply_numerics, (f3, f2))])
  64. def test_multiply_numerics(self):
  65. a, b, i2, i3, i6, f2, f3, f6 = tree('a,b,2,3,6,2.0,3.0,6.0')
  66. self.assertEqual(multiply_numerics(i3 * i2, (i3, i2)), 6)
  67. self.assertEqual(multiply_numerics(f3 * i2, (f3, i2)), 6.0)
  68. self.assertEqual(multiply_numerics(i3 * f2, (i3, f2)), 6.0)
  69. self.assertEqual(multiply_numerics(f3 * f2, (f3, f2)), 6.0)
  70. self.assertEqualNodes(multiply_numerics(a * i3 * i2 * b, (i3, i2)),
  71. a * 6 * b)