You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

273 lines
8.9 KiB

  1. import pytest
  2. import gc
  3. with pytest.suppress(ImportError):
  4. import numpy as np
  5. @pytest.fixture(scope='function')
  6. def arr():
  7. return np.array([[1, 2, 3], [4, 5, 6]], '<u2')
  8. @pytest.requires_numpy
  9. def test_array_attributes():
  10. from pybind11_tests.array import (
  11. ndim, shape, strides, writeable, size, itemsize, nbytes, owndata
  12. )
  13. a = np.array(0, 'f8')
  14. assert ndim(a) == 0
  15. assert all(shape(a) == [])
  16. assert all(strides(a) == [])
  17. with pytest.raises(IndexError) as excinfo:
  18. shape(a, 0)
  19. assert str(excinfo.value) == 'invalid axis: 0 (ndim = 0)'
  20. with pytest.raises(IndexError) as excinfo:
  21. strides(a, 0)
  22. assert str(excinfo.value) == 'invalid axis: 0 (ndim = 0)'
  23. assert writeable(a)
  24. assert size(a) == 1
  25. assert itemsize(a) == 8
  26. assert nbytes(a) == 8
  27. assert owndata(a)
  28. a = np.array([[1, 2, 3], [4, 5, 6]], 'u2').view()
  29. a.flags.writeable = False
  30. assert ndim(a) == 2
  31. assert all(shape(a) == [2, 3])
  32. assert shape(a, 0) == 2
  33. assert shape(a, 1) == 3
  34. assert all(strides(a) == [6, 2])
  35. assert strides(a, 0) == 6
  36. assert strides(a, 1) == 2
  37. with pytest.raises(IndexError) as excinfo:
  38. shape(a, 2)
  39. assert str(excinfo.value) == 'invalid axis: 2 (ndim = 2)'
  40. with pytest.raises(IndexError) as excinfo:
  41. strides(a, 2)
  42. assert str(excinfo.value) == 'invalid axis: 2 (ndim = 2)'
  43. assert not writeable(a)
  44. assert size(a) == 6
  45. assert itemsize(a) == 2
  46. assert nbytes(a) == 12
  47. assert not owndata(a)
  48. @pytest.requires_numpy
  49. @pytest.mark.parametrize('args, ret', [([], 0), ([0], 0), ([1], 3), ([0, 1], 1), ([1, 2], 5)])
  50. def test_index_offset(arr, args, ret):
  51. from pybind11_tests.array import index_at, index_at_t, offset_at, offset_at_t
  52. assert index_at(arr, *args) == ret
  53. assert index_at_t(arr, *args) == ret
  54. assert offset_at(arr, *args) == ret * arr.dtype.itemsize
  55. assert offset_at_t(arr, *args) == ret * arr.dtype.itemsize
  56. @pytest.requires_numpy
  57. def test_dim_check_fail(arr):
  58. from pybind11_tests.array import (index_at, index_at_t, offset_at, offset_at_t, data, data_t,
  59. mutate_data, mutate_data_t)
  60. for func in (index_at, index_at_t, offset_at, offset_at_t, data, data_t,
  61. mutate_data, mutate_data_t):
  62. with pytest.raises(IndexError) as excinfo:
  63. func(arr, 1, 2, 3)
  64. assert str(excinfo.value) == 'too many indices for an array: 3 (ndim = 2)'
  65. @pytest.requires_numpy
  66. @pytest.mark.parametrize('args, ret',
  67. [([], [1, 2, 3, 4, 5, 6]),
  68. ([1], [4, 5, 6]),
  69. ([0, 1], [2, 3, 4, 5, 6]),
  70. ([1, 2], [6])])
  71. def test_data(arr, args, ret):
  72. from pybind11_tests.array import data, data_t
  73. assert all(data_t(arr, *args) == ret)
  74. assert all(data(arr, *args)[::2] == ret)
  75. assert all(data(arr, *args)[1::2] == 0)
  76. @pytest.requires_numpy
  77. def test_mutate_readonly(arr):
  78. from pybind11_tests.array import mutate_data, mutate_data_t, mutate_at_t
  79. arr.flags.writeable = False
  80. for func, args in (mutate_data, ()), (mutate_data_t, ()), (mutate_at_t, (0, 0)):
  81. with pytest.raises(RuntimeError) as excinfo:
  82. func(arr, *args)
  83. assert str(excinfo.value) == 'array is not writeable'
  84. @pytest.requires_numpy
  85. @pytest.mark.parametrize('dim', [0, 1, 3])
  86. def test_at_fail(arr, dim):
  87. from pybind11_tests.array import at_t, mutate_at_t
  88. for func in at_t, mutate_at_t:
  89. with pytest.raises(IndexError) as excinfo:
  90. func(arr, *([0] * dim))
  91. assert str(excinfo.value) == 'index dimension mismatch: {} (ndim = 2)'.format(dim)
  92. @pytest.requires_numpy
  93. def test_at(arr):
  94. from pybind11_tests.array import at_t, mutate_at_t
  95. assert at_t(arr, 0, 2) == 3
  96. assert at_t(arr, 1, 0) == 4
  97. assert all(mutate_at_t(arr, 0, 2).ravel() == [1, 2, 4, 4, 5, 6])
  98. assert all(mutate_at_t(arr, 1, 0).ravel() == [1, 2, 4, 5, 5, 6])
  99. @pytest.requires_numpy
  100. def test_mutate_data(arr):
  101. from pybind11_tests.array import mutate_data, mutate_data_t
  102. assert all(mutate_data(arr).ravel() == [2, 4, 6, 8, 10, 12])
  103. assert all(mutate_data(arr).ravel() == [4, 8, 12, 16, 20, 24])
  104. assert all(mutate_data(arr, 1).ravel() == [4, 8, 12, 32, 40, 48])
  105. assert all(mutate_data(arr, 0, 1).ravel() == [4, 16, 24, 64, 80, 96])
  106. assert all(mutate_data(arr, 1, 2).ravel() == [4, 16, 24, 64, 80, 192])
  107. assert all(mutate_data_t(arr).ravel() == [5, 17, 25, 65, 81, 193])
  108. assert all(mutate_data_t(arr).ravel() == [6, 18, 26, 66, 82, 194])
  109. assert all(mutate_data_t(arr, 1).ravel() == [6, 18, 26, 67, 83, 195])
  110. assert all(mutate_data_t(arr, 0, 1).ravel() == [6, 19, 27, 68, 84, 196])
  111. assert all(mutate_data_t(arr, 1, 2).ravel() == [6, 19, 27, 68, 84, 197])
  112. @pytest.requires_numpy
  113. def test_bounds_check(arr):
  114. from pybind11_tests.array import (index_at, index_at_t, data, data_t,
  115. mutate_data, mutate_data_t, at_t, mutate_at_t)
  116. funcs = (index_at, index_at_t, data, data_t,
  117. mutate_data, mutate_data_t, at_t, mutate_at_t)
  118. for func in funcs:
  119. with pytest.raises(IndexError) as excinfo:
  120. func(arr, 2, 0)
  121. assert str(excinfo.value) == 'index 2 is out of bounds for axis 0 with size 2'
  122. with pytest.raises(IndexError) as excinfo:
  123. func(arr, 0, 4)
  124. assert str(excinfo.value) == 'index 4 is out of bounds for axis 1 with size 3'
  125. @pytest.requires_numpy
  126. def test_make_c_f_array():
  127. from pybind11_tests.array import (
  128. make_c_array, make_f_array
  129. )
  130. assert make_c_array().flags.c_contiguous
  131. assert not make_c_array().flags.f_contiguous
  132. assert make_f_array().flags.f_contiguous
  133. assert not make_f_array().flags.c_contiguous
  134. @pytest.requires_numpy
  135. def test_wrap():
  136. from pybind11_tests.array import wrap
  137. def assert_references(a, b):
  138. assert a is not b
  139. assert a.__array_interface__['data'][0] == b.__array_interface__['data'][0]
  140. assert a.shape == b.shape
  141. assert a.strides == b.strides
  142. assert a.flags.c_contiguous == b.flags.c_contiguous
  143. assert a.flags.f_contiguous == b.flags.f_contiguous
  144. assert a.flags.writeable == b.flags.writeable
  145. assert a.flags.aligned == b.flags.aligned
  146. assert a.flags.updateifcopy == b.flags.updateifcopy
  147. assert np.all(a == b)
  148. assert not b.flags.owndata
  149. assert b.base is a
  150. if a.flags.writeable and a.ndim == 2:
  151. a[0, 0] = 1234
  152. assert b[0, 0] == 1234
  153. a1 = np.array([1, 2], dtype=np.int16)
  154. assert a1.flags.owndata and a1.base is None
  155. a2 = wrap(a1)
  156. assert_references(a1, a2)
  157. a1 = np.array([[1, 2], [3, 4]], dtype=np.float32, order='F')
  158. assert a1.flags.owndata and a1.base is None
  159. a2 = wrap(a1)
  160. assert_references(a1, a2)
  161. a1 = np.array([[1, 2], [3, 4]], dtype=np.float32, order='C')
  162. a1.flags.writeable = False
  163. a2 = wrap(a1)
  164. assert_references(a1, a2)
  165. a1 = np.random.random((4, 4, 4))
  166. a2 = wrap(a1)
  167. assert_references(a1, a2)
  168. a1 = a1.transpose()
  169. a2 = wrap(a1)
  170. assert_references(a1, a2)
  171. a1 = a1.diagonal()
  172. a2 = wrap(a1)
  173. assert_references(a1, a2)
  174. @pytest.requires_numpy
  175. def test_numpy_view(capture):
  176. from pybind11_tests.array import ArrayClass
  177. with capture:
  178. ac = ArrayClass()
  179. ac_view_1 = ac.numpy_view()
  180. ac_view_2 = ac.numpy_view()
  181. assert np.all(ac_view_1 == np.array([1, 2], dtype=np.int32))
  182. del ac
  183. gc.collect()
  184. assert capture == """
  185. ArrayClass()
  186. ArrayClass::numpy_view()
  187. ArrayClass::numpy_view()
  188. """
  189. ac_view_1[0] = 4
  190. ac_view_1[1] = 3
  191. assert ac_view_2[0] == 4
  192. assert ac_view_2[1] == 3
  193. with capture:
  194. del ac_view_1
  195. del ac_view_2
  196. gc.collect()
  197. assert capture == """
  198. ~ArrayClass()
  199. """
  200. @pytest.requires_numpy
  201. def test_cast_numpy_int64_to_uint64():
  202. from pybind11_tests.array import function_taking_uint64
  203. function_taking_uint64(123)
  204. function_taking_uint64(np.uint64(123))
  205. @pytest.requires_numpy
  206. def test_isinstance():
  207. from pybind11_tests.array import isinstance_untyped, isinstance_typed
  208. assert isinstance_untyped(np.array([1, 2, 3]), "not an array")
  209. assert isinstance_typed(np.array([1.0, 2.0, 3.0]))
  210. @pytest.requires_numpy
  211. def test_constructors():
  212. from pybind11_tests.array import default_constructors, converting_constructors
  213. defaults = default_constructors()
  214. for a in defaults.values():
  215. assert a.size == 0
  216. assert defaults["array"].dtype == np.array([]).dtype
  217. assert defaults["array_t<int32>"].dtype == np.int32
  218. assert defaults["array_t<double>"].dtype == np.float64
  219. results = converting_constructors([1, 2, 3])
  220. for a in results.values():
  221. np.testing.assert_array_equal(a, [1, 2, 3])
  222. assert results["array"].dtype == np.int_
  223. assert results["array_t<int32>"].dtype == np.int32
  224. assert results["array_t<double>"].dtype == np.float64