-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathtest_utility_functions.py
More file actions
163 lines (138 loc) · 5.43 KB
/
Copy pathtest_utility_functions.py
File metadata and controls
163 lines (138 loc) · 5.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import pytest
from hypothesis import given
from hypothesis import strategies as st
from . import _array_module as xp
from . import dtype_helpers as dh
from . import hypothesis_helpers as hh
from . import pytest_helpers as ph
from . import shape_helpers as sh
@pytest.mark.unvectorized
@given(
x=hh.arrays(dtype=hh.all_dtypes, shape=hh.shapes(min_side=1)),
data=st.data(),
)
def test_all(x, data):
kw = data.draw(hh.kwargs(axis=hh.axes(x.ndim), keepdims=st.booleans()), label="kw")
keepdims = kw.get("keepdims", False)
repro_snippet = ph.format_snippet(f"xp.all({x!r}, **kw) with {kw = }")
try:
out = xp.all(x, **kw)
ph.assert_dtype("all", in_dtype=x.dtype, out_dtype=out.dtype, expected=xp.bool)
_axes = sh.normalize_axis(kw.get("axis", None), x.ndim)
ph.assert_keepdimable_shape(
"all", in_shape=x.shape, out_shape=out.shape, axes=_axes, keepdims=keepdims, kw=kw
)
scalar_type = dh.get_scalar_type(x.dtype)
for indices, out_idx in zip(sh.axes_ndindex(x.shape, _axes), sh.ndindex(out.shape)):
result = bool(out[out_idx])
elements = []
for idx in indices:
s = scalar_type(x[idx])
elements.append(s)
expected = all(elements)
ph.assert_scalar_equals("all", type_=scalar_type, idx=out_idx,
out=result, expected=expected, kw=kw)
except Exception as exc:
ph.add_note(exc, repro_snippet)
raise
@pytest.mark.unvectorized
@given(
x=hh.arrays(dtype=hh.all_dtypes, shape=hh.shapes()),
data=st.data(),
)
def test_any(x, data):
kw = data.draw(hh.kwargs(axis=hh.axes(x.ndim), keepdims=st.booleans()), label="kw")
keepdims = kw.get("keepdims", False)
repro_snippet = ph.format_snippet(f"xp.any({x!r}, **kw) with {kw = }")
try:
out = xp.any(x, **kw)
ph.assert_dtype("any", in_dtype=x.dtype, out_dtype=out.dtype, expected=xp.bool)
_axes = sh.normalize_axis(kw.get("axis", None), x.ndim)
ph.assert_keepdimable_shape(
"any", in_shape=x.shape, out_shape=out.shape, axes=_axes, keepdims=keepdims, kw=kw,
)
scalar_type = dh.get_scalar_type(x.dtype)
for indices, out_idx in zip(sh.axes_ndindex(x.shape, _axes), sh.ndindex(out.shape)):
result = bool(out[out_idx])
elements = []
for idx in indices:
s = scalar_type(x[idx])
elements.append(s)
expected = any(elements)
ph.assert_scalar_equals("any", type_=scalar_type, idx=out_idx,
out=result, expected=expected, kw=kw)
except Exception as exc:
ph.add_note(exc, repro_snippet)
raise
@pytest.mark.unvectorized
@pytest.mark.min_version("2024.12")
@given(
x=hh.arrays(hh.numeric_dtypes, hh.shapes(min_dims=1, min_side=1)),
data=st.data(),
)
def test_diff(x, data):
axis = data.draw(
st.integers(-x.ndim, max(x.ndim - 1, 0)) | st.none(),
label="axis"
)
if axis is None:
axis_kw = {"axis": -1}
n_axis = x.ndim - 1
else:
axis_kw = {"axis": axis}
n_axis = axis + x.ndim if axis < 0 else axis
n = data.draw(st.integers(1, min(x.shape[n_axis], 3)))
repro_snippet = ph.format_snippet(f"xp.diff({x!r}, **axis_kw, n={n!r}) with {axis_kw = }")
try:
out = xp.diff(x, **axis_kw, n=n)
expected_shape = list(x.shape)
expected_shape[n_axis] -= n
assert out.shape == tuple(expected_shape)
# value test
if n == 1:
for idx in sh.ndindex(out.shape):
l = list(idx)
l[n_axis] += 1
assert out[idx] == x[tuple(l)] - x[idx], f"diff failed with {idx = }"
except Exception as exc:
ph.add_note(exc, repro_snippet)
raise
@pytest.mark.min_version("2024.12")
@pytest.mark.unvectorized
@given(
x=hh.arrays(hh.numeric_dtypes, hh.shapes(min_dims=1, min_side=1)),
data=st.data(),
)
def test_diff_append_prepend(x, data):
axis = data.draw(
st.integers(-x.ndim, max(x.ndim - 1, 0)) | st.none(),
label="axis"
)
if axis is None:
axis_kw = {"axis": -1}
n_axis = x.ndim - 1
else:
axis_kw = {"axis": axis}
n_axis = axis + x.ndim if axis < 0 else axis
n = data.draw(st.integers(1, min(x.shape[n_axis], 3)))
append_shape = list(x.shape)
append_axis_len = data.draw(st.integers(1, 2*append_shape[n_axis]), label="append_axis")
append_shape[n_axis] = append_axis_len
append = data.draw(hh.arrays(dtype=x.dtype, shape=tuple(append_shape)), label="append")
prepend_shape = list(x.shape)
prepend_axis_len = data.draw(st.integers(1, 2*prepend_shape[n_axis]), label="prepend_axis")
prepend_shape[n_axis] = prepend_axis_len
prepend = data.draw(hh.arrays(dtype=x.dtype, shape=tuple(prepend_shape)), label="prepend")
repro_snippet = ph.format_snippet(
f"xp.diff({x!r}, **axis_kw, n={n!r}, append={append!r}, prepend={prepend!r}) with {axis_kw = }"
)
try:
out = xp.diff(x, **axis_kw, n=n, append=append, prepend=prepend)
in_1 = xp.concat((prepend, x, append), **axis_kw)
out_1 = xp.diff(in_1, **axis_kw, n=n)
assert out.shape == out_1.shape
for idx in sh.ndindex(out.shape):
assert out[idx] == out_1[idx], f"{idx = }"
except Exception as exc:
ph.add_note(exc, repro_snippet)
raise