[pypy-svn] pypy default: move all the matching-related functions to theirs own class, to ease testing

antocuni commits-noreply at bitbucket.org
Thu Feb 24 18:25:36 CET 2011


Author: Antonio Cuni <anto.cuni at gmail.com>
Branch: 
Changeset: r42262:45839e0a80b2
Date: 2011-02-24 16:18 +0100
http://bitbucket.org/pypy/pypy/changeset/45839e0a80b2/

Log:	move all the matching-related functions to theirs own class, to ease
	testing

diff --git a/pypy/module/pypyjit/test_pypy_c/model.py b/pypy/module/pypyjit/test_pypy_c/model.py
--- a/pypy/module/pypyjit/test_pypy_c/model.py
+++ b/pypy/module/pypyjit/test_pypy_c/model.py
@@ -150,6 +150,22 @@
                 for op in self._ops_for_chunk(chunk, include_debug_merge_points):
                     yield op
 
+    def match(self, expected_src):
+        ops = list(self.allops())
+        matcher = OpMatcher(ops)
+        return matcher.match(expected_src)
+
+    def match_by_id(self, id, expected_src):
+        ops = list(self.ops_by_id(id))
+        matcher = OpMatcher(ops)
+        return matcher.match(expected_src)
+
+
+class OpMatcher(object):
+
+    def __init__(self, ops):
+        self.ops = ops
+        self.alpha_map = None
 
     @classmethod
     def parse_ops(cls, src):
@@ -207,14 +223,13 @@
             return alpha_map[v1] == v2
         return match_var
 
-    @classmethod
-    def match_ops(cls, ops, expected_src):
-        expected_src = cls.preprocess_expected_src(expected_src)
-        match_var = cls._get_match_var()
+    def match(self, expected_src):
+        expected_src = self.preprocess_expected_src(expected_src)
+        match_var = self._get_match_var()
         #
-        expected_ops = cls.parse_ops(expected_src)
-        assert len(ops) == len(expected_ops), "wrong number of operations"
-        for op, (exp_opname, exp_res, exp_args) in zip(ops, expected_ops):
+        expected_ops = self.parse_ops(expected_src)
+        assert len(self.ops) == len(expected_ops), "wrong number of operations"
+        for op, (exp_opname, exp_res, exp_args) in zip(self.ops, expected_ops):
             assert op.name == exp_opname
             match_var(op.res, exp_res)
             assert len(op.args) == len(exp_args), "wrong number of arguments"
@@ -222,10 +237,3 @@
                 assert match_var(arg, exp_arg), "variable mismatch"
         return True
 
-    def match(self, expected_src):
-        ops = list(self.allops())
-        return self.match_ops(ops, expected_src)
-
-    def match_by_id(self, id, expected_src):
-        ops = list(self.ops_by_id(id))
-        return self.match_ops(ops, expected_src)

diff --git a/pypy/module/pypyjit/test_pypy_c/test_model.py b/pypy/module/pypyjit/test_pypy_c/test_model.py
--- a/pypy/module/pypyjit/test_pypy_c/test_model.py
+++ b/pypy/module/pypyjit/test_pypy_c/test_model.py
@@ -4,7 +4,8 @@
 from lib_pypy import disassembler
 from pypy.tool.udir import udir
 from pypy.tool import logparser
-from pypy.module.pypyjit.test_pypy_c.model import Log, find_ids_range, find_ids, LoopWithIds
+from pypy.module.pypyjit.test_pypy_c.model import Log, find_ids_range, find_ids, \
+    LoopWithIds, OpMatcher
 
 class BaseTestPyPyC(object):
     def setup_class(cls):
@@ -79,19 +80,20 @@
         opcodes_names = [opcode.__class__.__name__ for opcode in myline]
         assert opcodes_names == ['LOAD_FAST', 'LOAD_CONST', 'BINARY_ADD', 'STORE_FAST']
 
-class TestMath(object):
+class TestOpMatcher(object):
 
     def match(self, src1, src2):
         """Wrapper around LoopWithIds.match_ops"""
         from pypy.tool.jitlogparser.parser import parse
         loop = parse(src1)
+        matcher = OpMatcher(loop.operations)
         try:
-            return LoopWithIds.match_ops(loop.operations, src2)
+            return matcher.match(src2)
         except AssertionError:
             return False
 
     def test_match_var(self):
-        match_var = LoopWithIds._get_match_var()
+        match_var = OpMatcher._get_match_var()
         assert match_var('v0', 'V0')
         assert not match_var('v0', 'V1')
         assert match_var('v0', 'V0')
@@ -106,7 +108,13 @@
         assert not match_var('ConstClass(bar)', 'v1')
         assert not match_var('v2', 'ConstClass(baz)')
 
-    def test_match(self):
+    def test_parse_op(self):
+        res = OpMatcher.parse_op("  a =   int_add(  b,  3 ) # foo")
+        assert res == ("int_add", "a", ["b", "3"])
+        res = OpMatcher.parse_op("guard_true(a)")
+        assert res == ("guard_true", None, ["a"])
+
+    def test_exact_match(self):
         loop = """
             [i0]
             i2 = int_add(i0, 1)
@@ -123,6 +131,32 @@
             jump(i5)
         """
         assert not self.match(loop, expected)
+        #
+        expected = """
+            i5 = int_sub(i2, 1)
+            jump(i5)
+            extra_stuff(i5)
+        """
+        assert not self.match(loop, expected)
+
+
+    def test_partial_match(self):
+        py.test.skip('in-progress')
+        loop = """
+            [i0]
+            i1 = int_add(i0, 1)
+            i2 = int_sub(i1, 10)
+            i3 = int_floordiv(i2, 100)
+            i4 = int_mul(i1, 1000)
+            jump(i3)
+        """
+        expected = """
+            i1 = int_add(0, 1)
+            ...
+            i4 = int_mul(i1, 1000)
+        """
+        assert self.match(loop, expected)
+
 
 
 class TestRunPyPyC(BaseTestPyPyC):
@@ -235,12 +269,6 @@
             'jump'
             ]
 
-    def test_parse_op(self):
-        res = LoopWithIds.parse_op("  a =   int_add(  b,  3 ) # foo")
-        assert res == ("int_add", "a", ["b", "3"])
-        res = LoopWithIds.parse_op("guard_true(a)")
-        assert res == ("guard_true", None, ["a"])
-
     def test_match(self):
         def f():
             i = 0


More information about the Pypy-commit mailing list