def test_resize(self): data = np.zeros(10) B = DataBuffer(data=data) data = np.zeros(20) B.set_data(data) assert B.nbytes == data.nbytes assert B._need_resize is True
def test_set_data_base(self): dtype = np.dtype([('position', np.float32, 3), ('texcoord', np.float32, 2), ('color', np.float32, 4)]) data = np.zeros(10, dtype=dtype) B = DataBuffer(data, store=True) B.set_data(data) assert len(B._pending_data) == 1
def test_set_data_offset(self): data = np.zeros(100, np.float32) subdata = data[:10] B = DataBuffer(data) B.set_data(subdata, offset=10) offset = B._pending_data[-1][2] print (offset) assert offset == 10*4
def test_invalid_view_after_set_data(self): dtype = np.dtype([('position', np.float32, 3), ('texcoord', np.float32, 2), ('color', np.float32, 4)]) data = np.zeros(10, dtype=dtype) B = DataBuffer(data) Z = B[5:] B.set_data(np.zeros(15, dtype=dtype)) assert Z._valid is False
def test_getitem_field(self): dtype = np.dtype([('position', np.float32, 3), ('texcoord', np.float32, 2), ('color', np.float32, 4)]) data = np.zeros(10, dtype=dtype) B = DataBuffer(data) Z = B["position"] assert Z.nbytes == 10 * 3 * np.dtype(np.float32).itemsize assert Z.offset == 0 assert Z.size == 10 assert Z.itemsize == 3 * np.dtype(np.float32).itemsize assert Z.stride == (3 + 2 + 4) * np.dtype(np.float32).itemsize assert Z.dtype == (np.float32, 3) Z = B["texcoord"] assert Z.nbytes == 10 * 2 * np.dtype(np.float32).itemsize assert Z.offset == 3 * np.dtype(np.float32).itemsize assert Z.size == 10 assert Z.itemsize == 2 * np.dtype(np.float32).itemsize assert Z.stride == (3 + 2 + 4) * np.dtype(np.float32).itemsize assert Z.dtype == (np.float32, 2) Z = B["color"] assert Z.nbytes == 10 * 4 * np.dtype(np.float32).itemsize assert Z.offset == (2 + 3) * np.dtype(np.float32).itemsize assert Z.size == 10 assert Z.itemsize == 4 * np.dtype(np.float32).itemsize assert Z.stride == (3 + 2 + 4) * np.dtype(np.float32).itemsize assert Z.dtype == (np.float32, 4)
def test_no_resize_ellipsis(self): data = np.zeros(10) B = DataBuffer(data=data) data = np.zeros(30) # with self.assertRaises(ValueError): # B[...] = data self.assertRaises(ValueError, B.__setitem__, Ellipsis, data)
def test_resize_no_resizeable(self): data = np.zeros(10) B = DataBuffer(data=data, resizeable=False) data = np.zeros(20) # with self.assertRaises(ValueError): # B.set_data(data) self.assertRaises(ValueError, B.set_data, data)
def test_setitem_broadcast(self): dtype = np.dtype([('position', np.float32, 3), ('texcoord', np.float32, 2), ('color', np.float32, 4)]) data = np.zeros(10, dtype=dtype) B = DataBuffer(data, store=True) B['position'] = 1, 2, 3 assert np.allclose(data['position'].ravel(), np.resize([1, 2, 3], 30))
def test_setitem_field_no_storage(self): dtype = np.dtype([('position', np.float32, 3), ('texcoord', np.float32, 2), ('color', np.float32, 4)]) data = np.zeros(10, dtype=dtype) B = DataBuffer(data, store=False) # with self.assertRaises(ValueError): # B['position'] = 1, 2, 3 self.assertRaises(ValueError, B.__setitem__, 'position', (1, 2, 3))
def test_every_two_item_no_storage(self): dtype = np.dtype([('position', np.float32, 3), ('texcoord', np.float32, 2), ('color', np.float32, 4)]) data = np.zeros(10, dtype=dtype) B = DataBuffer(data, store=False) # with self.assertRaises(ValueError): # B[::2] = data[::2] s = slice(None, None, 2) self.assertRaises(ValueError, B.__setitem__, s, data[::2])
def test_setitem_strided(self): dtype = np.dtype([('position', np.float32, 3), ('texcoord', np.float32, 2), ('color', np.float32, 4)]) data1 = np.zeros(10, dtype=dtype) data2 = np.ones(10, dtype=dtype) B = DataBuffer(data1, store=True) B[::2] = data2[::2] assert np.allclose(data1['position'][::2], data2['position'][::2]) assert np.allclose(data1['texcoord'][::2], data2['texcoord'][::2]) assert np.allclose(data1['color'][::2], data2['color'][::2])
def test_setitem_half(self): dtype = np.dtype([('position', np.float32, 3), ('texcoord', np.float32, 2), ('color', np.float32, 4)]) data1 = np.zeros(10, dtype=dtype) data2 = np.ones(10, dtype=dtype) B = DataBuffer(data1, store=True) B[:5] = data2[:5] assert np.allclose(data1['position'][:5], data2['position'][:5]) assert np.allclose(data1['texcoord'][:5], data2['texcoord'][:5]) assert np.allclose(data1['color'][:5], data2['color'][:5]) assert len(B._pending_data) == 2
def test_structured_init(self): # Check structured type dtype = np.dtype([('position', np.float32, 3), ('texcoord', np.float32, 2), ('color', np.float32, 4)]) data = np.zeros(10, dtype=dtype) B = DataBuffer(data) assert B.nbytes == data.nbytes assert B.offset == 0 assert B.size == 10 assert B.itemsize == data.itemsize assert B.stride == data.itemsize assert B.dtype == data.dtype
def test_getitem_index(self): dtype = np.dtype([('position', np.float32, 3), ('texcoord', np.float32, 2), ('color', np.float32, 4)]) data = np.zeros(10, dtype=dtype) B = DataBuffer(data) Z = B[0] assert Z.nbytes == 1 * (3 + 2 + 4) * np.dtype(np.float32).itemsize assert Z.offset == 0 assert Z.size == 1 assert Z.itemsize == (3 + 2 + 4) * np.dtype(np.float32).itemsize assert Z.stride == (3 + 2 + 4) * np.dtype(np.float32).itemsize assert Z.dtype == B.dtype
def test_set_data_base_view(self): dtype = np.dtype([('position', np.float32, 3), ('texcoord', np.float32, 2), ('color', np.float32, 4)]) data = np.zeros(10, dtype=dtype) B = DataBuffer(data, store=True) # set_data on field is not allowed because set_data # can result in a buffer resize # with self.assertRaises(ValueError): # B['position'].set_data(data) Z = B['position'] self.assertRaises(ValueError, Z.set_data, data)
def test_default_init(self): # Check default storage and copy flags data = np.ones(100) B = DataBuffer(data) assert B._store is True assert B._copy is False assert B.nbytes == data.nbytes assert B.offset == 0 assert B.size == 100 assert B.itemsize == data.itemsize assert B.stride == data.itemsize assert B.dtype == data.dtype assert B._resizeable is True
def test_storage(self): data = np.ones(100) B = DataBuffer(data, store=True) assert B.data.base is data
def test_no_storage_copy(self): data = np.ones(100, np.float32) B = DataBuffer(data, store=False) assert B.data is None assert B.stride == 4
def test_storage_copy(self): data = np.ones(100, np.float32) B = DataBuffer(data.copy(), store=True) # we got rid of copy arg assert B.data is not None assert B.data is not data assert B.stride == 4
def test_non_contiguous_storage(self): # Ask to have CPU storage and to use data as storage # Not possible since data[::2] is not contiguous data = np.ones(100) B = DataBuffer(data[::2], store=True, copy=False) assert B._copy == True