[pypy-svn] r27361 - pypy/dist/pypy/rpython/test

antocuni at codespeak.net antocuni at codespeak.net
Wed May 17 14:59:07 CEST 2006


Author: antocuni
Date: Wed May 17 14:58:57 2006
New Revision: 27361

Added:
   pypy/dist/pypy/rpython/test/tool.py   (contents, props changed)
Modified:
   pypy/dist/pypy/rpython/test/test_rlist.py
   pypy/dist/pypy/rpython/test/test_rrange.py
   pypy/dist/pypy/rpython/test/test_rstr.py
   pypy/dist/pypy/rpython/test/test_rtuple.py
Log:
Added the module tool.py that contains some utility for writing tests
to be runned against both typesystem. Refactored tests for rrange,
rstr, rlist and rtuple for taking advantage of the new module.



Modified: pypy/dist/pypy/rpython/test/test_rlist.py
==============================================================================
--- pypy/dist/pypy/rpython/test/test_rlist.py	(original)
+++ pypy/dist/pypy/rpython/test/test_rlist.py	Wed May 17 14:58:57 2006
@@ -12,6 +12,7 @@
 from pypy.rpython.test.test_llinterp import interpret_raises
 from pypy.translator.translator import TranslationContext
 from pypy.objspace.flow.model import Constant, Variable
+from pypy.rpython.test.tool import BaseRtypingTest, LLRtypeMixin, OORtypeMixin
 
 # undo the specialization parameter
 for n1 in 'get set del'.split():
@@ -254,17 +255,7 @@
 
 
 
-class BaseTestListRtyping:
-
-    def interpret(self, fn, args):
-        return interpret(fn, args, type_system=self.ts)
-
-    def interpret_raises(self, exc, fn, args):
-        return interpret_raises(exc, fn, args, type_system=self.ts)
-
-    def _skip_oo(self, reason):
-        if self.ts == 'ootype':
-            py.test.skip("ootypesystem doesn't support %s, yet" % reason)
+class BaseTestRlist(BaseRtypingTest):
 
     def test_simple(self):
         def dummyfn():
@@ -1059,7 +1050,7 @@
 
         t = TranslationContext()
         s = t.buildannotator().build_types(f, [])
-        rtyper = t.buildrtyper(type_system=self.ts)
+        rtyper = t.buildrtyper(type_system=self.type_system)
         rtyper.specialize()
 
         s_A_list = s.items[0]
@@ -1087,7 +1078,7 @@
 
         t = TranslationContext()
         s = t.buildannotator().build_types(f, [])
-        rtyper = t.buildrtyper(type_system=self.ts)
+        rtyper = t.buildrtyper(type_system=self.type_system)
         rtyper.specialize()
 
         s_A_list = s.items[0]
@@ -1128,20 +1119,9 @@
         assert res == 77
 
 
-class TestLltypeRtyping(BaseTestListRtyping):
-
-    ts = "lltype"
+class TestLLtype(BaseTestRlist, LLRtypeMixin):
     rlist = ll_rlist
 
-    def ll_to_list(self, l):
-        return map(None, l.ll_items())[:l.ll_length()]
-
-    def ll_to_string(self, s):
-        return ''.join(s.chars)
-
-    def class_name(self, value):
-        return "".join(value.super.typeptr.name)[:-1]
-
     def test_memoryerror(self):
         def fn(i):
             lst = [0] * i
@@ -1154,16 +1134,5 @@
         self.interpret_raises(MemoryError, fn, [sys.maxint])
     
 
-class TestOotypeRtyping(BaseTestListRtyping):
-
-    ts = "ootype"
+class TestOOtype(BaseTestRlist, OORtypeMixin):
     rlist = oo_rlist
-
-    def ll_to_list(self, l):
-        return l._list[:]
-
-    def ll_to_string(self, s):
-        return s._str
-
-    def class_name(self, value):
-        return ootype.dynamicType(value)._name.split(".")[-1] 

Modified: pypy/dist/pypy/rpython/test/test_rrange.py
==============================================================================
--- pypy/dist/pypy/rpython/test/test_rrange.py	(original)
+++ pypy/dist/pypy/rpython/test/test_rrange.py	Wed May 17 14:58:57 2006
@@ -1,9 +1,9 @@
 from pypy.rpython.rrange import *
