def test_assign_str(self):
        """Check Assign.__str__()."""
        # Initialize some numbers, bools, variables, expressions
        #
        n1 = Number(1)
        n2 = Number(2)
        vx = Variable('x')
        vy = Variable('y')
        e1 = Add(n1, n2)
        e2 = Add(vx, vy)
        e3 = GreaterThan(n1, n2)
        e4 = GreaterThan(vx, vy)

        # Initialize some statements
        #
        ae1 = Assign('a', e1)
        ae2 = Assign('b', e2)
        ae3 = Assign('c', e3)
        ae4 = Assign('d', e4)

        # Check value string
        #
        self.assertEqual("a = 1 + 2;", str(ae1))
        self.assertEqual("b = x + y;", str(ae2))
        self.assertEqual("c = 1 > 2;", str(ae3))
        self.assertEqual("d = x > y;", str(ae4))
    def test_while_str(self):
        """Check While.__str__()."""
        # Initialize some numbers, bools, variables, expressions,
        # statements
        #
        n1 = Number(1)
        n3 = Number(3)
        n3m = Number(-3)
        va = Variable('a')
        vb = Variable('b')
        e1 = Add(va, n1)
        e2 = Add(vb, va)
        e3 = Subtract(va, n1)
        e4 = LessThan(va, n3)
        e5 = GreaterThan(va, n3m)
        s1 = Assign('a', e1)
        s2 = Assign('b', e2)
        s3 = Assign('a', e3)
        s4 = Sequence(s1, s2)
        s5 = Sequence(s3, s2)

        # Initialize some statements
        #
        sa1 = While(e4, s4)
        sa2 = While(e5, s5)
        sa3 = While(Boolean(False), s4)

        # Check value strings
        #
        self.assertEqual("while (a < 3) { a = a + 1; b = b + a; }", str(sa1))
        self.assertEqual("while (a > -3) { a = a - 1; b = b + a; }", str(sa2))
        self.assertEqual("while (false) { a = a + 1; b = b + a; }", str(sa3))
    def test_assign_init(self):
        """Check Assign.__init__()."""
        # Initialize some numbers, bools, variables, expressions
        #
        n1 = Number(1)
        n2 = Number(2)
        vx = Variable('x')
        vy = Variable('y')
        e1 = Add(n1, n2)
        e2 = Add(vx, vy)
        e3 = GreaterThan(n1, n2)
        e4 = GreaterThan(vx, vy)

        # Initialize some statements
        #
        ae1 = Assign('a', e1)
        ae2 = Assign('b', e2)
        ae3 = Assign('c', e3)
        ae4 = Assign('d', e4)

        # Check the initialization
        #
        self.assertEqual('a', ae1.name)
        self.assertEqual(e1, ae1.expression)
        self.assertEqual('b', ae2.name)
        self.assertEqual(e2, ae2.expression)
        self.assertEqual('c', ae3.name)
        self.assertEqual(e3, ae3.expression)
        self.assertEqual('d', ae4.name)
        self.assertEqual(e4, ae4.expression)
    def test_assign_to_python(self):
        """Check Assign.to_python()."""
        # Initialize some numbers, bools, variables, expressions
        #
        n1 = Number(1)
        n2 = Number(2)
        vx = Variable('x')
        vy = Variable('y')
        e1 = Add(n1, n2)
        e2 = Add(vx, vy)
        e3 = GreaterThan(n1, n2)
        e4 = GreaterThan(vx, vy)

        # Initialize some statements
        #
        ae1 = Assign('a', e1)
        ae2 = Assign('b', e2)
        ae3 = Assign('c', e3)
        ae4 = Assign('d', e4)

        # Produce python code strings
        #
        ae1p = ae1.to_python(0)
        ae2p = ae2.to_python(0)
        ae3p = ae3.to_python(0)
        ae4p = ae4.to_python(0)

        # Check python code strings
        #
        self.assertEqual("e['a'] = (1) + (2)", ae1p)
        self.assertEqual("e['b'] = (e['x']) + (e['y'])", ae2p)
        self.assertEqual("e['c'] = (1) > (2)", ae3p)
        self.assertEqual("e['d'] = (e['x']) > (e['y'])", ae4p)
    def test_sequence_init(self):
        """Check Sequence.__init__()."""
        # Initialize some numbers, bools, variables, expressions,
        # statements
        #
        n1 = Number(1)
        n2 = Number(2)
        vx = Variable('x')
        vy = Variable('y')
        e1 = Add(n1, n2)
        e2 = Add(vx, vy)
        e3 = GreaterThan(n1, n2)
        e4 = GreaterThan(vx, vy)
        s1 = Assign('a', e1)
        s2 = Assign('b', e2)
        s3 = Assign('c', e3)
        s4 = Assign('d', e4)

        # Initialize some statements
        #
        sa1 = Sequence(s1, s2)
        sa2 = Sequence(Sequence(s1, s2), s3)
        sa3 = Sequence(Sequence(Sequence(s1, s2), s3), s4)

        # Check the initialization
        #
        self.assertEqual(s1, sa1.first)
        self.assertEqual(s2, sa1.second)
        self.assertEqual(s1, sa2.first.first)
        self.assertEqual(s2, sa2.first.second)
        self.assertEqual(s3, sa2.second)
        self.assertEqual(s1, sa3.first.first.first)
        self.assertEqual(s2, sa3.first.first.second)
        self.assertEqual(s3, sa3.first.second)
        self.assertEqual(s4, sa3.second)
    def test_assign_evaluate(self):
        """Check Assign.evaluate()."""
        # Initialize some numbers, bools, variables, expressions
        #
        n1 = Number(1)
        n2 = Number(2)
        vx = Variable('x')
        vy = Variable('y')
        e1 = Add(n1, n2)
        e2 = Add(vx, vy)
        e3 = GreaterThan(n1, n2)
        e4 = GreaterThan(vx, vy)

        # Initialize some statements
        #
        ae1 = Assign('a', e1)
        ae2 = Assign('b', e2)
        ae3 = Assign('c', e3)
        ae4 = Assign('d', e4)

        # Evaluate the add objects
        #
        env = dict([('x', Number(5.2)), ('y', Number(3.4))])
        ae1e = ae1.evaluate(env)
        ae2e = ae2.evaluate(env)
        ae3e = ae3.evaluate(env)
        ae4e = ae4.evaluate(env)

        # check the results
        #
        self.assertEqual(Number(1 + 2), ae1e['a'])
        self.assertEqual(Number(5.2 + 3.4), ae2e['b'])
        self.assertEqual(Boolean(1 > 2), ae3e['c'])
        self.assertEqual(Boolean(5.2 > 3.4), ae4e['d'])
    def test_sequence_str(self):
        """Check Sequence.__str__()."""
        # Initialize some numbers, bools, variables, expressions,
        # statements
        #
        n1 = Number(1)
        n2 = Number(2)
        vx = Variable('x')
        vy = Variable('y')
        e1 = Add(n1, n2)
        e2 = Add(vx, vy)
        e3 = GreaterThan(n1, n2)
        e4 = GreaterThan(vx, vy)
        s1 = Assign('a', e1)
        s2 = Assign('b', e2)
        s3 = Assign('c', e3)
        s4 = Assign('d', e4)

        # Initialize some statements
        #
        sa1 = Sequence(s1, s2)
        sa2 = Sequence(Sequence(s1, s2), s3)
        sa3 = Sequence(Sequence(Sequence(s1, s2), s3), s4)

        # Check value strings
        #
        self.assertEqual("a = 1 + 2; b = x + y;", str(sa1))
        self.assertEqual("a = 1 + 2; b = x + y; c = 1 > 2;", str(sa2))
        self.assertEqual("a = 1 + 2; b = x + y; c = 1 > 2; d = x > y;",
                         str(sa3))
