27433934f0911ebe1257ace4baf970f5663a68d6
2 from pybind11_tests
import numpy_array
as m
4 pytestmark
= pytest
.requires_numpy
6 with pytest
.suppress(ImportError):
10 @pytest.fixture(scope
='function')
12 return np
.array([[1, 2, 3], [4, 5, 6]], '=u2')
15 def test_array_attributes():
18 assert all(m
.shape(a
) == [])
19 assert all(m
.strides(a
) == [])
20 with pytest
.raises(IndexError) as excinfo
:
22 assert str(excinfo
.value
) == 'invalid axis: 0 (ndim = 0)'
23 with pytest
.raises(IndexError) as excinfo
:
25 assert str(excinfo
.value
) == 'invalid axis: 0 (ndim = 0)'
28 assert m
.itemsize(a
) == 8
29 assert m
.nbytes(a
) == 8
32 a
= np
.array([[1, 2, 3], [4, 5, 6]], 'u2').view()
33 a
.flags
.writeable
= False
35 assert all(m
.shape(a
) == [2, 3])
36 assert m
.shape(a
, 0) == 2
37 assert m
.shape(a
, 1) == 3
38 assert all(m
.strides(a
) == [6, 2])
39 assert m
.strides(a
, 0) == 6
40 assert m
.strides(a
, 1) == 2
41 with pytest
.raises(IndexError) as excinfo
:
43 assert str(excinfo
.value
) == 'invalid axis: 2 (ndim = 2)'
44 with pytest
.raises(IndexError) as excinfo
:
46 assert str(excinfo
.value
) == 'invalid axis: 2 (ndim = 2)'
47 assert not m
.writeable(a
)
49 assert m
.itemsize(a
) == 2
50 assert m
.nbytes(a
) == 12
51 assert not m
.owndata(a
)
54 @pytest.mark
.parametrize('args, ret', [([], 0), ([0], 0), ([1], 3), ([0, 1], 1), ([1, 2], 5)])
55 def test_index_offset(arr
, args
, ret
):
56 assert m
.index_at(arr
, *args
) == ret
57 assert m
.index_at_t(arr
, *args
) == ret
58 assert m
.offset_at(arr
, *args
) == ret
* arr
.dtype
.itemsize
59 assert m
.offset_at_t(arr
, *args
) == ret
* arr
.dtype
.itemsize
62 def test_dim_check_fail(arr
):
63 for func
in (m
.index_at
, m
.index_at_t
, m
.offset_at
, m
.offset_at_t
, m
.data
, m
.data_t
,
64 m
.mutate_data
, m
.mutate_data_t
):
65 with pytest
.raises(IndexError) as excinfo
:
67 assert str(excinfo
.value
) == 'too many indices for an array: 3 (ndim = 2)'
70 @pytest.mark
.parametrize('args, ret',
71 [([], [1, 2, 3, 4, 5, 6]),
73 ([0, 1], [2, 3, 4, 5, 6]),
75 def test_data(arr
, args
, ret
):
76 from sys
import byteorder
77 assert all(m
.data_t(arr
, *args
) == ret
)
78 assert all(m
.data(arr
, *args
)[(0 if byteorder
== 'little' else 1)::2] == ret
)
79 assert all(m
.data(arr
, *args
)[(1 if byteorder
== 'little' else 0)::2] == 0)
82 @pytest.mark
.parametrize('dim', [0, 1, 3])
83 def test_at_fail(arr
, dim
):
84 for func
in m
.at_t
, m
.mutate_at_t
:
85 with pytest
.raises(IndexError) as excinfo
:
86 func(arr
, *([0] * dim
))
87 assert str(excinfo
.value
) == 'index dimension mismatch: {} (ndim = 2)'.format(dim
)
91 assert m
.at_t(arr
, 0, 2) == 3
92 assert m
.at_t(arr
, 1, 0) == 4
94 assert all(m
.mutate_at_t(arr
, 0, 2).ravel() == [1, 2, 4, 4, 5, 6])
95 assert all(m
.mutate_at_t(arr
, 1, 0).ravel() == [1, 2, 4, 5, 5, 6])
98 def test_mutate_readonly(arr
):
99 arr
.flags
.writeable
= False
100 for func
, args
in (m
.mutate_data
, ()), (m
.mutate_data_t
, ()), (m
.mutate_at_t
, (0, 0)):
101 with pytest
.raises(ValueError) as excinfo
:
103 assert str(excinfo
.value
) == 'array is not writeable'
106 def test_mutate_data(arr
):
107 assert all(m
.mutate_data(arr
).ravel() == [2, 4, 6, 8, 10, 12])
108 assert all(m
.mutate_data(arr
).ravel() == [4, 8, 12, 16, 20, 24])
109 assert all(m
.mutate_data(arr
, 1).ravel() == [4, 8, 12, 32, 40, 48])
110 assert all(m
.mutate_data(arr
, 0, 1).ravel() == [4, 16, 24, 64, 80, 96])
111 assert all(m
.mutate_data(arr
, 1, 2).ravel() == [4, 16, 24, 64, 80, 192])
113 assert all(m
.mutate_data_t(arr
).ravel() == [5, 17, 25, 65, 81, 193])
114 assert all(m
.mutate_data_t(arr
).ravel() == [6, 18, 26, 66, 82, 194])
115 assert all(m
.mutate_data_t(arr
, 1).ravel() == [6, 18, 26, 67, 83, 195])
116 assert all(m
.mutate_data_t(arr
, 0, 1).ravel() == [6, 19, 27, 68, 84, 196])
117 assert all(m
.mutate_data_t(arr
, 1, 2).ravel() == [6, 19, 27, 68, 84, 197])
120 def test_bounds_check(arr
):
121 for func
in (m
.index_at
, m
.index_at_t
, m
.data
, m
.data_t
,
122 m
.mutate_data
, m
.mutate_data_t
, m
.at_t
, m
.mutate_at_t
):
123 with pytest
.raises(IndexError) as excinfo
:
125 assert str(excinfo
.value
) == 'index 2 is out of bounds for axis 0 with size 2'
126 with pytest
.raises(IndexError) as excinfo
:
128 assert str(excinfo
.value
) == 'index 4 is out of bounds for axis 1 with size 3'
131 def test_make_c_f_array():
132 assert m
.make_c_array().flags
.c_contiguous
133 assert not m
.make_c_array().flags
.f_contiguous
134 assert m
.make_f_array().flags
.f_contiguous
135 assert not m
.make_f_array().flags
.c_contiguous
139 def assert_references(a
, b
, base
=None):
143 assert a
.__array
_interface
__['data'][0] == b
.__array
_interface
__['data'][0]
144 assert a
.shape
== b
.shape
145 assert a
.strides
== b
.strides
146 assert a
.flags
.c_contiguous
== b
.flags
.c_contiguous
147 assert a
.flags
.f_contiguous
== b
.flags
.f_contiguous
148 assert a
.flags
.writeable
== b
.flags
.writeable
149 assert a
.flags
.aligned
== b
.flags
.aligned
150 assert a
.flags
.updateifcopy
== b
.flags
.updateifcopy
151 assert np
.all(a
== b
)
152 assert not b
.flags
.owndata
153 assert b
.base
is base
154 if a
.flags
.writeable
and a
.ndim
== 2:
156 assert b
[0, 0] == 1234
158 a1
= np
.array([1, 2], dtype
=np
.int16
)
159 assert a1
.flags
.owndata
and a1
.base
is None
161 assert_references(a1
, a2
)
163 a1
= np
.array([[1, 2], [3, 4]], dtype
=np
.float32
, order
='F')
164 assert a1
.flags
.owndata
and a1
.base
is None
166 assert_references(a1
, a2
)
168 a1
= np
.array([[1, 2], [3, 4]], dtype
=np
.float32
, order
='C')
169 a1
.flags
.writeable
= False
171 assert_references(a1
, a2
)
173 a1
= np
.random
.random((4, 4, 4))
175 assert_references(a1
, a2
)
179 assert_references(a1t
, a2
, a1
)
183 assert_references(a1d
, a2
, a1
)
185 a1m
= a1
[::-1, ::-1, ::-1]
187 assert_references(a1m
, a2
, a1
)
190 def test_numpy_view(capture
):
193 ac_view_1
= ac
.numpy_view()
194 ac_view_2
= ac
.numpy_view()
195 assert np
.all(ac_view_1
== np
.array([1, 2], dtype
=np
.int32
))
198 assert capture
== """
200 ArrayClass::numpy_view()
201 ArrayClass::numpy_view()
205 assert ac_view_2
[0] == 4
206 assert ac_view_2
[1] == 3
212 assert capture
== """
217 @pytest.unsupported_on_pypy
218 def test_cast_numpy_int64_to_uint64():
219 m
.function_taking_uint64(123)
220 m
.function_taking_uint64(np
.uint64(123))
223 def test_isinstance():
224 assert m
.isinstance_untyped(np
.array([1, 2, 3]), "not an array")
225 assert m
.isinstance_typed(np
.array([1.0, 2.0, 3.0]))
228 def test_constructors():
229 defaults
= m
.default_constructors()
230 for a
in defaults
.values():
232 assert defaults
["array"].dtype
== np
.array([]).dtype
233 assert defaults
["array_t<int32>"].dtype
== np
.int32
234 assert defaults
["array_t<double>"].dtype
== np
.float64
236 results
= m
.converting_constructors([1, 2, 3])
237 for a
in results
.values():
238 np
.testing
.assert_array_equal(a
, [1, 2, 3])
239 assert results
["array"].dtype
== np
.int_
240 assert results
["array_t<int32>"].dtype
== np
.int32
241 assert results
["array_t<double>"].dtype
== np
.float64
244 def test_overload_resolution(msg
):
245 # Exact overload matches:
246 assert m
.overloaded(np
.array([1], dtype
='float64')) == 'double'
247 assert m
.overloaded(np
.array([1], dtype
='float32')) == 'float'
248 assert m
.overloaded(np
.array([1], dtype
='ushort')) == 'unsigned short'
249 assert m
.overloaded(np
.array([1], dtype
='intc')) == 'int'
250 assert m
.overloaded(np
.array([1], dtype
='longlong')) == 'long long'
251 assert m
.overloaded(np
.array([1], dtype
='complex')) == 'double complex'
252 assert m
.overloaded(np
.array([1], dtype
='csingle')) == 'float complex'
254 # No exact match, should call first convertible version:
255 assert m
.overloaded(np
.array([1], dtype
='uint8')) == 'double'
257 with pytest
.raises(TypeError) as excinfo
:
258 m
.overloaded("not an array")
259 assert msg(excinfo
.value
) == """
260 overloaded(): incompatible function arguments. The following argument types are supported:
261 1. (arg0: numpy.ndarray[float64]) -> str
262 2. (arg0: numpy.ndarray[float32]) -> str
263 3. (arg0: numpy.ndarray[int32]) -> str
264 4. (arg0: numpy.ndarray[uint16]) -> str
265 5. (arg0: numpy.ndarray[int64]) -> str
266 6. (arg0: numpy.ndarray[complex128]) -> str
267 7. (arg0: numpy.ndarray[complex64]) -> str
269 Invoked with: 'not an array'
272 assert m
.overloaded2(np
.array([1], dtype
='float64')) == 'double'
273 assert m
.overloaded2(np
.array([1], dtype
='float32')) == 'float'
274 assert m
.overloaded2(np
.array([1], dtype
='complex64')) == 'float complex'
275 assert m
.overloaded2(np
.array([1], dtype
='complex128')) == 'double complex'
276 assert m
.overloaded2(np
.array([1], dtype
='float32')) == 'float'
278 assert m
.overloaded3(np
.array([1], dtype
='float64')) == 'double'
279 assert m
.overloaded3(np
.array([1], dtype
='intc')) == 'int'
281 overloaded3(): incompatible function arguments. The following argument types are supported:
282 1. (arg0: numpy.ndarray[int32]) -> str
283 2. (arg0: numpy.ndarray[float64]) -> str
287 with pytest
.raises(TypeError) as excinfo
:
288 m
.overloaded3(np
.array([1], dtype
='uintc'))
289 assert msg(excinfo
.value
) == expected_exc
+ " array([1], dtype=uint32)"
290 with pytest
.raises(TypeError) as excinfo
:
291 m
.overloaded3(np
.array([1], dtype
='float32'))
292 assert msg(excinfo
.value
) == expected_exc
+ " array([ 1.], dtype=float32)"
293 with pytest
.raises(TypeError) as excinfo
:
294 m
.overloaded3(np
.array([1], dtype
='complex'))
295 assert msg(excinfo
.value
) == expected_exc
+ " array([ 1.+0.j])"
298 assert m
.overloaded4(np
.array([1], dtype
='double')) == 'double'
299 assert m
.overloaded4(np
.array([1], dtype
='longlong')) == 'long long'
300 # Non-exact matches requiring conversion. Since float to integer isn't a
301 # save conversion, it should go to the double overload, but short can go to
302 # either (and so should end up on the first-registered, the long long).
303 assert m
.overloaded4(np
.array([1], dtype
='float32')) == 'double'
304 assert m
.overloaded4(np
.array([1], dtype
='short')) == 'long long'
306 assert m
.overloaded5(np
.array([1], dtype
='double')) == 'double'
307 assert m
.overloaded5(np
.array([1], dtype
='uintc')) == 'unsigned int'
308 assert m
.overloaded5(np
.array([1], dtype
='float32')) == 'unsigned int'
311 def test_greedy_string_overload():
312 """Tests fix for #685 - ndarray shouldn't go to std::string overload"""
314 assert m
.issue685("abc") == "string"
315 assert m
.issue685(np
.array([97, 98, 99], dtype
='b')) == "array"
316 assert m
.issue685(123) == "other"
319 def test_array_unchecked_fixed_dims(msg
):
320 z1
= np
.array([[1, 2], [3, 4]], dtype
='float64')
322 assert np
.all(z1
== [[11, 12], [13, 14]])
324 with pytest
.raises(ValueError) as excinfo
:
325 m
.proxy_add2(np
.array([1., 2, 3]), 5.0)
326 assert msg(excinfo
.value
) == "array has incorrect number of dimensions: 1; expected 2"
328 expect_c
= np
.ndarray(shape
=(3, 3, 3), buffer=np
.array(range(3, 30)), dtype
='int')
329 assert np
.all(m
.proxy_init3(3.0) == expect_c
)
330 expect_f
= np
.transpose(expect_c
)
331 assert np
.all(m
.proxy_init3F(3.0) == expect_f
)
333 assert m
.proxy_squared_L2_norm(np
.array(range(6))) == 55
334 assert m
.proxy_squared_L2_norm(np
.array(range(6), dtype
="float64")) == 55
336 assert m
.proxy_auxiliaries2(z1
) == [11, 11, True, 2, 8, 2, 2, 4, 32]
337 assert m
.proxy_auxiliaries2(z1
) == m
.array_auxiliaries2(z1
)
340 def test_array_unchecked_dyn_dims(msg
):
341 z1
= np
.array([[1, 2], [3, 4]], dtype
='float64')
342 m
.proxy_add2_dyn(z1
, 10)
343 assert np
.all(z1
== [[11, 12], [13, 14]])
345 expect_c
= np
.ndarray(shape
=(3, 3, 3), buffer=np
.array(range(3, 30)), dtype
='int')
346 assert np
.all(m
.proxy_init3_dyn(3.0) == expect_c
)
348 assert m
.proxy_auxiliaries2_dyn(z1
) == [11, 11, True, 2, 8, 2, 2, 4, 32]
349 assert m
.proxy_auxiliaries2_dyn(z1
) == m
.array_auxiliaries2(z1
)
352 def test_array_failure():
353 with pytest
.raises(ValueError) as excinfo
:
355 assert str(excinfo
.value
) == 'cannot create a pybind11::array from a nullptr'
357 with pytest
.raises(ValueError) as excinfo
:
358 m
.array_t_fail_test()
359 assert str(excinfo
.value
) == 'cannot create a pybind11::array_t from a nullptr'
361 with pytest
.raises(ValueError) as excinfo
:
362 m
.array_fail_test_negative_size()
363 assert str(excinfo
.value
) == 'negative dimensions are not allowed'
366 def test_initializer_list():
367 assert m
.array_initializer_list1().shape
== (1,)
368 assert m
.array_initializer_list2().shape
== (1, 2)
369 assert m
.array_initializer_list3().shape
== (1, 2, 3)
370 assert m
.array_initializer_list4().shape
== (1, 2, 3, 4)
373 def test_array_resize(msg
):
374 a
= np
.array([1, 2, 3, 4, 5, 6, 7, 8, 9], dtype
='float64')
377 assert(np
.all(a
== [[1, 2, 3], [4, 5, 6], [7, 8, 9]]))
379 # total size change should succced with refcheck off
380 m
.array_resize3(a
, 4, False)
382 # ... and fail with refcheck on
384 m
.array_resize3(a
, 3, True)
385 except ValueError as e
:
386 assert(str(e
).startswith("cannot resize an array"))
387 # transposed array doesn't own data
390 m
.array_resize3(b
, 3, False)
391 except ValueError as e
:
392 assert(str(e
).startswith("cannot resize this array: it does not own its data"))
393 # ... but reshape should be fine
395 assert(b
.shape
== (8, 8))
398 @pytest.unsupported_on_pypy
399 def test_array_create_and_resize(msg
):
400 a
= m
.create_and_resize(2)
402 assert(np
.all(a
== 42.))