[pypy-svn] r18987 - in pypy/dist/pypy/rpython: . test

pedronis at codespeak.net pedronis at codespeak.net
Wed Oct 26 05:18:46 CEST 2005


Author: pedronis
Date: Wed Oct 26 05:18:45 2005
New Revision: 18987

Modified:
   pypy/dist/pypy/rpython/rlist.py
   pypy/dist/pypy/rpython/rstr.py
   pypy/dist/pypy/rpython/test/test_rlist.py
   pypy/dist/pypy/rpython/test/test_rstr.py
Log:
fixed-size list separate support.



Modified: pypy/dist/pypy/rpython/rlist.py
==============================================================================
--- pypy/dist/pypy/rpython/rlist.py	(original)
+++ pypy/dist/pypy/rpython/rlist.py	Wed Oct 26 05:18:45 2005
@@ -1,4 +1,4 @@
-from pypy.annotation.pairtype import pairtype
+from pypy.annotation.pairtype import pairtype, pair
 from pypy.annotation import model as annmodel
 from pypy.objspace.flow.model import Constant
 from pypy.rpython.rmodel import Repr, TyperError, IntegerRepr, inputconst
@@ -25,6 +25,11 @@
 #    'items' points to a C-like array in memory preceded by a 'length' header,
 #    where each item contains a primitive value or pointer to the actual list
 #    item.
+#
+#    or for fixed-size lists an array is directly used:
+#
+#    item_t list_items[]
+#
 
 class __extend__(annmodel.SomeList):
     def rtyper_makerepr(self, rtyper):
@@ -38,14 +43,18 @@
         else:
             # cannot do the rtyper.getrepr() call immediately, for the case
             # of recursive structures -- i.e. if the listdef contains itself
-            return ListRepr(rtyper, lambda: rtyper.getrepr(listitem.s_value),
-                            listitem)
+            if self.listdef.listitem.resized:
+                return ListRepr(rtyper, lambda: rtyper.getrepr(listitem.s_value),
+                                listitem)
+            else:
+                return FixedSizeListRepr(rtyper, lambda: rtyper.getrepr(listitem.s_value),
+                                         listitem)
 
     def rtyper_makekey(self):
         return self.__class__, self.listdef.listitem
 
 
-class ListRepr(Repr):
+class BaseListRepr(Repr):
 
     def __init__(self, rtyper, item_repr, listitem=None):
         self.rtyper = rtyper
@@ -63,25 +72,6 @@
     def recast(self, llops, v):
         return llops.convertvar(v, self.item_repr, self.external_item_repr)
 
-    def _setup_repr(self):
-        if 'item_repr' not in self.__dict__:
-            self.external_item_repr, self.item_repr = externalvsinternal(self.rtyper, self._item_repr_computer())
-        if isinstance(self.LIST, GcForwardReference):
-            ITEM = self.item_repr.lowleveltype
-            ITEMARRAY = GcArray(ITEM)
-            # XXX we might think of turning length stuff into Unsigned
-            self.LIST.become(GcStruct("list", ("length", Signed),
-                                              ("items", Ptr(ITEMARRAY)),
-                                      adtmeths = {
-                                          "ll_newlist": ll_newlist,
-                                          "ll_length": ll_length,
-                                          "ll_items": ll_items,
-                                      })
-                             )
-
-    def compact_repr(self):
-        return 'ListR %s' % (self.item_repr.compact_repr(),)
-
     def convert_const(self, listobj):
         # get object from bound list method
         #listobj = getattr(listobj, '__self__', listobj)
@@ -94,16 +84,19 @@
             return self.list_cache[key]
         except KeyError:
             self.setup()
-            result = malloc(self.LIST, immortal=True)
+            n = len(listobj)
+            result = self.prepare_const(n)
             self.list_cache[key] = result
-            result.length = len(listobj)
-            result.items = malloc(self.LIST.items.TO, result.length)
             r_item = self.item_repr
-            for i in range(result.length):
+            items = result.ll_items()
+            for i in range(n):
                 x = listobj[i]
-                result.items[i] = r_item.convert_const(x)
+                items[i] = r_item.convert_const(x)
             return result
 
+    def prepare_const(self, nitems):
+        raise NotImplementedError
+
     def get_eqfunc(self):
         return inputconst(Void, self.item_repr.get_ll_eq_function())
 
@@ -120,16 +113,69 @@
         v_lst, = hop.inputargs(self)
         return hop.gendirectcall(ll_list_is_true, v_lst)
 
-    def rtype_method_append(self, hop):
-        v_lst, v_value = hop.inputargs(self, self.item_repr)
-        hop.exception_cannot_occur()
-        hop.gendirectcall(ll_append, v_lst, v_value)
-
     def rtype_method_index(self, hop):
         v_lst, v_value = hop.inputargs(self, self.item_repr)
         hop.has_implicit_exception(ValueError)   # record that we know about it
         hop.exception_is_here()
         return hop.gendirectcall(ll_listindex, v_lst, v_value, self.get_eqfunc())
