def generic_test_negative(self, input_string, patt_string, preproc=False): 'Generic test for negative matching' input_ast = ast.parse(input_string) pattern_ast = ast.parse(patt_string) if not preproc: pat = pattern_matcher.PatternMatcher(input_ast) self.assertFalse(pat.visit(input_ast, pattern_ast)) self.assertFalse(pattern_matcher.match(input_string, patt_string))
def test_with_nbits(self): 'Test with nbits given by the user' tests = [("(x ^ 52) + 2*(x | 203)", 8), ("(x ^ 789) + 2*(64746 | x)", 16)] for input_string, nbits in tests: input_ast = ast.parse(input_string) pattern_ast = ast.parse("(A ^ ~B) + 2*(A | B)") pat = pattern_matcher.PatternMatcher(input_ast, nbits) self.assertTrue(pat.visit(input_ast, pattern_ast))
def test_leveled(self): 'Test positive matchings for leveled ast' pattern_string = "A + 2*B + 3*C" test_pos = ["x + 2*y + 3*z", "3*z + 2*y + x", "2*y + 3*z + x"] for input_string in test_pos: self.generic_test_positive(input_string, pattern_string, True) # actual pre-processing only level ADD nodes, but this test is # for code coverage test_neg = ast.parse("x ^ 2*y ^ 2*z") test_neg = pre_processing.all_preprocessings(ast.parse(test_neg)) test_neg = asttools.LevelOperators().visit(test_neg) patt_ast = ast.parse(pattern_string) patt_ast = pre_processing.all_preprocessings(patt_ast) patt_ast = asttools.LevelOperators(ast.Add).visit(patt_ast) pat = pattern_matcher.PatternMatcher(test_neg) self.assertFalse(pat.visit(test_neg, patt_ast))
def test_root(self): 'Test with different types of roots' pattern_ast = ast.parse("A + B", mode='eval') input_ast = ast.parse("x + y", mode='eval') pat = pattern_matcher.PatternMatcher(input_ast) self.assertTrue(pat.visit(input_ast, pattern_ast))