|
| 1 | +import unittest |
| 2 | + |
| 3 | +import numpy |
| 4 | +import pytest |
| 5 | + |
| 6 | +import dpnp as cupy |
| 7 | +from tests.helper import has_support_aspect64 |
| 8 | +from tests.third_party.cupy import testing |
| 9 | + |
| 10 | + |
| 11 | +class TestKind(unittest.TestCase): |
| 12 | + @pytest.mark.skip("dpnp.asarray_chkfinite() is not implemented yet") |
| 13 | + @testing.for_orders("CFAK") |
| 14 | + @testing.for_all_dtypes() |
| 15 | + @testing.numpy_cupy_array_equal() |
| 16 | + def test_asarray_chkfinite(self, xp, dtype, order): |
| 17 | + a = [0, 4, 0, 5] |
| 18 | + return xp.asarray_chkfinite(a, dtype=dtype, order=order) |
| 19 | + |
| 20 | + @pytest.mark.skip("dpnp.asarray_chkfinite() is not implemented yet") |
| 21 | + @testing.for_orders("CFAK") |
| 22 | + @testing.for_all_dtypes(no_bool=True) |
| 23 | + def test_asarray_chkfinite_non_finite_vals(self, dtype, order): |
| 24 | + a = [-numpy.inf, 0.0, numpy.inf, numpy.nan] |
| 25 | + for xp in (numpy, cupy): |
| 26 | + if xp.issubdtype(dtype, xp.integer): |
| 27 | + error = OverflowError |
| 28 | + else: |
| 29 | + error = ValueError |
| 30 | + with pytest.raises(error): |
| 31 | + xp.asarray_chkfinite(a, dtype=dtype, order=order) |
| 32 | + |
| 33 | + @testing.for_all_dtypes() |
| 34 | + def test_asfarray(self, dtype): |
| 35 | + a = cupy.asarray([1, 2, 3]) |
| 36 | + a_gpu = cupy.asfarray(a, dtype) |
| 37 | + a_cpu = numpy.asfarray(a, dtype) |
| 38 | + if ( |
| 39 | + has_support_aspect64() |
| 40 | + or cupy.issubdtype(dtype, cupy.complexfloating) |
| 41 | + or cupy.issubdtype(dtype, cupy.floating) |
| 42 | + ): |
| 43 | + assert a_cpu.dtype == a_gpu.dtype |
| 44 | + else: |
| 45 | + assert a_cpu.dtype == cupy.float64 |
| 46 | + assert a_gpu.dtype == cupy.float32 |
| 47 | + |
| 48 | + @testing.for_all_dtypes() |
| 49 | + def test_asfortranarray1(self, dtype): |
| 50 | + def func(xp): |
| 51 | + x = xp.zeros((2, 3), dtype=dtype) |
| 52 | + ret = xp.asfortranarray(x) |
| 53 | + assert x.flags.c_contiguous |
| 54 | + assert ret.flags.f_contiguous |
| 55 | + |
| 56 | + assert func(numpy) == func(cupy) |
| 57 | + |
| 58 | + @testing.for_all_dtypes() |
| 59 | + def test_asfortranarray2(self, dtype): |
| 60 | + def func(xp): |
| 61 | + x = xp.zeros((2, 3, 4), dtype=dtype) |
| 62 | + ret = xp.asfortranarray(x) |
| 63 | + assert x.flags.c_contiguous |
| 64 | + assert ret.flags.f_contiguous |
| 65 | + |
| 66 | + assert func(numpy) == func(cupy) |
| 67 | + |
| 68 | + @testing.for_all_dtypes() |
| 69 | + def test_asfortranarray3(self, dtype): |
| 70 | + def func(xp): |
| 71 | + x = xp.zeros((2, 3, 4), dtype=dtype) |
| 72 | + ret = xp.asfortranarray(xp.asfortranarray(x)) |
| 73 | + assert x.flags.c_contiguous |
| 74 | + assert ret.flags.f_contiguous |
| 75 | + |
| 76 | + assert func(numpy) == func(cupy) |
| 77 | + |
| 78 | + @testing.for_all_dtypes() |
| 79 | + def test_asfortranarray4(self, dtype): |
| 80 | + def func(xp): |
| 81 | + x = xp.zeros((2, 3), dtype=dtype) |
| 82 | + x = xp.transpose(x, (1, 0)) |
| 83 | + ret = xp.asfortranarray(x) |
| 84 | + assert ret.flags.f_contiguous |
| 85 | + |
| 86 | + assert func(numpy) == func(cupy) |
| 87 | + |
| 88 | + @testing.for_all_dtypes() |
| 89 | + def test_asfortranarray5(self, dtype): |
| 90 | + def func(xp): |
| 91 | + x = testing.shaped_arange((2, 3), xp, dtype) |
| 92 | + ret = xp.asfortranarray(x) |
| 93 | + assert x.flags.c_contiguous |
| 94 | + assert ret.flags.f_contiguous |
| 95 | + |
| 96 | + assert func(numpy) == func(cupy) |
| 97 | + |
| 98 | + @pytest.mark.skip("dpnp.require() is not implemented yet") |
| 99 | + @testing.for_all_dtypes() |
| 100 | + def test_require_flag_check(self, dtype): |
| 101 | + possible_flags = [["C_CONTIGUOUS"], ["F_CONTIGUOUS"]] |
| 102 | + x = cupy.zeros((2, 3, 4), dtype=dtype) |
| 103 | + for flags in possible_flags: |
| 104 | + arr = cupy.require(x, dtype, flags) |
| 105 | + for parameter in flags: |
| 106 | + assert arr.flags[parameter] |
| 107 | + assert arr.dtype == dtype |
| 108 | + |
| 109 | + @pytest.mark.skip("dpnp.require() is not implemented yet") |
| 110 | + @testing.for_all_dtypes() |
| 111 | + def test_require_owndata(self, dtype): |
| 112 | + x = cupy.zeros((2, 3, 4), dtype=dtype) |
| 113 | + arr = x.view() |
| 114 | + arr = cupy.require(arr, dtype, ["O"]) |
| 115 | + assert arr.flags["OWNDATA"] |
| 116 | + |
| 117 | + @pytest.mark.skip("dpnp.require() is not implemented yet") |
| 118 | + @testing.for_all_dtypes() |
| 119 | + def test_require_C_and_F_flags(self, dtype): |
| 120 | + x = cupy.zeros((2, 3, 4), dtype=dtype) |
| 121 | + with pytest.raises(ValueError): |
| 122 | + cupy.require(x, dtype, ["C", "F"]) |
| 123 | + |
| 124 | + @pytest.mark.skip("dpnp.require() is not implemented yet") |
| 125 | + @testing.for_all_dtypes() |
| 126 | + def test_require_incorrect_requirments(self, dtype): |
| 127 | + x = cupy.zeros((2, 3, 4), dtype=dtype) |
| 128 | + with pytest.raises(ValueError): |
| 129 | + cupy.require(x, dtype, ["W"]) |
| 130 | + |
| 131 | + @pytest.mark.skip("dpnp.require() is not implemented yet") |
| 132 | + @testing.for_all_dtypes() |
| 133 | + def test_require_incorrect_dtype(self, dtype): |
| 134 | + x = cupy.zeros((2, 3, 4), dtype=dtype) |
| 135 | + with pytest.raises(ValueError): |
| 136 | + cupy.require(x, "random", "C") |
| 137 | + |
| 138 | + @pytest.mark.skip("dpnp.require() is not implemented yet") |
| 139 | + @testing.for_all_dtypes() |
| 140 | + def test_require_empty_requirements(self, dtype): |
| 141 | + x = cupy.zeros((2, 3, 4), dtype=dtype) |
| 142 | + x = cupy.require(x, dtype, []) |
| 143 | + assert x.flags["C_CONTIGUOUS"] |
0 commit comments