2021-12-10 07:58:52 +00:00
|
|
|
import sys
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
from pybind11_tests import numpy_scalars as m
|
|
|
|
|
|
|
|
np = pytest.importorskip("numpy")
|
|
|
|
|
2021-12-10 08:16:36 +00:00
|
|
|
SCALAR_TYPES = dict(
|
|
|
|
[
|
|
|
|
(np.bool_, False),
|
|
|
|
(np.int8, -7),
|
|
|
|
(np.int16, -15),
|
|
|
|
(np.int32, -31),
|
|
|
|
(np.int64, -63),
|
|
|
|
(np.uint8, 9),
|
|
|
|
(np.uint16, 17),
|
|
|
|
(np.uint32, 33),
|
|
|
|
(np.uint64, 65),
|
|
|
|
(np.single, 1.125),
|
|
|
|
(np.double, 1.25),
|
|
|
|
(np.complex64, 1 - 0.125j),
|
|
|
|
(np.complex128, 1 - 0.25j),
|
|
|
|
]
|
|
|
|
)
|
2021-12-10 07:58:52 +00:00
|
|
|
ALL_TYPES = [int, bool, float, bytes, str] + list(SCALAR_TYPES)
|
|
|
|
|
|
|
|
|
|
|
|
def type_name(tp):
|
|
|
|
try:
|
2021-12-10 08:16:36 +00:00
|
|
|
return tp.__name__.rstrip("_")
|
2021-12-10 07:58:52 +00:00
|
|
|
except BaseException:
|
|
|
|
# no numpy
|
|
|
|
return str(tp)
|
|
|
|
|
|
|
|
|
2021-12-10 08:16:36 +00:00
|
|
|
@pytest.fixture(scope="module", params=list(SCALAR_TYPES), ids=type_name)
|
2021-12-10 07:58:52 +00:00
|
|
|
def scalar_type(request):
|
|
|
|
return request.param
|
|
|
|
|
|
|
|
|
|
|
|
def expected_signature(tp):
|
2021-12-10 08:16:36 +00:00
|
|
|
s = "str" if sys.version_info[0] >= 3 else "unicode"
|
2021-12-10 07:58:52 +00:00
|
|
|
t = type_name(tp)
|
2021-12-10 08:16:36 +00:00
|
|
|
return "test_{t}(x: {t}) -> Tuple[{s}, {t}]\n".format(s=s, t=t)
|
2021-12-10 07:58:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_numpy_scalars(scalar_type):
|
|
|
|
expected = SCALAR_TYPES[scalar_type]
|
|
|
|
name = type_name(scalar_type)
|
2021-12-10 08:16:36 +00:00
|
|
|
func = getattr(m, "test_" + name)
|
2021-12-10 07:58:52 +00:00
|
|
|
assert func.__doc__ == expected_signature(scalar_type)
|
|
|
|
for tp in ALL_TYPES:
|
|
|
|
value = tp(1)
|
|
|
|
if tp is scalar_type:
|
|
|
|
result = func(value)
|
|
|
|
assert result[0] == name
|
|
|
|
assert isinstance(result[1], tp)
|
|
|
|
assert result[1] == tp(expected)
|
|
|
|
else:
|
|
|
|
with pytest.raises(TypeError):
|
|
|
|
func(value)
|