|
| 1 | +import math |
| 2 | +from itertools import product |
| 3 | +from typing import Sequence, Union |
| 4 | + |
1 | 5 | import pytest
|
2 |
| -from hypothesis import given |
| 6 | +from hypothesis import assume, given, note |
3 | 7 | from hypothesis import strategies as st
|
4 | 8 |
|
5 | 9 | from . import _array_module as xp
|
6 | 10 | from . import dtype_helpers as dh
|
7 | 11 | from . import hypothesis_helpers as hh
|
8 | 12 | from . import pytest_helpers as ph
|
9 | 13 | from . import xps
|
10 |
| -from .typing import DataType, Param, ScalarType |
| 14 | +from .typing import DataType, Param, Scalar, ScalarType, Shape |
| 15 | + |
11 | 16 |
|
| 17 | +def reshape( |
| 18 | + flat_seq: Sequence[Scalar], shape: Shape |
| 19 | +) -> Union[Scalar, Sequence[Scalar]]: |
| 20 | + """Reshape a flat sequence""" |
| 21 | + if len(shape) == 0: |
| 22 | + assert len(flat_seq) == 1 # sanity check |
| 23 | + return flat_seq[0] |
| 24 | + elif len(shape) == 1: |
| 25 | + return flat_seq |
| 26 | + size = len(flat_seq) |
| 27 | + n = math.prod(shape[1:]) |
| 28 | + return [reshape(flat_seq[i * n : (i + 1) * n], shape[1:]) for i in range(size // n)] |
12 | 29 |
|
13 |
| -@given(hh.shapes(), st.data()) |
| 30 | + |
| 31 | +@given(hh.shapes(min_side=1), st.data()) # TODO: test 0-sided arrays |
14 | 32 | def test_getitem(shape, data):
|
15 |
| - x = data.draw(xps.arrays(dtype=xps.scalar_dtypes(), shape=shape), label="x") |
| 33 | + size = math.prod(shape) |
| 34 | + dtype = data.draw(xps.scalar_dtypes(), label="dtype") |
| 35 | + obj = data.draw( |
| 36 | + st.lists( |
| 37 | + xps.from_dtype(dtype), |
| 38 | + min_size=size, |
| 39 | + max_size=size, |
| 40 | + ).map(lambda l: reshape(l, shape)), |
| 41 | + label="obj", |
| 42 | + ) |
| 43 | + x = xp.asarray(obj, dtype=dtype) |
| 44 | + note(f"{x=}") |
16 | 45 | key = data.draw(xps.indices(shape=shape), label="key")
|
17 | 46 |
|
18 | 47 | out = x[key]
|
19 | 48 |
|
20 |
| - ph.assert_dtype("__getitem__", x.dtype, out.dtype) |
21 |
| - |
22 | 49 | _key = tuple(key) if isinstance(key, tuple) else (key,)
|
23 | 50 | if Ellipsis in _key:
|
24 | 51 | start_a = _key.index(Ellipsis)
|
25 | 52 | stop_a = start_a + (len(shape) - (len(_key) - 1))
|
26 | 53 | slices = tuple(slice(None, None) for _ in range(start_a, stop_a))
|
27 | 54 | _key = _key[:start_a] + slices + _key[start_a + 1 :]
|
28 |
| - expected = [] |
| 55 | + axes_indices = [] |
29 | 56 | for a, i in enumerate(_key):
|
30 |
| - if isinstance(i, slice): |
31 |
| - r = range(shape[a])[i] |
32 |
| - expected.append(len(r)) |
33 |
| - expected = tuple(expected) |
34 |
| - ph.assert_shape("__getitem__", out.shape, expected) |
35 |
| - |
36 |
| - # TODO: fold in all remaining concepts from test_indexing.py |
| 57 | + if isinstance(i, int): |
| 58 | + axes_indices.append([i]) |
| 59 | + else: |
| 60 | + side = shape[a] |
| 61 | + indices = range(side)[i] |
| 62 | + assume(len(indices) > 0) # TODO: test 0-sided arrays |
| 63 | + axes_indices.append(indices) |
| 64 | + expected = [] |
| 65 | + for idx in product(*axes_indices): |
| 66 | + val = obj |
| 67 | + for i in idx: |
| 68 | + val = val[i] |
| 69 | + expected.append(val) |
| 70 | + expected = reshape(expected, out.shape) |
| 71 | + expected = xp.asarray(expected, dtype=dtype) |
| 72 | + ph.assert_array("__getitem__", out, expected) |
37 | 73 |
|
38 | 74 |
|
39 | 75 | # TODO: test_setitem
|
|
0 commit comments