def test_fixed_string_type_properties(self): d = ndt.make_fixed_string(10, 'ascii') self.assertEqual(str(d), "fixed_string[10, 'ascii']") self.assertEqual(d.data_size, 10) self.assertEqual(d.data_alignment, 1) # self.assertEqual(d.encoding, 'ascii') d = ndt.make_fixed_string(10, 'ucs2') self.assertEqual(str(d), "fixed_string[10, 'ucs2']") self.assertEqual(d.data_size, 20) self.assertEqual(d.data_alignment, 2) # self.assertEqual(d.encoding, 'ucs2') d = ndt.make_fixed_string(10, 'utf8') self.assertEqual(str(d), 'fixed_string[10]') self.assertEqual(d.data_size, 10) self.assertEqual(d.data_alignment, 1) # self.assertEqual(d.encoding, 'utf8') d = ndt.make_fixed_string(10, 'utf16') self.assertEqual(str(d), "fixed_string[10, 'utf16']") self.assertEqual(d.data_size, 20) self.assertEqual(d.data_alignment, 2) # self.assertEqual(d.encoding, 'utf16') d = ndt.make_fixed_string(10, 'utf32') self.assertEqual(str(d), "fixed_string[10, 'utf32']") self.assertEqual(d.data_size, 40) self.assertEqual(d.data_alignment, 4)
def test_fixed_string_type_properties(self): d = ndt.make_fixed_string(10, 'ascii') self.assertEqual(str(d), "fixed_string[10,'ascii']") self.assertEqual(d.data_size, 10) self.assertEqual(d.data_alignment, 1) # self.assertEqual(d.encoding, 'ascii') d = ndt.make_fixed_string(10, 'ucs2') self.assertEqual(str(d), "fixed_string[10,'ucs2']") self.assertEqual(d.data_size, 20) self.assertEqual(d.data_alignment, 2) # self.assertEqual(d.encoding, 'ucs2') d = ndt.make_fixed_string(10, 'utf8') self.assertEqual(str(d), 'fixed_string[10]') self.assertEqual(d.data_size, 10) self.assertEqual(d.data_alignment, 1) # self.assertEqual(d.encoding, 'utf8') d = ndt.make_fixed_string(10, 'utf16') self.assertEqual(str(d), "fixed_string[10,'utf16']") self.assertEqual(d.data_size, 20) self.assertEqual(d.data_alignment, 2) # self.assertEqual(d.encoding, 'utf16') d = ndt.make_fixed_string(10, 'utf32') self.assertEqual(str(d), "fixed_string[10,'utf32']") self.assertEqual(d.data_size, 40) self.assertEqual(d.data_alignment, 4)
def test__type_from_numpy_dtype(self): # Tests converting numpy dtypes to dynd types # native byte order self.assertEqual(ndt.bool, ndt.type(np.dtype(np.bool))) self.assertEqual(ndt.int8, ndt.type(np.dtype(np.int8))) self.assertEqual(ndt.int16, ndt.type(np.dtype(np.int16))) self.assertEqual(ndt.int32, ndt.type(np.dtype(np.int32))) self.assertEqual(ndt.int64, ndt.type(np.dtype(np.int64))) self.assertEqual(ndt.uint8, ndt.type(np.dtype(np.uint8))) self.assertEqual(ndt.uint16, ndt.type(np.dtype(np.uint16))) self.assertEqual(ndt.uint32, ndt.type(np.dtype(np.uint32))) self.assertEqual(ndt.uint64, ndt.type(np.dtype(np.uint64))) self.assertEqual(ndt.float32, ndt.type(np.dtype(np.float32))) self.assertEqual(ndt.float64, ndt.type(np.dtype(np.float64))) self.assertEqual(ndt.complex_float32, ndt.type(np.dtype(np.complex64))) self.assertEqual(ndt.complex_float64, ndt.type(np.dtype(np.complex128))) self.assertEqual(ndt.make_fixed_string(10, 'ascii'), ndt.type(np.dtype('S10'))) self.assertEqual(ndt.make_fixed_string(10, 'utf_32'), ndt.type(np.dtype('U10'))) # non-native byte order nonnative = self.nonnative """
def test_numpy_dynd_fixed_string_interop(self): # Tests converting fixed-size string arrays to/from numpy # ASCII Numpy -> dynd a = np.array(['abc', 'testing', 'array']) b = nd.view(a) if sys.version_info >= (3, 0): self.assertEqual(ndt.make_fixed_string(7, 'utf_32'), nd.dtype_of(b)) else: self.assertEqual(ndt.make_fixed_string(7, 'ascii'), nd.dtype_of(b)) self.assertEqual(nd.dtype_of(b), ndt.type(a.dtype)) # Make sure it's ascii a = a.astype('S7') b = nd.view(a) # ASCII dynd -> Numpy c = np.asarray(b) self.assertEqual(a.dtype, c.dtype) assert_array_equal(a, c) # verify 'a' and 'c' are looking at the same data a[1] = 'modify' assert_array_equal(a, c) # ASCII dynd -> UTF32 dynd b_u = b.cast(ndt.make_fixed_dim(3, ndt.make_fixed_string(7, 'utf_32'))) # Evaluate to its value array b_u = b_u self.assertEqual( ndt.make_fixed_string(7, 'utf_32'), nd.dtype_of(b_u)) # UTF32 dynd -> Numpy c_u = np.asarray(b_u) self.assertEqual(nd.dtype_of(b_u), ndt.type(c_u.dtype)) assert_array_equal(a.astype('U'), c_u) # 'a' and 'c_u' are not looking at the same data a[1] = 'diff' self.assertFalse(np.all(a == c_u))
def test_numpy_dynd_fixed_string_interop(self): # Tests converting fixed-size string arrays to/from numpy # ASCII Numpy -> dynd a = np.array(['abc', 'testing', 'array']) b = nd.view(a) if sys.version_info >= (3, 0): self.assertEqual(ndt.make_fixed_string(7, 'utf_32'), nd.dtype_of(b)) else: self.assertEqual(ndt.make_fixed_string(7, 'ascii'), nd.dtype_of(b)) self.assertEqual(nd.dtype_of(b), ndt.type(a.dtype)) # Make sure it's ascii a = a.astype('S7') b = nd.view(a) # ASCII dynd -> Numpy c = np.asarray(b) self.assertEqual(a.dtype, c.dtype) assert_array_equal(a, c) # verify 'a' and 'c' are looking at the same data a[1] = 'modify' assert_array_equal(a, c) # ASCII dynd -> UTF32 dynd b_u = b.ucast(ndt.make_fixed_string(7, 'utf_32')) self.assertEqual( ndt.make_convert( ndt.make_fixed_string(7, 'utf_32'), ndt.make_fixed_string(7, 'ascii')), nd.dtype_of(b_u)) # Evaluate to its value array b_u = b_u.eval() self.assertEqual( ndt.make_fixed_string(7, 'utf_32'), nd.dtype_of(b_u)) # UTF32 dynd -> Numpy c_u = np.asarray(b_u) self.assertEqual(nd.dtype_of(b_u), ndt.type(c_u.dtype)) assert_array_equal(a.astype('U'), c_u) # 'a' and 'c_u' are not looking at the same data a[1] = 'diff' self.assertFalse(np.all(a == c_u))
def test_utf_encodings(self): # Ensure all of the UTF encodings work ok for a basic string x = u'\uc548\ub155 hello' # UTF-8 a = nd.array(x) a = a.cast(ndt.make_fixed_string(16, 'utf_8')) a = a.eval() self.assertEqual(nd.type_of(a), ndt.make_fixed_string(16, 'utf_8')) self.assertEqual(type(nd.as_py(a)), unicode) self.assertEqual(nd.as_py(a), x) # UTF-16 a = nd.array(x) a = a.cast(ndt.make_fixed_string(8, 'utf_16')) a = a.eval() self.assertEqual(nd.type_of(a), ndt.make_fixed_string(8, 'utf_16')) self.assertEqual(type(nd.as_py(a)), unicode) self.assertEqual(nd.as_py(a), x) # UTF-32 a = nd.array(x) a = a.cast(ndt.make_fixed_string(8, 'utf_32')) a = a.eval() self.assertEqual(nd.type_of(a), ndt.make_fixed_string(8, 'utf_32')) self.assertEqual(type(nd.as_py(a)), unicode) self.assertEqual(nd.as_py(a), x)