Exemplo n.º 1
0
    def test_order_rewrite_literal(self):
        "Tests rewrite of literal values"
        # Model the predicate:
        # foo < bar or foo >= bar or foo > zip or foo <= zip
        l = ast.Literal('foo')
        v = ast.Literal('bar')
        v2 = ast.Literal('zip')

        cmp1 = ast.CompareOperator('<', l, v)
        cmp2 = ast.CompareOperator('>=', l, v)
        cmp3 = ast.CompareOperator('>', l, v2)
        cmp4 = ast.CompareOperator('<=', l, v2)
        or1 = ast.LogicalOperator('or', cmp1, cmp2)
        or2 = ast.LogicalOperator('or', cmp3, cmp4)
        or3 = ast.LogicalOperator('or', or1, or2)

        # Rewrite foo < bar as True
        name = merge.node_name(cmp1, True)
        r = compare.order_rewrite(ast.dup(or3), name, cmp1, True)

        assert isinstance(r.left.left, ast.Constant)
        assert r.left.left.value == True
        assert isinstance(r.left.right, ast.Constant)
        assert r.left.right.value == False
        assert ASTPattern(or2).matches(r.right)

        # Rewrite foo < bar as False
        name = merge.node_name(cmp1, True)
        r = compare.order_rewrite(ast.dup(or3), name, cmp1, False)

        assert isinstance(r.left.left, ast.Constant)
        assert r.left.left.value == False
        assert isinstance(r.left.right, ast.Constant)
        assert r.left.right.value == True
        assert ASTPattern(or2).matches(r.right)
Exemplo n.º 2
0
    def test_order_rewrite_literal(self):
        "Tests rewrite of literal values"
        # Model the predicate:
        # foo < bar or foo >= bar or foo > zip or foo <= zip
        l = ast.Literal('foo')
        v = ast.Literal('bar')
        v2 = ast.Literal('zip')

        cmp1 = ast.CompareOperator('<', l, v)
        cmp2 = ast.CompareOperator('>=', l, v)
        cmp3 = ast.CompareOperator('>', l, v2)
        cmp4 = ast.CompareOperator('<=', l, v2)
        or1 = ast.LogicalOperator('or', cmp1, cmp2)
        or2 = ast.LogicalOperator('or', cmp3, cmp4)
        or3 = ast.LogicalOperator('or', or1, or2)

        # Rewrite foo < bar as True
        name = merge.node_name(cmp1, True)
        r = compare.order_rewrite(ast.dup(or3), name, cmp1, True)

        assert isinstance(r.left.left, ast.Constant)
        assert r.left.left.value == True
        assert isinstance(r.left.right, ast.Constant)
        assert r.left.right.value == False
        assert ASTPattern(or2).matches(r.right)

        # Rewrite foo < bar as False
        name = merge.node_name(cmp1, True)
        r = compare.order_rewrite(ast.dup(or3), name, cmp1, False)

        assert isinstance(r.left.left, ast.Constant)
        assert r.left.left.value == False
        assert isinstance(r.left.right, ast.Constant)
        assert r.left.right.value == True
        assert ASTPattern(or2).matches(r.right)
