test_node.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import unittest
  2. from src.node import ExpressionNode as N, ExpressionLeaf as L
  3. class TestNode(unittest.TestCase):
  4. def setUp(self):
  5. self.l = [L(1), N('*', L(2), L(3)), L(4), L(5)]
  6. def test_is_power_true(self):
  7. self.assertTrue(N('^', *self.l[:2]).is_power())
  8. self.assertFalse(N('+', *self.l[:2]).is_power())
  9. def test_is_nary(self):
  10. self.assertTrue(N('+', *self.l[:2]).is_nary())
  11. self.assertTrue(N('-', *self.l[:2]).is_nary())
  12. self.assertTrue(N('*', *self.l[:2]).is_nary())
  13. self.assertFalse(N('^', *self.l[:2]).is_nary())
  14. def test_is_identifier(self):
  15. self.assertTrue(L('a').is_identifier())
  16. self.assertFalse(L(1).is_identifier())
  17. def test_is_int(self):
  18. self.assertTrue(L(1).is_int())
  19. self.assertFalse(L(1.5).is_int())
  20. self.assertFalse(L('a').is_int())
  21. def test_is_float(self):
  22. self.assertTrue(L(1.5).is_float())
  23. self.assertFalse(L(1).is_float())
  24. self.assertFalse(L('a').is_float())
  25. def test_is_numeric(self):
  26. self.assertTrue(L(1).is_numeric())
  27. self.assertTrue(L(1.5).is_numeric())
  28. self.assertFalse(L('a').is_numeric())
  29. def test_extract_polynome_properties_identifier(self):
  30. self.assertEqual(L('a').extract_polynome_properties(),
  31. (L(1), L('a'), L(1)))
  32. def test_extract_polynome_properties_None(self):
  33. self.assertIsNone(N('+').extract_polynome_properties())
  34. def test_extract_polynome_properties_power(self):
  35. power = N('^', L('a'), L(2))
  36. self.assertEqual(power.extract_polynome_properties(),
  37. (L(1), L('a'), L(2)))
  38. def test_extract_polynome_properties_coefficient_exponent_int(self):
  39. times = N('*', L(3), N('^', L('a'), L(2)))
  40. self.assertEqual(times.extract_polynome_properties(),
  41. (L(3), L('a'), L(2)))
  42. def test_extract_polynome_properties_coefficient_exponent_id(self):
  43. times = N('*', L(3), N('^', L('a'), L('b')))
  44. self.assertEqual(times.extract_polynome_properties(),
  45. (L(3), L('a'), L('b')))
  46. def test_get_scope_binary(self):
  47. plus = N('+', *self.l[:2])
  48. self.assertEqual(plus.get_scope(), self.l[:2])
  49. def test_get_scope_nested_left(self):
  50. plus = N('+', N('+', *self.l[:2]), self.l[2])
  51. self.assertEqual(plus.get_scope(), self.l[:3])
  52. def test_get_scope_nested_right(self):
  53. plus = N('+', self.l[0], N('+', *self.l[1:3]))
  54. self.assertEqual(plus.get_scope(), self.l[:3])
  55. def test_get_scope_nested_deep(self):
  56. plus = N('+', N('+', N('+', *self.l[:2]), self.l[2]), self.l[3])
  57. self.assertEqual(plus.get_scope(), self.l)