def test_assignment_arrfunc(self): af = _lowlevel.make_arrfunc_from_assignment( ndt.float32, ndt.int64, "nocheck") self.assertEqual(nd.as_py(af.proto), ndt.type("(int64) -> float32")) a = nd.array(1234, type=ndt.int64) b = af(a) self.assertEqual(nd.type_of(b), ndt.float32) self.assertEqual(nd.as_py(b), 1234) # Instantiate as a strided kernel with _lowlevel.ckernel.CKernelBuilder() as ckb: meta = (ctypes.c_void_p * 2)() ectx = nd.eval_context() _lowlevel.arrfunc_instantiate(af, ckb, 0, ndt.float32, 0, [ndt.int64], [0], "strided", ectx._ectx_ptr) ck = ckb.ckernel(_lowlevel.ExprStridedOperation) # Do an assignment using ctypes i64 = (ctypes.c_int64 * 3)() for i, v in enumerate([3,7,21]): i64[i] = v pi64 = ctypes.pointer(i64) i64_stride = c_ssize_t(8) f32 = (ctypes.c_float * 3)() ck(ctypes.addressof(f32), 4, ctypes.addressof(pi64), ctypes.pointer(i64_stride), 3) self.assertEqual([f32[i] for i in range(3)], [3,7,21])
def test_creation(self): af = nd.empty('arrfunc') self.assertEqual(nd.type_of(af).type_id, 'arrfunc') # Test there is a string version of a NULL arrfunc self.assertTrue(str(af) != '') self.assertEqual(nd.as_py(af.proto), ndt.type()) # Test there is a string version of an initialized arrfunc af = _lowlevel.make_arrfunc_from_assignment( ndt.float32, ndt.int64, "nocheck") self.assertTrue(str(af) != '') self.assertEqual(nd.as_py(af.proto), ndt.type("(int64) -> float32"))