Пример #8
0
    def test_and_xny(self):
        """Test parsing logical and expression x && !y."""
        ast = parse("x && !y", p.Expression)
        c = compose(ast, indent="  ", autoblank=False)
        e = ast.to_simple()

        self.assertEqual("x && !y", c)
        self.assertEqual(And(Variable('x'), Not(Variable('y'))), e)
Пример #9
0
    def test_lessthan_xy(self):
        """Test parsing less than expression x < y."""
        ast = parse("x < y", p.Expression)
        c = compose(ast, indent="  ", autoblank=False)
        e = ast.to_simple()

        self.assertEqual("x < y", c)
        self.assertEqual(LessThan(Variable('x'), Variable('y')), e)
Пример #10
0
    def test_subtraction_xy(self):
        """Test parsing subtraction expression x - y."""
        ast = parse("x - y", p.Expression)
        c = compose(ast, indent="  ", autoblank=False)
        e = ast.to_simple()

        self.assertEqual("x - y", c)
        self.assertEqual(Subtract(Variable('x'), Variable('y')), e)
Пример #11
0
    def test_multiplication_xy(self):
        """Test parsing multiplication expression x * y."""
        ast = parse("x * y", p.Expression)
        c = compose(ast, indent="  ", autoblank=False)
        e = ast.to_simple()

        self.assertEqual("x * y", c)
        self.assertEqual(Multiply(Variable('x'), Variable('y')), e)
