Skip to content

Commit e4262e3

Browse files
committed
ENH: test indexing with arrays
1 parent f7a74a6 commit e4262e3

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

Diff for: array_api_tests/test_array_object.py

+51
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,57 @@ def test_setitem_masking(shape, data):
242242
)
243243

244244

245+
@given(shape=hh.shapes(), data=st.data())
246+
def test_getitem_arrays_and_ints(shape, data):
247+
assume((len(shape) > 0) and all(sh > 0 for sh in shape))
248+
249+
dtype = xp.int64
250+
obj = data.draw(scalar_objects(dtype, shape), label="obj")
251+
x = xp.asarray(obj, dtype=dtype)
252+
253+
# draw a mix of ints and index arrays
254+
arr_index = [data.draw(st.booleans()) for _ in range(len(shape))]
255+
assume(sum(arr_index) > 0)
256+
257+
# draw shapes for index arrays
258+
if sum(arr_index) > 0:
259+
index_shapes = data.draw(
260+
hh.mutually_broadcastable_shapes(sum(arr_index), min_dims=1, min_side=1)
261+
)
262+
index_shapes = list(index_shapes)
263+
264+
# prepare the indexing tuple, a mix of integer indices and index arrays
265+
key = []
266+
for i,typ in enumerate(arr_index):
267+
if typ:
268+
# draw an array index
269+
this_idx = data.draw(
270+
xps.arrays(
271+
xp.int64,
272+
shape=index_shapes.pop(),
273+
elements=st.integers(0, shape[i]-1)
274+
)
275+
)
276+
key.append(this_idx)
277+
278+
else:
279+
# draw an integer
280+
key.append(data.draw(st.integers(-shape[i], shape[i]-1)))
281+
282+
key = tuple(key)
283+
out = x[key]
284+
285+
# XXX: how to properly check
286+
import numpy as np
287+
x_np = np.asarray(x)
288+
out_np = np.asarray(out)
289+
key_np = tuple(k if isinstance(k, int) else np.asarray(k) for k in key)
290+
291+
print(f"{x.shape = } {out.shape} -- {[k if isinstance(k, int) else k.shape for k in key]}")
292+
293+
np.testing.assert_equal(out_np, x_np[key_np])
294+
295+
245296
def make_scalar_casting_param(
246297
method_name: str, dtype: DataType, stype: ScalarType
247298
) -> Param:

0 commit comments

Comments
 (0)