Skip to content

ENH: add a test for can_cast(complex dtypes) #320

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 8 additions & 22 deletions array_api_tests/test_data_type_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,31 +107,17 @@ def test_broadcast_to(x, data):
# TODO: test values


@given(_from=non_complex_dtypes(), to=non_complex_dtypes(), data=st.data())
def test_can_cast(_from, to, data):
from_ = data.draw(
st.just(_from) | hh.arrays(dtype=_from, shape=hh.shapes()), label="from_"
)
@given(_from=hh.all_dtypes, to=hh.all_dtypes)
def test_can_cast(_from, to):
out = xp.can_cast(_from, to)

out = xp.can_cast(from_, to)
expected = False
for other in dh.all_dtypes:
if dh.promotion_table.get((_from, other)) == to:
expected = True
break

f_func = f"[can_cast({dh.dtype_to_name[_from]}, {dh.dtype_to_name[to]})]"
assert isinstance(out, bool), f"{type(out)=}, but should be bool {f_func}"
if _from == xp.bool:
expected = to == xp.bool
else:
same_family = None
for dtypes in [dh.all_int_dtypes, dh.real_float_dtypes, dh.complex_dtypes]:
if _from in dtypes:
same_family = to in dtypes
break
assert same_family is not None # sanity check
if same_family:
from_min, from_max = dh.dtype_ranges[_from]
to_min, to_max = dh.dtype_ranges[to]
expected = from_min >= to_min and from_max <= to_max
else:
expected = False
if expected:
# cross-kind casting is not explicitly disallowed. We can only test
# the cases where it should return True. TODO: if expected=False,
Expand Down