Skip to content

Tests for converting NumPy dtypes to their equivalent #128

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 1 commit into from
May 17, 2023
Merged
Show file tree
Hide file tree
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
51 changes: 51 additions & 0 deletions torch_np/tests/test_dtype.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import numpy as np
import pytest

import torch_np as tnp

dtype_names = [
"bool_",
*[f"int{w}" for w in [8, 16, 32, 64]],
"uint8",
*[f"float{w}" for w in [16, 32, 64]],
*[f"complex{w}" for w in [64, 128]],
]
np_dtype_params = []
np_dtype_params.append(pytest.param("bool", "bool", id="'bool'"))
np_dtype_params.append(
pytest.param(
"bool",
np.dtype("bool"),
id=f"np.dtype('bool')",
marks=pytest.mark.xfail(reason="XXX: np.dtype() objects not supported"),
)
)
for name in dtype_names:
np_dtype_params.append(pytest.param(name, name, id=repr(name)))
np_dtype_params.append(
pytest.param(
name,
getattr(np, name),
id=f"np.{name}",
marks=pytest.mark.xfail(reason="XXX: namespaced dtypes not supported"),
)
)
np_dtype_params.append(
pytest.param(
name,
np.dtype(name),
id=f"np.dtype({name!r})",
marks=pytest.mark.xfail(reason="XXX: np.dtype() objects not supported"),
)
)


@pytest.mark.parametrize("name, np_dtype", np_dtype_params)
def test_convert_np_dtypes(name, np_dtype):
tnp_dtype = tnp.dtype(np_dtype)
if name == "bool_":
assert tnp_dtype == tnp.bool_
elif tnp_dtype.name == "bool_":
assert name.startswith("bool")
else:
assert tnp_dtype.name == name
10 changes: 10 additions & 0 deletions torch_np/tests/test_xps.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,13 @@ def test_put(np_x, data):
note(f"(after put) {tnp_x=}")

assert_array_equal(tnp_x, tnp.asarray(np_x).astype(tnp_x.dtype))


@pytest.mark.xfail(reason="XXX: support converting namespaced dtypes")
@given(a=nps.arrays(dtype=nps.scalar_dtypes(), shape=nps.array_shapes()))
def test_asarray_np_arrays(a):
x = tnp.asarray(a)
if a.dtype == np.bool_:
assert x.dtype == tnp.bool
else:
assert x.dtype.name == a.dtype.name