+    
+    def rtype_method_reverse(self, hop):
+        v_lst, = hop.inputargs(self)
+        hop.exception_cannot_occur()
+        hop.gendirectcall(ll_reverse,v_lst)
+
+    def make_iterator_repr(self):
+        return ListIteratorRepr(self)
+
+    def ll_str(self, l):
+        items = l.ll_items()
+        length = l.ll_length()
+        item_repr = self.item_repr
+
+        temp = malloc(TEMP, length)
+        i = 0
+        while i < length:
+            temp[i] = item_repr.ll_str(items[i])
+            i += 1
+
+        return rstr.ll_strconcat(
+            rstr.list_str_open_bracket,
+            rstr.ll_strconcat(rstr.ll_join(rstr.list_str_sep,
+                                           length,
+                                           temp),
+                              rstr.list_str_close_bracket))
+
+class ListRepr(BaseListRepr):
+
+    def _setup_repr(self):
+        if 'item_repr' not in self.__dict__:
+            self.external_item_repr, self.item_repr = externalvsinternal(self.rtyper, self._item_repr_computer())
+        if isinstance(self.LIST, GcForwardReference):
+            ITEM = self.item_repr.lowleveltype
+            ITEMARRAY = GcArray(ITEM)
+            # XXX we might think of turning length stuff into Unsigned
+            self.LIST.become(GcStruct("list", ("length", Signed),
+                                              ("items", Ptr(ITEMARRAY)),
+                                      adtmeths = {
+                                          "ll_newlist": ll_newlist,
+                                          "ll_length": ll_length,
+                                          "ll_items": ll_items,
+                                      })
+                             )
+
+    def compact_repr(self):
+        return 'ListR %s' % (self.item_repr.compact_repr(),)
+
+    def prepare_const(self, n):
+        result = malloc(self.LIST, immortal=True)
+        result.length = n
+        result.items = malloc(self.LIST.items.TO, n)
+        return result
+
+    def rtype_method_append(self, hop):
+        v_lst, v_value = hop.inputargs(self, self.item_repr)
+        hop.exception_cannot_occur()
+        hop.gendirectcall(ll_append, v_lst, v_value)
 
     def rtype_method_insert(self, hop):
         v_lst, v_index, v_value = hop.inputargs(self, Signed, self.item_repr)
@@ -146,14 +192,9 @@
         hop.gendirectcall(llfn, *args)
 
     def rtype_method_extend(self, hop):
-        v_lst1, v_lst2 = hop.inputargs(self, self)
+        v_lst1, v_lst2 = hop.inputargs(*hop.args_r)
         hop.exception_cannot_occur()
         hop.gendirectcall(ll_extend, v_lst1, v_lst2)
-    
-    def rtype_method_reverse(self, hop):
-        v_lst, = hop.inputargs(self)
-        hop.exception_cannot_occur()
-        hop.gendirectcall(ll_reverse,v_lst)
 
     def rtype_method_pop(self, hop):
         if hop.has_implicit_exception(IndexError):
@@ -179,36 +220,37 @@
         v_res = hop.gendirectcall(llfn, v_func, *args)
         return self.recast(hop.llops, v_res)
 
-    def make_iterator_repr(self):
-        return ListIteratorRepr(self)
+class FixedSizeListRepr(BaseListRepr):
 
-    def ll_str(self, l):
-        items = l.ll_items()
-        length = l.ll_length()
-        item_repr = self.item_repr
+    def _setup_repr(self):
+        if 'item_repr' not in self.__dict__:
+            self.external_item_repr, self.item_repr = externalvsinternal(self.rtyper, self._item_repr_computer())
+        if isinstance(self.LIST, GcForwardReference):
+            ITEM = self.item_repr.lowleveltype
+            ITEMARRAY = GcArray(ITEM,
+                                adtmeths = {
+                                     "ll_newlist": ll_fixed_newlist,
+                                     "ll_length": ll_fixed_length,
+                                     "ll_items": ll_fixed_items,
+                                })
 
-        temp = malloc(TEMP, length)
-        i = 0
-        while i < length:
-            temp[i] = item_repr.ll_str(items[i])
-            i += 1
+            self.LIST.become(ITEMARRAY)
 
-        return rstr.ll_strconcat(
-            rstr.list_str_open_bracket,
-            rstr.ll_strconcat(rstr.ll_join(rstr.list_str_sep,
-                                           length,
-                                           temp),
-                              rstr.list_str_close_bracket))
+    def compact_repr(self):
+        return 'FixedSizeListR %s' % (self.item_repr.compact_repr(),)
 
+    def prepare_const(self, n):
+        result = malloc(self.LIST, n, immortal=True)
+        return result
 
-class __extend__(pairtype(ListRepr, Repr)):
+class __extend__(pairtype(BaseListRepr, Repr)):
 
     def rtype_contains((r_lst, _), hop):
         v_lst, v_any = hop.inputargs(r_lst, r_lst.item_repr)
         return hop.gendirectcall(ll_listcontains, v_lst, v_any, r_lst.get_eqfunc())
 
 
-class __extend__(pairtype(ListRepr, IntegerRepr)):
+class __extend__(pairtype(BaseListRepr, IntegerRepr)):
 
     def rtype_getitem((r_lst, r_int), hop):
         if hop.has_implicit_exception(IndexError):
@@ -239,6 +281,13 @@
         hop.exception_is_here()
         return hop.gendirectcall(llfn, v_func, v_lst, v_index, v_item)
 
