8 from pybind11_tests
import numpy_dtypes
as m
10 np = pytest.importorskip(
"numpy")
13 @pytest.fixture(scope=
"module")
15 ld = np.dtype(
"longdouble")
18 "names": [
"bool_",
"uint_",
"float_",
"ldbl_"],
19 "formats": [
"?",
"u4",
"f4",
"f{}".format(ld.itemsize)],
20 "offsets": [0, 4, 8, (16
if ld.alignment > 4
else 12)],
25 @pytest.fixture(scope=
"module")
27 return np.dtype([(
"bool_",
"?"), (
"uint_",
"u4"), (
"float_",
"f4"), (
"ldbl_",
"g")])
31 from sys
import byteorder
33 e =
"<" if byteorder ==
"little" else ">"
35 "{{'names':['bool_','uint_','float_','ldbl_'],"
36 " 'formats':['?','" + e +
"u4','" + e +
"f4','" + e +
"f{}'],"
37 " 'offsets':[0,4,8,{}], 'itemsize':{}}}"
42 ld = np.dtype(
"longdouble")
43 simple_ld_off = 12 + 4 * (ld.alignment > 4)
44 return dt_fmt().format(ld.itemsize, simple_ld_off, simple_ld_off + ld.itemsize)
48 from sys
import byteorder
50 return "[('bool_', '?'), ('uint_', '{e}u4'), ('float_', '{e}f4'), ('ldbl_', '{e}f{}')]".format(
51 np.dtype(
"longdouble").itemsize, e=
"<" if byteorder ==
"little" else ">"
58 + 4 * (np.dtype(
"uint64").alignment > 4)
60 + 8 * (np.dtype(
"longdouble").alignment > 8)
65 ld = np.dtype(
"longdouble")
67 return dt_fmt().format(ld.itemsize, partial_ld_off, partial_ld_off + ld.itemsize)
71 ld = np.dtype(
"longdouble")
72 partial_nested_off = 8 + 8 * (ld.alignment > 8)
74 partial_nested_size = partial_nested_off * 2 + partial_ld_off + ld.itemsize
75 return "{{'names':['a'], 'formats':[{}], 'offsets':[{}], 'itemsize':{}}}".format(
81 np.testing.assert_equal(actual, np.array(expected_data, dtype=expected_dtype))
85 with pytest.raises(RuntimeError)
as excinfo:
86 m.get_format_unbound()
88 "^NumPy type info missing for .*UnboundStruct.*$",
str(excinfo.value)
91 ld = np.dtype(
"longdouble")
92 ldbl_fmt = (
"4x" if ld.alignment > 4
else "") + ld.char
93 ss_fmt =
"^T{?:bool_:3xI:uint_:f:float_:" + ldbl_fmt +
":ldbl_:}"
94 dbl = np.dtype(
"double")
96 "^T{?:bool_:3xI:uint_:f:float_:"
97 +
str(4 * (dbl.alignment > 4) + dbl.itemsize + 8 * (ld.alignment > 8))
100 nested_extra =
str(max(8, ld.alignment))
101 assert m.print_format_descriptors() == [
103 "^T{?:bool_:I:uint_:f:float_:g:ldbl_:}",
104 "^T{" + ss_fmt +
":a:^T{?:bool_:I:uint_:f:float_:g:ldbl_:}:b:}",
106 "^T{" + nested_extra +
"x" + partial_fmt +
":a:" + nested_extra +
"x}",
108 "^T{(3)4s:a:(2)i:b:(3)B:c:1x(4, 2)f:d:}",
110 "^T{Zf:cflt:Zd:cdbl:}",
115 from sys
import byteorder
117 e =
"<" if byteorder ==
"little" else ">"
119 assert m.print_dtypes() == [
125 "[('a', 'S3'), ('b', 'S3')]",
127 "{{'names':['a','b','c','d'], "
128 +
"'formats':[('S4', (3,)),('"
130 +
"i4', (2,)),('u1', (3,)),('"
133 +
"'offsets':[0,12,20,24], 'itemsize':56}}"
135 "[('e1', '" + e +
"i8'), ('e2', 'u1')]",
136 "[('x', 'i1'), ('y', '" + e +
"u8')]",
137 "[('cflt', '" + e +
"c8'), ('cdbl', '" + e +
"c16')]",
143 "formats": [
"int32",
"float64"],
148 d2 = np.dtype([(
"a",
"i4"), (
"b",
"f4")])
149 assert m.test_dtype_ctors() == [
159 assert m.test_dtype_methods() == [
164 np.dtype(
"int32").itemsize,
165 simple_dtype.itemsize,
168 assert m.trailing_padding_dtype() == m.buffer_to_dtype(
169 np.zeros(1, m.trailing_padding_dtype())
174 elements = [(
False, 0, 0.0, -0.0), (
True, 1, 1.5, -2.5), (
False, 2, 3.0, -5.0)]
177 (m.create_rec_simple, simple_dtype),
178 (m.create_rec_packed, packed_dtype),
181 assert arr.dtype == dtype
186 assert arr.dtype == dtype
191 assert type(arr[0]) == np.void
192 assert type(arr[0].item()) == tuple
194 if dtype == simple_dtype:
195 assert m.print_rec_simple(arr) == [
201 assert m.print_rec_packed(arr) == [
207 nested_dtype = np.dtype([(
"a", simple_dtype), (
"b", packed_dtype)])
209 arr = m.create_rec_nested(0)
210 assert arr.dtype == nested_dtype
213 arr = m.create_rec_nested(3)
214 assert arr.dtype == nested_dtype
218 ((
False, 0, 0.0, -0.0), (
True, 1, 1.5, -2.5)),
219 ((
True, 1, 1.5, -2.5), (
False, 2, 3.0, -5.0)),
220 ((
False, 2, 3.0, -5.0), (
True, 3, 4.5, -7.5)),
224 assert m.print_rec_nested(arr) == [
225 "n:a=s:0,0,0,-0;b=p:1,1,1.5,-2.5",
226 "n:a=s:1,1,1.5,-2.5;b=p:0,2,3,-5",
227 "n:a=s:0,2,3,-5;b=p:1,3,4.5,-7.5",
230 arr = m.create_rec_partial(3)
232 partial_dtype = arr.dtype
233 assert "" not in arr.dtype.fields
234 assert partial_dtype.itemsize > simple_dtype.itemsize
238 arr = m.create_rec_partial_nested(3)
240 assert "" not in arr.dtype.fields
241 assert "" not in arr.dtype.fields[
"a"][0].fields
242 assert arr.dtype.itemsize > partial_dtype.itemsize
243 np.testing.assert_equal(arr[
"a"], m.create_rec_partial(3))
247 data = np.arange(1, 7, dtype=
"int32")
249 np.testing.assert_array_equal(m.test_array_ctors(10 + i), data.reshape((3, 2)))
250 np.testing.assert_array_equal(m.test_array_ctors(20 + i), data.reshape((3, 2)))
252 np.testing.assert_array_equal(m.test_array_ctors(30 + i), data)
253 np.testing.assert_array_equal(m.test_array_ctors(40 + i), data)
257 arr = m.create_string_array(
True)
258 assert str(arr.dtype) ==
"[('a', 'S3'), ('b', 'S3')]"
259 assert m.print_string_array(arr) == [
266 assert arr[
"a"].tolist() == [b
"", b
"a", b
"ab", b
"abc"]
267 assert arr[
"b"].tolist() == [b
"", b
"a", b
"ab", b
"abc"]
268 arr = m.create_string_array(
False)
269 assert dtype == arr.dtype
273 from sys
import byteorder
275 e =
"<" if byteorder ==
"little" else ">"
277 arr = m.create_array_array(3)
278 assert str(arr.dtype) == (
279 "{{'names':['a','b','c','d'], "
280 +
"'formats':[('S4', (3,)),('"
282 +
"i4', (2,)),('u1', (3,)),('{e}f4', (4, 2))], "
283 +
"'offsets':[0,12,20,24], 'itemsize':56}}"
285 assert m.print_array_array(arr) == [
286 "a={{A,B,C,D},{K,L,M,N},{U,V,W,X}},b={0,1},"
287 +
"c={0,1,2},d={{0,1},{10,11},{20,21},{30,31}}",
288 "a={{W,X,Y,Z},{G,H,I,J},{Q,R,S,T}},b={1000,1001},"
289 +
"c={10,11,12},d={{100,101},{110,111},{120,121},{130,131}}",
290 "a={{S,T,U,V},{C,D,E,F},{M,N,O,P}},b={2000,2001},"
291 +
"c={20,21,22},d={{200,201},{210,211},{220,221},{230,231}}",
293 assert arr[
"a"].tolist() == [
294 [b
"ABCD", b
"KLMN", b
"UVWX"],
295 [b
"WXYZ", b
"GHIJ", b
"QRST"],
296 [b
"STUV", b
"CDEF", b
"MNOP"],
298 assert arr[
"b"].tolist() == [[0, 1], [1000, 1001], [2000, 2001]]
299 assert m.create_array_array(0).dtype == arr.dtype
303 from sys
import byteorder
305 e =
"<" if byteorder ==
"little" else ">"
307 arr = m.create_enum_array(3)
309 assert dtype == np.dtype([(
"e1", e +
"i8"), (
"e2",
"u1")])
310 assert m.print_enum_array(arr) == [
"e1=A,e2=X",
"e1=B,e2=Y",
"e1=A,e2=X"]
311 assert arr[
"e1"].tolist() == [-1, 1, -1]
312 assert arr[
"e2"].tolist() == [1, 2, 1]
313 assert m.create_enum_array(0).dtype == dtype
317 from sys
import byteorder
319 e =
"<" if byteorder ==
"little" else ">"
321 arr = m.create_complex_array(3)
323 assert dtype == np.dtype([(
"cflt", e +
"c8"), (
"cdbl", e +
"c16")])
324 assert m.print_complex_array(arr) == [
325 "c:(0,0.25),(0.5,0.75)",
326 "c:(1,1.25),(1.5,1.75)",
327 "c:(2,2.25),(2.5,2.75)",
329 assert arr[
"cflt"].tolist() == [0.0 + 0.25j, 1.0 + 1.25j, 2.0 + 2.25j]
330 assert arr[
"cdbl"].tolist() == [0.5 + 0.75j, 1.5 + 1.75j, 2.5 + 2.75j]
331 assert m.create_complex_array(0).dtype == dtype
336 doc(m.create_rec_nested)
337 ==
"create_rec_nested(arg0: int) -> numpy.ndarray[NestedStruct]"
344 m.create_rec_simple(n),
345 m.create_rec_packed(n),
346 m.create_rec_nested(n),
347 m.create_enum_array(n),
349 funcs = [m.f_simple, m.f_packed, m.f_nested]
351 for i, func
in enumerate(funcs):
352 for j, arr
in enumerate(arrays):
354 assert [func(arr[k])
for k
in range(n)] == [k * 10
for k
in range(n)]
356 with pytest.raises(TypeError)
as excinfo:
358 assert "incompatible function arguments" in str(excinfo.value)
363 array = m.create_rec_simple(n)
364 values = m.f_simple_vectorized(array)
365 np.testing.assert_array_equal(values, [0, 10, 20])
366 array_2 = m.f_simple_pass_thru_vectorized(array)
367 np.testing.assert_array_equal(array, array_2)
372 assert s.astuple() == (
False, 0, 0.0, 0.0)
373 assert m.SimpleStruct.fromtuple(s.astuple()).astuple() == s.astuple()
376 assert m.f_simple(s) == 20
379 s_recarray = np.array([(
False, 2, 0.0, 0.0)], dtype=simple_dtype)
381 np.testing.assert_array_equal(m.f_simple_vectorized(s_recarray), [20])
384 s_scalar = s_recarray[0]
386 assert m.f_simple(s_scalar) == 20
390 s_recarray_scalar = s_recarray.reshape(())
391 assert isinstance(s_recarray_scalar, np.ndarray)
392 assert s_recarray_scalar.dtype == simple_dtype
393 with pytest.raises(TypeError)
as excinfo:
394 m.f_simple(s_recarray_scalar)
395 assert "incompatible function arguments" in str(excinfo.value)
397 assert m.f_simple(m.SimpleStruct.fromtuple(s_recarray_scalar.item())) == 20
400 s_array_object = np.array([s])
401 assert s_array_object.dtype == object
402 with pytest.raises(TypeError)
as excinfo:
403 m.f_simple_vectorized(s_array_object)
404 assert "incompatible function arguments" in str(excinfo.value)
406 s_array = np.array([s.astuple()], dtype=simple_dtype)
407 np.testing.assert_array_equal(m.f_simple_vectorized(s_array), [20])
411 with pytest.raises(RuntimeError)
as excinfo:
413 assert "dtype is already registered" in str(excinfo.value)
416 @pytest.mark.xfail(
"env.PYPY")
418 from sys
import getrefcount
422 start = getrefcount(fmt)
423 d = m.dtype_wrapper(fmt)
424 assert d
is np.dtype(
"f4")
427 assert getrefcount(fmt) == start
431 assert all(m.compare_buffer_info())