|
| 1 | +from math import prod |
| 2 | + |
| 3 | +import numpy |
| 4 | +import pytest |
| 5 | +from numpy.testing import ( |
| 6 | + assert_equal, |
| 7 | +) |
| 8 | + |
| 9 | +import dpnp |
| 10 | + |
| 11 | +from .helper import ( |
| 12 | + get_all_dtypes, |
| 13 | +) |
| 14 | + |
| 15 | + |
| 16 | +class TestFlip: |
| 17 | + @pytest.mark.parametrize("dtype", get_all_dtypes()) |
| 18 | + def test_arange_2d_default_axis(self, dtype): |
| 19 | + sh = (2, 3) if dtype != dpnp.bool else (1, 1) |
| 20 | + dp_a = dpnp.arange(prod(sh), dtype=dtype).reshape(sh) |
| 21 | + np_a = numpy.arange(prod(sh), dtype=dtype).reshape(sh) |
| 22 | + |
| 23 | + assert_equal(dpnp.flip(dp_a), numpy.flip(np_a)) |
| 24 | + |
| 25 | + @pytest.mark.parametrize("axis", list(range(3))) |
| 26 | + @pytest.mark.parametrize( |
| 27 | + "dtype", get_all_dtypes(no_bool=True, no_none=True) |
| 28 | + ) |
| 29 | + def test_arange_3d(self, axis, dtype): |
| 30 | + sh = (2, 2, 2) |
| 31 | + dp_a = dpnp.arange(prod(sh), dtype=dtype).reshape(sh) |
| 32 | + np_a = numpy.arange(prod(sh), dtype=dtype).reshape(sh) |
| 33 | + |
| 34 | + assert_equal(dpnp.flip(dp_a, axis=axis), numpy.flip(np_a, axis=axis)) |
| 35 | + |
| 36 | + @pytest.mark.parametrize("axis", [(), (0, 2), (1, 2)]) |
| 37 | + @pytest.mark.parametrize( |
| 38 | + "dtype", get_all_dtypes(no_bool=True, no_none=True) |
| 39 | + ) |
| 40 | + def test_arange_3d_multiple_axes(self, axis, dtype): |
| 41 | + sh = (2, 2, 2) |
| 42 | + dp_a = dpnp.arange(prod(sh), dtype=dtype).reshape(sh) |
| 43 | + np_a = numpy.arange(prod(sh), dtype=dtype).reshape(sh) |
| 44 | + |
| 45 | + assert_equal(dpnp.flip(dp_a, axis=axis), numpy.flip(np_a, axis=axis)) |
| 46 | + |
| 47 | + @pytest.mark.parametrize("axis", list(range(4))) |
| 48 | + @pytest.mark.parametrize( |
| 49 | + "dtype", get_all_dtypes(no_bool=True, no_none=True) |
| 50 | + ) |
| 51 | + def test_arange_4d(self, axis, dtype): |
| 52 | + sh = (2, 3, 4, 5) |
| 53 | + dp_a = dpnp.arange(prod(sh), dtype=dtype).reshape(sh) |
| 54 | + np_a = numpy.arange(prod(sh), dtype=dtype).reshape(sh) |
| 55 | + |
| 56 | + assert_equal(dpnp.flip(dp_a, axis=axis), numpy.flip(np_a, axis=axis)) |
| 57 | + |
| 58 | + @pytest.mark.parametrize( |
| 59 | + "dtype", get_all_dtypes(no_bool=True, no_none=True) |
| 60 | + ) |
| 61 | + def test_lr_equivalent(self, dtype): |
| 62 | + dp_a = dpnp.arange(4, dtype=dtype) |
| 63 | + dp_a = dp_a[:, dpnp.newaxis] + dp_a[dpnp.newaxis, :] |
| 64 | + assert_equal(dpnp.flip(dp_a, 1), dpnp.fliplr(dp_a)) |
| 65 | + |
| 66 | + np_a = numpy.arange(4, dtype=dtype) |
| 67 | + np_a = numpy.add.outer(np_a, np_a) |
| 68 | + assert_equal(dpnp.flip(dp_a, 1), numpy.flip(np_a, 1)) |
| 69 | + |
| 70 | + @pytest.mark.parametrize( |
| 71 | + "dtype", get_all_dtypes(no_bool=True, no_none=True) |
| 72 | + ) |
| 73 | + def test_ud_equivalent(self, dtype): |
| 74 | + dp_a = dpnp.arange(4, dtype=dtype) |
| 75 | + dp_a = dp_a[:, dpnp.newaxis] + dp_a[dpnp.newaxis, :] |
| 76 | + assert_equal(dpnp.flip(dp_a, 0), dpnp.flipud(dp_a)) |
| 77 | + |
| 78 | + np_a = numpy.arange(4, dtype=dtype) |
| 79 | + np_a = numpy.add.outer(np_a, np_a) |
| 80 | + assert_equal(dpnp.flip(dp_a, 0), numpy.flip(np_a, 0)) |
| 81 | + |
| 82 | + @pytest.mark.parametrize( |
| 83 | + "x, axis", |
| 84 | + [ |
| 85 | + pytest.param(dpnp.ones(4), 1, id="1-d, axis=1"), |
| 86 | + pytest.param(dpnp.ones((4, 4)), 2, id="2-d, axis=2"), |
| 87 | + pytest.param(dpnp.ones((4, 4)), -3, id="2-d, axis=-3"), |
| 88 | + pytest.param(dpnp.ones((4, 4)), (0, 3), id="2-d, axis=(0, 3)"), |
| 89 | + ], |
| 90 | + ) |
| 91 | + def test_axes(self, x, axis): |
| 92 | + with pytest.raises(numpy.AxisError): |
| 93 | + dpnp.flip(x, axis=axis) |
| 94 | + |
| 95 | + |
| 96 | +class TestFliplr: |
| 97 | + @pytest.mark.parametrize("dtype", get_all_dtypes()) |
| 98 | + def test_arange(self, dtype): |
| 99 | + sh = (2, 3) if dtype != dpnp.bool else (1, 1) |
| 100 | + dp_a = dpnp.arange(prod(sh), dtype=dtype).reshape(sh) |
| 101 | + np_a = numpy.arange(prod(sh), dtype=dtype).reshape(sh) |
| 102 | + |
| 103 | + assert_equal(dpnp.fliplr(dp_a), numpy.fliplr(np_a)) |
| 104 | + |
| 105 | + @pytest.mark.parametrize( |
| 106 | + "dtype", get_all_dtypes(no_bool=True, no_none=True) |
| 107 | + ) |
| 108 | + def test_equivalent(self, dtype): |
| 109 | + dp_a = dpnp.arange(4, dtype=dtype) |
| 110 | + dp_a = dp_a[:, dpnp.newaxis] + dp_a[dpnp.newaxis, :] |
| 111 | + assert_equal(dpnp.fliplr(dp_a), dp_a[:, ::-1]) |
| 112 | + |
| 113 | + np_a = numpy.arange(4, dtype=dtype) |
| 114 | + np_a = numpy.add.outer(np_a, np_a) |
| 115 | + assert_equal(dpnp.fliplr(dp_a), numpy.fliplr(np_a)) |
| 116 | + |
| 117 | + @pytest.mark.parametrize( |
| 118 | + "val", |
| 119 | + [-1.2, numpy.arange(7), [2, 7, 3.6], (-3, 4), range(4)], |
| 120 | + ids=["scalar", "numpy.array", "list", "tuple", "range"], |
| 121 | + ) |
| 122 | + def test_raises_array_type(self, val): |
| 123 | + with pytest.raises( |
| 124 | + TypeError, match="An array must be any of supported type, but got" |
| 125 | + ): |
| 126 | + dpnp.fliplr(val) |
| 127 | + |
| 128 | + def test_raises_1d(self): |
| 129 | + a = dpnp.ones(4) |
| 130 | + with pytest.raises(ValueError, match="Input must be >= 2-d, but got"): |
| 131 | + dpnp.fliplr(a) |
| 132 | + |
| 133 | + |
| 134 | +class TestFlipud: |
| 135 | + @pytest.mark.parametrize("dtype", get_all_dtypes()) |
| 136 | + def test_arange(self, dtype): |
| 137 | + sh = (2, 3) if dtype != dpnp.bool else (1, 1) |
| 138 | + dp_a = dpnp.arange(prod(sh), dtype=dtype).reshape(sh) |
| 139 | + np_a = numpy.arange(prod(sh), dtype=dtype).reshape(sh) |
| 140 | + |
| 141 | + assert_equal(dpnp.flipud(dp_a), numpy.flipud(np_a)) |
| 142 | + |
| 143 | + @pytest.mark.parametrize( |
| 144 | + "dtype", get_all_dtypes(no_bool=True, no_none=True) |
| 145 | + ) |
| 146 | + def test_equivalent(self, dtype): |
| 147 | + dp_a = dpnp.arange(4, dtype=dtype) |
| 148 | + dp_a = dp_a[:, dpnp.newaxis] + dp_a[dpnp.newaxis, :] |
| 149 | + assert_equal(dpnp.flipud(dp_a), dp_a[::-1, :]) |
| 150 | + |
| 151 | + np_a = numpy.arange(4, dtype=dtype) |
| 152 | + np_a = numpy.add.outer(np_a, np_a) |
| 153 | + assert_equal(dpnp.flipud(dp_a), numpy.flipud(np_a)) |
| 154 | + |
| 155 | + @pytest.mark.parametrize( |
| 156 | + "val", |
| 157 | + [3.4, numpy.arange(6), [2, -1.7, 6], (-2, 4), range(5)], |
| 158 | + ids=["scalar", "numpy.array", "list", "tuple", "range"], |
| 159 | + ) |
| 160 | + def test_raises_array_type(self, val): |
| 161 | + with pytest.raises( |
| 162 | + TypeError, match="An array must be any of supported type, but got" |
| 163 | + ): |
| 164 | + dpnp.flipud(val) |
| 165 | + |
| 166 | + def test_raises_0d(self): |
| 167 | + a = dpnp.array(3) |
| 168 | + with pytest.raises(ValueError, match="Input must be >= 1-d, but got"): |
| 169 | + dpnp.flipud(a) |
0 commit comments