+    def rtype_mul((r_lst, r_int), hop):
+        cRESLIST = hop.inputconst(Void, hop.r_result.LIST)
+        v_lst, v_factor = hop.inputargs(r_lst, Signed)
+        return hop.gendirectcall(ll_mul, cRESLIST, v_lst, v_factor)
+
+class __extend__(pairtype(ListRepr, IntegerRepr)):
+
     def rtype_delitem((r_lst, r_int), hop):
         if hop.has_implicit_exception(IndexError):
             spec = dum_checkidx
@@ -253,16 +302,11 @@
         hop.exception_is_here()
         return hop.gendirectcall(llfn, v_func, v_lst, v_index)
 
-    def rtype_mul((r_lst, r_int), hop):
-        cRESLIST = hop.inputconst(Void, hop.r_result.LIST)
-        v_lst, v_factor = hop.inputargs(r_lst, Signed)
-        return hop.gendirectcall(ll_mul, cRESLIST, v_lst, v_factor)
-
     def rtype_inplace_mul((r_lst, r_int), hop):
         v_lst, v_factor = hop.inputargs(r_lst, Signed)
         return hop.gendirectcall(ll_inplace_mul, v_lst, v_factor)
 
-class __extend__(pairtype(ListRepr, SliceRepr)):
+class __extend__(pairtype(BaseListRepr, SliceRepr)):
 
     def rtype_getitem((r_lst, r_slic), hop):
         cRESLIST = hop.inputconst(Void, hop.r_result.LIST)
@@ -282,11 +326,13 @@
         #    not implemented
         if r_slic == startstop_slice_repr:
             v_lst, v_slice, v_lst2 = hop.inputargs(r_lst, startstop_slice_repr,
-                                                   r_lst)
+                                                   hop.args_r[2])
             hop.gendirectcall(ll_listsetslice, v_lst, v_slice, v_lst2)
             return
         raise TyperError('setitem does not support slices with %r' % (r_slic,))
 
+class __extend__(pairtype(ListRepr, SliceRepr)):
+
     def rtype_delitem((r_lst, r_slic), hop):
         if r_slic == startonly_slice_repr:
             v_lst, v_start = hop.inputargs(r_lst, startonly_slice_repr)
@@ -298,7 +344,7 @@
             return
         raise TyperError('delitem does not support slices with %r' % (r_slic,))
 
-class __extend__(pairtype(ListRepr, ListRepr)):
+class __extend__(pairtype(BaseListRepr, BaseListRepr)):
     def convert_from_to((r_lst1, r_lst2), v, llops):
         if r_lst1.listitem is None or r_lst2.listitem is None:
             return NotImplemented
@@ -307,25 +353,40 @@
                 return NotImplemented
         return v
 
-    def rtype_add((self, _), hop):
-        v_lst1, v_lst2 = hop.inputargs(self, self)
+    def rtype_is_((r_lst1, r_lst2), hop):
+        if r_lst1.lowleveltype != r_lst2.lowleveltype:
+            # obscure logic, the is can be true only if both are None
+            v_lst1, v_lst2 = hop.inputargs(r_lst1, r_lst2)
+            return hop.gendirectcall(ll_both_none, v_lst1, v_lst2)
+
+        return pairtype(Repr, Repr).rtype_is_(pair(r_lst1, r_lst2), hop)
+
+    def rtype_add((r_lst1, r_lst2), hop):
+        v_lst1, v_lst2 = hop.inputargs(r_lst1, r_lst2)
         cRESLIST = hop.inputconst(Void, hop.r_result.LIST)
         return hop.gendirectcall(ll_concat, cRESLIST, v_lst1, v_lst2)
 
-    def rtype_inplace_add((self, _), hop):
-        v_lst1, v_lst2 = hop.inputargs(self, self)
+    def rtype_eq((r_lst1, r_lst2), hop):
+        assert r_lst1.item_repr == r_lst2.item_repr
+        v_lst1, v_lst2 = hop.inputargs(r_lst1, r_lst2)
+        return hop.gendirectcall(ll_listeq, v_lst1, v_lst2, r_lst1.get_eqfunc())
+
+    def rtype_ne((r_lst1, r_lst2), hop):
+        assert r_lst1.item_repr == r_lst2.item_repr
+        v_lst1, v_lst2 = hop.inputargs(r_lst1, r_lst2)
+        flag = hop.gendirectcall(ll_listeq, v_lst1, v_lst2, r_lst1.get_eqfunc())
+        return hop.genop('bool_not', [flag], resulttype=Bool)
+
+def ll_both_none(lst1, lst2):
+    return not lst1 and not lst2
+
+class __extend__(pairtype(ListRepr, BaseListRepr)):
+
+    def rtype_inplace_add((r_lst1, r_lst2), hop):
+        v_lst1, v_lst2 = hop.inputargs(r_lst1, r_lst2)
         hop.gendirectcall(ll_extend, v_lst1, v_lst2)
         return v_lst1
 