Пример #12
0
    def test_division_xy(self):
        """Test parsing division expression x / y."""
        ast = parse("x / y", p.Expression)
        c = compose(ast, indent="  ", autoblank=False)
        e = ast.to_simple()

        self.assertEqual("x / y", c)
        self.assertEqual(Divide(Variable('x'), Variable('y')), e)
Пример #13
0
    def test_assign_xyz(self):
        """Test parsing assignment statement."""
        ast = parse("x = y + z;", p.Assign)
        c = compose(ast, indent="  ", autoblank=False)
        s = ast.to_simple()

        self.assertEqual("x = y + z;", c)
        self.assertEqual(Assign('x', Add(Variable('y'), Variable('z'))), s)
Пример #14
0
    def test_addition_xy(self):
        """Test parsing addition expression x + y."""
        ast = parse("x + y", p.Expression)
        c = compose(ast, indent="  ", autoblank=False)
        e = ast.to_simple()

        self.assertEqual("x + y", c)
        self.assertEqual(Add(Variable('x'), Variable('y')), e)
Пример #15
0
    def test_or_nxy(self):
        """Test parsing logical or expression !x || y."""
        ast = parse("!x || y", p.Expression)
        c = compose(ast, indent="  ", autoblank=False)
        e = ast.to_simple()

        self.assertEqual("!x || y", c)
        self.assertEqual(Or(Not(Variable('x')), Variable('y')), e)
    def test_sequence_evaluate(self):
        """Check Sequence.evaluate()."""
        # Initialize some numbers, bools, variables, expressions,
        # statements
        #
        n1 = Number(1)
        n2 = Number(2)
        vx = Variable('x')
        vy = Variable('y')
        e1 = Add(n1, n2)
        e2 = Add(vx, vy)
        e3 = GreaterThan(n1, n2)
        e4 = GreaterThan(vx, vy)
        s1 = Assign('a', e1)
        s2 = Assign('b', e2)
        s3 = Assign('c', e3)
        s4 = Assign('d', e4)

        # Initialize some statements
        #
        sa1 = Sequence(s1, s2)
        sa2 = Sequence(Sequence(s1, s2), s3)
        sa3 = Sequence(Sequence(Sequence(s1, s2), s3), s4)

        # Evaluate the add objects
        #
        env = dict([('x', Number(5.2)), ('y', Number(3.4))])
        sa1e = sa1.evaluate(env)
        sa2e = sa2.evaluate(env)
        sa3e = sa3.evaluate(env)

        # check the results
        #
        self.assertIn('a', sa1e)
        self.assertIn('b', sa1e)
        self.assertNotIn('c', sa1e)
        self.assertNotIn('d', sa1e)
        self.assertEqual(Number(1 + 2), sa1e['a'])
        self.assertEqual(Number(5.2 + 3.4), sa1e['b'])

        self.assertIn('a', sa2e)
        self.assertIn('b', sa2e)
        self.assertIn('c', sa2e)
        self.assertNotIn('d', sa2e)
        self.assertEqual(Number(1 + 2), sa2e['a'])
        self.assertEqual(Number(5.2 + 3.4), sa2e['b'])
        self.assertEqual(Boolean(1 > 2), sa2e['c'])

        self.assertIn('a', sa3e)
        self.assertIn('b', sa3e)
        self.assertIn('c', sa3e)
        self.assertIn('d', sa3e)
        self.assertEqual(Number(1 + 2), sa3e['a'])
        self.assertEqual(Number(5.2 + 3.4), sa3e['b'])
        self.assertEqual(Boolean(1 > 2), sa3e['c'])
        self.assertEqual(Boolean(5.2 > 3.4), sa3e['d'])
    def test_if_init(self):
        """Check If.__init__()."""
        # Initialize some numbers, bools, variables, expressions,
        # statements
        #
        n1 = Number(1)
        n2 = Number(2)
        vx = Variable('x')
        vy = Variable('y')
        e1 = Add(n1, n2)
        e2 = Add(vx, vy)
        e3 = GreaterThan(n1, n2)
        e4 = GreaterThan(vx, vy)
        s1 = Assign('a', e1)
        s2 = Assign('b', e2)
        s3 = Assign('c', e3)
        s4 = Assign('d', e4)
        s5 = Sequence(s1, s2)
        s6 = Sequence(s3, s4)

        # Initialize some statements
        #
        sa1 = If(e3, s1, s2)
        sa2 = If(e4, s3, s4)
        sa3 = If(Boolean(True), s5, s6)
        sa4 = If(Boolean(False), s5, s6)
        sa5 = If(e3, s5, s6)
        sa6 = If(e4, sa1, sa2)

        # Check the initialization
        #
        self.assertEqual(e3, sa1.condition)
        self.assertEqual(s1, sa1.consequence)
        self.assertEqual(s2, sa1.alternative)

        self.assertEqual(e4, sa2.condition)
        self.assertEqual(s3, sa2.consequence)
        self.assertEqual(s4, sa2.alternative)

        self.assertEqual(Boolean(True), sa3.condition)
        self.assertEqual(s5, sa3.consequence)
        self.assertEqual(s6, sa3.alternative)

        self.assertEqual(Boolean(False), sa4.condition)
        self.assertEqual(s5, sa4.consequence)
        self.assertEqual(s6, sa4.alternative)

        self.assertEqual(e3, sa5.condition)
        self.assertEqual(s5, sa5.consequence)
        self.assertEqual(s6, sa5.alternative)

        self.assertEqual(e4, sa6.condition)
        self.assertEqual(sa1, sa6.consequence)
        self.assertEqual(sa2, sa6.alternative)
    def test_if_ne(self):
        """Check If.__ne__()."""
        # Initialize some numbers, bools, variables, expressions,
        # statements
        #
        n1 = Number(1)
        n2 = Number(2)
        vx = Variable('x')
        vy = Variable('y')
        e1 = Add(n1, n2)
        e2 = Add(vx, vy)
        e3 = GreaterThan(n1, n2)
        e4 = GreaterThan(vx, vy)
        s1 = Assign('a', e1)
        s2 = Assign('b', e2)
        s3 = Assign('c', e3)
        s4 = Assign('d', e4)
        s5 = Sequence(s1, s2)
        s6 = Sequence(s3, s4)

        # Initialize some statements
        #
        sa1 = If(e3, s1, s2)
        sa2 = If(e4, s5, s6)
        sa3 = If(e3, sa1, sa2)
        sa4 = If(e4, sa1, sa2)
        sb1 = If(e3, s1, s2)
        sb2 = If(e4, s5, s6)
        sb3 = If(e3, sb1, sb2)
        sb4 = If(e4, sb1, sb2)

        # Check equality with itself
        #
        self.assertFalse(sa1 != sa1)
        self.assertFalse(sa2 != sa2)
        self.assertFalse(sa3 != sa3)
        self.assertFalse(sa4 != sa4)

        # Check equality with same value but different objects
        #
        self.assertFalse(sa1 != sb1)
        self.assertFalse(sa2 != sb2)
        self.assertFalse(sa3 != sb3)
        self.assertFalse(sa4 != sb4)

        # Check equality with different value
        #
        self.assertTrue(sa1 != sa4)
        self.assertTrue(sa2 != sa1)
        self.assertTrue(sa3 != sa2)
        self.assertTrue(sa4 != sa3)
    def test_while_evaluate(self):
        """Check While.evaluate()."""
        # Initialize some numbers, bools, variables, expressions,
        # statements
        #
        n1 = Number(1)
        n3 = Number(3)
        n3m = Number(-3)
        va = Variable('a')
        vb = Variable('b')
        e1 = Add(va, n1)
        e2 = Add(vb, va)
        e3 = Subtract(va, n1)
        e4 = LessThan(va, n3)
        e5 = GreaterThan(va, n3m)
        s1 = Assign('a', e1)
        s2 = Assign('b', e2)
        s3 = Assign('a', e3)
        s4 = Sequence(s1, s2)
        s5 = Sequence(s3, s2)

        # Initialize some statements
        #
        sa1 = While(e4, s4)
        sa2 = While(e5, s5)
        sa3 = While(Boolean(False), s4)

        # Evaluate the add objects
        #
        env = dict([('a', Number(0)), ('b', Number(0))])
        sa1e = sa1.evaluate(env)
        sa2e = sa2.evaluate(env)
        sa3e = sa3.evaluate(env)

        # check the results
        #
        self.assertIn('a', sa1e)
        self.assertIn('b', sa1e)
        self.assertEqual(Number(3), sa1e['a'])
        self.assertEqual(Number(6), sa1e['b'])

        self.assertIn('a', sa1e)
        self.assertIn('b', sa1e)
        self.assertEqual(Number(-3), sa2e['a'])
        self.assertEqual(Number(-6), sa2e['b'])

        self.assertIn('a', sa1e)
        self.assertIn('b', sa1e)
        self.assertEqual(Number(0), sa3e['a'])
        self.assertEqual(Number(0), sa3e['b'])
    def test_if_repr(self):
        """Check If.__repr__()."""
        # Initialize some numbers, bools, variables, expressions,
        # statements
        #
        n1 = Number(1)
        n2 = Number(2)
        vx = Variable('x')
        vy = Variable('y')
        e1 = Add(n1, n2)
        e2 = Add(vx, vy)
        e3 = GreaterThan(n1, n2)
        e4 = GreaterThan(vx, vy)
        s1 = Assign('a', e1)
        s2 = Assign('b', e2)
        s3 = Assign('c', e3)
        s4 = Assign('d', e4)
        s5 = Sequence(s1, s2)
        s6 = Sequence(s3, s4)

        # Initialize some statements
        #
        sa1 = If(e3, s1, s2)
        sa2 = If(e4, s3, s4)
        sa3 = If(Boolean(True), s5, s6)
        sa4 = If(Boolean(False), s5, s6)
        sa5 = If(e3, s5, s6)
        sa6 = If(e4, sa1, sa2)

        # Check value strings
        #
        self.assertEqual("«if (1 > 2) { a = 1 + 2; } else { b = x + y; }»",
                         repr(sa1))
        self.assertEqual("«if (x > y) { c = 1 > 2; } else { d = x > y; }»",
                         repr(sa2))
        self.assertEqual(
            "«if (true) { a = 1 + 2; b = x + y; } " +
            "else { c = 1 > 2; d = x > y; }»", repr(sa3))
        self.assertEqual(
            "«if (false) { a = 1 + 2; b = x + y; } " +
            "else { c = 1 > 2; d = x > y; }»", repr(sa4))
        self.assertEqual(
            "«if (1 > 2) { a = 1 + 2; b = x + y; } " +
            "else { c = 1 > 2; d = x > y; }»", repr(sa5))
        self.assertEqual(
            "«if (x > y) { if (1 > 2) { a = 1 + 2; } else { b = x + y; } } " +
            "else { if (x > y) { c = 1 > 2; } else { d = x > y; } }»",
            repr(sa6))
    def test_while_ne(self):
        """Check While.__ne__()."""
        # Initialize some numbers, bools, variables, expressions,
        # statements
        #
        n1 = Number(1)
        n3 = Number(3)
        n3m = Number(-3)
        va = Variable('a')
        vb = Variable('b')
        e1 = Add(va, n1)
        e2 = Add(vb, va)
        e3 = Subtract(va, n1)
        e4 = LessThan(va, n3)
        e5 = GreaterThan(va, n3m)
        s1 = Assign('a', e1)
        s2 = Assign('b', e2)
        s3 = Assign('a', e3)
        s4 = Sequence(s1, s2)
        s5 = Sequence(s3, s2)

        # Initialize some statements
        #
        sa1 = While(e4, s4)
        sa2 = While(e5, s5)
        sa3 = While(Boolean(False), s4)
        sb1 = While(e4, s4)
        sb2 = While(e5, s5)
        sb3 = While(Boolean(False), s4)

        # Check equality with itself
        #
        self.assertFalse(sa1 != sa1)
        self.assertFalse(sa2 != sa2)
        self.assertFalse(sa3 != sa3)

        # Check equality with same value but different objects
        #
        self.assertFalse(sa1 != sb1)
        self.assertFalse(sa2 != sb2)
        self.assertFalse(sa3 != sb3)

        # Check equality with different value
        #
        self.assertTrue(sa1 != sa3)
        self.assertTrue(sa2 != sa1)
        self.assertTrue(sa3 != sa2)
