def test_tile(self): p = tiler.SimplePattern('types:CompareOperator AND op:=', 'types:Literal', 'types:Literal') l1 = ast.Literal('foo') r1 = ast.Literal('bar') n1 = ast.CompareOperator('=', l1, r1) l2 = ast.Literal('zip') r2 = ast.Literal('baz') n2 = ast.CompareOperator('=', l2, r2) n = ast.LogicalOperator('or', n1, n2) i = {'count': 0} def func(pattern, node): assert pattern == p count = i['count'] if count == 0: assert node == n1 if count == 1: assert node == n2 i['count'] += 1 assert n == tiler.tile(n, [p], func) assert i['count'] == 2
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)
def test_simple_pattern_op_type(self): l = ast.Literal('foo') r = ast.Literal('foo') n = ast.CompareOperator('>', l, r) p = tiler.SimplePattern('types:CompareOperator AND op:=', 'types:Literal', 'types:Literal') assert not p.matches(n) n = ast.CompareOperator('=', l, r) assert p.matches(n)
def test_ast_pattern(self): l = ast.Literal('foo') b = ast.Literal('bar') c = ast.CompareOperator('is', l, b) p = tiler.ASTPattern(c) assert p.matches(c) # Change a node z = ast.Number(42) c1 = ast.CompareOperator('is', l, z) assert not p.matches(c1)
def test_select_rewrite_ord_literals(self): "Test rewrite selection for ordered with literals" l = ast.Literal('foo') v = ast.Literal('bar') v2 = ast.Literal('baz') cmp1 = ast.CompareOperator('>', l, v2) cmp2 = ast.CompareOperator('<', l, v) cmp3 = ast.CompareOperator('>', l, v) name = merge.node_name(cmp1, True) select = compare.select_rewrite_expression(name, [cmp1, cmp2, cmp3]) assert select is cmp2
def test_select_rewrite_eq(self): "Test rewrite selection for equality" l = ast.Literal('foo') v = ast.Number(42) v2 = ast.Number(11) cmp1 = ast.CompareOperator('=', l, v2) cmp2 = ast.CompareOperator('=', l, v) cmp3 = ast.CompareOperator('=', l, v) name = merge.node_name(cmp1, True) select = compare.select_rewrite_expression(name, [cmp1, cmp2, cmp3]) assert select is cmp2
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
def test_select_rewrite_ord_numeric(self): "Test rewrite selection for ordered with numerics" l = ast.Literal('foo') v = ast.Number(42) v2 = ast.Number(11) v3 = ast.Number(100) cmp1 = ast.CompareOperator('>', l, v) cmp2 = ast.CompareOperator('<', l, v2) cmp3 = ast.CompareOperator('>', l, v3) name = merge.node_name(cmp1, True) select = compare.select_rewrite_expression(name, [cmp1, cmp2, cmp3]) assert select is cmp1
def test_cache(self): l = ast.Literal('foo') v = ast.Number(42) gt1 = ast.CompareOperator('>', l, v) l1 = ast.Literal('foo') v1 = ast.Number(42) gt2 = ast.CompareOperator('>', l1, v1) n = ast.LogicalOperator('or', gt1, gt2) cache.cache_expressions(n) # Both sides should be dedupped assert n.left is n.right assert n.left.cache_id == 0
def test_compact(self): l = ast.Literal('foo') v = ast.Number(42) gt = ast.CompareOperator('>', l, v) l1 = ast.Literal('foo') v1 = ast.Number(42) lt = ast.CompareOperator('<', l1, v1) n = ast.LogicalOperator('or', gt, lt) compact.compact(n) # Literal and number should be de-dupped assert l is n.right.left assert v is n.right.right
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
def test_simple_pattern_value(self): l = ast.Literal('foo') r = ast.Constant(True) n = ast.CompareOperator('=', l, r) p = tiler.SimplePattern('types:CompareOperator AND ops:=', 'types:Literal', 'types:Constant AND value:True') assert p.matches(n)
def test_simple_pattern_ops(self): l = ast.Literal('foo') r = ast.Literal('foo') n = ast.CompareOperator('<=', l, r) p = tiler.SimplePattern('ops:>,>=,<,<=', 'types:Literal', 'types:Literal') assert p.matches(n)
def test_rewrite_compare(self): "Checks that a compare rewrite uses compare module" l = ast.Literal('foo') r = ast.Literal('bar') n = ast.CompareOperator('>', l, r) name = merge.node_name(n) with patch('pypred.merge.compare.compare_rewrite') as c: merge.rewrite_ast(n, name, n, True) assert c.called
def test_canonical_non_literal(self): "Test canonicalization, literals on left" v = ast.Number(42) l = ast.Literal('foo') cmp = ast.CompareOperator('=', v, l) compare.canonicalize(cmp) assert cmp.left is l assert cmp.right is v
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
def test_canonical_literal_order(self): "Test canonicalization, literals on left" l = ast.Literal('foo') r = ast.Literal('zip') cmp = ast.CompareOperator('>', r, l) compare.canonicalize(cmp) assert cmp.left is l assert cmp.right is r assert cmp.type == "<"
def test_select_expr_compare(self): "Checks that a compare operation uses compare module" l = ast.Literal('foo') r = ast.Literal('bar') n = ast.CompareOperator('>', l, r) name = merge.node_name(n) with patch('pypred.merge.compare.select_rewrite_expression') as c: merge.select_rewrite_expression(DEEP, name, [n]) assert c.called
def test_or_dead(self): "Tests removing OR with dead branch" f = ast.Constant(False) l = ast.Literal('foo') v = ast.Number(42) cmp = ast.CompareOperator('=', l, v) n = ast.LogicalOperator('or', f, cmp) # Should reduce to to the compare c, r = optimizer.optimization_pass(n) assert c == 1 assert r is cmp
def test_canonical_non_static(self): "Test canonicalization, literals on left" static = ast.Literal("'string'") static.static = True static.static_val = 'string' l = ast.Literal('foo') cmp = ast.CompareOperator('=', static, l) compare.canonicalize(cmp) assert cmp.left is l assert cmp.right is static
def test_and_dead_right(self): "Tests removing AND with dead branch, right side" t = ast.Constant(True) l = ast.Literal('foo') v = ast.Number(42) cmp = ast.CompareOperator('=', l, v) n = ast.LogicalOperator('and', cmp, t) # Should reduce to to the compare c, r = optimizer.optimization_pass(n) assert c == 1 assert r is cmp
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
def test_branch(self): l = ast.Literal('a') r = ast.Literal('b') check = ast.CompareOperator('>', l, r) true = ast.Constant(True) false = ast.Constant(False) b = ast.Branch(check, true, false) assert b.evaluate(MockPred(), {'a': 2, 'b':1}) res, ctx = b.analyze(MockPred(), {'a': 1, 'b':2}) assert not res assert ctx.literals["a"] == 1 assert ctx.literals["b"] == 2 assert ctx.failed[-1].startswith("Right hand side")
def test_compare_empty(self, type): l = ast.Literal("l") r = ast.Literal("r") a = ast.CompareOperator(type, l, r) d = {"l": 1, "r": ast.Empty()} res, ctx = a.analyze(MockPred(), d) # Determine the expected result if type == "!=": assert res else: assert not res if not res: assert ("%s comparison at" % type.upper()) in ctx.failed[0] assert ctx.literals["l"] == d["l"] assert ctx.literals["r"] == ast.Empty()
def test_canonical_static_order(self): "Test canonicalization, static ordering" static = ast.Literal("'string'") static.static = True static.static_val = 'string' static2 = ast.Literal("'foo'") static2.static = True static2.static_val = 'foo' cmp = ast.CompareOperator('<', static, static2) compare.canonicalize(cmp) assert cmp.left is static2 assert cmp.right is static assert cmp.type == ">"
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
def test_compare(self, type): l = ast.Literal("l") r = ast.Literal("r") a = ast.CompareOperator(type, l, r) d = {"l": 1, "r": 5} res, ctx = a.analyze(MockPred(), d) # Determine the expected result if type == "=": s = '%d %s %d' % (d["l"], "==", d["r"]) else: s = '%d %s %d' % (d["l"], type, d["r"]) expected = eval(s) assert res == expected if not res: assert ("%s comparison at" % type.upper()) in ctx.failed[0] assert ctx.literals["l"] == d["l"] assert ctx.literals["r"] == d["r"]
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
def test_bad_child(self): c = ast.CompareOperator("!", ast.Literal("foo"), ast.Empty()) a = ast.LogicalOperator("and", ast.Literal("foo"), c) valid, info = a.validate() assert not valid assert "Unknown compare" in info["errors"][0]
def test_bad_compare(self): a = ast.CompareOperator("!", ast.Literal("foo"), ast.Empty()) valid, info = a.validate() assert not valid assert "Unknown compare" in info["errors"][0]