|
| 1 | +import unittest |
| 2 | + |
| 3 | +import numpy |
| 4 | +import pytest |
| 5 | + |
| 6 | +import dpnp as cupy |
| 7 | +from tests.third_party.cupy import testing |
| 8 | + |
| 9 | + |
| 10 | +def _generate_type_routines_input(xp, dtype, obj_type): |
| 11 | + dtype = numpy.dtype(dtype) |
| 12 | + if obj_type == "dtype": |
| 13 | + return dtype |
| 14 | + if obj_type == "specifier": |
| 15 | + return str(dtype) |
| 16 | + if obj_type == "scalar": |
| 17 | + return dtype.type(3) |
| 18 | + if obj_type == "array": |
| 19 | + return xp.zeros(3, dtype=dtype) |
| 20 | + if obj_type == "primitive": |
| 21 | + return type(dtype.type(3).tolist()) |
| 22 | + assert False |
| 23 | + |
| 24 | + |
| 25 | +@testing.parameterize( |
| 26 | + *testing.product( |
| 27 | + { |
| 28 | + "obj_type": ["dtype", "specifier", "scalar", "array", "primitive"], |
| 29 | + } |
| 30 | + ) |
| 31 | +) |
| 32 | +class TestCanCast(unittest.TestCase): |
| 33 | + @testing.for_all_dtypes_combination(names=("from_dtype", "to_dtype")) |
| 34 | + @testing.numpy_cupy_equal() |
| 35 | + def test_can_cast(self, xp, from_dtype, to_dtype): |
| 36 | + if self.obj_type == "scalar": |
| 37 | + pytest.skip("to be aligned with NEP-50") |
| 38 | + |
| 39 | + from_obj = _generate_type_routines_input(xp, from_dtype, self.obj_type) |
| 40 | + |
| 41 | + ret = xp.can_cast(from_obj, to_dtype) |
| 42 | + assert isinstance(ret, bool) |
| 43 | + return ret |
| 44 | + |
| 45 | + |
| 46 | +@pytest.mark.skip("dpnp.common_type() is not implemented yet") |
| 47 | +class TestCommonType(unittest.TestCase): |
| 48 | + @testing.numpy_cupy_equal() |
| 49 | + def test_common_type_empty(self, xp): |
| 50 | + ret = xp.common_type() |
| 51 | + assert type(ret) == type |
| 52 | + return ret |
| 53 | + |
| 54 | + @testing.for_all_dtypes(no_bool=True) |
| 55 | + @testing.numpy_cupy_equal() |
| 56 | + def test_common_type_single_argument(self, xp, dtype): |
| 57 | + array = _generate_type_routines_input(xp, dtype, "array") |
| 58 | + ret = xp.common_type(array) |
| 59 | + assert type(ret) == type |
| 60 | + return ret |
| 61 | + |
| 62 | + @testing.for_all_dtypes_combination( |
| 63 | + names=("dtype1", "dtype2"), no_bool=True |
| 64 | + ) |
| 65 | + @testing.numpy_cupy_equal() |
| 66 | + def test_common_type_two_arguments(self, xp, dtype1, dtype2): |
| 67 | + array1 = _generate_type_routines_input(xp, dtype1, "array") |
| 68 | + array2 = _generate_type_routines_input(xp, dtype2, "array") |
| 69 | + ret = xp.common_type(array1, array2) |
| 70 | + assert type(ret) == type |
| 71 | + return ret |
| 72 | + |
| 73 | + @testing.for_all_dtypes() |
| 74 | + def test_common_type_bool(self, dtype): |
| 75 | + for xp in (numpy, cupy): |
| 76 | + array1 = _generate_type_routines_input(xp, dtype, "array") |
| 77 | + array2 = _generate_type_routines_input(xp, "bool_", "array") |
| 78 | + with pytest.raises(TypeError): |
| 79 | + xp.common_type(array1, array2) |
| 80 | + |
| 81 | + |
| 82 | +@testing.parameterize( |
| 83 | + *testing.product( |
| 84 | + { |
| 85 | + "obj_type1": ["dtype", "specifier", "scalar", "array", "primitive"], |
| 86 | + "obj_type2": ["dtype", "specifier", "scalar", "array", "primitive"], |
| 87 | + } |
| 88 | + ) |
| 89 | +) |
| 90 | +class TestResultType(unittest.TestCase): |
| 91 | + @testing.for_all_dtypes_combination(names=("dtype1", "dtype2")) |
| 92 | + @testing.numpy_cupy_equal() |
| 93 | + def test_result_type(self, xp, dtype1, dtype2): |
| 94 | + if "scalar" in {self.obj_type1, self.obj_type2}: |
| 95 | + pytest.skip("to be aligned with NEP-50") |
| 96 | + |
| 97 | + input1 = _generate_type_routines_input(xp, dtype1, self.obj_type1) |
| 98 | + |
| 99 | + input2 = _generate_type_routines_input(xp, dtype2, self.obj_type2) |
| 100 | + ret = xp.result_type(input1, input2) |
| 101 | + assert isinstance(ret, numpy.dtype) |
| 102 | + return ret |
0 commit comments