Exemplo n.º 3
0
    def test_equality_rewrite_diff_static(self):
        "Test an equality rewrite with different static values"
        l = ast.Literal('foo')
        s = ast.Literal('"test"')
        s.static = True
        s.static_val = "test"

        s1 = ast.Literal('"other"')
        s1.static = True
        s1.static_val = "other"

        cmp1 = ast.CompareOperator('=', l, s)
        cmp2 = ast.CompareOperator('=', l, s1)
        or1 = ast.LogicalOperator('or', cmp1, cmp2)

        # Rewrite foo = "test" as True
        # Left should be True, right should be False
        name = merge.node_name(cmp1, True)
        r = compare.equality_rewrite(ast.dup(or1), name, cmp1, True)

        assert isinstance(r.left, ast.Constant)
        assert r.left.value == True
        assert isinstance(r.right, ast.Constant)
        assert r.right.value == False

        # Rewrite foo = "test" as False
        # Left should be False, right should be same
        name = merge.node_name(cmp1, True)
        r = compare.equality_rewrite(ast.dup(or1), name, cmp1, False)

        assert isinstance(r.left, ast.Constant)
        assert r.left.value == False
        assert ASTPattern(cmp2).matches(r.right)

        # Rewrite foo = "other" as True
        # Left should be False, right should be True
        name = merge.node_name(cmp2, True)
        r = compare.equality_rewrite(ast.dup(or1), name, cmp2, True)

        assert isinstance(r.left, ast.Constant)
        assert r.left.value == False
        assert isinstance(r.right, ast.Constant)
        assert r.right.value == True

        # Rewrite foo = "other" as False
        # Left should be same, right should be False
        name = merge.node_name(cmp2, True)
        r = compare.equality_rewrite(ast.dup(or1), name, cmp2, False)

        assert ASTPattern(cmp1).matches(r.left)
        assert isinstance(r.right, ast.Constant)
        assert r.right.value == False
Exemplo n.º 4
0
    def test_equality_rewrite_diff_static(self):
        "Test an equality rewrite with different static values"
        l = ast.Literal('foo')
        s = ast.Literal('"test"')
        s.static = True
        s.static_val = "test"

        s1 = ast.Literal('"other"')
        s1.static = True
        s1.static_val = "other"

        cmp1 = ast.CompareOperator('=', l, s)
        cmp2 = ast.CompareOperator('=', l, s1)
        or1 = ast.LogicalOperator('or', cmp1, cmp2)

        # Rewrite foo = "test" as True
        # Left should be True, right should be False
        name = merge.node_name(cmp1, True)
        r = compare.equality_rewrite(ast.dup(or1), name, cmp1, True)

        assert isinstance(r.left, ast.Constant)
        assert r.left.value == True
        assert isinstance(r.right, ast.Constant)
        assert r.right.value == False

        # Rewrite foo = "test" as False
        # Left should be False, right should be same
        name = merge.node_name(cmp1, True)
        r = compare.equality_rewrite(ast.dup(or1), name, cmp1, False)

        assert isinstance(r.left, ast.Constant)
        assert r.left.value == False
        assert ASTPattern(cmp2).matches(r.right)

        # Rewrite foo = "other" as True
        # Left should be False, right should be True
        name = merge.node_name(cmp2, True)
        r = compare.equality_rewrite(ast.dup(or1), name, cmp2, True)

        assert isinstance(r.left, ast.Constant)
        assert r.left.value == False
        assert isinstance(r.right, ast.Constant)
        assert r.right.value == True

        # Rewrite foo = "other" as False
        # Left should be same, right should be False
        name = merge.node_name(cmp2, True)
        r = compare.equality_rewrite(ast.dup(or1), name, cmp2, False)

        assert ASTPattern(cmp1).matches(r.left)
        assert isinstance(r.right, ast.Constant)
        assert r.right.value == False
Exemplo n.º 5
0
    def test_equality_rewrite_static(self):
        "Test an equality rewrite with static values"
        l = ast.Literal('foo')
        s = ast.Literal('"test"')
        s.static = True
        s.static_val = "test"
        cmp1 = ast.CompareOperator('=', l, s)
        cmp2 = ast.CompareOperator('!=', l, s)
        or1 = ast.LogicalOperator('or', cmp1, cmp2)

        # Rewrite foo = "test" as True
        # Left should be True, right should be False
        name = merge.node_name(cmp1, True)
        r = compare.equality_rewrite(ast.dup(or1), name, cmp1, True)

        assert isinstance(r.left, ast.Constant)
        assert r.left.value == True
        assert isinstance(r.right, ast.Constant)
        assert r.right.value == False

        # Rewrite foo = "test" as False
        # Left should be False, right should be True
        name = merge.node_name(cmp1, True)
        r = compare.equality_rewrite(ast.dup(or1), name, cmp1, False)

        assert isinstance(r.left, ast.Constant)
        assert r.left.value == False
        assert isinstance(r.right, ast.Constant)
        assert r.right.value == True

        # Rewrite foo != "test" as True
        # Left should be False, right should be True
        name = merge.node_name(cmp2, True)
        r = compare.equality_rewrite(ast.dup(or1), name, cmp2, True)

        assert isinstance(r.left, ast.Constant)
        assert r.left.value == False
        assert isinstance(r.right, ast.Constant)
        assert r.right.value == True

        # Rewrite foo != "test" as False
        # Left should be False, right should be True
        name = merge.node_name(cmp2, True)
        r = compare.equality_rewrite(ast.dup(or1), name, cmp2, False)

        assert isinstance(r.left, ast.Constant)
        assert r.left.value == True
        assert isinstance(r.right, ast.Constant)
        assert r.right.value == False