-    def rtype_eq((self, _), hop):
-        v_lst1, v_lst2 = hop.inputargs(self, self)
-        return hop.gendirectcall(ll_listeq, v_lst1, v_lst2, self.get_eqfunc())
-
-    def rtype_ne((self, _), hop):
-        v_lst1, v_lst2 = hop.inputargs(self, self)
-        flag = hop.gendirectcall(ll_listeq, v_lst1, v_lst2, self.get_eqfunc())
-        return hop.genop('bool_not', [flag], resulttype=Bool)
-
 
 # ____________________________________________________________
 #
@@ -420,7 +481,7 @@
     return new_lst
 
 def ll_len(l):
-    return l.length
+    return l.ll_length()
 
 def ll_list_is_true(l):
     # check if a list is True, allowing for None
@@ -810,6 +871,19 @@
 def ll_items(l):
     return l.items
 
+# fixed size versions
+
+def ll_fixed_newlist(LIST, length):
+    l = malloc(LIST, length)
+    return l
+ll_fixed_newlist = typeMethod(ll_fixed_newlist)
+
+def ll_fixed_length(l):
+    return len(l)
+
+def ll_fixed_items(l):
+    return l
+
 def rtype_newlist(hop):
     nb_args = hop.nb_args
     r_list = hop.r_result

Modified: pypy/dist/pypy/rpython/rstr.py
==============================================================================
--- pypy/dist/pypy/rpython/rstr.py	(original)
+++ pypy/dist/pypy/rpython/rstr.py	Wed Oct 26 05:18:45 2005
@@ -153,8 +153,8 @@
         if hop.s_result.is_constant():
             return inputconst(string_repr, hop.s_result.const)
         r_lst = hop.args_r[1]