-from pypy.rpython.test.test_llinterp import interpret
+from pypy.rpython.test.tool import BaseRtypingTest, LLRtypeMixin, OORtypeMixin
 from pypy.rpython.rarithmetic import intmask
 
 
-class AbstractTestRange:
+class BaseTestRrange(BaseRtypingTest):
 
     def test_rlist_range(self):
         def test1(start, stop, step, varstep):
@@ -34,7 +34,7 @@
             for i in range(N):
                 total += i
             return total
-        res = interpret(dummyfn, [10], type_system=self.ts)
+        res = self.interpret(dummyfn, [10])
         assert res == 45
 
     def test_range_is_lazy(self):
@@ -45,16 +45,16 @@
                     break
                 total += i
             return total
-        res = interpret(dummyfn, [10, 2147418112], type_system=self.ts)
+        res = self.interpret(dummyfn, [10, 2147418112])
         assert res == 45
 
     def test_range_item(self):
         def dummyfn(start, stop, i):
             r = range(start, stop)
             return r[i]
-        res = interpret(dummyfn, [10, 17, 4], type_system=self.ts)
+        res = self.interpret(dummyfn, [10, 17, 4])
         assert res == 14
-        res = interpret(dummyfn, [10, 17, -2], type_system=self.ts)
+        res = self.interpret(dummyfn, [10, 17, -2])
         assert res == 15
 
     def test_xrange(self):
@@ -63,7 +63,7 @@
             for i in xrange(N):
                 total += i
             return total
-        res = interpret(dummyfn, [10], type_system=self.ts)
+        res = self.interpret(dummyfn, [10])
         assert res == 45
 
     def test_range_len(self):
@@ -71,7 +71,7 @@
             r = range(start, stop)
             return len(r)
         start, stop = 10, 17
-        res = interpret(dummyfn, [start, stop], type_system=self.ts)
+        res = self.interpret(dummyfn, [start, stop])
         assert res == dummyfn(start, stop)
 
     def test_range2list(self):
@@ -80,13 +80,12 @@
             r.reverse()
             return r[0]
         start, stop = 10, 17
-        res = interpret(dummyfn, [start, stop], type_system=self.ts)
+        res = self.interpret(dummyfn, [start, stop])
         assert res == dummyfn(start, stop)
 
     def check_failed(self, func, *args):
         try:
-            kwargs = {"type_system": self.ts}
-            interpret(func, *args, **kwargs)
+            self.interpret(func, *args, **kwargs)
         except:
             return True
         else:
@@ -102,7 +101,7 @@
             r = range(10, 17, step)
             return r[-1]
         step = 3
-        res = interpret(failingfn_var, [step], type_system=self.ts)
+        res = self.interpret(failingfn_var, [step])
         assert res == failingfn_var(step)
         step = 0
         assert self.check_failed(failingfn_var, [step])
@@ -121,17 +120,13 @@
                 res = res * 51 + i
             return res
         for args in [2, 7, 0], [7, 2, 0], [10, 50, 7], [50, -10, -3]:
-            res = interpret(fn, args, type_system=self.ts)
+            res = self.interpret(fn, args)
             assert res == intmask(fn(*args))
 
 
-class TestRangeLltype(AbstractTestRange):
-
-    ts = "lltype"
+class TestLLtype(BaseTestRrange, LLRtypeMixin):
     from pypy.rpython.lltypesystem import rrange 
 
 
-class TestRangeOotype(AbstractTestRange):
-
-    ts = "ootype"
+class TestOOtype(BaseTestRrange, OORtypeMixin):
     from pypy.rpython.ootypesystem import rrange 

Modified: pypy/dist/pypy/rpython/test/test_rstr.py
==============================================================================
--- pypy/dist/pypy/rpython/test/test_rstr.py	(original)
+++ pypy/dist/pypy/rpython/test/test_rstr.py	Wed May 17 14:58:57 2006
@@ -3,43 +3,16 @@
 from pypy.rpython.rstr import AbstractLLHelpers
 from pypy.rpython.lltypesystem.rstr import LLHelpers, STR
 from pypy.rpython.rtyper import RPythonTyper, TyperError