Пример #22
0
    def test_subtraction_15x(self):
        """Test parsing subtraction expression 1.5 - x."""
        ast = parse("1.5 - x", p.Expression)
        c = compose(ast, indent="  ", autoblank=False)
        e = ast.to_simple()

        self.assertEqual("1.5 - x", c)
        self.assertEqual(Subtract(Number(1.5), Variable('x')), e)
Пример #23
0
    def test_greaterthan_x25(self):
        """Test parsing greater than expression x > 2.5."""
        ast = parse("x > 2.5", p.Expression)
        c = compose(ast, indent="  ", autoblank=False)
        e = ast.to_simple()

        self.assertEqual("x > 2.5", c)
        self.assertEqual(GreaterThan(Variable('x'), Number(2.5)), e)
Пример #24
0
    def test_and_1my(self):
        """Test parsing logical and expression -1 && y."""
        ast = parse("-1 && y", p.Expression)
        c = compose(ast, indent="  ", autoblank=False)
        e = ast.to_simple()

        self.assertEqual("-1 && y", c)
        self.assertEqual(And(Number(-1), Variable('y')), e)
Пример #25
0
    def test_not_x(self):
        """Test parsing logical negation expression !x."""
        ast = parse("!x", p.Expression)
        c = compose(ast, indent="  ", autoblank=False)
        e = ast.to_simple()

        self.assertEqual("!x", c)
        self.assertEqual(Not(Variable('x')), e)
