コード例 #1
0
ファイル: test_contains.py プロジェクト: moaazsidat/pypred
    def test_select_rewrite(self):
        "Test rewrite selection"
        settings = merge.RefactorSettings.deep()
        s1 = ast.LiteralSet([ast.Number(1), ast.Number(2), ast.Number(3)])
        s2 = ast.LiteralSet([ast.Number(1)])
        s3 = ast.LiteralSet([ast.Number(1), ast.Number(2), ast.Number(3), ast.Number(4), ast.Number(5)])
        l = ast.Literal('foo')
        c1 = ast.ContainsOperator(s1, l)
        c2 = ast.ContainsOperator(s2, l)
        c3 = ast.ContainsOperator(s3, l)

        name = merge.node_name(c1, True)
        select = contains.select_rewrite_expression(settings, name, [c1, c2, c3])
        assert select is c1
コード例 #2
0
ファイル: test_optimizer.py プロジェクト: stuntgoat/pypred
    def test_branch_true_dead(self):
        "Test branch with the false branch being dead"
        t = ast.Constant(True)
        f = ast.Constant(False)
        l = ast.Literal('foo')
        v = ast.Number(42)
        cmp = ast.CompareOperator('=', l, v)
        n = ast.Branch(f, t, cmp)

        # Should reduce to to the compare
        c, r = optimizer.optimization_pass(n)
        assert c == 1
        assert r is cmp
コード例 #3
0
ファイル: test_merge.py プロジェクト: moaazsidat/pypred
    def test_names(self):
        n1 = ast.Literal("foo")
        assert ("Literal", "foo") == merge.node_name(n1)
        n2 = ast.Number(12)
        assert ("Number", 12) == merge.node_name(n2)
        n3 = ast.Constant(True)
        assert ("Constant", True) == merge.node_name(n3)
        n4 = ast.Regex("^tubez$")
        assert ("Regex", "^tubez$") == merge.node_name(n4)
        n5 = ast.Undefined()
        assert "Undefined" == merge.node_name(n5)
        n6 = ast.Empty()
        assert "Empty" == merge.node_name(n6)

        # Negate does not emit the operator!
        n7 = ast.NegateOperator(n3)
        assert ("Constant", True) == merge.node_name(n7)

        n8 = ast.CompareOperator('=', n1, n2)
        n8_name = merge.node_name(n8)
        assert ("CompareOperator", "=", ("Literal", "foo"), ("Number",
                                                             12)) == n8_name
        n8_static = merge.node_name(n8, True)
        assert ("CompareOperator", "equality", ("Literal", "foo"),
                ("Number", "static")) == n8_static

        n9 = ast.MatchOperator(n1, n4)
        n9_name = merge.node_name(n9)
        assert ("MatchOperator", ("Literal", "foo"), ("Regex",
                                                      "^tubez$")) == n9_name
        n10 = ast.ContainsOperator(n1, n2)
        n10_name = merge.node_name(n10)
        assert ("ContainsOperator", ("Literal", "foo"), ("Number",
                                                         12.0)) == n10_name

        # Logical operator returns literal!
        n11 = ast.LogicalOperator('and', n1, n3)
        n11_name = merge.node_name(n11)
        assert ("Literal", "foo") == n11_name

        # Literal set just uses name
        n12 = ast.LiteralSet([n1, n2])
        n12_name = merge.node_name(n12)
        assert "LiteralSet" == n12_name
