def test_modify(): r3 = CudaRn(3) xd = r3.element([1, 2, 3]) yd = r3.element(data_ptr=xd.data_ptr) yd[:] = [5, 6, 7] assert all_equal(xd, yd)
def test_offset_sub_vector(): r6 = CudaRn(6) r3 = CudaRn(3) xd = r6.element([1, 2, 3, 4, 5, 6]) yd = r3.element(data_ptr=xd.data_ptr + 3 * xd.space.dtype.itemsize) yd[:] = [7, 8, 9] assert all_equal([1, 2, 3, 7, 8, 9], xd)
def test_sub_vector(): r6 = CudaRn(6) r3 = CudaRn(3) xd = r6.element([1, 2, 3, 4, 5, 6]) yd = r3.element(data_ptr=xd.data_ptr) yd[:] = [7, 8, 9] assert all_almost_equal([7, 8, 9, 4, 5, 6], xd)
def test_ndarray_init(): r3 = CudaRn(3) x0 = np.array([1., 2., 3.]) x = r3.element(x0) assert all_equal(x, x0) x0 = np.array([1, 2, 3], dtype=float64) x = r3.element(x0) assert all_equal(x, x0) x0 = np.array([1, 2, 3], dtype=int) x = r3.element(x0) assert all_equal(x, x0)
def test_getslice_index_error(): r3 = CudaRn(3) xd = r3.element([1, 2, 3]) # Bad slice with pytest.raises(IndexError): xd[10:13]
def _test_getslice(slice): # Validate get against python list behaviour r6 = CudaRn(6) y = [0, 1, 2, 3, 4, 5] x = r6.element(y) assert all_equal(x[slice], y[slice])
def test_setitem(): r3 = CudaRn(3) x = r3.element([42, 42, 42]) for index in [0, 1, 2, -1, -2, -3]: x[index] = index assert x[index] == index
def test_getitem(): r3 = CudaRn(3) y = [1, 2, 3] x = r3.element(y) for index in [0, 1, 2, -1, -2, -3]: assert x[index] == y[index]
def test_setitem_index_error(): r3 = CudaRn(3) x = r3.element([1, 2, 3]) with pytest.raises(IndexError): x[-4] = 0 with pytest.raises(IndexError): x[3] = 0
def test_inner(): r3 = CudaRn(3) x = r3.element([1, 2, 3]) y = r3.element([5, 3, 9]) correct_inner = 1 * 5 + 2 * 3 + 3 * 9 # Space function assert almost_equal(r3.inner(x, y), correct_inner) # Exponent != 2 -> no inner product r3 = CudaRn(3, exponent=1) x = r3.element([1, 2, 3]) y = r3.element([5, 3, 9]) with pytest.raises(NotImplementedError): r3.inner(x, y) with pytest.raises(NotImplementedError): x.inner(y)
def _test_setslice(slice): # Validate set against python list behaviour r6 = CudaRn(6) z = [7, 8, 9, 10, 11, 10] y = [0, 1, 2, 3, 4, 5] x = r6.element(y) x[slice] = z[slice] y[slice] = z[slice] assert all_equal(x, y)
def test_slice_is_view(): # Verify that modifications of a view modify the original data r10 = CudaRn(10) xh = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) xd = r10.element(xh) yh = xh[1:8:2] yh[:] = [0, 0, 0, 0] yd = xd[1:8:2] yd[:] = [0, 0, 0, 0] assert all_equal(xh, xd) assert all_equal(yh, yd)
def test_slice_of_slice(): # Verify that creating slices from slices works as expected r10 = CudaRn(10) xh = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] xd = r10.element(xh) yh = xh[1:8:2] yd = xd[1:8:2] assert all_equal(yh, yd) zh = yh[1::2] zd = yd[1::2] assert all_equal(zh, zd)
def test_setslice_index_error(): r3 = CudaRn(3) xd = r3.element([1, 2, 3]) # Bad slice with pytest.raises(IndexError): xd[10:13] = [1, 2, 3] # Bad size of rhs with pytest.raises(IndexError): xd[:] = [] with pytest.raises(IndexError): xd[:] = [1, 2] with pytest.raises(IndexError): xd[:] = [1, 2, 3, 4]
def test_vector_init(): rn = CudaRn(5) weight_vec = _pos_vector(rn) CudaFnVectorWeighting(weight_vec) CudaFnVectorWeighting(rn.element(weight_vec))
def test_iterator(): r3 = CudaRn(3) y = [1, 2, 3] x = r3.element(y) assert all_equal([a for a in x], [b for b in y])