| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- import unittest
- from node import Node, Leaf
- from line import generate_line
- class TestLine(unittest.TestCase):
- def setUp(self):
- pass
- def tearDown(self):
- pass
- def test_simple(self):
- l0, l1 = Leaf(1), Leaf(2)
- plus = Node('+', l0, l1)
- self.assertEquals(generate_line(plus), '1 + 2')
- def test_parentheses(self):
- l0, l1 = Leaf(1), Leaf(2)
- plus = Node('+', l0, l1)
- times = Node('*', plus, plus)
- self.assertEquals(generate_line(times), '(1 + 2) * (1 + 2)')
- def test_parentheses_equal_precedence(self):
- l0, l1, l2 = Leaf(1), Leaf(2), Leaf(3)
- plus = Node('+', l1, l2)
- minus = Node('-', l0, plus)
- self.assertEquals(generate_line(minus), '1 - (2 + 3)')
- def test_parentheses_nary(self):
- l0, l1, l2 = Leaf(1), Leaf(2), Leaf(3)
- plus = Node('+', Node('+', l0, l1), l2)
- self.assertEquals(generate_line(plus), '1 + 2 + 3')
- def test_function(self):
- exp = Leaf('x')
- inf = Leaf('oo')
- minus_inf = Node('-', inf)
- integral = Node('int', exp, minus_inf, inf)
- self.assertEquals(generate_line(integral), 'int(x, -oo, oo)')
- def test_mod(self):
- l0, l1 = Leaf(1), Leaf(2)
- mod = Node('mod', l1, l0)
- self.assertEquals(generate_line(mod), '2 mod 1')
- def test_n_ary(self):
- l0, l1, l2 = Leaf(1), Leaf(2), Leaf(3)
- plus = Node('+', l0, l1, l2)
- self.assertEquals(generate_line(plus), '1 + 2 + 3')
- def test_pow_basic(self):
- a, b, c = Leaf('a'), Leaf('b'), Leaf('c')
- node_pow = Node('^', a, Node('+', b, c))
- self.assertEquals(generate_line(node_pow), 'a ^ (b + c)')
- def test_pow_intermediate1(self):
- # expression: (a(b+c))^(d+e)
- a, b, c, d, e = Leaf('a'), Leaf('b'), Leaf('c'), Leaf('d'), Leaf('e')
- node_bc = Node('+', b, c)
- node_de = Node('+', d, e)
- node_mul = Node('*', a, node_bc)
- node_pow = Node('^', node_mul, node_de)
- self.assertEquals(generate_line(node_pow), '(a * (b + c)) ^ (d + e)')
- def test_pow_intermediate2(self):
- # expression: a(b+c)^(d+e)
- a, b, c, d, e = Leaf('a'), Leaf('b'), Leaf('c'), Leaf('d'), Leaf('e')
- node_bc = Node('+', b, c)
- node_de = Node('+', d, e)
- node_pow = Node('^', node_bc, node_de)
- node_mul = Node('*', a, node_pow)
- self.assertEquals(generate_line(node_mul), 'a * (b + c) ^ (d + e)')
|