コード例 #4
0
ファイル: test_compact.py プロジェクト: stuntgoat/pypred
    def test_names(self):
        n1 = ast.Literal("foo")
        assert ("Literal", "foo") == compact.node_name(n1)

        n2 = ast.Number(12)
        assert ("Number", 12) == compact.node_name(n2)

        n3 = ast.Constant(True)
        assert ("Constant", True) == compact.node_name(n3)

        n4 = ast.Regex("^tubez$")
        assert ("Regex", "^tubez$") == compact.node_name(n4)

        n5 = ast.Undefined()
        assert "Undefined" == compact.node_name(n5)

        n6 = ast.Empty()
        assert "Empty" == compact.node_name(n6)

        n7 = ast.NegateOperator(n3)
        assert ("NegateOperator", ("Constant", True)) == compact.node_name(n7)

        n8 = ast.CompareOperator('=', n1, n2)
        n8_name = compact.node_name(n8)
        assert ("CompareOperator", "=", ("Literal", "foo"), ("Number",
                                                             12)) == n8_name

        n9 = ast.MatchOperator(n1, n4)
        n9_name = compact.node_name(n9)
        assert ("MatchOperator", ("Literal", "foo"), ("Regex",
                                                      "^tubez$")) == n9_name

        n10 = ast.ContainsOperator(n1, n2)
        n10_name = compact.node_name(n10)
        assert ("ContainsOperator", ("Literal", "foo"), ("Number",
                                                         12.0)) == n10_name

        n11 = ast.LogicalOperator('and', n1, n3)
        n11_name = compact.node_name(n11)
        assert ("LogicalOperator", "and", ("Literal", "foo"),
                ("Constant", True)) == n11_name
コード例 #5
0
ファイル: test_contains.py プロジェクト: moaazsidat/pypred
    def test_contains_rewrite(self):
        s1 = ast.LiteralSet([ast.Number(1), ast.Number(2), ast.Number(3)])
        s2 = ast.LiteralSet([ast.Number(1)])
        s3 = ast.LiteralSet([ast.Number(1), ast.Number(2), ast.Number(3), ast.Number(4), ast.Number(5)])
        s4 = ast.LiteralSet([ast.Number(6)])
        s5 = ast.LiteralSet([ast.Number(2), ast.Number(3), ast.Number(4)])
        l = ast.Literal('foo')
        c1 = ast.ContainsOperator(s1, l)
        c2 = ast.ContainsOperator(s2, l)
        c3 = ast.ContainsOperator(s3, l)
        c4 = ast.ContainsOperator(s4, l)
        c5 = ast.ContainsOperator(s5, l)

        # Rewrite set1 as True, s3 is super set, should be True
        name = merge.node_name(c1, True)
        r = contains.contains_rewrite(c3, name, c1, True)
        assert isinstance(r, ast.Constant) and r.value == True

        # Rewrite set1 as False, s3 is super set, should be trimed
        name = merge.node_name(c1, True)
        r = contains.contains_rewrite(c3, name, c1, False)
        assert len(r.left.value) == 2
        assert ast.Number(4) in r.left.value
        assert ast.Number(5) in r.left.value

        # Rewrite set1 as True, s2 is sub set, should check value
        r = contains.contains_rewrite(c2, name, c1, True)
        assert len(r.left.value) == 1

        # Rewrite set1 as False, s2 is subset, should be false
        r = contains.contains_rewrite(c2, name, c1, False)
        assert isinstance(r, ast.Constant) and r.value == False

        # Rewrite set1 as True, s4 has no overlap, should be false
        r = contains.contains_rewrite(c4, name, c1, True)
        assert isinstance(r, ast.Constant) and r.value == False

        # Rewrite set1 as False, s4 is no overlap, should check
        r = contains.contains_rewrite(c4, name, c1, False)
        assert len(r.left.value) == 1
        assert ast.Number(6) in r.left.value

        # Rewrite set1 as True, s5 is has mostly overlap, should check
        # the negation of s1 - s5
        r = contains.contains_rewrite(c5, name, c1, True)
        assert isinstance(r, ast.NegateOperator)
        assert isinstance(r.left, ast.ContainsOperator)
        assert len(r.left.left.value)
        assert ast.Number(1) in r.left.left.value
コード例 #6
0
ファイル: test_ast.py プロジェクト: dungba88/pypred
 def test_bad_number(self):
     a = ast.Number("0..0")
     valid, info = a.validate()
     assert not valid
     assert "Failed to convert" in info["errors"][0]
コード例 #7
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