Exemplo n.º 6
0
    def test_equality_rewrite_static(self):
        "Test an equality rewrite with static values"
        l = ast.Literal('foo')
        s = ast.Literal('"test"')
        s.static = True
        s.static_val = "test"
        cmp1 = ast.CompareOperator('=', l, s)
        cmp2 = ast.CompareOperator('!=', l, s)
        or1 = ast.LogicalOperator('or', cmp1, cmp2)

        # Rewrite foo = "test" as True
        # Left should be True, right should be False
        name = merge.node_name(cmp1, True)
        r = compare.equality_rewrite(ast.dup(or1), name, cmp1, True)

        assert isinstance(r.left, ast.Constant)
        assert r.left.value == True
        assert isinstance(r.right, ast.Constant)
        assert r.right.value == False

        # Rewrite foo = "test" as False
        # Left should be False, right should be True
        name = merge.node_name(cmp1, True)
        r = compare.equality_rewrite(ast.dup(or1), name, cmp1, False)

        assert isinstance(r.left, ast.Constant)
        assert r.left.value == False
        assert isinstance(r.right, ast.Constant)
        assert r.right.value == True

        # Rewrite foo != "test" as True
        # Left should be False, right should be True
        name = merge.node_name(cmp2, True)
        r = compare.equality_rewrite(ast.dup(or1), name, cmp2, True)

        assert isinstance(r.left, ast.Constant)
        assert r.left.value == False
        assert isinstance(r.right, ast.Constant)
        assert r.right.value == True

        # Rewrite foo != "test" as False
        # Left should be False, right should be True
        name = merge.node_name(cmp2, True)
        r = compare.equality_rewrite(ast.dup(or1), name, cmp2, False)

        assert isinstance(r.left, ast.Constant)
        assert r.left.value == True
        assert isinstance(r.right, ast.Constant)
        assert r.right.value == False
Exemplo n.º 7
0
    def test_equality_rewrite_literals(self):
        "Test an equality rewrite with different literals"
        l = ast.Literal('foo')
        v = ast.Literal('bar')
        v2 = ast.Literal('baz')
        cmp1 = ast.CompareOperator('=', l, v)
        cmp2 = ast.CompareOperator('=', l, v2)
        or1 = ast.LogicalOperator('or', cmp1, cmp2)

        # Rewrite foo = bar as True
        # Left should be True, right should be unchanged
        name = merge.node_name(cmp1, True)
        r = compare.equality_rewrite(ast.dup(or1), name, cmp1, True)

        assert isinstance(r.left, ast.Constant)
        assert r.left.value == True
        assert ASTPattern(cmp2).matches(r.right)

        # Rewrite foo = bar as False
        # Left should be False, right should be same
        name = merge.node_name(cmp1, True)
        r = compare.equality_rewrite(ast.dup(or1), name, cmp1, False)

        assert isinstance(r.left, ast.Constant)
        assert r.left.value == False
        assert ASTPattern(cmp2).matches(r.right)

        # Rewrite foo = baz as True
        # Left should be same, right should be True
        name = merge.node_name(cmp2, True)
        r = compare.equality_rewrite(ast.dup(or1), name, cmp2, True)

        assert ASTPattern(cmp1).matches(r.left)
        assert isinstance(r.right, ast.Constant)
        assert r.right.value == True

        # Rewrite foo = baz as False
        # Left should be same, right should be False
        name = merge.node_name(cmp2, True)
        r = compare.equality_rewrite(ast.dup(or1), name, cmp2, False)

        assert ASTPattern(cmp1).matches(r.left)
        assert isinstance(r.right, ast.Constant)
        assert r.right.value == False
