test_line.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import unittest
  2. from node import Node, Leaf
  3. from line import generate_line
  4. class TestLine(unittest.TestCase):
  5. def setUp(self):
  6. pass
  7. def tearDown(self):
  8. pass
  9. def test_simple(self):
  10. l0, l1 = Leaf(1), Leaf(2)
  11. plus = Node('+', l0, l1)
  12. self.assertEquals(generate_line(plus), '1 + 2')
  13. def test_parentheses(self):
  14. l0, l1 = Leaf(1), Leaf(2)
  15. plus = Node('+', l0, l1)
  16. times = Node('*', plus, plus)
  17. self.assertEquals(generate_line(times), '(1 + 2) * (1 + 2)')
  18. def test_function(self):
  19. exp = Leaf('x')
  20. inf = Leaf('oo')
  21. minus_inf = Node('-', inf)
  22. integral = Node('int', exp, minus_inf, inf)
  23. self.assertEquals(generate_line(integral), 'int(x, -oo, oo)')
  24. def test_mod(self):
  25. l0, l1 = Leaf(1), Leaf(2)
  26. mod = Node('mod', l1, l0)
  27. self.assertEquals(generate_line(mod), '2 mod 1')
  28. def test_n_ary(self):
  29. l0, l1, l2 = Leaf(1), Leaf(2), Leaf(3)
  30. plus = Node('+', l0, l1, l2)
  31. self.assertEquals(generate_line(plus), '1 + 2 + 3')
  32. def test_pow_basic(self):
  33. a, b, c = Leaf('a'), Leaf('b'), Leaf('c')
  34. node_pow = Node('^', a, Node('+', b, c))
  35. self.assertEquals(generate_line(node_pow), 'a ^ (b + c)')
  36. def test_pow_intermediate1(self):
  37. # expression: (a(b+c))^(d+e)
  38. a, b, c, d, e = Leaf('a'), Leaf('b'), Leaf('c'), Leaf('d'), Leaf('e')
  39. node_bc = Node('+', b, c)
  40. node_de = Node('+', d, e)
  41. node_mul = Node('*', a, node_bc)
  42. node_pow = Node('^', node_mul, node_de)
  43. self.assertEquals(generate_line(node_pow), '(a * (b + c)) ^ (d + e)')
  44. def test_pow_intermediate2(self):
  45. # expression: a(b+c)^(d+e)
  46. a, b, c, d, e = Leaf('a'), Leaf('b'), Leaf('c'), Leaf('d'), Leaf('e')
  47. node_bc = Node('+', b, c)
  48. node_de = Node('+', d, e)
  49. node_pow = Node('^', node_bc, node_de)
  50. node_mul = Node('*', a, node_pow)
  51. self.assertEquals(generate_line(node_mul), 'a * (b + c) ^ (d + e)')