test_line.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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_parentheses_equal_precedence(self):
  19. l0, l1, l2 = Leaf(1), Leaf(2), Leaf(3)
  20. plus = Node('+', l1, l2)
  21. minus = Node('-', l0, plus)
  22. self.assertEquals(generate_line(minus), '1 - (2 + 3)')
  23. def test_parentheses_nary(self):
  24. l0, l1, l2 = Leaf(1), Leaf(2), Leaf(3)
  25. plus = Node('+', Node('+', l0, l1), l2)
  26. self.assertEquals(generate_line(plus), '1 + 2 + 3')
  27. def test_function(self):
  28. exp = Leaf('x')
  29. inf = Leaf('oo')
  30. minus_inf = Node('-', inf)
  31. integral = Node('int', exp, minus_inf, inf)
  32. self.assertEquals(generate_line(integral), 'int(x, -oo, oo)')
  33. def test_mod(self):
  34. l0, l1 = Leaf(1), Leaf(2)
  35. mod = Node('mod', l1, l0)
  36. self.assertEquals(generate_line(mod), '2 mod 1')
  37. def test_n_ary(self):
  38. l0, l1, l2 = Leaf(1), Leaf(2), Leaf(3)
  39. plus = Node('+', l0, l1, l2)
  40. self.assertEquals(generate_line(plus), '1 + 2 + 3')
  41. def test_pow_basic(self):
  42. a, b, c = Leaf('a'), Leaf('b'), Leaf('c')
  43. node_pow = Node('^', a, Node('+', b, c))
  44. self.assertEquals(generate_line(node_pow), 'a ^ (b + c)')
  45. def test_pow_intermediate1(self):
  46. # expression: (a(b+c))^(d+e)
  47. a, b, c, d, e = Leaf('a'), Leaf('b'), Leaf('c'), Leaf('d'), Leaf('e')
  48. node_bc = Node('+', b, c)
  49. node_de = Node('+', d, e)
  50. node_mul = Node('*', a, node_bc)
  51. node_pow = Node('^', node_mul, node_de)
  52. self.assertEquals(generate_line(node_pow), '(a * (b + c)) ^ (d + e)')
  53. def test_pow_intermediate2(self):
  54. # expression: a(b+c)^(d+e)
  55. a, b, c, d, e = Leaf('a'), Leaf('b'), Leaf('c'), Leaf('d'), Leaf('e')
  56. node_bc = Node('+', b, c)
  57. node_de = Node('+', d, e)
  58. node_pow = Node('^', node_bc, node_de)
  59. node_mul = Node('*', a, node_pow)
  60. self.assertEquals(generate_line(node_mul), 'a * (b + c) ^ (d + e)')