Author: Richard Plangger <[email protected]>
Branch: py3.5-memoryview
Changeset: r86685:981849b5f33c
Date: 2016-08-29 13:08 +0200
http://bitbucket.org/pypy/pypy/changeset/981849b5f33c/

Log:    missing test code in last merge

diff --git a/pypy/objspace/std/test/test_memoryobject.py 
b/pypy/objspace/std/test/test_memoryobject.py
--- a/pypy/objspace/std/test/test_memoryobject.py
+++ b/pypy/objspace/std/test/test_memoryobject.py
@@ -177,3 +177,178 @@
 
     def test_hex(self):
         assert memoryview(b"abc").hex() == u'616263'
+
+class MockBuffer(Buffer):
+    def __init__(self, space, w_arr, w_dim, w_fmt, \
+                 w_itemsize, w_strides, w_shape):
+        self.space = space
+        self.w_arr = w_arr
+        self.arr = []
+        self.ndim = space.int_w(w_dim)
+        self.format = space.str_w(w_fmt)
+        self.itemsize = space.int_w(w_itemsize)
+        self.strides = []
+        for w_i in w_strides.getitems_unroll():
+            self.strides.append(space.int_w(w_i))
+        self.shape = []
+        for w_i in w_shape.getitems_unroll():
+            self.shape.append(space.int_w(w_i))
+        self.readonly = True
+        self.shape.append(space.len_w(w_arr))
+        self.data = []
+        itemsize = 1
+        worklist = [(1,w_arr)]
+        while worklist:
+            dim, w_work = worklist.pop()
+            if space.isinstance_w(w_work, space.w_list):
+                for j, w_obj in enumerate(w_work.getitems_unroll()):
+                    worklist.insert(0, (dim+1, w_obj))
+                continue
+            byte = struct.pack(self.format, space.int_w(w_work))
+            for c in byte:
+                self.data.append(c)
+        self.data = ''.join(self.data)
+
+    def getslice(self, start, stop, step, size):
+        items = []
+        if size == 0:
+            return ''
+        return ''.join([self.getitem(i) for i in range(start,stop,step)])
+        #bytecount = (stop - start)
+        ## data is stores as list of ints, thus this gets around the
+        ## issue that one cannot advance in bytes
+        #count = bytecount // size
+        #start = start // size
+        #for i in range(start, start+count, step):
+        #    items.append(self.getitem(i))
+        #return ''.join(items)
+
+
+    def getformat(self):
+        return self.format
+
+    def getitem(self, index):
+        return self.data[index:index+1]
+
+    def getlength(self):
+        return len(self.data)
+
+    def getitemsize(self):
+        return self.itemsize
+
+    def getndim(self):
+        return self.ndim
+
+    def getstrides(self):
+        return self.strides
+
+    def getshape(self):
+        return self.shape
+
+    def is_contiguous(self, format):
+        return format == 'C'
+
+class W_MockArray(W_Root):
+    def __init__(self, w_list, w_dim, w_fmt, w_size, w_strides, w_shape):
+        self.w_list = w_list
+        self.w_dim = w_dim
+        self.w_fmt = w_fmt
+        self.w_size = w_size
+        self.w_strides = w_strides
+        self.w_shape = w_shape
+
+    @staticmethod
+    def descr_new(space, w_type, w_list, w_dim, w_fmt, \
+                         w_size, w_strides, w_shape):
+        return W_MockArray(w_list, w_dim, w_fmt, w_size, w_strides, w_shape)
+
+    def buffer_w(self, space, flags):
+        return MockBuffer(space, self.w_list, self.w_dim, self.w_fmt, \
+                          self.w_size, self.w_strides, self.w_shape)
+
+    def buffer_w_ex(self, space, flags):
+        return self.buffer_w(space, flags), space.str_w(self.w_fmt), 
space.int_w(self.w_size)
+
+W_MockArray.typedef = TypeDef("MockArray",
+    __new__ = interp2app(W_MockArray.descr_new),
+)
+
+from pypy.objspace.std.transparent import register_proxyable
+from pypy.conftest import option
+
+class AppTestMemoryViewMockBuffer(object):
+    spaceconfig = dict(usemodules=[])
+    def setup_class(cls):
+        if option.runappdirect:
+            py.test.skip("Impossible to run on appdirect")
+        cls.w_MockArray = cls.space.gettypefor(W_MockArray)
+
+    def test_tuple_indexing(self):
+        content = self.MockArray([[0,1,2,3], [4,5,6,7], [8,9,10,11]],
+                                 dim=2, fmt='B', size=1,
+                                 strides=[4,1], shape=[3,4])
+        view = memoryview(content)
+        assert view[0,0] == 0
+        assert view[2,0] == 8
+        assert view[2,3] == 11
+        assert view[-1,-1] == 11
+        assert view[-3,-4] == 0
+
+        try:
+            view.__getitem__((2**63-1,0))
+            assert False, "must not succeed"
+        except IndexError: pass
+
+        try:
+            view.__getitem__((0, 0, 0))
+            assert False, "must not succeed"
+        except TypeError: pass
+
+    def test_tuple_indexing_int(self):
+        content = self.MockArray([ [[1],[2],[3]], [[4],[5],[6]] ],
+                                 dim=3, fmt='i', size=4,
+                                 strides=[12,4,4], shape=[2,3,1])
+        view = memoryview(content)
+        assert view[0,0,0] == 1
+        assert view[-1,2,0] == 6
+
+    def test_cast_non_byte(self):
+        empty = self.MockArray([], dim=1, fmt='i', size=4, strides=[1], 
shape=[1])
+        view = memoryview(empty)
+        try:
+            view.cast('l')
+            assert False, "i -> l not possible. buffer must be byte format"
+        except TypeError:
+            pass
+
+    def test_cast_empty(self):
+        empty = self.MockArray([], dim=1, fmt='b', size=1, strides=[1], 
shape=[1])
+        view = memoryview(empty)
+        cview = view.cast('i')
+        assert cview.tobytes() == b''
+        assert cview.tolist() == []
+        assert view.format == 'b'
+        assert cview.format == 'i'
+        #
+        #assert cview.cast('i').cast('b').cast('i').tolist() == []
+        #
+        assert cview.format == 'i'
+        try:
+            cview.cast('i')
+            assert False, "cast must fail"
+        except TypeError:
+            pass
+
+    def test_cast_with_shape(self):
+        empty = self.MockArray([1,0,2,0,3,0],
+                    dim=1, fmt='h', size=2,
+                    strides=[8], shape=[6])
+        view = memoryview(empty)
+        byteview = view.cast('b')
+        #assert byteview.tolist() == [1,0,0,0,2,0,0,0,3,0,0,0]
+        i32view = byteview.cast('i', shape=[1,3])
+        assert i32view.format == 'i'
+        assert i32view.itemsize == 4
+        assert i32view.tolist() == [[1,2,3]]
+        i32view = byteview.cast('i', shape=(1,3))
+        assert i32view.tolist() == [[1,2,3]]
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to