test_line.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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_intermediate(self):
  37. a, b, c, d, e = Leaf('a'), Leaf('b'), Leaf('c'), Leaf('d'), Leaf('e')
  38. node_bc = Node('+', b, c)
  39. node_de = Node('+', d, e)
  40. node_mul = Node('*', a, node_bc)
  41. node_pow = Node('^', node_mul, node_de)
  42. self.assertEquals(generate_line(node_pow), 'a * (b + c) ^ (d + e)')