Exemplo n.º 8
0
    def test_equality_rewrite_literals(self):
        "Test an equality rewrite with different literals"
        l = ast.Literal('foo')
        v = ast.Literal('bar')
        v2 = ast.Literal('baz')
        cmp1 = ast.CompareOperator('=', l, v)
        cmp2 = ast.CompareOperator('=', l, v2)
        or1 = ast.LogicalOperator('or', cmp1, cmp2)

        # Rewrite foo = bar as True
        # Left should be True, right should be unchanged
        name = merge.node_name(cmp1, True)
        r = compare.equality_rewrite(ast.dup(or1), name, cmp1, True)

        assert isinstance(r.left, ast.Constant)
        assert r.left.value == True
        assert ASTPattern(cmp2).matches(r.right)

        # Rewrite foo = bar as False
        # Left should be False, right should be same
        name = merge.node_name(cmp1, True)
        r = compare.equality_rewrite(ast.dup(or1), name, cmp1, False)

        assert isinstance(r.left, ast.Constant)
        assert r.left.value == False
        assert ASTPattern(cmp2).matches(r.right)

        # Rewrite foo = baz as True
        # Left should be same, right should be True
        name = merge.node_name(cmp2, True)
        r = compare.equality_rewrite(ast.dup(or1), name, cmp2, True)

        assert ASTPattern(cmp1).matches(r.left)
        assert isinstance(r.right, ast.Constant)
        assert r.right.value == True

        # Rewrite foo = baz as False
        # Left should be same, right should be False
        name = merge.node_name(cmp2, True)
        r = compare.equality_rewrite(ast.dup(or1), name, cmp2, False)

        assert ASTPattern(cmp1).matches(r.left)
        assert isinstance(r.right, ast.Constant)
        assert r.right.value == False
