|
| 1 | +import math |
| 2 | +from itertools import product |
| 3 | +from typing import Sequence, Union, get_args |
| 4 | + |
| 5 | +import pytest |
| 6 | +from hypothesis import assume, given, note |
| 7 | +from hypothesis import strategies as st |
| 8 | + |
| 9 | +from . import _array_module as xp |
| 10 | +from . import dtype_helpers as dh |
| 11 | +from . import hypothesis_helpers as hh |
| 12 | +from . import pytest_helpers as ph |
| 13 | +from . import xps |
| 14 | +from .typing import DataType, Param, Scalar, ScalarType, Shape |
| 15 | + |
| 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)] |
| 29 | + |
| 30 | + |
| 31 | +@given(hh.shapes(min_side=1), st.data()) # TODO: test 0-sided arrays |
| 32 | +def test_getitem(shape, data): |
| 33 | + size = math.prod(shape) |
| 34 | + dtype = data.draw(xps.scalar_dtypes(), label="dtype") |
| 35 | + obj = data.draw( |
| 36 | + st.lists(xps.from_dtype(dtype), min_size=size, max_size=size).map( |
| 37 | + lambda l: reshape(l, shape) |
| 38 | + ), |
| 39 | + label="obj", |
| 40 | + ) |
| 41 | + x = xp.asarray(obj, dtype=dtype) |
| 42 | + note(f"{x=}") |
| 43 | + key = data.draw(xps.indices(shape=shape), label="key") |
| 44 | + |
| 45 | + out = x[key] |
| 46 | + |
| 47 | + ph.assert_dtype("__getitem__", x.dtype, out.dtype) |
| 48 | + _key = tuple(key) if isinstance(key, tuple) else (key,) |
| 49 | + if Ellipsis in _key: |
| 50 | + start_a = _key.index(Ellipsis) |
| 51 | + stop_a = start_a + (len(shape) - (len(_key) - 1)) |
| 52 | + slices = tuple(slice(None, None) for _ in range(start_a, stop_a)) |
| 53 | + _key = _key[:start_a] + slices + _key[start_a + 1 :] |
| 54 | + axes_indices = [] |
| 55 | + out_shape = [] |
| 56 | + for a, i in enumerate(_key): |
| 57 | + if isinstance(i, int): |
| 58 | + axes_indices.append([i]) |
| 59 | + else: |
| 60 | + side = shape[a] |
| 61 | + indices = range(side)[i] |
| 62 | + axes_indices.append(indices) |
| 63 | + out_shape.append(len(indices)) |
| 64 | + out_shape = tuple(out_shape) |
| 65 | + ph.assert_shape("__getitem__", out.shape, out_shape) |
| 66 | + assume(all(len(indices) > 0 for indices in axes_indices)) |
| 67 | + out_obj = [] |
| 68 | + for idx in product(*axes_indices): |
| 69 | + val = obj |
| 70 | + for i in idx: |
| 71 | + val = val[i] |
| 72 | + out_obj.append(val) |
| 73 | + out_obj = reshape(out_obj, out_shape) |
| 74 | + expected = xp.asarray(out_obj, dtype=dtype) |
| 75 | + ph.assert_array("__getitem__", out, expected) |
| 76 | + |
| 77 | + |
| 78 | +@given(hh.shapes(min_side=1), st.data()) # TODO: test 0-sided arrays |
| 79 | +def test_setitem(shape, data): |
| 80 | + size = math.prod(shape) |
| 81 | + dtype = data.draw(xps.scalar_dtypes(), label="dtype") |
| 82 | + obj = data.draw( |
| 83 | + st.lists(xps.from_dtype(dtype), min_size=size, max_size=size).map( |
| 84 | + lambda l: reshape(l, shape) |
| 85 | + ), |
| 86 | + label="obj", |
| 87 | + ) |
| 88 | + x = xp.asarray(obj, dtype=dtype) |
| 89 | + note(f"{x=}") |
| 90 | + key = data.draw(xps.indices(shape=shape, max_dims=0), label="key") |
| 91 | + value = data.draw( |
| 92 | + xps.from_dtype(dtype) | xps.arrays(dtype=dtype, shape=()), label="value" |
| 93 | + ) |
| 94 | + |
| 95 | + res = xp.asarray(x, copy=True) |
| 96 | + res[key] = value |
| 97 | + |
| 98 | + ph.assert_dtype("__setitem__", x.dtype, res.dtype, repr_name="x.dtype") |
| 99 | + ph.assert_shape("__setitem__", res.shape, x.shape, repr_name="x.shape") |
| 100 | + if isinstance(value, get_args(Scalar)): |
| 101 | + msg = f"x[{key}]={res[key]!r}, but should be {value=} [__setitem__()]" |
| 102 | + if math.isnan(value): |
| 103 | + assert xp.isnan(res[key]), msg |
| 104 | + else: |
| 105 | + assert res[key] == value, msg |
| 106 | + else: |
| 107 | + ph.assert_0d_equals("__setitem__", "value", value, f"x[{key}]", res[key]) |
| 108 | + |
| 109 | + |
| 110 | +# TODO: test boolean indexing |
| 111 | + |
| 112 | + |
| 113 | +def make_param(method_name: str, dtype: DataType, stype: ScalarType) -> Param: |
| 114 | + return pytest.param( |
| 115 | + method_name, dtype, stype, id=f"{method_name}({dh.dtype_to_name[dtype]})" |
| 116 | + ) |
| 117 | + |
| 118 | + |
| 119 | +@pytest.mark.parametrize( |
| 120 | + "method_name, dtype, stype", |
| 121 | + [make_param("__bool__", xp.bool, bool)] |
| 122 | + + [make_param("__int__", d, int) for d in dh.all_int_dtypes] |
| 123 | + + [make_param("__index__", d, int) for d in dh.all_int_dtypes] |
| 124 | + + [make_param("__float__", d, float) for d in dh.float_dtypes], |
| 125 | +) |
| 126 | +@given(data=st.data()) |
| 127 | +def test_duck_typing(method_name, dtype, stype, data): |
| 128 | + x = data.draw(xps.arrays(dtype, shape=()), label="x") |
| 129 | + method = getattr(x, method_name) |
| 130 | + out = method() |
| 131 | + assert isinstance( |
| 132 | + out, stype |
| 133 | + ), f"{method_name}({x})={out}, which is not a {stype.__name__} scalar" |
0 commit comments