-        from pypy.rpython.rlist import ListRepr
-        if not isinstance(r_lst, ListRepr):
+        from pypy.rpython.rlist import BaseListRepr
+        if not isinstance(r_lst, BaseListRepr):
             raise TyperError("string.join of non-list: %r" % r_lst)
         v_str, v_lst = hop.inputargs(string_repr, r_lst)
         LIST = r_lst.lowleveltype.TO

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 Oct 26 05:18:45 2005
@@ -18,96 +18,143 @@
 """ % (name, name)
 del n1, n2, name
 
-def sample_list():    # [42, 43, 44, 45]
-    rlist = ListRepr(None, signed_repr)
-    rlist.setup()
-    l = ll_newlist(rlist.lowleveltype.TO, 3)
-    ll_setitem(l, 0, 42)
-    ll_setitem(l, -2, 43)
-    ll_setitem_nonneg(l, 2, 44)
-    ll_append(l, 45)
-    return l
-
-def check_list(l1, expected):
-    assert ll_len(l1) == len(expected)
-    for i, x in zip(range(len(expected)), expected):
-        assert ll_getitem_nonneg(l1, i) == x
-
-def test_rlist_basic():
-    l = sample_list()
-    assert ll_getitem(l, -4) == 42
-    assert ll_getitem_nonneg(l, 1) == 43
-    assert ll_getitem(l, 2) == 44
-    assert ll_getitem(l, 3) == 45
-    assert ll_len(l) == 4
-    check_list(l, [42, 43, 44, 45])
-
-def test_rlist_set_del():
-    l = sample_list()
-    ll_setitem(l, -1, 99)
-    check_list(l, [42, 43, 44, 99])
-    ll_setitem_nonneg(l, 1, 77)
-    check_list(l, [42, 77, 44, 99])
-    ll_delitem_nonneg(l, 0)
-    check_list(l, [77, 44, 99])
-    ll_delitem(l, -2)
-    check_list(l, [77, 99])
-    ll_delitem(l, 1)
-    check_list(l, [77])
-    ll_delitem(l, 0)
-    check_list(l, [])
-
-def test_rlist_extend_concat():
-    l = sample_list()
-    ll_extend(l, l)
-    check_list(l, [42, 43, 44, 45] * 2)
-    l1 = ll_concat(typeOf(l).TO, l, l)
-    assert l1 != l
-    check_list(l1, [42, 43, 44, 45] * 4)
-
-def test_rlist_slice():
-    l = sample_list()
-    LIST = typeOf(l).TO
-    check_list(ll_listslice_startonly(LIST, l, 0), [42, 43, 44, 45])
-    check_list(ll_listslice_startonly(LIST, l, 1), [43, 44, 45])
-    check_list(ll_listslice_startonly(LIST, l, 2), [44, 45])
-    check_list(ll_listslice_startonly(LIST, l, 3), [45])
-    check_list(ll_listslice_startonly(LIST, l, 4), [])
-    for start in range(5):
-        for stop in range(start, 8):
-            s = ll_newslice(start, stop)
-            check_list(ll_listslice(LIST, l, s), [42, 43, 44, 45][start:stop])
-
-def test_rlist_delslice():
-    l = sample_list()
-    ll_listdelslice_startonly(l, 3)
-    check_list(l, [42, 43, 44])
-    ll_listdelslice_startonly(l, 0)
-    check_list(l, [])
-    for start in range(5):
-        for stop in range(start, 8):
-            l = sample_list()
-            s = ll_newslice(start, stop)
-            ll_listdelslice(l, s)
-            expected = [42, 43, 44, 45]
-            del expected[start:stop]
-            check_list(l, expected)
-
-def test_rlist_setslice():
-    n = 100
-    for start in range(5):
-        for stop in range(start, 5):
-            l1 = sample_list()
-            l2 = sample_list()
-            expected = [42, 43, 44, 45]
-            for i in range(start, stop):
-                expected[i] = n
-                ll_setitem(l2, i, n)
-                n += 1
-            s = ll_newslice(start, stop)
-            l2 = ll_listslice(typeOf(l2).TO, l2, s)
-            ll_listsetslice(l1, s, l2)
-            check_list(l1, expected)
+class BaseTestListImpl:
+
+    def check_list(self, l1, expected):
+        assert ll_len(l1) == len(expected)
+        for i, x in zip(range(len(expected)), expected):
+            assert ll_getitem_nonneg(l1, i) == x
+
+    def test_rlist_basic(self):
+        l = self.sample_list()
+        assert ll_getitem(l, -4) == 42
+        assert ll_getitem_nonneg(l, 1) == 43
+        assert ll_getitem(l, 2) == 44
+        assert ll_getitem(l, 3) == 45
+        assert ll_len(l) == 4
+        self.check_list(l, [42, 43, 44, 45])
+
+    def test_rlist_set(self):
+        l = self.sample_list()
+        ll_setitem(l, -1, 99)
+        self.check_list(l, [42, 43, 44, 99])
+        ll_setitem_nonneg(l, 1, 77)
+        self.check_list(l, [42, 77, 44, 99])
+
+    def test_rlist_slice(self):
+        l = self.sample_list()
+        LIST = typeOf(l).TO
+        self.check_list(ll_listslice_startonly(LIST, l, 0), [42, 43, 44, 45])
+        self.check_list(ll_listslice_startonly(LIST, l, 1), [43, 44, 45])
+        self.check_list(ll_listslice_startonly(LIST, l, 2), [44, 45])
+        self.check_list(ll_listslice_startonly(LIST, l, 3), [45])
+        self.check_list(ll_listslice_startonly(LIST, l, 4), [])
+        for start in range(5):
+            for stop in range(start, 8):
+                s = ll_newslice(start, stop)
+                self.check_list(ll_listslice(LIST, l, s), [42, 43, 44, 45][start:stop])
+
+    def test_rlist_setslice(self):
+        n = 100
+        for start in range(5):
+            for stop in range(start, 5):
+                l1 = self.sample_list()
+                l2 = self.sample_list()
+                expected = [42, 43, 44, 45]
+                for i in range(start, stop):
+                    expected[i] = n
+                    ll_setitem(l2, i, n)
+                    n += 1
+                s = ll_newslice(start, stop)
+                l2 = ll_listslice(typeOf(l2).TO, l2, s)
+                ll_listsetslice(l1, s, l2)
+                self.check_list(l1, expected)
+
+class TestListImpl(BaseTestListImpl):
+
+    def sample_list(self):    # [42, 43, 44, 45]
+        rlist = ListRepr(None, signed_repr)
+        rlist.setup()
+        l = ll_newlist(rlist.lowleveltype.TO, 3)
+        ll_setitem(l, 0, 42)
+        ll_setitem(l, -2, 43)
+        ll_setitem_nonneg(l, 2, 44)
+        ll_append(l, 45)
+        return l
+
+    def test_rlist_del(self):
+        l = self.sample_list()
+        ll_delitem_nonneg(l, 0)
+        self.check_list(l, [43, 44, 45])
+        ll_delitem(l, -2)
+        self.check_list(l, [43, 45])
+        ll_delitem(l, 1)
+        self.check_list(l, [43])
+        ll_delitem(l, 0)
+        self.check_list(l, [])
+
+    def test_rlist_extend_concat(self):
+        l = self.sample_list()
+        ll_extend(l, l)
+        self.check_list(l, [42, 43, 44, 45] * 2)
+        l1 = ll_concat(typeOf(l).TO, l, l)
+        assert typeOf(l1) == typeOf(l)
+        assert l1 != l
+        self.check_list(l1, [42, 43, 44, 45] * 4)
+
+    def test_rlist_delslice(self):
+        l = self.sample_list()
+        ll_listdelslice_startonly(l, 3)
+        self.check_list(l, [42, 43, 44])
+        ll_listdelslice_startonly(l, 0)
+        self.check_list(l, [])
+        for start in range(5):
+            for stop in range(start, 8):
+                l = self.sample_list()
+                s = ll_newslice(start, stop)
+                ll_listdelslice(l, s)
+                expected = [42, 43, 44, 45]
+                del expected[start:stop]
+                self.check_list(l, expected)
+
+class TestFixedSizeListImpl(BaseTestListImpl):
+
+    def sample_list(self):    # [42, 43, 44, 45]
+        rlist = FixedSizeListRepr(None, signed_repr)
+        rlist.setup()
+        l = ll_fixed_newlist(rlist.lowleveltype.TO, 4)
+        ll_setitem(l, 0, 42)
+        ll_setitem(l, -3, 43)
+        ll_setitem_nonneg(l, 2, 44)
+        ll_setitem(l, 3, 45)
+        return l
+
+    def test_rlist_extend_concat(self):
+        l = self.sample_list()
+        lvar = TestListImpl.sample_list(TestListImpl())
+        ll_extend(lvar, l)
+        self.check_list(lvar, [42, 43, 44, 45] * 2)
+
+        l1 = ll_concat(typeOf(l).TO, lvar, l)
+        assert typeOf(l1) == typeOf(l)
+        assert l1 != l
+        self.check_list(l1, [42, 43, 44, 45] * 3)
+
+        l1 = ll_concat(typeOf(l).TO, l, lvar)
+        assert typeOf(l1) == typeOf(l)
+        assert l1 != l
+        self.check_list(l1, [42, 43, 44, 45] * 3)
+
+        lvar1 = ll_concat(typeOf(lvar).TO, lvar, l)
+        assert typeOf(lvar1) == typeOf(lvar)
+        assert lvar1 != lvar
+        self.check_list(l1, [42, 43, 44, 45] * 3)
+
+        lvar1 = ll_concat(typeOf(lvar).TO, l, lvar)
+        assert typeOf(lvar1) == typeOf(lvar)
+        assert lvar1 != lvar
+        self.check_list(lvar1, [42, 43, 44, 45] * 3)
+
 
 # ____________________________________________________________
 
@@ -125,21 +172,36 @@
     def dummyfn():
         l = [10, 20, 30]
         return l[2]
-    rtype(dummyfn)
+    res = interpret(dummyfn, [])
+    assert res == 30
 
 def test_append():
     def dummyfn():
         l = []
-        l.append(5)
-        l.append(6)
-        return l[0]
-    rtype(dummyfn)
+        l.append(50)
+        l.append(60)
+        l.append(70)
+        l.append(80)
+        l.append(90)
+        return len(l), l[0], l[-1]
+    res = interpret(dummyfn, [])
+    assert res.item0 == 5 
+    assert res.item1 == 50
+    assert res.item2 == 90
 
 def test_len():
     def dummyfn():
         l = [5, 10]
         return len(l)
-    rtype(dummyfn)
+    res = interpret(dummyfn, [])
+    assert res == 2
+
+    def dummyfn():
+        l = [5]
+        l.append(6)
+        return len(l)
+    res = interpret(dummyfn, [])
+    assert res == 2
 
 def test_iterate():
     def dummyfn():
@@ -147,7 +209,17 @@
         for x in [1, 3, 5, 7, 9]:
             total += x
         return total
-    rtype(dummyfn)
+    res = interpret(dummyfn, [])
+    assert res == 25
+    def dummyfn():
+        total = 0
+        l = [1, 3, 5, 7]
+        l.append(9)
+        for x in l:
+            total += x
+        return total
+    res = interpret(dummyfn, [])
+    assert res == 25
 
 def test_recursive():
     def dummyfn(N):
@@ -156,30 +228,86 @@
             l = [l]
             N -= 1
         return len(l)
-    rtype(dummyfn, [int]) #.view()
+    res = interpret(dummyfn, [5])
+    assert res == 1
+
+    def dummyfn(N):
+        l = []
+        while N > 0:
+            l.append(l)
+            N -= 1
+        return len(l)
+    res = interpret(dummyfn, [5])
+    assert res == 5
+
+def tolst(l):
+    return map(None, l.ll_items())[:l.ll_length()]
 
 def test_add():
     def dummyfn():
         l = [5]
         l += [6,7]
         return l + [8]
-    rtype(dummyfn)
+    res = interpret(dummyfn, [])
+    assert tolst(res) == [5, 6, 7, 8]
+
+    def dummyfn():
+        l = [5]
+        l += [6,7]
+        l2 =  l + [8]
+        l2.append(9)
+        return l2
+    res = interpret(dummyfn, [])
+    assert tolst(res) == [5, 6, 7, 8, 9]
 
 def test_slice():
     def dummyfn():
         l = [5, 6, 7, 8, 9]
         return l[:2], l[1:4], l[3:]
-    rtype(dummyfn)
+    res = interpret(dummyfn, [])
+    assert tolst(res.item0) == [5, 6]
+    assert tolst(res.item1) == [6, 7, 8]
+    assert tolst(res.item2) == [8, 9]
+
+    def dummyfn():
+        l = [5, 6, 7, 8]
+        l.append(9)
+        return l[:2], l[1:4], l[3:]
+    res = interpret(dummyfn, [])
+    assert tolst(res.item0) == [5, 6]
+    assert tolst(res.item1) == [6, 7, 8]
+    assert tolst(res.item2) == [8, 9]
 
 def test_set_del_item():
     def dummyfn():
         l = [5, 6, 7]
         l[1] = 55
         l[-1] = 66
+        return l
+    res = interpret(dummyfn, [])
+    assert tolst(res) == [5, 55, 66]
+
+    def dummyfn():
+        l = []
+        l.append(5)
+        l.append(6)
+        l.append(7)
+        l[1] = 55
+        l[-1] = 66
+        return l
+    res = interpret(dummyfn, [])
+    assert tolst(res) == [5, 55, 66]
+
+    def dummyfn():
+        l = [5, 6, 7]
+        l[1] = 55
+        l[-1] = 66
         del l[0]
         del l[-1]
         del l[:]
-    rtype(dummyfn)
+        return len(l)
+    res = interpret(dummyfn, [])
+    assert res == 0
 
 def test_setslice():
     def dummyfn():
@@ -192,6 +320,17 @@
     assert res.item2 == 8
     assert res.item3 == 7
 
+    def dummyfn():
+        l = [10, 9, 8]
+        l.append(7)
+        l[:2] = [6, 5]
+        return l[0], l[1], l[2], l[3]
+    res = interpret(dummyfn, ())
+    assert res.item0 == 6
+    assert res.item1 == 5
+    assert res.item2 == 8
+    assert res.item3 == 7
+
 def test_insert_pop():
     def dummyfn():
         l = [6, 7, 8]
@@ -226,6 +365,15 @@
     res = interpret(dummyfn, ())
     assert res == 235
 
+    def dummyfn():
+        l = [5]
+        l.append(3)
+        l.append(2)
+        l.reverse()
+        return l[0]*100 + l[1]*10 + l[2]
+    res = interpret(dummyfn, ())
+    assert res == 235
+
 def test_prebuilt_list():
     klist = ['a', 'd', 'z', 'k']
     def dummyfn(n):
@@ -237,6 +385,19 @@
     res = interpret(dummyfn, [-2])
     assert res == 'z'
 
+    klist = ['a', 'd', 'z']
+    def mkdummyfn():
+        def dummyfn(n):
+            klist.append('k')
+            return klist[n]
+        return dummyfn
+    res = interpret(mkdummyfn(), [0])
+    assert res == 'a'
+    res = interpret(mkdummyfn(), [3])
+    assert res == 'k'
+    res = interpret(mkdummyfn(), [-2])
+    assert res == 'z'
+
 def test_bound_list_method():
     klist = [1, 2, 3]
     # for testing constant methods without actually mutating the constant
@@ -263,7 +424,8 @@
     assert res is False
     def dummyfn():
         l1 = [1, 2]
-        l2 = [1, 2]
+        l2 = [1]
+        l2.append(2)
         return l1 is l2
     res = interpret(dummyfn, [])
     assert res is False
@@ -275,6 +437,14 @@
     res = interpret(dummyfn, [])
     assert res is False
 
+    def dummyfn():
+        l1 = None
+        l2 = [1]
+        l2.append(2)
+        return l1 is l2
+    res = interpret(dummyfn, [])
+    assert res is False
+
 def test_list_compare():
     def fn(i, j, neg=False):
         s1 = [[1, 2, 3], [4, 5, 1], None]
@@ -288,6 +458,21 @@
                 res = interpret(fn, [i,j,case])
                 assert res is fn(i, j, case)
 
+    def fn(i, j, neg=False):
+        s1 = [[1, 2, 3], [4, 5, 1], None]
+        l = []
+        l.extend([1,2,3])
+        s2 = [l, [4, 5, 1], [1], [1, 2], [4, 5, 1, 6],
+              [7, 1, 1, 8, 9, 10], None]
+        if neg: return s1[i] != s2[i]
+        return s1[i] == s2[j]
+    for i in range(3):
+        for j in range(7):
+            for case in False, True:
+                res = interpret(fn, [i,j,case])
+                assert res is fn(i, j, case)
+
+
 def test_list_comparestr():
     def fn(i, j, neg=False):
         s1 = [["hell"], ["hello", "world"]]
@@ -320,6 +505,24 @@
                 res = interpret(fn, [i, j, case])
                 assert res is fn(i, j, case)
 
+    def fn(i, j, neg=False):
+        foo1 = Foo()
+        foo2 = Foo()
+        bar1 = Bar()
+        s1 = [[foo1], [foo2], [bar1]]
+        s2 = s1[:]
+
+        s2[0].extend([])
+        
+        if neg: return s1[i] != s2[i]
+        return s1[i] == s2[j]
+    for i in range(3):
+        for j in range(3):
+            for case in False, True:
+                res = interpret(fn, [i, j, case])
+                assert res is fn(i, j, case)
+
+
 def test_list_contains():
     def fn(i, neg=False):
         foo1 = Foo()
@@ -335,6 +538,22 @@
             res = interpret(fn, [i, case])
             assert res is fn(i, case)
 
+    def fn(i, neg=False):
+        foo1 = Foo()
+        foo2 = Foo()
+        bar1 = Bar()
+        bar2 = Bar()
+        lis = [foo1, foo2, bar1]
+        lis.append(lis.pop())
+        args = lis + [bar2]
+        if neg : return args[i] not in lis
+        return args[i] in lis
+    for i in range(4):
+        for case in False, True:
+            res = interpret(fn, [i, case])
+            assert res is fn(i, case)
+
+
 def test_not_a_char_list_after_all():
     def fn():
         l = ['h', 'e', 'l', 'l', 'o']
@@ -352,12 +571,32 @@
         args = lis + [bar2]
         return lis.index(args[i])
     for i in range(4):
-        try:
-            res2 = fn(i)
-            res1 = interpret(fn, [i])
-            assert res1 == res2
-        except Exception, e:
-            interpret_raises(e.__class__, fn, [i])
+        for varsize in False, True:
+            try:
+                res2 = fn(i)
+                res1 = interpret(fn, [i])
+                assert res1 == res2
+            except Exception, e:
+                interpret_raises(e.__class__, fn, [i])
+
+    def fn(i):
+        foo1 = Foo()
+        foo2 = Foo()
+        bar1 = Bar()
+        bar2 = Bar()
+        lis = [foo1, foo2, bar1]
+        args = lis + [bar2]
+        lis.append(lis.pop())
+        return lis.index(args[i])
+    for i in range(4):
+        for varsize in False, True:
+            try:
+                res2 = fn(i)
+                res1 = interpret(fn, [i])
+                assert res1 == res2
+            except Exception, e:
+                interpret_raises(e.__class__, fn, [i])
+
 
 def test_list_str():
     def fn():
@@ -372,6 +611,22 @@
     res = interpret(fn, [])
     assert ''.join(res.chars) == fn()
 
+    def fn():
+        l = [1,2]
+        l.append(3)
+        return str(l)
+    
+    res = interpret(fn, [])
+    assert ''.join(res.chars) == fn()
+
+    def fn():
+        l = [1,2]
+        l.append(3)
+        return str([l])
+    
+    res = interpret(fn, [])
+    assert ''.join(res.chars) == fn()
+
 def test_list_or_None():
     empty_list = []
     nonempty_list = [1, 2]
@@ -389,6 +644,27 @@
     res = interpret(fn, [2])
     assert res == 1
 
+
+    nonempty_list = [1, 2]
+    def fn(i):
+        empty_list = [1]
+        empty_list.pop()
+        nonempty_list = []
+        nonempty_list.extend([1,2])
+        test = [None, empty_list, nonempty_list][i]
+        if test:
+            return 1
+        else:
+            return 0
+
+    res = interpret(fn, [0])
+    assert res == 0
+    res = interpret(fn, [1])
+    assert res == 0
+    res = interpret(fn, [2])
+    assert res == 1
+ 
+
 def test_inst_list():
     def fn():
         l = [None]
@@ -406,6 +682,20 @@
     res = interpret(fn, [])
     assert ''.join(res.chars) == '<Foo object>;[<Foo object>, <Bar object>, <Bar object>, <Foo object>, <Foo object>]'
 
+    def fn():
+        l = [None] * 2
+        l[0] = Foo()
+        l[1] = Bar()
+        l2 = [l[1], l[0], l[0]]
+        l = l + [None] * 3
+        i = 2
+        for x in l2:
+            l[i] = x
+            i += 1
+        return str(l)
+    res = interpret(fn, [])
+    assert ''.join(res.chars) == '[<Foo object>, <Bar object>, <Bar object>, <Foo object>, <Foo object>]'
+
 def test_list_slice_minusone():
     def fn(i):
         lst = [i, i+1, i+2]
@@ -414,6 +704,14 @@
     res = interpret(fn, [5])
     assert res == 42
 
+    def fn(i):
+        lst = [i, i+1, i+2, 7]
+        lst.pop()
+        lst2 = lst[:-1]
+        return lst[-1] * lst2[-1]
+    res = interpret(fn, [5])
+    assert res == 42
+
 def test_list_multiply():
     def fn(i):
         lst = [i] * i
@@ -476,6 +774,26 @@
     res = interpret(fn, [-2])
     assert res._obj.value == "oups"
 
+    def fn(i):
+        l = [5, 8]
+        l.append(3)
+        try:
+            l[i] = 99
+        except IndexError:
+            pass
+        try:
+            del l[i]
+        except IndexError:
+            pass
+        try:
+            return l[2]    # implicit int->PyObj conversion here
+        except IndexError:
+            return "oups"
+    res = interpret(fn, [6])
+    assert res._obj.value == 3
+    res = interpret(fn, [-2])
+    assert res._obj.value == "oups"
+
 def list_is_clear(lis, idx):
     items = lis._obj.items._obj.items
     for i in range(idx, len(items)):
@@ -537,6 +855,26 @@
     res = interpret(fn, [6])
     assert res == 100
 
+    def fn(i):
+        l = [5, 8]
+        l.append(3)
+        try:
+            l[i] = 99
+        except IndexError:
+            pass
+        try:
+            del l[i]
+        except IndexError:
+            pass
+        try:
+            return l[2]    # implicit int->PyObj conversion here
+        except IndexError:
+            return "oups"
+    res = interpret(fn, [6])
+    assert res._obj.value == 3
+    res = interpret(fn, [-2])
+    assert res._obj.value == "oups"
+
 def test_memoryerror():
     def fn(i):
         lst = [0] * i

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 Oct 26 05:18:45 2005
@@ -299,6 +299,16 @@
             res = interpret(fn, [i,j])
             assert ''.join(res.chars) == fn(i, j)
 
+    def fn(i, j):
+        s1 = [ '', ',', ' and ']
+        s2 = [ [], ['foo'], ['bar', 'baz', 'bazz']]
+        s2[1].extend(['x'])
+        return s1[i].join(s2[j])
+    for i in range(3):
+        for j in range(3):
+            res = interpret(fn, [i,j])
+            assert ''.join(res.chars) == fn(i, j)
+
 def test_parse_fmt():
     assert parse_fmt_string('a') == ['a']
     assert parse_fmt_string('%s') == [('s',)]



More information about the Pypy-commit mailing list