Exemplo n.º 9
0
    def test_order_rewrite_numeric(self):
        "Tests rewrite of numeric values"
        # Model the predicate:
        # foo < 25 or foo >= 50 or foo > 75 or foo <= 100
        l = ast.Literal('foo')
        v = ast.Number(25)
        v1 = ast.Number(50)
        v2 = ast.Number(75)
        v3 = ast.Number(100)
        cmp1 = ast.CompareOperator('<', l, v)
        cmp2 = ast.CompareOperator('>=', l, v1)
        cmp3 = ast.CompareOperator('>', l, v2)
        cmp4 = ast.CompareOperator('<=', l, v3)
        or1 = ast.LogicalOperator('or', cmp1, cmp2)
        or2 = ast.LogicalOperator('or', cmp3, cmp4)
        or3 = ast.LogicalOperator('or', or1, or2)

        # Rewrite foo < 25 as true
        name = merge.node_name(cmp1, True)
        r = compare.order_rewrite(ast.dup(or3), name, cmp1, True)

        assert isinstance(r.left.left, ast.Constant)
        assert r.left.left.value == True
        assert isinstance(r.left.right, ast.Constant)
        assert r.left.right.value == False
        assert isinstance(r.right.left, ast.Constant)
        assert r.right.left.value == False
        assert isinstance(r.right.right, ast.Constant)
        assert r.right.right.value == True

        # Rewrite foo < 25 as false
        name = merge.node_name(cmp1, True)
        r = compare.order_rewrite(ast.dup(or3), name, cmp1, False)

        assert isinstance(r.left.left, ast.Constant)
        assert r.left.left.value == False

        # other cmps unchanges
        assert ASTPattern(cmp2).matches(r.left.right)
        assert ASTPattern(cmp3).matches(r.right.left)
        assert ASTPattern(cmp4).matches(r.right.right)

        # Rewrite foo >= 50 as true
        name = merge.node_name(cmp2, True)
        r = compare.order_rewrite(ast.dup(or3), name, cmp2, True)

        assert isinstance(r.left.left, ast.Constant)
        assert r.left.left.value == False
        assert isinstance(r.left.right, ast.Constant)
        assert r.left.right.value == True
        assert ASTPattern(cmp3).matches(r.right.left)
        assert ASTPattern(cmp4).matches(r.right.right)

        # Rewrite foo >= 50 as false
        name = merge.node_name(cmp2, True)
        r = compare.order_rewrite(ast.dup(or3), name, cmp2, False)

        assert ASTPattern(cmp1).matches(r.left.left)
        assert isinstance(r.left.right, ast.Constant)
        assert r.left.right.value == False
        assert isinstance(r.right.left, ast.Constant)
        assert r.right.left.value == False
        assert isinstance(r.right.right, ast.Constant)
        assert r.right.right.value == True

        # Rewrite foo > 75 as true
        name = merge.node_name(cmp3, True)
        r = compare.order_rewrite(ast.dup(or3), name, cmp3, True)

        assert isinstance(r.left.left, ast.Constant)
        assert r.left.left.value == False
        assert isinstance(r.left.right, ast.Constant)
        assert r.left.right.value == True
        assert isinstance(r.right.left, ast.Constant)
        assert r.right.left.value == True
        assert ASTPattern(cmp4).matches(r.right.right)

        # Rewrite foo > 75 as False
        name = merge.node_name(cmp3, True)
        r = compare.order_rewrite(ast.dup(or3), name, cmp3, False)

        assert ASTPattern(cmp1).matches(r.left.left)
        assert ASTPattern(cmp2).matches(r.left.right)
        assert isinstance(r.right.left, ast.Constant)
        assert r.right.left.value == False
        assert isinstance(r.right.right, ast.Constant)
        assert r.right.right.value == True

        # Rewrite foo <= 100 as True
        name = merge.node_name(cmp4, True)
        r = compare.order_rewrite(ast.dup(or3), name, cmp4, True)

        assert ASTPattern(cmp1).matches(r.left.left)
        assert ASTPattern(cmp2).matches(r.left.right)
        assert ASTPattern(cmp3).matches(r.right.left)
        assert isinstance(r.right.right, ast.Constant)
        assert r.right.right.value == True

        # Rewrite foo <= 100 as False
        name = merge.node_name(cmp4, True)
        r = compare.order_rewrite(ast.dup(or3), name, cmp4, False)

        assert isinstance(r.left.left, ast.Constant)
        assert r.left.left.value == False
        assert isinstance(r.left.right, ast.Constant)
        assert r.left.right.value == True
        assert isinstance(r.right.left, ast.Constant)
        assert r.right.left.value == True
        assert isinstance(r.right.right, ast.Constant)
        assert r.right.right.value == False