-from pypy.rpython.test.test_llinterp import interpret, interpret_raises
+from pypy.rpython.test.tool import BaseRtypingTest, LLRtypeMixin, OORtypeMixin
 from pypy.rpython.llinterp import LLException
 
-def llstr(s):
-    p = malloc(STR, len(s))
-    for i in range(len(s)):
-        p.chars[i] = s[i]
-    return p
-
-def test_ll_find_rfind():
-    for i in range(50):
-        n1 = random.randint(0, 10)
-        s1 = ''.join([random.choice("ab") for i in range(n1)])
-        n2 = random.randint(0, 5)
-        s2 = ''.join([random.choice("ab") for i in range(n2)])
-        res = LLHelpers.ll_find(llstr(s1), llstr(s2), 0, n1)
-        assert res == s1.find(s2)
-        res = LLHelpers.ll_rfind(llstr(s1), llstr(s2), 0, n1)
-        assert res == s1.rfind(s2)
-
 def test_parse_fmt():
-    assert AbstractLLHelpers.parse_fmt_string('a') == ['a']
-    assert AbstractLLHelpers.parse_fmt_string('%s') == [('s',)]
-    assert AbstractLLHelpers.parse_fmt_string("name '%s' is not defined") == ["name '", ("s",), "' is not defined"]
-
-
-class AbstractTestRstr:
-
-    def interpret(self, fn, args):
-        return interpret(fn, args, type_system=self.ts)
+    parse = AbstractLLHelpers.parse_fmt_string
+    assert parse('a') == ['a']
+    assert parse('%s') == [('s',)]
+    assert parse("name '%s' is not defined") == ["name '", ("s",), "' is not defined"]
 
-    def interpret_raises(self, exc, fn, args):
-        return interpret_raises(exc, fn, args, type_system=self.ts)
-
-    def _skip_oo(self, reason):
-        if self.ts == 'ootype':
-            py.test.skip("ootypesystem doesn't support %s, yet" % reason)
+class BaseTestRstr(BaseRtypingTest):
 
     def test_simple(self):
         def fn(i):
@@ -513,11 +486,11 @@
         def fn():
             s = 'abbccc'
             s = s.replace('a', 'baz')
-        raises (TyperError, interpret, fn, ())
+        raises(TyperError, self.interpret, fn, ())
         def fn():
             s = 'abbccc'
             s = s.replace('abb', 'c')
-        raises (TyperError, interpret, fn, ())
+        raises(TyperError, self.interpret, fn, ())
 
     def test_int(self):
         s1 = [ '42', '01001', 'abc', 'ABC', '4aBc', ' 12ef ', '+42', 'foo', '42foo', '42.1', '']
@@ -580,11 +553,25 @@
     res = interpret(g, [-2])
     assert res._obj.value == 42
 
-class TestLltype(AbstractTestRstr):
-    ts = "lltype"
+class TestLLtype(BaseTestRstr, LLRtypeMixin):
+
+    def llstr(self, s):
+        p = malloc(STR, len(s))
+        for i in range(len(s)):
+            p.chars[i] = s[i]
+        return p
+
+    def test_ll_find_rfind(self):
+        for i in range(50):
+            n1 = random.randint(0, 10)
+            s1 = ''.join([random.choice("ab") for i in range(n1)])
+            n2 = random.randint(0, 5)
+            s2 = ''.join([random.choice("ab") for i in range(n2)])
+            res = LLHelpers.ll_find(self.llstr(s1), self.llstr(s2), 0, n1)
+            assert res == s1.find(s2)
+            res = LLHelpers.ll_rfind(self.llstr(s1), self.llstr(s2), 0, n1)
+            assert res == s1.rfind(s2)
 
-    def ll_to_string(self, s):
-        return ''.join(s.chars)
 
     def test_hash(self):
         def fn(i):
@@ -599,8 +586,5 @@
         assert typeOf(res) == Signed
 
 
-class TestOotype(AbstractTestRstr):
-    ts = "ootype"
-
-    def ll_to_string(self, s):
-        return s._str
+class TestOOtype(BaseTestRstr, OORtypeMixin):
+    pass

