test_rules_numerics.py 3.2 KB

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