Exemplo n.º 10
0
    def test_order_rewrite_numeric(self):
        "Tests rewrite of numeric values"
        # Model the predicate:
        # foo < 25 or foo >= 50 or foo > 75 or foo <= 100
        l = ast.Literal('foo')
        v = ast.Number(25)
        v1 = ast.Number(50)
        v2 = ast.Number(75)
        v3 = ast.Number(100)
        cmp1 = ast.CompareOperator('<', l, v)
        cmp2 = ast.CompareOperator('>=', l, v1)
        cmp3 = ast.CompareOperator('>', l, v2)
        cmp4 = ast.CompareOperator('<=', l, v3)
        or1 = ast.LogicalOperator('or', cmp1, cmp2)
        or2 = ast.LogicalOperator('or', cmp3, cmp4)
        or3 = ast.LogicalOperator('or', or1, or2)

        # Rewrite foo < 25 as true
        name = merge.node_name(cmp1, True)
        r = compare.order_rewrite(ast.dup(or3), name, cmp1, True)

        assert isinstance(r.left.left, ast.Constant)
        assert r.left.left.value == True
        assert isinstance(r.left.right, ast.Constant)
        assert r.left.right.value == False
        assert isinstance(r.right.left, ast.Constant)
        assert r.right.left.value == False
        assert isinstance(r.right.right, ast.Constant)
        assert r.right.right.value == True

        # Rewrite foo < 25 as false
        name = merge.node_name(cmp1, True)
        r = compare.order_rewrite(ast.dup(or3), name, cmp1, False)

        assert isinstance(r.left.left, ast.Constant)
        assert r.left.left.value == False

        # other cmps unchanges
        assert ASTPattern(cmp2).matches(r.left.right)
        assert ASTPattern(cmp3).matches(r.right.left)
        assert ASTPattern(cmp4).matches(r.right.right)

        # Rewrite foo >= 50 as true
        name = merge.node_name(cmp2, True)
        r = compare.order_rewrite(ast.dup(or3), name, cmp2, True)

        assert isinstance(r.left.left, ast.Constant)
        assert r.left.left.value == False
        assert isinstance(r.left.right, ast.Constant)
        assert r.left.right.value == True
        assert ASTPattern(cmp3).matches(r.right.left)
        assert ASTPattern(cmp4).matches(r.right.right)

        # Rewrite foo >= 50 as false
        name = merge.node_name(cmp2, True)
        r = compare.order_rewrite(ast.dup(or3), name, cmp2, False)

        assert ASTPattern(cmp1).matches(r.left.left)
        assert isinstance(r.left.right, ast.Constant)
        assert r.left.right.value == False
        assert isinstance(r.right.left, ast.Constant)
        assert r.right.left.value == False
        assert isinstance(r.right.right, ast.Constant)
        assert r.right.right.value == True

        # Rewrite foo > 75 as true
        name = merge.node_name(cmp3, True)
        r = compare.order_rewrite(ast.dup(or3), name, cmp3, True)

        assert isinstance(r.left.left, ast.Constant)
        assert r.left.left.value == False
        assert isinstance(r.left.right, ast.Constant)
        assert r.left.right.value == True
        assert isinstance(r.right.left, ast.Constant)
        assert r.right.left.value == True
        assert ASTPattern(cmp4).matches(r.right.right)

        # Rewrite foo > 75 as False
        name = merge.node_name(cmp3, True)
        r = compare.order_rewrite(ast.dup(or3), name, cmp3, False)

        assert ASTPattern(cmp1).matches(r.left.left)
        assert ASTPattern(cmp2).matches(r.left.right)
        assert isinstance(r.right.left, ast.Constant)
        assert r.right.left.value == False
        assert isinstance(r.right.right, ast.Constant)
        assert r.right.right.value == True

        # Rewrite foo <= 100 as True
        name = merge.node_name(cmp4, True)
        r = compare.order_rewrite(ast.dup(or3), name, cmp4, True)

        assert ASTPattern(cmp1).matches(r.left.left)
        assert ASTPattern(cmp2).matches(r.left.right)
        assert ASTPattern(cmp3).matches(r.right.left)
        assert isinstance(r.right.right, ast.Constant)
        assert r.right.right.value == True

        # Rewrite foo <= 100 as False
        name = merge.node_name(cmp4, True)
        r = compare.order_rewrite(ast.dup(or3), name, cmp4, False)

        assert isinstance(r.left.left, ast.Constant)
        assert r.left.left.value == False
        assert isinstance(r.left.right, ast.Constant)
        assert r.left.right.value == True
        assert isinstance(r.right.left, ast.Constant)
        assert r.right.left.value == True
        assert isinstance(r.right.right, ast.Constant)
        assert r.right.right.value == False