Modified: pypy/dist/pypy/rpython/test/test_rtuple.py
==============================================================================
--- pypy/dist/pypy/rpython/test/test_rtuple.py	(original)
+++ pypy/dist/pypy/rpython/test/test_rtuple.py	Wed May 17 14:58:57 2006
@@ -3,7 +3,8 @@
 from pypy.rpython.ootypesystem import ootype
 from pypy.rpython.rint import signed_repr
 from pypy.rpython.rbool import bool_repr
-from pypy.rpython.test.test_llinterp import interpret 
+from pypy.rpython.test.test_llinterp import interpret
+from pypy.rpython.test.tool import BaseRtypingTest, LLRtypeMixin, OORtypeMixin
 import py
 
 def test_rtuple():
@@ -16,10 +17,7 @@
 
 # ____________________________________________________________
 
-class AbstractTestRTuple:
-
-    def interpret(self, f, args):
-        return interpret(f, args, type_system=self.type_system)
+class BaseTestRtuple(BaseRtypingTest):
 
     def test_simple(self):
         def dummyfn(x):
@@ -88,8 +86,6 @@
         assert res is False 
 
     def test_conv(self):
-        if self.type_system == "ootype":
-            py.test.skip("fix me if ootypes supports strings")
         def t0():
             return (3, 2, None)
         def t1():
@@ -102,13 +98,11 @@
 
         res = self.interpret(f, [1])
         assert res.item0 == 7
-        # XXX this assertion will fail once ootypesystem properly supports
-        # strings, we're leaving the fix up to that point
-        assert isinstance(typeOf(res.item2), Ptr) and ''.join(res.item2.chars) == "xy"
+        assert self.ll_to_string(res.item2) == "xy"
+
         res = self.interpret(f, [0])
         assert res.item0 == 3
-        # XXX see above
-        assert isinstance(typeOf(res.item2), Ptr) and not res.item2
+        assert not res.item2
 
     def test_constant_tuples_shared(self):
         def g(n):
@@ -193,10 +187,7 @@
             return list((i, j))
 
         res = self.interpret(f, [2, 3])
-        if self.type_system == "lltype":
-            assert res._obj.items == [2, 3]
-        else:
-            assert res._list == [2, 3]
+        assert self.ll_to_list(res) == [2, 3]
 
     def test_tuple_iterator_length1(self):
         def f(i):
@@ -239,17 +230,9 @@
         res = self.interpret(g, [3])
         assert res == 3
 
-class TestLLTuple(AbstractTestRTuple):
-
-    type_system = "lltype"
-
-    def class_name(self, value):
-        return "".join(value.super.typeptr.name)[:-1]
-
-class TestOOTuple(AbstractTestRTuple):
-
-    type_system = "ootype"
+class TestLLtype(BaseTestRtuple, LLRtypeMixin):
+    pass
 
-    def class_name(self, value):
-        return ootype.dynamicType(value)._name.split(".")[-1] 
+class TestOOtype(BaseTestRtuple, OORtypeMixin):
+    pass
 

Added: pypy/dist/pypy/rpython/test/tool.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/rpython/test/tool.py	Wed May 17 14:58:57 2006
@@ -0,0 +1,40 @@
+import py
+from pypy.rpython.ootypesystem import ootype
+from pypy.rpython.test.test_llinterp import interpret, interpret_raises
+
+class BaseRtypingTest(object):
+    def interpret(self, fn, args):
+        return interpret(fn, args, type_system=self.type_system)
+
+    def interpret_raises(self, exc, fn, args):
+        return interpret_raises(exc, fn, args, type_system=self.type_system)
+
+    def _skip_oo(self, reason):
+        if self.type_system == 'ootype':
+            py.test.skip("ootypesystem doesn't support %s, yet" % reason)
+    
+
+class LLRtypeMixin(object):
+    type_system = 'lltype'
+
+    def ll_to_string(self, s):
+        return ''.join(s.chars)
+
+    def ll_to_list(self, l):
+        return map(None, l.ll_items())[:l.ll_length()]
+
+    def class_name(self, value):
+        return "".join(value.super.typeptr.name)[:-1]
+
+
+class OORtypeMixin(object):
+    type_system = 'ootype'
+
+    def ll_to_string(self, s):
+        return s._str
+
+    def ll_to_list(self, l):
+        return l._list[:]
+
+    def class_name(self, value):
+        return ootype.dynamicType(value)._name.split(".")[-1] 



More information about the Pypy-commit mailing list