[pypy-svn] pypy default: start to write unit tests for match()

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


Author: Antonio Cuni <anto.cuni at gmail.com>
Branch: 
Changeset: r42261:ceaa2b31c3c8
Date: 2011-02-24 15:49 +0100
http://bitbucket.org/pypy/pypy/changeset/ceaa2b31c3c8/

Log:	start to write unit tests for match()

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
@@ -178,7 +178,8 @@
         args = map(str.strip, args)
         return opname, resvar, args
 
-    def preprocess_expected_src(self, src):
+    @classmethod
+    def preprocess_expected_src(cls, src):
         # all loops decrement the tick-counter at the end. The rpython code is
         # in jump_absolute() in pypyjit/interp.py. The string --TICK-- is
         # replaced with the corresponding operations, so that tests don't have
@@ -206,11 +207,12 @@
             return alpha_map[v1] == v2
         return match_var
 
-    def match_ops(self, ops, expected_src):
-        expected_src = self.preprocess_expected_src(expected_src)
-        match_var = self._get_match_var()
+    @classmethod
+    def match_ops(cls, ops, expected_src):
+        expected_src = cls.preprocess_expected_src(expected_src)
+        match_var = cls._get_match_var()
         #
-        expected_ops = self.parse_ops(expected_src)
+        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):
             assert op.name == exp_opname

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
@@ -79,6 +79,17 @@
         opcodes_names = [opcode.__class__.__name__ for opcode in myline]
         assert opcodes_names == ['LOAD_FAST', 'LOAD_CONST', 'BINARY_ADD', 'STORE_FAST']
 
+class TestMath(object):
+
+    def match(self, src1, src2):
+        """Wrapper around LoopWithIds.match_ops"""
+        from pypy.tool.jitlogparser.parser import parse
+        loop = parse(src1)
+        try:
+            return LoopWithIds.match_ops(loop.operations, src2)
+        except AssertionError:
+            return False
+
     def test_match_var(self):
         match_var = LoopWithIds._get_match_var()
         assert match_var('v0', 'V0')
@@ -95,6 +106,25 @@
         assert not match_var('ConstClass(bar)', 'v1')
         assert not match_var('v2', 'ConstClass(baz)')
 
+    def test_match(self):
+        loop = """
+            [i0]
+            i2 = int_add(i0, 1)
+            jump(i2)
+        """
+        expected = """
+            i5 = int_add(i2, 1)
+            jump(i5)
+        """
+        assert self.match(loop, expected)
+        #
+        expected = """
+            i5 = int_sub(i2, 1)
+            jump(i5)
+        """
+        assert not self.match(loop, expected)
+
+
 class TestRunPyPyC(BaseTestPyPyC):
 
     def test_run_function(self):


More information about the Pypy-commit mailing list