Пример #26
0
    def test_while_tx1yx2y(self):
        """Test parsing while statement whil (x > y) {...}."""
        expected = "while (x > y)\n"
        expected += "{\n"
        expected += "  x = 1;\n"
        expected += "  y = x * 3;\n"
        expected += "}"
        ast = parse(expected, p.While)
        c = compose(ast, indent="  ", autoblank=False)
        e = ast.to_simple()

        self.assertEqual(expected, c)
        self.assertEqual(
            While(
                GreaterThan(Variable('x'), Variable('y')),
                Sequence(Assign('x', Number(1)),
                         Assign('y', Multiply(Variable('x'), Number(3))))), e)
Пример #27
0
    def test_multiplication_15x(self):
        """Test parsing multiplication expression 1.5 * x."""
        ast = parse("1.5 * x", p.Expression)
        c = compose(ast, indent="  ", autoblank=False)
        e = ast.to_simple()

        self.assertEqual("1.5 * x", c)
        self.assertEqual(Multiply(Number(1.5), Variable('x')), e)
Пример #28
0
    def test_lessthan_15x(self):
        """Test parsing less than expression 1.5 < x."""
        ast = parse("1.5 < x", p.Expression)
        c = compose(ast, indent="  ", autoblank=False)
        e = ast.to_simple()

        self.assertEqual("1.5 < x", c)
        self.assertEqual(LessThan(Number(1.5), Variable('x')), e)
Пример #29
0
    def test_variable_mixedcase(self):
        """Test parsing variable MixedCase."""
        ast = parse("MixedCase", p.term_expression)
        c = compose(ast, indent="  ", autoblank=False)
        v = ast.to_simple()

        self.assertEqual("MixedCase", c)
        self.assertEqual(Variable('MixedCase'), v)
Пример #30
0
    def test_or_xf(self):
        """Test parsing logical or expression x || false."""
        ast = parse("x || false", p.Expression)
        c = compose(ast, indent="  ", autoblank=False)
        e = ast.to_simple()

        self.assertEqual("x || false", c)
        self.assertEqual(Or(Variable('x'), Boolean(False)), e)