|
| 1 | +import numpy |
| 2 | +import pytest |
| 3 | +from numpy.testing import ( |
| 4 | + assert_array_equal, |
| 5 | +) |
| 6 | + |
| 7 | +import dpnp |
| 8 | + |
| 9 | +from .helper import ( |
| 10 | + generate_random_numpy_array, |
| 11 | + get_all_dtypes, |
| 12 | + get_complex_dtypes, |
| 13 | + get_float_dtypes, |
| 14 | + is_win_platform, |
| 15 | +) |
| 16 | + |
| 17 | +""" |
| 18 | +The scope includes tests with only functions which are instances of |
| 19 | +`DPNPUnaryTwoOutputsFunc` class. |
| 20 | +
|
| 21 | +""" |
| 22 | + |
| 23 | + |
| 24 | +@pytest.mark.parametrize("func", ["frexp", "modf"]) |
| 25 | +class TestUnaryTwoOutputs: |
| 26 | + ALL_DTYPES = get_all_dtypes(no_none=True) |
| 27 | + ALL_DTYPES_NO_COMPLEX = get_all_dtypes( |
| 28 | + no_none=True, no_float16=False, no_complex=True |
| 29 | + ) |
| 30 | + ALL_FLOAT_DTYPES = get_float_dtypes(no_float16=False) |
| 31 | + |
| 32 | + def _get_out_dtypes(self, func, dt): |
| 33 | + if func == "frexp": |
| 34 | + return (dt, numpy.int32) |
| 35 | + return (dt, dt) |
| 36 | + |
| 37 | + @pytest.mark.parametrize("dt", ALL_DTYPES_NO_COMPLEX) |
| 38 | + def test_basic(self, func, dt): |
| 39 | + a = generate_random_numpy_array((2, 5), dtype=dt) |
| 40 | + ia = dpnp.array(a) |
| 41 | + |
| 42 | + res1, res2 = getattr(dpnp, func)(ia) |
| 43 | + exp1, exp2 = getattr(numpy, func)(a) |
| 44 | + assert_array_equal(res1, exp1) |
| 45 | + assert_array_equal(res2, exp2) |
| 46 | + |
| 47 | + @pytest.mark.parametrize("dt", ALL_FLOAT_DTYPES) |
| 48 | + def test_out(self, func, dt): |
| 49 | + a = numpy.array(5.7, dtype=dt) |
| 50 | + ia = dpnp.array(a) |
| 51 | + |
| 52 | + dt1, dt2 = self._get_out_dtypes(func, dt) |
| 53 | + out1 = numpy.empty((), dtype=dt1) |
| 54 | + out2 = numpy.empty((), dtype=dt2) |
| 55 | + iout1, iout2 = dpnp.array(out1), dpnp.array(out2) |
| 56 | + |
| 57 | + res1, res2 = getattr(dpnp, func)(ia, iout1) |
| 58 | + exp1, exp2 = getattr(numpy, func)(a, out1) |
| 59 | + assert_array_equal(res1, exp1) |
| 60 | + assert_array_equal(res2, exp2) |
| 61 | + assert res1 is iout1 |
| 62 | + |
| 63 | + res1, res2 = getattr(dpnp, func)(ia, None, iout2) |
| 64 | + exp1, exp2 = getattr(numpy, func)(a, None, out2) |
| 65 | + assert_array_equal(res1, exp1) |
| 66 | + assert_array_equal(res2, exp2) |
| 67 | + assert res2 is iout2 |
| 68 | + |
| 69 | + res1, res2 = getattr(dpnp, func)(ia, iout1, iout2) |
| 70 | + exp1, exp2 = getattr(numpy, func)(a, out1, out2) |
| 71 | + assert_array_equal(res1, exp1) |
| 72 | + assert_array_equal(res2, exp2) |
| 73 | + assert res1 is iout1 |
| 74 | + assert res2 is iout2 |
| 75 | + |
| 76 | + @pytest.mark.parametrize("dt", ALL_DTYPES_NO_COMPLEX) |
| 77 | + @pytest.mark.parametrize("out1_dt", ALL_DTYPES) |
| 78 | + @pytest.mark.parametrize("out2_dt", ALL_DTYPES) |
| 79 | + def test_out_all_dtypes(self, func, dt, out1_dt, out2_dt): |
| 80 | + a = numpy.ones(9, dtype=dt) |
| 81 | + ia = dpnp.array(a) |
| 82 | + |
| 83 | + out1 = numpy.zeros(9, dtype=out1_dt) |
| 84 | + out2 = numpy.zeros(9, dtype=out2_dt) |
| 85 | + iout1, iout2 = dpnp.array(out1), dpnp.array(out2) |
| 86 | + |
| 87 | + try: |
| 88 | + res1, res2 = getattr(dpnp, func)(ia, out=(iout1, iout2)) |
| 89 | + except TypeError: |
| 90 | + # expect numpy to fail with the same reason |
| 91 | + with pytest.raises(TypeError): |
| 92 | + _ = getattr(numpy, func)(a, out=(out1, out2)) |
| 93 | + else: |
| 94 | + exp1, exp2 = getattr(numpy, func)(a, out=(out1, out2)) |
| 95 | + assert_array_equal(res1, exp1) |
| 96 | + assert_array_equal(res2, exp2) |
| 97 | + assert res1 is iout1 |
| 98 | + assert res2 is iout2 |
| 99 | + |
| 100 | + @pytest.mark.parametrize("stride", [-4, -2, -1, 1, 2, 4]) |
| 101 | + @pytest.mark.parametrize("dt", ALL_FLOAT_DTYPES) |
| 102 | + def test_strides_out(self, func, stride, dt): |
| 103 | + if func == "frexp" and is_win_platform(): |
| 104 | + pytest.skip( |
| 105 | + "numpy.frexp gives different answers for NAN/INF on Windows and Linux" |
| 106 | + ) |
| 107 | + |
| 108 | + a = numpy.array( |
| 109 | + [numpy.nan, numpy.nan, numpy.inf, -numpy.inf, 0.0, -0.0, 1.0, -1.0], |
| 110 | + dtype=dt, |
| 111 | + ) |
| 112 | + ia = dpnp.array(a) |
| 113 | + |
| 114 | + dt1, dt2 = self._get_out_dtypes(func, dt) |
| 115 | + out_mant = numpy.ones_like(a, dtype=dt1) |
| 116 | + out_exp = 2 * numpy.ones_like(a, dtype=dt2) |
| 117 | + iout_mant, iout_exp = dpnp.array(out_mant), dpnp.array(out_exp) |
| 118 | + |
| 119 | + res1, res2 = getattr(dpnp, func)( |
| 120 | + ia[::stride], out=(iout_mant[::stride], iout_exp[::stride]) |
| 121 | + ) |
| 122 | + exp1, exp2 = getattr(numpy, func)( |
| 123 | + a[::stride], out=(out_mant[::stride], out_exp[::stride]) |
| 124 | + ) |
| 125 | + assert_array_equal(res1, exp1) |
| 126 | + assert_array_equal(res2, exp2) |
| 127 | + |
| 128 | + assert_array_equal(iout_mant, out_mant) |
| 129 | + assert_array_equal(iout_exp, out_exp) |
| 130 | + |
| 131 | + @pytest.mark.parametrize("dt", ALL_FLOAT_DTYPES) |
| 132 | + def test_out1_overlap(self, func, dt): |
| 133 | + size = 15 |
| 134 | + a = numpy.ones(2 * size, dtype=dt) |
| 135 | + ia = dpnp.array(a) |
| 136 | + |
| 137 | + # out1 overlaps memory of input array |
| 138 | + _ = getattr(dpnp, func)(ia[size::], ia[::2]) |
| 139 | + _ = getattr(numpy, func)(a[size::], a[::2]) |
| 140 | + assert_array_equal(ia, a) |
| 141 | + |
| 142 | + @pytest.mark.parametrize("dt", ALL_FLOAT_DTYPES) |
| 143 | + def test_empty(self, func, dt): |
| 144 | + a = numpy.empty(0, dtype=dt) |
| 145 | + ia = dpnp.array(a) |
| 146 | + |
| 147 | + res1, res2 = getattr(dpnp, func)(ia) |
| 148 | + exp1, exp2 = getattr(numpy, func)(a) |
| 149 | + assert_array_equal(res1, exp1, strict=True) |
| 150 | + assert_array_equal(res2, exp2, strict=True) |
| 151 | + |
| 152 | + @pytest.mark.parametrize("xp", [numpy, dpnp]) |
| 153 | + @pytest.mark.parametrize("dt", get_complex_dtypes()) |
| 154 | + def test_complex_dtype(self, func, xp, dt): |
| 155 | + a = xp.array([-2, 5, 1, 4, 3], dtype=dt) |
| 156 | + with pytest.raises((TypeError, ValueError)): |
| 157 | + _ = getattr(xp, func)(a) |
0 commit comments